aghoraguru commited on
Commit
4b4df40
·
verified ·
1 Parent(s): dca47b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -46
app.py CHANGED
@@ -20,62 +20,52 @@ interpreter.auto_run = True
20
  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 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
-
34
- def new_chat():
35
- interpreter.messages = []
36
- return [], ""
37
 
 
 
 
 
 
 
 
38
 
39
  def create_chat_widget():
40
  with gr.Blocks() as chatblock:
41
- chatbot = gr.Chatbot(
42
- [],
43
- elem_id="chatbot",
44
- elem_classes="chatbot",
45
- layout="llm",
46
- bubble_full_width=False,
47
- height=600,
48
- )
49
- txt = gr.Textbox(
50
- placeholder="Enter text and press enter to chat with the bot.",
51
- show_label=False,
52
- )
53
- send_button = gr.Button("Send")
54
  with gr.Row():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  txt.submit(update_bot, inputs=[txt, chatbot], outputs=[chatbot, txt])
56
  send_button.click(update_bot, inputs=[txt, chatbot], outputs=[chatbot, txt])
57
- return chatblock
58
 
59
- def json_to_markdown(json_data):
60
- full_message = []
61
- images = []
62
- # Regex to detect URLs; this is a simple version and might need to be adapted
63
- url_pattern = r'(http[s]?://\S+|sandbox:/\S+)'
64
-
65
- for item in json_data:
66
- if item['role'] == 'assistant' and item['type'] == 'message':
67
- content = item.get('content', " ")
68
- # Find all URLs in the content
69
- urls = re.findall(url_pattern, content)
70
- # Append any detected URLs to the images list
71
- images.extend(urls)
72
- # Remove URLs from the content
73
- content = re.sub(url_pattern, "", content).strip()
74
- if content:
75
- full_message.append(content)
76
- return full_message, images
77
 
 
78
 
 
 
 
 
 
79
 
80
  with gr.Blocks() as demo:
81
  with gr.Tab("HEXON Chatbot Assignment"):
 
20
  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 list_png_files(directory):
24
+ """List .png files in a given directory, sorted by modification time."""
25
+ png_files = [os.path.join(directory, f) for f in os.listdir(directory) if f.endswith('.png')]
26
+ png_files.sort(key=lambda x: os.path.getmtime(x), reverse=True) # Newest first
27
+ return png_files
 
 
 
 
 
 
 
 
 
28
 
29
+ def update_images(image_component, directory="/home/user/app/"):
30
+ """Update the image component with the latest image from the directory."""
31
+ png_files = list_png_files(directory)
32
+ if png_files:
33
+ # Load the most recent image file
34
+ return png_files[0]
35
+ return "No images available"
36
 
37
  def create_chat_widget():
38
  with gr.Blocks() as chatblock:
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  with gr.Row():
40
+ chatbot = gr.Chatbot(
41
+ [],
42
+ elem_id="gpt4",
43
+ elem_classes="gpt4",
44
+ layout="llm",
45
+ bubble_full_width=False,
46
+ height=600,
47
+ )
48
+ with gr.Row():
49
+ txt = gr.Textbox(
50
+ placeholder="Enter text and press enter to chat with the bot.",
51
+ show_label=False,
52
+ )
53
+ send_button = gr.Button("Send")
54
  txt.submit(update_bot, inputs=[txt, chatbot], outputs=[chatbot, txt])
55
  send_button.click(update_bot, inputs=[txt, chatbot], outputs=[chatbot, txt])
 
56
 
57
+ with gr.Row():
58
+ image_display = gr.Image()
59
+ update_image_button = gr.Button("Update Image")
60
+ update_image_button.click(update_images, inputs=[], outputs=image_display)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
+ return chatblock
63
 
64
+ def update_bot(text, chatbot):
65
+ response_json = interpreter.chat(text, stream=True, display=False)
66
+ formatted_response = json_to_markdown(response_json)
67
+ chatbot.append((text, formatted_response))
68
+ return chatbot, ""
69
 
70
  with gr.Blocks() as demo:
71
  with gr.Tab("HEXON Chatbot Assignment"):