Spaces:
Running
Running
modified
Browse files
app.py
CHANGED
@@ -19,7 +19,6 @@ logger.addHandler(console_handler)
|
|
19 |
logger.addHandler(file_handler)
|
20 |
|
21 |
# Initialize Groq Client
|
22 |
-
|
23 |
client = Groq(api_key=os.getenv("GROQ_API_KEY_2"))
|
24 |
|
25 |
# client = Groq(
|
@@ -39,10 +38,13 @@ def encode_image(uploaded_image):
|
|
39 |
raise
|
40 |
|
41 |
# Function to handle text and image inputs
|
42 |
-
def customLLMBot(user_input, uploaded_image):
|
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)
|
@@ -84,13 +86,17 @@ def customLLMBot(user_input, uploaded_image):
|
|
84 |
LLM_reply = response.choices[0].message.content
|
85 |
logger.debug(f"LLM reply: {LLM_reply}")
|
86 |
|
|
|
|
|
|
|
87 |
# Generate audio for response
|
88 |
audio_file = f"response_{uuid.uuid4().hex}.mp3"
|
89 |
tts = gTTS(LLM_reply, lang='en')
|
90 |
tts.save(audio_file)
|
91 |
logger.info(f"Audio response saved as {audio_file}")
|
92 |
|
93 |
-
|
|
|
94 |
|
95 |
except Exception as e:
|
96 |
# Handle errors gracefully
|
@@ -100,6 +106,8 @@ def customLLMBot(user_input, uploaded_image):
|
|
100 |
|
101 |
# Gradio Interface
|
102 |
def chatbot_ui():
|
|
|
|
|
103 |
with gr.Blocks() as demo:
|
104 |
gr.Markdown("# Healthcare Chatbot Doctor")
|
105 |
|
@@ -122,7 +130,7 @@ def chatbot_ui():
|
|
122 |
# Define actions
|
123 |
def handle_submit(user_query, image):
|
124 |
logger.info("User submitted a query.")
|
125 |
-
response, audio = customLLMBot(user_query, image)
|
126 |
return response, audio, ""
|
127 |
|
128 |
# Submit on pressing Enter key
|
@@ -152,4 +160,4 @@ def chatbot_ui():
|
|
152 |
# Launch the interface
|
153 |
chatbot_ui().launch(server_name="0.0.0.0", server_port=7860)
|
154 |
|
155 |
-
#chatbot_ui().launch(server_name="localhost", server_port=7860)
|
|
|
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(
|
|
|
38 |
raise
|
39 |
|
40 |
# Function to handle text and image inputs
|
41 |
+
def customLLMBot(user_input, uploaded_image, chat_history):
|
42 |
try:
|
43 |
logger.info("Processing input...")
|
44 |
|
45 |
+
# Append user input to the chat history
|
46 |
+
chat_history.append(("User", user_input))
|
47 |
+
|
48 |
if uploaded_image is not None:
|
49 |
# Encode the image to base64
|
50 |
base64_image = encode_image(uploaded_image)
|
|
|
86 |
LLM_reply = response.choices[0].message.content
|
87 |
logger.debug(f"LLM reply: {LLM_reply}")
|
88 |
|
89 |
+
# Append the bot's response to the chat history
|
90 |
+
chat_history.append(("Bot", LLM_reply))
|
91 |
+
|
92 |
# Generate audio for response
|
93 |
audio_file = f"response_{uuid.uuid4().hex}.mp3"
|
94 |
tts = gTTS(LLM_reply, lang='en')
|
95 |
tts.save(audio_file)
|
96 |
logger.info(f"Audio response saved as {audio_file}")
|
97 |
|
98 |
+
# Return the chat history and audio file
|
99 |
+
return [(entry[0], entry[1]) for entry in chat_history], audio_file
|
100 |
|
101 |
except Exception as e:
|
102 |
# Handle errors gracefully
|
|
|
106 |
|
107 |
# Gradio Interface
|
108 |
def chatbot_ui():
|
109 |
+
chat_history = [] # Initialize empty chat history
|
110 |
+
|
111 |
with gr.Blocks() as demo:
|
112 |
gr.Markdown("# Healthcare Chatbot Doctor")
|
113 |
|
|
|
130 |
# Define actions
|
131 |
def handle_submit(user_query, image):
|
132 |
logger.info("User submitted a query.")
|
133 |
+
response, audio = customLLMBot(user_query, image, chat_history)
|
134 |
return response, audio, ""
|
135 |
|
136 |
# Submit on pressing Enter key
|
|
|
160 |
# Launch the interface
|
161 |
chatbot_ui().launch(server_name="0.0.0.0", server_port=7860)
|
162 |
|
163 |
+
#chatbot_ui().launch(server_name="localhost", server_port=7860)
|