saihv commited on
Commit
73feeaa
1 Parent(s): 2897ed9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -33
app.py CHANGED
@@ -1,18 +1,14 @@
1
- '''
2
  ChatGPT + Robotics Gradio demo.
3
  Author: Sai Vemprala
4
-
5
  For details, please check out our blog post: https://aka.ms/ChatGPT-Robotics, and our paper:
6
  https://www.microsoft.com/en-us/research/uploads/prod/2023/02/ChatGPT___Robotics.pdf
7
-
8
  In this demo, we provide a quick way to interact with ChatGPT in robotics settings using some custom prompts.
9
  As seen in our paper, we provide prompts for several scenarios: robot manipulation, drone navigation
10
  (in a simulated setting (airsim) as well as real life), and embodied AI. embodied_agent_closed_loop is an
11
  experimental setting where observations from a scene can be described to ChatGPT as text.
12
-
13
  Parts of the code were inspired by https://huggingface.co/spaces/VladislavMotkov/chatgpt_webui/
14
-
15
- '''
16
 
17
  import gradio as gr
18
  from revChatGPT.V1 import Chatbot
@@ -20,21 +16,23 @@ import glob, os
20
 
21
  access_token = None
22
 
 
23
  def parse_text(text):
24
  lines = text.split("\n")
25
- for i,line in enumerate(lines):
26
  if "```" in line:
27
- items = line.split('`')
28
  if items[-1]:
29
  lines[i] = f'<pre><code class="{items[-1]}">'
30
  else:
31
- lines[i] = f'</code></pre>'
32
  else:
33
- if i>0:
34
- lines[i] = '<br/>'+line.replace(" ", "&nbsp;")
35
  return "".join(lines)
36
 
37
- def configure_chatgpt(info):
 
38
  access_token = info
39
  config = {}
40
  config.update({"access_token": access_token})
@@ -42,51 +40,59 @@ def configure_chatgpt(info):
42
  global chatgpt
43
  chatgpt = Chatbot(config=config)
44
 
 
45
  def ask(prompt):
46
  message = ""
47
  for data in chatgpt.ask(prompt):
48
  message = data["message"]
49
  return parse_text(message)
50
-
 
51
  def query_chatgpt(inputs, history, message):
52
  history = history or []
53
  output = ask(inputs)
54
  history.append((inputs, output))
55
- return history, history, ''
 
56
 
57
  def initialize_prompt(prompt_type, history):
58
  history = history or []
59
-
60
  if prompt_type:
61
- prompt_file = './prompts/' + str(prompt_type) + '.txt'
62
 
63
  with open(prompt_file, "r") as f:
64
  prompt = f.read()
65
  output = ask(prompt)
66
  history.append(("<ORIGINAL PROMPT>", output))
67
-
68
- return history, history
 
69
 
70
  def display_prompt(show, prompt_type):
71
  if not prompt_type:
72
  show = False
73
- return 'Error - prompt not selected'
74
 
75
  else:
76
  if show:
77
- prompt_file = './prompts/' + str(prompt_type) + '.txt'
78
 
79
  with open(prompt_file, "r") as f:
80
  prompt = f.read()
81
 
82
  return prompt
83
  else:
84
- return ''
 
85
 
86
  with gr.Blocks() as demo:
87
  gr.Markdown("""<h3><center>ChatGPT + Robotics</center></h3>""")
88
  gr.Markdown(
89
- "This is a companion app to the work [ChatGPT for Robotics: Design Principles and Model Abilities](https://aka.ms/ChatGPT-Robotics). See [README](https://huggingface.co/spaces/microsoft/ChatGPT-Robotics/blob/main/README.md) for instructions.")
 
 
 
90
 
91
  if not access_token:
92
  gr.Markdown("""<h4>Login to ChatGPT</h4>""")
@@ -96,27 +102,36 @@ with gr.Blocks() as demo:
96
  with gr.Row():
97
  login = gr.Button("Login")
98
  login.click(configure_chatgpt, inputs=[info])
99
-
100
- l=os.listdir('./prompts')
101
- li=[x.split('.')[0] for x in l]
102
 
103
- gr.Markdown("""<h4>Initial Prompt (based on scenario)</h4>""")
104
- prompt_type = gr.components.Dropdown(li, label="Select sample prompt", value=None)
105
-
 
 
 
 
 
 
 
 
106
  show_prompt = gr.Checkbox(label="Display prompt")
107
  prompt_display = gr.Textbox(interactive=False, label="Prompt")
108
  show_prompt.change(fn=display_prompt, inputs=[show_prompt, prompt_type], outputs=prompt_display)
109
-
110
  initialize = gr.Button(value="Initialize")
111
 
112
  gr.Markdown("""<h4>Conversation</h4>""")
113
  chatgpt_robot = gr.Chatbot()
114
- message = gr.Textbox(placeholder="Enter query", label="")
115
-
 
 
 
 
116
  state = gr.State()
117
-
118
  initialize.click(fn=initialize_prompt, inputs=[prompt_type, state], outputs=[chatgpt_robot, state])
119
-
120
  message.submit(query_chatgpt, inputs=[message, state], outputs=[chatgpt_robot, state, message])
121
 
122
  demo.launch()
 
