Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -16,13 +16,23 @@ If the information is not in the context, respond with "I don't find that inform
|
|
16 |
Keep responses to 1-2 lines maximum.
|
17 |
""".strip()
|
18 |
|
19 |
-
#
|
20 |
PREDEFINED_QUESTIONS = [
|
21 |
"Select a question...",
|
22 |
"Tell me how can I navigate to a specific pose - include replanning aspects in your answer.",
|
23 |
-
"Can you provide me with code for this task?"
|
|
|
|
|
|
|
24 |
]
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
def generate_prompt(context: str, question: str, system_prompt: str = DEFAULT_SYSTEM_PROMPT) -> str:
|
27 |
return f"""
|
28 |
[INST] <<SYS>>
|
@@ -68,6 +78,10 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
68 |
# Initialize chat history if None
|
69 |
history = history or []
|
70 |
|
|
|
|
|
|
|
|
|
71 |
model, tokenizer = initialize_model()
|
72 |
|
73 |
# Get context from database
|
@@ -105,18 +119,28 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
105 |
history.append((message, f"An error occurred: {str(e)}"))
|
106 |
return history
|
107 |
|
|
|
|
|
|
|
108 |
# Create the Gradio interface
|
109 |
with gr.Blocks(title="ROS2 Expert Assistant") as demo:
|
110 |
gr.Markdown("# ROS2 Expert Assistant")
|
111 |
gr.Markdown("Ask questions about ROS2, navigation, and robotics. I'll provide concise answers based on the available documentation.")
|
112 |
|
113 |
with gr.Row():
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
with gr.Row():
|
122 |
# Chat interface
|
@@ -162,6 +186,16 @@ with gr.Blocks(title="ROS2 Expert Assistant") as demo:
|
|
162 |
label="Top-p"
|
163 |
)
|
164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
# Event handlers
|
166 |
question_dropdown.change(
|
167 |
question_selected,
|
@@ -169,8 +203,14 @@ with gr.Blocks(title="ROS2 Expert Assistant") as demo:
|
|
169 |
outputs=[msg]
|
170 |
)
|
171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
submit.click(
|
173 |
-
|
174 |
inputs=[
|
175 |
msg,
|
176 |
chatbot,
|
@@ -179,12 +219,12 @@ with gr.Blocks(title="ROS2 Expert Assistant") as demo:
|
|
179 |
temperature,
|
180 |
top_p
|
181 |
],
|
182 |
-
outputs=[chatbot]
|
183 |
)
|
184 |
|
185 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
186 |
msg.submit(
|
187 |
-
|
188 |
inputs=[
|
189 |
msg,
|
190 |
chatbot,
|
@@ -193,7 +233,7 @@ with gr.Blocks(title="ROS2 Expert Assistant") as demo:
|
|
193 |
temperature,
|
194 |
top_p
|
195 |
],
|
196 |
-
outputs=[chatbot]
|
197 |
)
|
198 |
|
199 |
if __name__ == "__main__":
|
|
|
16 |
Keep responses to 1-2 lines maximum.
|
17 |
""".strip()
|
18 |
|
19 |
+
# Expanded pre-populated questions
|
20 |
PREDEFINED_QUESTIONS = [
|
21 |
"Select a question...",
|
22 |
"Tell me how can I navigate to a specific pose - include replanning aspects in your answer.",
|
23 |
+
"Can you provide me with code for this task?",
|
24 |
+
"How do I set up obstacle avoidance in ROS2 navigation?",
|
25 |
+
"What are the key parameters for tuning the nav2 planner?",
|
26 |
+
"How do I integrate custom recovery behaviors?"
|
27 |
]
|
28 |
|
29 |
+
# Helper text for tooltip
|
30 |
+
DROPDOWN_TOOLTIP = """
|
31 |
+
You can either:
|
32 |
+
• Select a predefined question from this dropdown
|
33 |
+
• Type your own question in the text box below
|
34 |
+
"""
|
35 |
+
|
36 |
def generate_prompt(context: str, question: str, system_prompt: str = DEFAULT_SYSTEM_PROMPT) -> str:
|
37 |
return f"""
|
38 |
[INST] <<SYS>>
|
|
|
78 |
# Initialize chat history if None
|
79 |
history = history or []
|
80 |
|
81 |
+
if not message.strip():
|
82 |
+
history.append((message, "Please enter a question or select one from the dropdown menu."))
|
83 |
+
return history
|
84 |
+
|
85 |
model, tokenizer = initialize_model()
|
86 |
|
87 |
# Get context from database
|
|
|
119 |
history.append((message, f"An error occurred: {str(e)}"))
|
120 |
return history
|
121 |
|
122 |
+
def clear_input():
|
123 |
+
return gr.Textbox.update(value="")
|
124 |
+
|
125 |
# Create the Gradio interface
|
126 |
with gr.Blocks(title="ROS2 Expert Assistant") as demo:
|
127 |
gr.Markdown("# ROS2 Expert Assistant")
|
128 |
gr.Markdown("Ask questions about ROS2, navigation, and robotics. I'll provide concise answers based on the available documentation.")
|
129 |
|
130 |
with gr.Row():
|
131 |
+
with gr.Column(scale=8):
|
132 |
+
# Dropdown for predefined questions
|
133 |
+
question_dropdown = gr.Dropdown(
|
134 |
+
choices=PREDEFINED_QUESTIONS,
|
135 |
+
value="Select a question...",
|
136 |
+
label="Pre-defined Questions"
|
137 |
+
)
|
138 |
+
with gr.Column(scale=1):
|
139 |
+
# Info icon with tooltip
|
140 |
+
gr.Markdown(
|
141 |
+
"""<div title="{}">ℹ️</div>""".format(DROPDOWN_TOOLTIP),
|
142 |
+
elem_classes=["tooltip"]
|
143 |
+
)
|
144 |
|
145 |
with gr.Row():
|
146 |
# Chat interface
|
|
|
186 |
label="Top-p"
|
187 |
)
|
188 |
|
189 |
+
# Add custom CSS for tooltip
|
190 |
+
gr.Markdown("""
|
191 |
+
<style>
|
192 |
+
.tooltip {
|
193 |
+
cursor: help;
|
194 |
+
font-size: 1.2em;
|
195 |
+
}
|
196 |
+
</style>
|
197 |
+
""")
|
198 |
+
|
199 |
# Event handlers
|
200 |
question_dropdown.change(
|
201 |
question_selected,
|
|
|
203 |
outputs=[msg]
|
204 |
)
|
205 |
|
206 |
+
def submit_and_clear(message, history, system_message, max_tokens, temperature, top_p):
|
207 |
+
# First get the response
|
208 |
+
new_history = respond(message, history, system_message, max_tokens, temperature, top_p)
|
209 |
+
# Then clear the input
|
210 |
+
return new_history, gr.Textbox.update(value="")
|
211 |
+
|
212 |
submit.click(
|
213 |
+
submit_and_clear,
|
214 |
inputs=[
|
215 |
msg,
|
216 |
chatbot,
|
|
|
219 |
temperature,
|
220 |
top_p
|
221 |
],
|
222 |
+
outputs=[chatbot, msg]
|
223 |
)
|
224 |
|
225 |
+
clear.click(lambda: (None, ""), None, [chatbot, msg], queue=False)
|
226 |
msg.submit(
|
227 |
+
submit_and_clear,
|
228 |
inputs=[
|
229 |
msg,
|
230 |
chatbot,
|
|
|
233 |
temperature,
|
234 |
top_p
|
235 |
],
|
236 |
+
outputs=[chatbot, msg]
|
237 |
)
|
238 |
|
239 |
if __name__ == "__main__":
|