Spaces:
Runtime error
Runtime error
Final working
Browse files
app.py
CHANGED
|
@@ -39,15 +39,14 @@ def list_png_files(directory):
|
|
| 39 |
png_files.sort(key=lambda x: os.path.getmtime(x), reverse=True) # Newest first
|
| 40 |
return png_files
|
| 41 |
|
| 42 |
-
def update_images(image_component
|
| 43 |
-
"""Update the image component with the latest image from the directory."""
|
| 44 |
-
|
|
|
|
| 45 |
if png_files:
|
| 46 |
-
# Load the most recent image file
|
| 47 |
-
return png_files[0]
|
| 48 |
return "No images available"
|
| 49 |
|
| 50 |
-
|
| 51 |
def create_chat_widget():
|
| 52 |
with gr.Blocks() as chatblock:
|
| 53 |
# Declare chatbot and txt here so they are accessible later
|
|
@@ -88,25 +87,31 @@ def create_chat_widget():
|
|
| 88 |
|
| 89 |
def new_chat():
|
| 90 |
return [], ""
|
| 91 |
-
|
| 92 |
-
|
| 93 |
def update_bot(text, chatbot):
|
| 94 |
-
if text: #
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
| 96 |
response_json = interpreter.chat(text, stream=True, display=False)
|
| 97 |
formatted_response, images = json_to_markdown(response_json)
|
| 98 |
-
|
|
|
|
| 99 |
if formatted_response.strip():
|
| 100 |
-
chatbot.append(("Assistant
|
| 101 |
-
|
|
|
|
| 102 |
for img_path in images:
|
| 103 |
if os.path.exists(img_path) and img_path.endswith('.png'):
|
| 104 |
-
|
|
|
|
| 105 |
else:
|
| 106 |
-
chatbot.append(("Assistant", "Image not found or path is invalid."))
|
|
|
|
| 107 |
return chatbot, ""
|
| 108 |
|
| 109 |
|
|
|
|
| 110 |
with gr.Blocks() as demo:
|
| 111 |
with gr.Tab("HEXON Chatbot Assignment"):
|
| 112 |
chat_interface = create_chat_widget()
|
|
|
|
| 39 |
png_files.sort(key=lambda x: os.path.getmtime(x), reverse=True) # Newest first
|
| 40 |
return png_files
|
| 41 |
|
| 42 |
+
def update_images(image_component):
|
| 43 |
+
"""Update the image component with the latest image from the current directory."""
|
| 44 |
+
current_directory = os.getcwd() # Get the current working directory
|
| 45 |
+
png_files = list_png_files(current_directory)
|
| 46 |
if png_files:
|
| 47 |
+
return png_files[0] # Load the most recent image file
|
|
|
|
| 48 |
return "No images available"
|
| 49 |
|
|
|
|
| 50 |
def create_chat_widget():
|
| 51 |
with gr.Blocks() as chatblock:
|
| 52 |
# Declare chatbot and txt here so they are accessible later
|
|
|
|
| 87 |
|
| 88 |
def new_chat():
|
| 89 |
return [], ""
|
|
|
|
|
|
|
| 90 |
def update_bot(text, chatbot):
|
| 91 |
+
if text.strip(): # Ensure the input text is not empty or just whitespace
|
| 92 |
+
# Append user input to the chat with specific formatting
|
| 93 |
+
chatbot.append(("User: " + text.strip(), ""))
|
| 94 |
+
|
| 95 |
+
# Simulate getting a response from the interpreter
|
| 96 |
response_json = interpreter.chat(text, stream=True, display=False)
|
| 97 |
formatted_response, images = json_to_markdown(response_json)
|
| 98 |
+
|
| 99 |
+
# Append the assistant's response with the desired formatting
|
| 100 |
if formatted_response.strip():
|
| 101 |
+
chatbot.append(("Assistant: " + formatted_response.strip(), ""))
|
| 102 |
+
|
| 103 |
+
# Handle images here if necessary
|
| 104 |
for img_path in images:
|
| 105 |
if os.path.exists(img_path) and img_path.endswith('.png'):
|
| 106 |
+
# Optionally display image paths
|
| 107 |
+
chatbot.append(("Assistant (image):", img_path))
|
| 108 |
else:
|
| 109 |
+
chatbot.append(("Assistant:", "Image not found or path is invalid."))
|
| 110 |
+
|
| 111 |
return chatbot, ""
|
| 112 |
|
| 113 |
|
| 114 |
+
|
| 115 |
with gr.Blocks() as demo:
|
| 116 |
with gr.Tab("HEXON Chatbot Assignment"):
|
| 117 |
chat_interface = create_chat_widget()
|