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,21 +98,24 @@ 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 [(
|
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=
|
|
|
|
|
|
|
|
|
116 |
user_input = gr.Textbox(
|
117 |
label="Ask a health-related question",
|
118 |
placeholder="Describe your symptoms...",
|
@@ -126,30 +129,30 @@ def chatbot_ui():
|
|
126 |
audio_output = gr.Audio(label="Audio Response")
|
127 |
|
128 |
# Define actions
|
129 |
-
def handle_submit(user_query, image
|
130 |
logger.info("User submitted a query.")
|
131 |
-
response, audio = customLLMBot(user_query, image,
|
132 |
-
return response, audio, ""
|
133 |
|
134 |
# Submit on pressing Enter key
|
135 |
user_input.submit(
|
136 |
handle_submit,
|
137 |
-
inputs=[user_input, uploaded_image
|
138 |
-
outputs=[chatbot, audio_output, user_input
|
139 |
)
|
140 |
|
141 |
# Submit on button click
|
142 |
submit_btn.click(
|
143 |
handle_submit,
|
144 |
-
inputs=[user_input, uploaded_image
|
145 |
-
outputs=[chatbot, audio_output, user_input
|
146 |
)
|
147 |
|
148 |
# Action for clearing all fields
|
149 |
clear_btn.click(
|
150 |
-
lambda: ([], "", None,
|
151 |
inputs=[],
|
152 |
-
outputs=[chatbot, user_input, uploaded_image,
|
153 |
)
|
154 |
|
155 |
return demo
|
|
|
39 |
logger.info("Processing input...")
|
40 |
|
41 |
# Append user input to the chat history
|
42 |
+
chat_history.append((user_input, None))
|
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[-1] = (user_input, 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_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 |
+
align_self_right=True,
|
116 |
+
align_self_left=False,
|
117 |
+
match=False,
|
118 |
+
)
|
119 |
user_input = gr.Textbox(
|
120 |
label="Ask a health-related question",
|
121 |
placeholder="Describe your symptoms...",
|
|
|
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, chat_history)
|
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, None),
|
154 |
inputs=[],
|
155 |
+
outputs=[chatbot, user_input, uploaded_image, audio_output],
|
156 |
)
|
157 |
|
158 |
return demo
|