aghoraguru commited on
Commit
77a601c
·
verified ·
1 Parent(s): 9799c81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -4
app.py CHANGED
@@ -22,8 +22,12 @@ interpreter.custom_instructions = "First ask the user what they want to do. Base
22
 
23
  def update_bot(text, chatbot):
24
  response_json = interpreter.chat(text, stream=True, display=False)
25
- formatted_response = json_to_markdown(response_json)
26
- chatbot.append((text, formatted_response))
 
 
 
 
27
  return chatbot, ""
28
 
29
 
@@ -59,11 +63,23 @@ def create_chat_widget():
59
 
60
  def json_to_markdown(json_data):
61
  full_message = []
 
 
 
 
62
  for item in json_data:
63
  if item['role'] == 'assistant' and item['type'] == 'message':
64
  content = item.get('content', " ")
65
- full_message.append(content) # Append the content to the list
66
- return "**Assistant:**\n" + " ".join(full_message) # Join all parts into one string and format as a single message
 
 
 
 
 
 
 
 
67
 
68
 
69
  with gr.Blocks() as demo:
 
22
 
23
  def update_bot(text, chatbot):
24
  response_json = interpreter.chat(text, stream=True, display=False)
25
+ formatted_response, images = json_to_markdown(response_json)
26
+ message = " ".join(formatted_response)
27
+ if message:
28
+ chatbot.append(("Assistant", message))
29
+ for img_path in images:
30
+ chatbot.append(("Assistant", img_path)) # Append image paths directly
31
  return chatbot, ""
32
 
33
 
 
63
 
64
  def json_to_markdown(json_data):
65
  full_message = []
66
+ images = []
67
+ # Regex to detect URLs; this is a simple version and might need to be adapted
68
+ url_pattern = r'(http[s]?://\S+|sandbox:/\S+)'
69
+
70
  for item in json_data:
71
  if item['role'] == 'assistant' and item['type'] == 'message':
72
  content = item.get('content', " ")
73
+ # Find all URLs in the content
74
+ urls = re.findall(url_pattern, content)
75
+ # Append any detected URLs to the images list
76
+ images.extend(urls)
77
+ # Remove URLs from the content
78
+ content = re.sub(url_pattern, "", content).strip()
79
+ if content:
80
+ full_message.append(content)
81
+ return full_message, images
82
+
83
 
84
 
85
  with gr.Blocks() as demo: