aghoraguru commited on
Commit
b4f3a1d
·
verified ·
1 Parent(s): aec9a27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -17
app.py CHANGED
@@ -21,23 +21,17 @@ interpreter.llm.model = "gpt-4-turbo"
21
  interpreter.custom_instructions = "First ask the user what they want to do. Based on the input, describe the next steps. If user agrees, proceed; if not, ask what they want next.If it is anything to display , always at the end open up the file."
22
 
23
  def json_to_markdown(json_data):
24
- full_message = []
25
  images = []
26
- # Regex to detect URLs; this is a simple version and might need to be adapted
27
- url_pattern = r'(http[s]?://\S+|sandbox:/\S+)'
28
-
29
  for item in json_data:
30
- if item['role'] == 'assistant' and item['type'] == 'message':
31
- content = item.get('content', " ")
32
- # Find all URLs in the content
33
- urls = re.findall(url_pattern, content)
34
- # Append any detected URLs to the images list
35
- images.extend(urls)
36
- # Remove URLs from the content
37
- content = re.sub(url_pattern, "", content).strip()
38
- if content:
39
- full_message.append(content)
40
- return full_message, images
41
 
42
  def list_png_files(directory):
43
  """List .png files in a given directory, sorted by modification time."""
@@ -81,11 +75,22 @@ def create_chat_widget():
81
  return chatblock
82
 
83
  def update_bot(text, chatbot):
 
 
84
  response_json = interpreter.chat(text, stream=True, display=False)
85
- formatted_response = json_to_markdown(response_json)
86
- chatbot.append((text, formatted_response))
 
 
 
 
 
 
 
 
87
  return chatbot, ""
88
 
 
89
  with gr.Blocks() as demo:
90
  with gr.Tab("HEXON Chatbot Assignment"):
91
  chat_interface = create_chat_widget()
 
21
  interpreter.custom_instructions = "First ask the user what they want to do. Based on the input, describe the next steps. If user agrees, proceed; if not, ask what they want next.If it is anything to display , always at the end open up the file."
22
 
23
  def json_to_markdown(json_data):
24
+ full_message = ""
25
  images = []
 
 
 
26
  for item in json_data:
27
+ if item['role'] == 'assistant':
28
+ if item['type'] == 'message':
29
+ content = item.get('content', " ")
30
+ full_message += content + " "
31
+ elif item['type'] == 'image' and 'path' in item:
32
+ images.append(item['path'])
33
+ return full_message.strip(), images
34
+
 
 
 
35
 
36
  def list_png_files(directory):
37
  """List .png files in a given directory, sorted by modification time."""
 
75
  return chatblock
76
 
77
  def update_bot(text, chatbot):
78
+ if text: # Append user input to the chat first
79
+ chatbot.append(("User", text))
80
  response_json = interpreter.chat(text, stream=True, display=False)
81
+ formatted_response, images = json_to_markdown(response_json)
82
+ # Append text response if it exists
83
+ if formatted_response.strip():
84
+ chatbot.append(("Assistant", formatted_response))
85
+ # Append images if any
86
+ for img_path in images:
87
+ if os.path.exists(img_path) and img_path.endswith('.png'):
88
+ chatbot.append(("Assistant", img_path)) # Assuming the path is accessible
89
+ else:
90
+ chatbot.append(("Assistant", "Image not found or path is invalid."))
91
  return chatbot, ""
92
 
93
+
94
  with gr.Blocks() as demo:
95
  with gr.Tab("HEXON Chatbot Assignment"):
96
  chat_interface = create_chat_widget()