mannadamay12 commited on
Commit
d6a5c7c
·
verified ·
1 Parent(s): 5191399

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -78
app.py CHANGED
@@ -9,14 +9,12 @@ from langchain.prompts import PromptTemplate
9
  from langchain.chains import RetrievalQA
10
  from langchain_community.llms import HuggingFacePipeline
11
 
12
- # System prompts
13
  DEFAULT_SYSTEM_PROMPT = """
14
  You are a ROS2 expert assistant. Based on the context provided, give direct and concise answers.
15
  If the information is not in the context, respond with "I don't find that information in the available documentation."
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.",
@@ -26,13 +24,6 @@ PREDEFINED_QUESTIONS = [
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>>
@@ -43,7 +34,6 @@ Question: {question}
43
  Answer: [/INST]
44
  """.strip()
45
 
46
- # Initialize embeddings and database
47
  embeddings = HuggingFaceInstructEmbeddings(
48
  model_name="hkunlp/instructor-base",
49
  model_kwargs={"device": "cpu"}
@@ -75,7 +65,6 @@ def question_selected(question):
75
  @spaces.GPU
76
  def respond(message, history, system_message, max_tokens, temperature, top_p):
77
  try:
78
- # Initialize chat history if None
79
  history = history or []
80
 
81
  if not message.strip():
@@ -84,15 +73,12 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
84
 
85
  model, tokenizer = initialize_model()
86
 
87
- # Get context from database
88
  retriever = db.as_retriever(search_kwargs={"k": 2})
89
  docs = retriever.get_relevant_documents(message)
90
  context = "\n".join([doc.page_content for doc in docs])
91
 
92
- # Generate prompt
93
  prompt = generate_prompt(context=context, question=message, system_prompt=system_message)
94
 
95
- # Set up the pipeline
96
  text_pipeline = pipeline(
97
  "text-generation",
98
  model=model,
@@ -103,56 +89,40 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
103
  repetition_penalty=1.15
104
  )
105
 
106
- # Generate response
107
  output = text_pipeline(
108
  prompt,
109
  return_full_text=False,
110
  max_new_tokens=max_tokens
111
  )[0]['generated_text']
112
 
113
- # Add the new exchange to history
114
  history.append((message, output.strip()))
115
-
116
  return history
117
 
118
  except Exception as e:
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
147
- chatbot = gr.Chatbot()
148
 
149
- with gr.Row():
150
- # Message input
151
- msg = gr.Textbox(
152
- label="Your Question",
153
- placeholder="Type your question here or select one from the dropdown above...",
154
- lines=2
155
- )
156
 
157
  with gr.Row():
158
  submit = gr.Button("Submit")
@@ -186,55 +156,24 @@ with gr.Blocks(title="ROS2 Expert Assistant") as demo:
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,
202
  inputs=[question_dropdown],
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,
217
- system_message,
218
- max_tokens,
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,
231
- system_message,
232
- max_tokens,
233
- temperature,
234
- top_p
235
- ],
236
  outputs=[chatbot, msg]
237
  )
238
 
239
  if __name__ == "__main__":
240
- demo.launch(share)
 
9
  from langchain.chains import RetrievalQA
10
  from langchain_community.llms import HuggingFacePipeline
11
 
 
12
  DEFAULT_SYSTEM_PROMPT = """
13
  You are a ROS2 expert assistant. Based on the context provided, give direct and concise answers.
14
  If the information is not in the context, respond with "I don't find that information in the available documentation."
15
  Keep responses to 1-2 lines maximum.
16
  """.strip()
17
 
 
18
  PREDEFINED_QUESTIONS = [
19
  "Select a question...",
20
  "Tell me how can I navigate to a specific pose - include replanning aspects in your answer.",
 
24
  "How do I integrate custom recovery behaviors?"
25
  ]
26
 
 
 
 
 
 
 
 
27
  def generate_prompt(context: str, question: str, system_prompt: str = DEFAULT_SYSTEM_PROMPT) -> str:
28
  return f"""
29
  [INST] <<SYS>>
 
34
  Answer: [/INST]
35
  """.strip()
36
 
 
37
  embeddings = HuggingFaceInstructEmbeddings(
38
  model_name="hkunlp/instructor-base",
39
  model_kwargs={"device": "cpu"}
 
65
  @spaces.GPU
66
  def respond(message, history, system_message, max_tokens, temperature, top_p):
67
  try:
 
68
  history = history or []
69
 
70
  if not message.strip():
 
73
 
74
  model, tokenizer = initialize_model()
75
 
 
76
  retriever = db.as_retriever(search_kwargs={"k": 2})
77
  docs = retriever.get_relevant_documents(message)
78
  context = "\n".join([doc.page_content for doc in docs])
79
 
 
80
  prompt = generate_prompt(context=context, question=message, system_prompt=system_message)
81
 
 
82
  text_pipeline = pipeline(
83
  "text-generation",
84
  model=model,
 
89
  repetition_penalty=1.15
90
  )
91
 
 
92
  output = text_pipeline(
93
  prompt,
94
  return_full_text=False,
95
  max_new_tokens=max_tokens
96
  )[0]['generated_text']
97
 
 
98
  history.append((message, output.strip()))
 
99
  return history
100
 
101
  except Exception as e:
102
  history.append((message, f"An error occurred: {str(e)}"))
103
  return history
104
 
105
+ def submit_and_clear(message, history, system_message, max_tokens, temperature, top_p):
106
+ new_history = respond(message, history, system_message, max_tokens, temperature, top_p)
107
+ return new_history, gr.Textbox.update(value="")
108
 
 
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
+ question_dropdown = gr.Dropdown(
114
+ choices=PREDEFINED_QUESTIONS,
115
+ value="Select a question...",
116
+ label="Pre-defined Questions"
117
+ )
 
 
 
 
 
 
 
 
 
118
 
119
+ chatbot = gr.Chatbot()
 
 
120
 
121
+ msg = gr.Textbox(
122
+ label="Your Question",
123
+ placeholder="Type your question here or select one from the dropdown above...",
124
+ lines=2
125
+ )
 
 
126
 
127
  with gr.Row():
128
  submit = gr.Button("Submit")
 
156
  label="Top-p"
157
  )
158
 
 
 
 
 
 
 
 
 
 
 
 
159
  question_dropdown.change(
160
  question_selected,
161
  inputs=[question_dropdown],
162
  outputs=[msg]
163
  )
164
 
 
 
 
 
 
 
165
  submit.click(
166
  submit_and_clear,
167
+ inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p],
 
 
 
 
 
 
 
168
  outputs=[chatbot, msg]
169
  )
170
 
171
  clear.click(lambda: (None, ""), None, [chatbot, msg], queue=False)
172
  msg.submit(
173
  submit_and_clear,
174
+ inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p],
 
 
 
 
 
 
 
175
  outputs=[chatbot, msg]
176
  )
177
 
178
  if __name__ == "__main__":
179
+ demo.launch()