ThiSecur commited on
Commit
9be352e
·
verified ·
1 Parent(s): 6270c1e

Update Gradio_UI.py

Browse files
Files changed (1) hide show
  1. Gradio_UI.py +144 -2
Gradio_UI.py CHANGED
@@ -137,10 +137,152 @@ class GradioUI:
137
  self.file_upload_folder = file_upload_folder
138
  if self.file_upload_folder is not None:
139
  if not os.path.exists(file_upload_folder):
140
- os.mkdir(file_upload_folder)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
 
142
  def interact_with_agent(self, prompt, messages):
143
  import gradio as gr
144
  messages.append(gr.ChatMessage(role="user", content=prompt))
145
  yield messages
146
-
 
 
 
 
137
  self.file_upload_folder = file_upload_folder
138
  if self.file_upload_folder is not None:
139
  if not os.path.exists(file_upload_folder):
140
+ os.makedirs(file_upload_folder, exist_ok=True)
141
+
142
+ def launch(self, **kwargs):
143
+ import gradio as gr
144
+
145
+ with gr.Blocks(title="Multi-Tool AI Assistant", theme=gr.themes.Soft(), fill_height=True) as demo:
146
+ # Header with capabilities overview
147
+ gr.Markdown("""
148
+ # 🛠️ Multi-Tool AI Assistant
149
+
150
+ This assistant specializes in:
151
+ - **Time zone conversions** (e.g., "What time is 3pm EST in Tokyo?")
152
+ - **Weather lookups** (e.g., "What's the weather in Paris?")
153
+ - **Unit conversions** (e.g., "Convert 50 miles to kilometers")
154
+ - **Web search** (e.g., "Find recent news about AI")
155
+ - **Image generation** (e.g., "Create an image of a futuristic city")
156
+ - **Code execution** (e.g., "Calculate factorial of 5")
157
+ """)
158
+
159
+ # State management
160
+ stored_messages = gr.State([])
161
+ file_uploads_log = gr.State([])
162
+
163
+ # Chat interface
164
+ with gr.Row():
165
+ chatbot = gr.Chatbot(
166
+ label="Conversation",
167
+ avatar_images=(
168
+ None,
169
+ "https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/Alfred.png",
170
+ ),
171
+ height=500,
172
+ render=True,
173
+ bubble_full_width=False
174
+ )
175
+
176
+ # File upload and input section
177
+ with gr.Row():
178
+ if self.file_upload_folder is not None:
179
+ with gr.Column(scale=1):
180
+ upload_file = gr.File(
181
+ label="Upload documents (PDF, DOCX, TXT)",
182
+ file_types=[".pdf", ".docx", ".txt"],
183
+ height=100
184
+ )
185
+ upload_status = gr.Textbox(
186
+ label="Upload Status",
187
+ interactive=False,
188
+ visible=False
189
+ )
190
+
191
+ with gr.Column(scale=4):
192
+ text_input = gr.Textbox(
193
+ placeholder="Type your question or request here...",
194
+ label="Your message",
195
+ lines=2,
196
+ max_lines=5,
197
+ container=False
198
+ )
199
+
200
+ # Control buttons
201
+ with gr.Row():
202
+ submit_btn = gr.Button("Send", variant="primary")
203
+ clear_btn = gr.Button("Clear Chat")
204
+
205
+ # Event handlers
206
+ upload_file.change(
207
+ self.upload_file,
208
+ [upload_file, file_uploads_log],
209
+ [upload_status, file_uploads_log],
210
+ )
211
+
212
+ text_input.submit(
213
+ self.log_user_message,
214
+ [text_input, file_uploads_log],
215
+ [stored_messages, text_input],
216
+ ).then(
217
+ self.interact_with_agent,
218
+ [stored_messages, chatbot],
219
+ [chatbot]
220
+ )
221
+
222
+ submit_btn.click(
223
+ self.log_user_message,
224
+ [text_input, file_uploads_log],
225
+ [stored_messages, text_input],
226
+ ).then(
227
+ self.interact_with_agent,
228
+ [stored_messages, chatbot],
229
+ [chatbot]
230
+ )
231
+
232
+ clear_btn.click(
233
+ lambda: (None, [], []),
234
+ outputs=[chatbot, stored_messages, file_uploads_log]
235
+ )
236
+
237
+ demo.launch(**kwargs)
238
+
239
+ def upload_file(self, file, file_uploads_log, allowed_file_types=[
240
+ "application/pdf",
241
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
242
+ "text/plain"]):
243
+ import gradio as gr
244
+ if file is None:
245
+ return gr.Textbox("No file uploaded", visible=True), file_uploads_log
246
+
247
+ try:
248
+ mime_type, _ = mimetypes.guess_type(file.name)
249
+ except Exception as e:
250
+ return gr.Textbox(f"Error: {e}", visible=True), file_uploads_log
251
+
252
+ if mime_type not in allowed_file_types:
253
+ return gr.Textbox("File type disallowed", visible=True), file_uploads_log
254
+
255
+ original_name = os.path.basename(file.name)
256
+ sanitized_name = re.sub(r"[^\w\-.]", "_", original_name)
257
+
258
+ type_to_ext = {}
259
+ for ext, t in mimetypes.types_map.items():
260
+ if t not in type_to_ext:
261
+ type_to_ext[t] = ext
262
+
263
+ sanitized_name = sanitized_name.split(".")[:-1]
264
+ sanitized_name.append("" + type_to_ext[mime_type])
265
+ sanitized_name = "".join(sanitized_name)
266
+
267
+ file_path = os.path.join(self.file_upload_folder, os.path.basename(sanitized_name))
268
+ shutil.copy(file.name, file_path)
269
+
270
+ return gr.Textbox(f"File uploaded: {file_path}", visible=True), file_uploads_log + [file_path]
271
+
272
+ def log_user_message(self, text_input, file_uploads_log):
273
+ return (
274
+ text_input + (
275
+ f"\nYou have been provided with these files, which might be helpful or not: {file_uploads_log}"
276
+ if len(file_uploads_log) > 0 else ""
277
+ ),
278
+ "",
279
+ )
280
 
281
  def interact_with_agent(self, prompt, messages):
282
  import gradio as gr
283
  messages.append(gr.ChatMessage(role="user", content=prompt))
284
  yield messages
285
+ for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
286
+ messages.append(msg)
287
+ yield messages
288
+ yield messages