1
+ """
2
  ChatGPT + Robotics Gradio demo.
3
  Author: Sai Vemprala
 
4
  For details, please check out our blog post: https://aka.ms/ChatGPT-Robotics, and our paper:
5
  https://www.microsoft.com/en-us/research/uploads/prod/2023/02/ChatGPT___Robotics.pdf
 
6
  In this demo, we provide a quick way to interact with ChatGPT in robotics settings using some custom prompts.
7
  As seen in our paper, we provide prompts for several scenarios: robot manipulation, drone navigation
8
  (in a simulated setting (airsim) as well as real life), and embodied AI. embodied_agent_closed_loop is an
9
  experimental setting where observations from a scene can be described to ChatGPT as text.
 
10
  Parts of the code were inspired by https://huggingface.co/spaces/VladislavMotkov/chatgpt_webui/
11
+ """
 
12
 
13
  import gradio as gr
14
  from revChatGPT.V1 import Chatbot
 
16
 
17
  access_token = None
18
 
19
+
20
  def parse_text(text):
21
  lines = text.split("\n")
22
+ for i, line in enumerate(lines):
23
  if "```" in line:
24
+ items = line.split("`")
25
  if items[-1]:
26
  lines[i] = f'<pre><code class="{items[-1]}">'
27
  else:
28
+ lines[i] = f"</code></pre>"
29
  else:
30
+ if i > 0:
31
+ lines[i] = "<br/>" + line.replace(" ", "&nbsp;")
32
  return "".join(lines)
33
 
34
+
35
+ def configure_chatgpt(info):
36
  access_token = info
37
  config = {}
38
  config.update({"access_token": access_token})
 
40
  global chatgpt
41
  chatgpt = Chatbot(config=config)
42
 
43
+
44
  def ask(prompt):
45
  message = ""
46
  for data in chatgpt.ask(prompt):
47
  message = data["message"]
48
  return parse_text(message)
49
+
50
+
51
  def query_chatgpt(inputs, history, message):
52
  history = history or []
53
  output = ask(inputs)
54
  history.append((inputs, output))
55
+ return history, history, ""
56
+
57
 
58
  def initialize_prompt(prompt_type, history):
59
  history = history or []
60
+
61
  if prompt_type:
62
+ prompt_file = "./prompts/" + str(prompt_type) + ".txt"
63
 
64
  with open(prompt_file, "r") as f:
65
  prompt = f.read()
66
  output = ask(prompt)
67
  history.append(("<ORIGINAL PROMPT>", output))
68
+
69
+ return history, history
70
+
71
 
72
  def display_prompt(show, prompt_type):
73
  if not prompt_type:
74
  show = False
75
+ return "Error - prompt not selected"
76
 
77
  else:
78
  if show:
79
+ prompt_file = "./prompts/" + str(prompt_type) + ".txt"
80
 
81
  with open(prompt_file, "r") as f:
82
  prompt = f.read()
83
 
84
  return prompt
85
  else:
86
+ return ""
87
+
88
 
89
  with gr.Blocks() as demo:
90
  gr.Markdown("""<h3><center>ChatGPT + Robotics</center></h3>""")
91
  gr.Markdown(
92
+ "This is a companion app to the work [ChatGPT for Robotics: Design Principles and Model Abilities](https://aka.ms/ChatGPT-Robotics). <br>
93
+ This space allows you to work with ChatGPT to get it to generate code for robotics tasks, such as get a robot arm to manipulate objects, or have a drone fly around in a 3D world. <br>
94
+ See [README](https://huggingface.co/spaces/microsoft/ChatGPT-Robotics/blob/main/README.md) for detailed instructions."
95
+ )
96
 
97
  if not access_token:
98
  gr.Markdown("""<h4>Login to ChatGPT</h4>""")
 
102
  with gr.Row():
103
  login = gr.Button("Login")
104
  login.click(configure_chatgpt, inputs=[info])
 
 
 
105
 
106
+ l = os.listdir("./prompts")
107
+ li = [x.split(".")[0] for x in l]
108
+
109
+ gr.Markdown("""<h4>Initial Prompt for ChatGPT</h4>""")
110
+ prompt_type = gr.components.Dropdown(
111
+ li,
112
+ label="Select sample prompt",
113
+ value=None,
114
+ info="Choose a prompt based on the robot/scenario you're interested in (e.g. pick airsim or real_drone to start a drone scenario)",
115
+ )
116
+
117
  show_prompt = gr.Checkbox(label="Display prompt")
118
  prompt_display = gr.Textbox(interactive=False, label="Prompt")
119
  show_prompt.change(fn=display_prompt, inputs=[show_prompt, prompt_type], outputs=prompt_display)
120
+
121
  initialize = gr.Button(value="Initialize")
122
 
123
  gr.Markdown("""<h4>Conversation</h4>""")
124
  chatgpt_robot = gr.Chatbot()
125
+ message = gr.Textbox(
126
+ placeholder="Enter query",
127
+ label="",
128
+ info='Talk to ChatGPT and ask it to help with specific tasks! For example, "take off and reach an altitude of five meters"',
129
+ )
130
+
131
  state = gr.State()
132
+
133
  initialize.click(fn=initialize_prompt, inputs=[prompt_type, state], outputs=[chatgpt_robot, state])
134
+
135
  message.submit(query_chatgpt, inputs=[message, state], outputs=[chatgpt_robot, state, message])
136
 
137
  demo.launch()