Spaces:
Sleeping
Sleeping
modified
Browse files
app.py
CHANGED
@@ -39,7 +39,7 @@ def customLLMBot(user_input, uploaded_image, chat_history):
|
|
39 |
logger.info("Processing input...")
|
40 |
|
41 |
# Append user input to the chat history
|
42 |
-
chat_history.append((
|
43 |
|
44 |
if uploaded_image is not None:
|
45 |
# Encode the image to base64
|
@@ -84,7 +84,7 @@ def customLLMBot(user_input, uploaded_image, chat_history):
|
|
84 |
logger.debug(f"LLM reply: {LLM_reply}")
|
85 |
|
86 |
# Append the bot's response to the chat history
|
87 |
-
chat_history
|
88 |
|
89 |
# Generate audio for response
|
90 |
audio_file = f"response_{uuid.uuid4().hex}.mp3"
|
@@ -98,23 +98,22 @@ def customLLMBot(user_input, uploaded_image, chat_history):
|
|
98 |
except Exception as e:
|
99 |
# Handle errors gracefully
|
100 |
logger.error(f"Error in customLLMBot function: {e}")
|
101 |
-
return [(user_input, f"An error occurred: {e}")], None
|
102 |
|
103 |
|
104 |
# Gradio Interface
|
105 |
def chatbot_ui():
|
106 |
-
chat_history = [] # Initialize empty chat history
|
107 |
-
|
108 |
with gr.Blocks() as demo:
|
109 |
gr.Markdown("# Healthcare Chatbot Doctor")
|
110 |
|
|
|
|
|
|
|
111 |
# Layout for chatbot and input box alignment
|
112 |
with gr.Row():
|
113 |
with gr.Column(scale=3): # Main column for chatbot
|
114 |
chatbot = gr.Chatbot(label=None, elem_id="chatbot").style(
|
115 |
-
|
116 |
-
align_self_left=False,
|
117 |
-
match=False,
|
118 |
)
|
119 |
user_input = gr.Textbox(
|
120 |
label="Ask a health-related question",
|
@@ -129,30 +128,30 @@ def chatbot_ui():
|
|
129 |
audio_output = gr.Audio(label="Audio Response")
|
130 |
|
131 |
# Define actions
|
132 |
-
def handle_submit(user_query, image):
|
133 |
logger.info("User submitted a query.")
|
134 |
-
response, audio = customLLMBot(user_query, image,
|
135 |
-
return response, audio, ""
|
136 |
|
137 |
# Submit on pressing Enter key
|
138 |
user_input.submit(
|
139 |
handle_submit,
|
140 |
-
inputs=[user_input, uploaded_image],
|
141 |
-
outputs=[chatbot, audio_output, user_input],
|
142 |
)
|
143 |
|
144 |
# Submit on button click
|
145 |
submit_btn.click(
|
146 |
handle_submit,
|
147 |
-
inputs=[user_input, uploaded_image],
|
148 |
-
outputs=[chatbot, audio_output, user_input],
|
149 |
)
|
150 |
|
151 |
# Action for clearing all fields
|
152 |
clear_btn.click(
|
153 |
-
lambda: ([], "", None,
|
154 |
inputs=[],
|
155 |
-
outputs=[chatbot, user_input, uploaded_image,
|
156 |
)
|
157 |
|
158 |
return demo
|
|
|
39 |
logger.info("Processing input...")
|
40 |
|
41 |
# Append user input to the chat history
|
42 |
+
chat_history.append(("user", user_input))
|
43 |
|
44 |
if uploaded_image is not None:
|
45 |
# Encode the image to base64
|
|
|
84 |
logger.debug(f"LLM reply: {LLM_reply}")
|
85 |
|
86 |
# Append the bot's response to the chat history
|
87 |
+
chat_history.append(("bot", LLM_reply))
|
88 |
|
89 |
# Generate audio for response
|
90 |
audio_file = f"response_{uuid.uuid4().hex}.mp3"
|
|
|
98 |
except Exception as e:
|
99 |
# Handle errors gracefully
|
100 |
logger.error(f"Error in customLLMBot function: {e}")
|
101 |
+
return [(("user", user_input or "Image uploaded"), ("bot", f"An error occurred: {e}"))], None
|
102 |
|
103 |
|
104 |
# Gradio Interface
|
105 |
def chatbot_ui():
|
|
|
|
|
106 |
with gr.Blocks() as demo:
|
107 |
gr.Markdown("# Healthcare Chatbot Doctor")
|
108 |
|
109 |
+
# State for user chat history
|
110 |
+
chat_history = gr.State([])
|
111 |
+
|
112 |
# Layout for chatbot and input box alignment
|
113 |
with gr.Row():
|
114 |
with gr.Column(scale=3): # Main column for chatbot
|
115 |
chatbot = gr.Chatbot(label=None, elem_id="chatbot").style(
|
116 |
+
align_self_left=True, # Align bot's response to the left
|
|
|
|
|
117 |
)
|
118 |
user_input = gr.Textbox(
|
119 |
label="Ask a health-related question",
|
|
|
128 |
audio_output = gr.Audio(label="Audio Response")
|
129 |
|
130 |
# Define actions
|
131 |
+
def handle_submit(user_query, image, history):
|
132 |
logger.info("User submitted a query.")
|
133 |
+
response, audio = customLLMBot(user_query, image, history)
|
134 |
+
return response, audio, "", history
|
135 |
|
136 |
# Submit on pressing Enter key
|
137 |
user_input.submit(
|
138 |
handle_submit,
|
139 |
+
inputs=[user_input, uploaded_image, chat_history],
|
140 |
+
outputs=[chatbot, audio_output, user_input, chat_history],
|
141 |
)
|
142 |
|
143 |
# Submit on button click
|
144 |
submit_btn.click(
|
145 |
handle_submit,
|
146 |
+
inputs=[user_input, uploaded_image, chat_history],
|
147 |
+
outputs=[chatbot, audio_output, user_input, chat_history],
|
148 |
)
|
149 |
|
150 |
# Action for clearing all fields
|
151 |
clear_btn.click(
|
152 |
+
lambda: ([], "", None, []),
|
153 |
inputs=[],
|
154 |
+
outputs=[chatbot, user_input, uploaded_image, chat_history],
|
155 |
)
|
156 |
|
157 |
return demo
|