Spaces:
Sleeping
Sleeping
modified
Browse files
app.py
CHANGED
@@ -1,188 +1,3 @@
|
|
1 |
-
# from groq import Groq
|
2 |
-
# import gradio as gr
|
3 |
-
# from gtts import gTTS
|
4 |
-
# import uuid
|
5 |
-
# import base64
|
6 |
-
# from io import BytesIO
|
7 |
-
# import os
|
8 |
-
# import logging
|
9 |
-
|
10 |
-
# # Set up logger
|
11 |
-
# logger = logging.getLogger(__name__)
|
12 |
-
# logger.setLevel(logging.DEBUG)
|
13 |
-
# console_handler = logging.StreamHandler()
|
14 |
-
# file_handler = logging.FileHandler('chatbot_log.log')
|
15 |
-
# formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
16 |
-
# console_handler.setFormatter(formatter)
|
17 |
-
# file_handler.setFormatter(formatter)
|
18 |
-
# logger.addHandler(console_handler)
|
19 |
-
# logger.addHandler(file_handler)
|
20 |
-
|
21 |
-
# # Initialize Groq Client
|
22 |
-
# #client = Groq(api_key=os.getenv("GROQ_API_KEY_2"))
|
23 |
-
|
24 |
-
# client = Groq(
|
25 |
-
# api_key="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",
|
26 |
-
# )
|
27 |
-
|
28 |
-
|
29 |
-
# # Function to encode the image
|
30 |
-
# def encode_image(uploaded_image):
|
31 |
-
# try:
|
32 |
-
# logger.debug("Encoding image...")
|
33 |
-
# buffered = BytesIO()
|
34 |
-
# uploaded_image.save(buffered, format="PNG") # Ensure the correct format
|
35 |
-
# logger.debug("Image encoding complete.")
|
36 |
-
# return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
37 |
-
# except Exception as e:
|
38 |
-
# logger.error(f"Error encoding image: {e}")
|
39 |
-
# raise
|
40 |
-
|
41 |
-
# # Function to handle text and image inputs
|
42 |
-
# def customLLMBot(user_input, uploaded_image, chat_history):
|
43 |
-
# try:
|
44 |
-
# logger.info("Processing input...")
|
45 |
-
|
46 |
-
# if uploaded_image is not None:
|
47 |
-
# # Encode the image to base64
|
48 |
-
# base64_image = encode_image(uploaded_image)
|
49 |
-
# logger.debug(f"Image received, size: {len(base64_image)} bytes")
|
50 |
-
|
51 |
-
# # Create a message specifically for image prompts
|
52 |
-
# messages = [
|
53 |
-
# {
|
54 |
-
# "role": "user",
|
55 |
-
# "content": [
|
56 |
-
# {"type": "text", "text": "What's in this image?"},
|
57 |
-
# {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_image}"}}]
|
58 |
-
# }
|
59 |
-
# ]
|
60 |
-
|
61 |
-
# logger.info("Sending image to Groq API for processing...")
|
62 |
-
# # Send the image message to the Groq API
|
63 |
-
# response = client.chat.completions.create(
|
64 |
-
# model="llama-3.2-11b-vision-preview",
|
65 |
-
# messages=messages,
|
66 |
-
# )
|
67 |
-
# logger.info("Image processed successfully.")
|
68 |
-
# else:
|
69 |
-
# # Process text input
|
70 |
-
# logger.info("Processing text input...")
|
71 |
-
# messages = [
|
72 |
-
# {"role": "system", "content": "You are Dr. HealthBuddy, a professional virtual doctor chatbot."},
|
73 |
-
# {"role": "user", "content": user_input},
|
74 |
-
# ]
|
75 |
-
# response = client.chat.completions.create(
|
76 |
-
# model="llama-3.2-11b-vision-preview",
|
77 |
-
# messages=messages,
|
78 |
-
# )
|
79 |
-
# logger.info("Text processed successfully.")
|
80 |
-
|
81 |
-
# # Extract the reply
|
82 |
-
# LLM_reply = response.choices[0].message.content.strip()
|
83 |
-
# logger.debug(f"LLM reply: {LLM_reply}")
|
84 |
-
|
85 |
-
# # Append user input and bot reply as a tuple to the chat history
|
86 |
-
# chat_history.append((user_input, LLM_reply))
|
87 |
-
|
88 |
-
# return chat_history
|
89 |
-
|
90 |
-
# except Exception as e:
|
91 |
-
# logger.error(f"Error in customLLMBot function: {e}")
|
92 |
-
# return chat_history
|
93 |
-
|
94 |
-
# # Log Viewer Function
|
95 |
-
# def get_logs():
|
96 |
-
# try:
|
97 |
-
# with open('/tmp/chatbot_log.log', 'r') as f:
|
98 |
-
# return f.read()[-3000:] # Return the last 3000 characters of the log
|
99 |
-
# except Exception as e:
|
100 |
-
# return f"Error reading logs: {e}"
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
# def chatbot_ui():
|
106 |
-
|
107 |
-
# chat_history = [] # Initialize empty chat history
|
108 |
-
|
109 |
-
# with gr.Blocks() as demo:
|
110 |
-
# gr.Markdown("# Healthcare Chatbot Doctor")
|
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="Responses", elem_id="chatbot")
|
116 |
-
# user_input = gr.Textbox(
|
117 |
-
# label="Ask a health-related question",
|
118 |
-
# placeholder="Describe your symptoms...",
|
119 |
-
# elem_id="user-input",
|
120 |
-
# lines=1,
|
121 |
-
# )
|
122 |
-
# with gr.Column(scale=1): # Side column for image and buttons
|
123 |
-
# uploaded_image = gr.Image(label="Upload an Image", type="pil")
|
124 |
-
# submit_btn = gr.Button("Submit")
|
125 |
-
# clear_btn = gr.Button("Clear")
|
126 |
-
# audio_output = gr.Audio(label="Audio Response")
|
127 |
-
# # Log Viewer
|
128 |
-
# with gr.Row():
|
129 |
-
# log_view = gr.Textbox(label="Logs", lines=10, interactive=False)
|
130 |
-
# view_logs_btn = gr.Button("View Logs")
|
131 |
-
# download_logs_btn = gr.Button("Download Logs")
|
132 |
-
|
133 |
-
# # Define actions
|
134 |
-
# def handle_submit(user_query, image):
|
135 |
-
# response = customLLMBot(user_query, image, chat_history)
|
136 |
-
# return response, None, "" # Return chat history and reset input
|
137 |
-
|
138 |
-
# # Submit on pressing Enter key
|
139 |
-
# user_input.submit(
|
140 |
-
# handle_submit,
|
141 |
-
# inputs=[user_input, uploaded_image],
|
142 |
-
# outputs=[chatbot, audio_output, user_input],
|
143 |
-
# )
|
144 |
-
|
145 |
-
# # Submit on button click
|
146 |
-
# submit_btn.click(
|
147 |
-
# handle_submit,
|
148 |
-
# inputs=[user_input, uploaded_image],
|
149 |
-
# outputs=[chatbot, audio_output, user_input],
|
150 |
-
# )
|
151 |
-
|
152 |
-
# # Clear button action
|
153 |
-
# clear_btn.click(
|
154 |
-
# lambda: ([], None, ""),
|
155 |
-
# inputs=[],
|
156 |
-
# outputs=[chatbot, uploaded_image, user_input],
|
157 |
-
# )
|
158 |
-
|
159 |
-
# # View Logs Button
|
160 |
-
# view_logs_btn.click(
|
161 |
-
# get_logs,
|
162 |
-
# inputs=[],
|
163 |
-
# outputs=[log_view],
|
164 |
-
# )
|
165 |
-
|
166 |
-
# # Download Logs Button
|
167 |
-
# def download_logs():
|
168 |
-
# return '/tmp/chatbot_log.log'
|
169 |
-
|
170 |
-
# download_logs_btn.click(
|
171 |
-
# download_logs,
|
172 |
-
# inputs=[],
|
173 |
-
# outputs=[gr.File()],
|
174 |
-
# )
|
175 |
-
|
176 |
-
# return demo
|
177 |
-
|
178 |
-
|
179 |
-
# # Launch the interface
|
180 |
-
# #chatbot_ui().launch(share=True,server_name="0.0.0.0", server_port=7860)
|
181 |
-
|
182 |
-
# chatbot_ui().launch(share=True,server_name="localhost", server_port=7860)
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
from groq import Groq
|
187 |
import gradio as gr
|
188 |
from gtts import gTTS
|
@@ -286,14 +101,14 @@ def customLLMBot(user_input, uploaded_image, chat_history):
|
|
286 |
return [(("user", user_input or "Image uploaded"), ("bot", f"An error occurred: {e}"))], None
|
287 |
|
288 |
|
289 |
-
|
290 |
# Gradio Interface
|
291 |
def chatbot_ui():
|
292 |
-
chat_history = [] # Initialize empty chat history
|
293 |
-
|
294 |
with gr.Blocks() as demo:
|
295 |
gr.Markdown("# Healthcare Chatbot Doctor")
|
296 |
|
|
|
|
|
|
|
297 |
# Layout for chatbot and input box alignment
|
298 |
with gr.Row():
|
299 |
with gr.Column(scale=3): # Main column for chatbot
|
@@ -311,30 +126,30 @@ def chatbot_ui():
|
|
311 |
audio_output = gr.Audio(label="Audio Response")
|
312 |
|
313 |
# Define actions
|
314 |
-
def handle_submit(user_query, image):
|
315 |
logger.info("User submitted a query.")
|
316 |
-
response, audio = customLLMBot(user_query, image,
|
317 |
-
return response, audio, ""
|
318 |
|
319 |
# Submit on pressing Enter key
|
320 |
user_input.submit(
|
321 |
handle_submit,
|
322 |
-
inputs=[user_input, uploaded_image],
|
323 |
-
outputs=[chatbot, audio_output, user_input],
|
324 |
)
|
325 |
|
326 |
# Submit on button click
|
327 |
submit_btn.click(
|
328 |
handle_submit,
|
329 |
-
inputs=[user_input, uploaded_image],
|
330 |
-
outputs=[chatbot, audio_output, user_input],
|
331 |
)
|
332 |
|
333 |
# Action for clearing all fields
|
334 |
clear_btn.click(
|
335 |
-
lambda: ([], "", None,
|
336 |
inputs=[],
|
337 |
-
outputs=[chatbot, user_input, uploaded_image,
|
338 |
)
|
339 |
|
340 |
return demo
|
@@ -342,5 +157,3 @@ def chatbot_ui():
|
|
342 |
|
343 |
# Launch the interface
|
344 |
chatbot_ui().launch(server_name="0.0.0.0", server_port=7860)
|
345 |
-
|
346 |
-
#chatbot_ui().launch(server_name="localhost", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from groq import Groq
|
2 |
import gradio as gr
|
3 |
from gtts import gTTS
|
|
|
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
|
|
|
126 |
audio_output = gr.Audio(label="Audio Response")
|
127 |
|
128 |
# Define actions
|
129 |
+
def handle_submit(user_query, image, history):
|
130 |
logger.info("User submitted a query.")
|
131 |
+
response, audio = customLLMBot(user_query, image, history)
|
132 |
+
return response, audio, "", history
|
133 |
|
134 |
# Submit on pressing Enter key
|
135 |
user_input.submit(
|
136 |
handle_submit,
|
137 |
+
inputs=[user_input, uploaded_image, chat_history],
|
138 |
+
outputs=[chatbot, audio_output, user_input, chat_history],
|
139 |
)
|
140 |
|
141 |
# Submit on button click
|
142 |
submit_btn.click(
|
143 |
handle_submit,
|
144 |
+
inputs=[user_input, uploaded_image, chat_history],
|
145 |
+
outputs=[chatbot, audio_output, user_input, chat_history],
|
146 |
)
|
147 |
|
148 |
# Action for clearing all fields
|
149 |
clear_btn.click(
|
150 |
+
lambda: ([], "", None, []),
|
151 |
inputs=[],
|
152 |
+
outputs=[chatbot, user_input, uploaded_image, chat_history],
|
153 |
)
|
154 |
|
155 |
return demo
|
|
|
157 |
|
158 |
# Launch the interface
|
159 |
chatbot_ui().launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|