oort commited on
Commit
531101b
·
verified ·
1 Parent(s): 3e30e7d

Update Gradio_UI with examples

Browse files
Files changed (1) hide show
  1. Gradio_UI.py +64 -13
Gradio_UI.py CHANGED
@@ -31,9 +31,12 @@ def pull_messages_from_step(
31
  """Extract ChatMessage objects from agent steps with proper nesting"""
32
  import gradio as gr
33
 
 
34
  if isinstance(step_log, ActionStep):
35
  # Output the step number
36
  step_number = f"Step {step_log.step_number}" if step_log.step_number is not None else ""
 
 
37
  yield gr.ChatMessage(role="assistant", content=f"**{step_number}**")
38
 
39
  # First yield the thought/reasoning from the LLM
@@ -172,7 +175,7 @@ def stream_to_gradio(
172
  content={"path": final_answer.to_string(), "mime_type": "audio/wav"},
173
  )
174
  else:
175
- yield gr.ChatMessage(role="assistant", content=f"**Final answer:** {str(final_answer)}")
176
 
177
 
178
  class GradioUI:
@@ -192,11 +195,30 @@ class GradioUI:
192
  def interact_with_agent(self, prompt, messages):
193
  import gradio as gr
194
 
 
195
  messages.append(gr.ChatMessage(role="user", content=prompt))
196
  yield messages
 
 
 
 
 
 
 
 
 
 
 
197
  for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
 
 
 
198
  messages.append(msg)
199
  yield messages
 
 
 
 
200
  yield messages
201
 
202
  def upload_file(
@@ -261,7 +283,25 @@ class GradioUI:
261
  def launch(self, **kwargs):
262
  import gradio as gr
263
 
264
- with gr.Blocks(fill_height=True) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265
  stored_messages = gr.State([])
266
  file_uploads_log = gr.State([])
267
  chatbot = gr.Chatbot(
@@ -269,27 +309,38 @@ class GradioUI:
269
  type="messages",
270
  avatar_images=(
271
  None,
272
- "https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/Alfred.png",
273
  ),
274
  resizeable=True,
275
  scale=1,
276
  )
277
- # If an upload folder is provided, enable the upload feature
278
- if self.file_upload_folder is not None:
279
- upload_file = gr.File(label="Upload a file")
280
- upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=False)
281
- upload_file.change(
282
- self.upload_file,
283
- [upload_file, file_uploads_log],
284
- [upload_status, file_uploads_log],
285
- )
286
- text_input = gr.Textbox(lines=1, label="Chat Message")
 
 
 
 
 
287
  text_input.submit(
288
  self.log_user_message,
289
  [text_input, file_uploads_log],
290
  [stored_messages, text_input],
291
  ).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
292
 
 
 
 
 
 
 
293
  demo.launch(debug=True, share=True, **kwargs)
294
 
295
 
 
31
  """Extract ChatMessage objects from agent steps with proper nesting"""
32
  import gradio as gr
33
 
34
+
35
  if isinstance(step_log, ActionStep):
36
  # Output the step number
37
  step_number = f"Step {step_log.step_number}" if step_log.step_number is not None else ""
38
+ if step_log.step_number == 1:
39
+ yield gr.ChatMessage(role="assistant", content=f"***Thinking***")
40
  yield gr.ChatMessage(role="assistant", content=f"**{step_number}**")
41
 
42
  # First yield the thought/reasoning from the LLM
 
175
  content={"path": final_answer.to_string(), "mime_type": "audio/wav"},
176
  )
177
  else:
178
+ yield gr.ChatMessage(role="assistant", content=f"***Final answer:*** {str(final_answer)}")
179
 
180
 
181
  class GradioUI:
 
195
  def interact_with_agent(self, prompt, messages):
196
  import gradio as gr
197
 
198
+ # Add user message
199
  messages.append(gr.ChatMessage(role="user", content=prompt))
200
  yield messages
201
+
202
+ # Add loading message
203
+ loading_msg = gr.ChatMessage(
204
+ role="assistant",
205
+ content="😴 Thinking...",
206
+ metadata={"class": "loading-message"}
207
+ )
208
+ messages.append(loading_msg)
209
+ yield messages
210
+
211
+ # Process agent response
212
  for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
213
+ # Remove loading message before adding real responses
214
+ if len(messages) > 0 and messages[-1].metadata.get("class") == "loading-message":
215
+ messages.pop()
216
  messages.append(msg)
217
  yield messages
218
+
219
+ # Ensure loading message is removed at the end
220
+ if len(messages) > 0 and messages[-1].metadata.get("class") == "loading-message":
221
+ messages.pop()
222
  yield messages
223
 
224
  def upload_file(
 
283
  def launch(self, **kwargs):
284
  import gradio as gr
285
 
286
+ with gr.Blocks(
287
+ title="Draw me...",
288
+ fill_height=True,
289
+ css="""
290
+ .loading-message {
291
+ animation: pulse 2s cubic-bezier(.4,0,.6,1) infinite;
292
+ }
293
+ @keyframes pulse {
294
+ 0%, 100% { opacity: 1; }
295
+ 50% { opacity: 0.5; }
296
+ }
297
+ """
298
+ ) as demo:
299
+ gr.Markdown(
300
+ """
301
+ #'Draw me ...' agent
302
+ I will enhance your basic prompt to make it perfect for Flux image generation.
303
+ """
304
+ )
305
  stored_messages = gr.State([])
306
  file_uploads_log = gr.State([])
307
  chatbot = gr.Chatbot(
 
309
  type="messages",
310
  avatar_images=(
311
  None,
312
+ "https://drive.google.com/file/d/1WIvyckpHHR8ctxYRhcvKEOFAAeP5nHKR/view?usp=sharing",
313
  ),
314
  resizeable=True,
315
  scale=1,
316
  )
317
+ text_input = gr.Textbox(
318
+ label="Draw me [write what you want to bi in the generated picture]",
319
+ placeholder="Type your question here...",
320
+ )
321
+ submit_button = gr.Button("Send")
322
+ gr.Examples(
323
+ examples=[
324
+ "Draw me a pair of curious giraffe in front of Mona Lisa in Louvre",
325
+ "Draw me a portrait of a beautiful hipster woman, expressive lips, almond shaped eyes, bright sunny day",
326
+ "Draw me abstract style waterfalls with wildlife inside the silhouette of a man’s head",
327
+ "Draw me a polaroid family photo of a husband wife and kids in the 1980ies with dog on a summer everning"
328
+ ],
329
+ inputs=text_input,
330
+ label="Example prompts"
331
+ )
332
  text_input.submit(
333
  self.log_user_message,
334
  [text_input, file_uploads_log],
335
  [stored_messages, text_input],
336
  ).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
337
 
338
+ submit_button.click(
339
+ self.log_user_message,
340
+ [text_input, file_uploads_log],
341
+ [stored_messages, text_input],
342
+ ).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
343
+
344
  demo.launch(debug=True, share=True, **kwargs)
345
 
346