Spaces:
Sleeping
Sleeping
modified
Browse files- app.py +38 -12
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -5,11 +5,20 @@ import uuid
|
|
| 5 |
import base64
|
| 6 |
from io import BytesIO
|
| 7 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Initialize Groq Client
|
| 10 |
-
# client = Groq(
|
| 11 |
-
# api_key="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",
|
| 12 |
-
# )
|
| 13 |
|
| 14 |
client = Groq(api_key=os.getenv("GROQ_API_KEY_2"))
|
| 15 |
|
|
@@ -19,35 +28,48 @@ client = Groq(api_key=os.getenv("GROQ_API_KEY_2"))
|
|
| 19 |
|
| 20 |
# Function to encode the image
|
| 21 |
def encode_image(uploaded_image):
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# Function to handle text and image inputs
|
| 27 |
def customLLMBot(user_input, uploaded_image):
|
| 28 |
try:
|
|
|
|
|
|
|
| 29 |
if uploaded_image is not None:
|
| 30 |
# Encode the image to base64
|
| 31 |
base64_image = encode_image(uploaded_image)
|
| 32 |
|
| 33 |
-
#
|
|
|
|
|
|
|
|
|
|
| 34 |
messages = [
|
| 35 |
{
|
| 36 |
"role": "user",
|
| 37 |
"content": [
|
| 38 |
{"type": "text", "text": "What's in this image?"},
|
| 39 |
-
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_image}"}}
|
| 40 |
-
],
|
| 41 |
}
|
| 42 |
]
|
| 43 |
|
|
|
|
| 44 |
# Send the image message to the Groq API
|
| 45 |
response = client.chat.completions.create(
|
| 46 |
model="llama-3.2-11b-vision-preview",
|
| 47 |
messages=messages,
|
| 48 |
)
|
|
|
|
| 49 |
else:
|
| 50 |
-
# Process text input
|
|
|
|
| 51 |
messages = [
|
| 52 |
{"role": "system", "content": "You are Dr. HealthBuddy, a professional virtual doctor chatbot."},
|
| 53 |
{"role": "user", "content": user_input},
|
|
@@ -56,21 +78,24 @@ def customLLMBot(user_input, uploaded_image):
|
|
| 56 |
model="llama-3.2-11b-vision-preview",
|
| 57 |
messages=messages,
|
| 58 |
)
|
|
|
|
| 59 |
|
| 60 |
# Extract the reply
|
| 61 |
LLM_reply = response.choices[0].message.content
|
|
|
|
| 62 |
|
| 63 |
# Generate audio for response
|
| 64 |
audio_file = f"response_{uuid.uuid4().hex}.mp3"
|
| 65 |
tts = gTTS(LLM_reply, lang='en')
|
| 66 |
tts.save(audio_file)
|
|
|
|
| 67 |
|
| 68 |
return [(user_input or "Image uploaded", LLM_reply)], audio_file
|
| 69 |
|
| 70 |
except Exception as e:
|
| 71 |
# Handle errors gracefully
|
| 72 |
-
|
| 73 |
-
return [(user_input or "Image uploaded",
|
| 74 |
|
| 75 |
|
| 76 |
# Gradio Interface
|
|
@@ -96,6 +121,7 @@ def chatbot_ui():
|
|
| 96 |
|
| 97 |
# Define actions
|
| 98 |
def handle_submit(user_query, image):
|
|
|
|
| 99 |
response, audio = customLLMBot(user_query, image)
|
| 100 |
return response, audio, ""
|
| 101 |
|
|
|
|
| 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 |
|
| 23 |
client = Groq(api_key=os.getenv("GROQ_API_KEY_2"))
|
| 24 |
|
|
|
|
| 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):
|
| 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 |
|
| 50 |
+
# Log the image size and type
|
| 51 |
+
logger.debug(f"Image received, size: {len(base64_image)} bytes")
|
| 52 |
+
|
| 53 |
+
# Create a message specifically for image prompts
|
| 54 |
messages = [
|
| 55 |
{
|
| 56 |
"role": "user",
|
| 57 |
"content": [
|
| 58 |
{"type": "text", "text": "What's in this image?"},
|
| 59 |
+
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_image}"}}]
|
|
|
|
| 60 |
}
|
| 61 |
]
|
| 62 |
|
| 63 |
+
logger.info("Sending image to Groq API for processing...")
|
| 64 |
# Send the image message to the Groq API
|
| 65 |
response = client.chat.completions.create(
|
| 66 |
model="llama-3.2-11b-vision-preview",
|
| 67 |
messages=messages,
|
| 68 |
)
|
| 69 |
+
logger.info("Image processed successfully.")
|
| 70 |
else:
|
| 71 |
+
# Process text input
|
| 72 |
+
logger.info("Processing text input...")
|
| 73 |
messages = [
|
| 74 |
{"role": "system", "content": "You are Dr. HealthBuddy, a professional virtual doctor chatbot."},
|
| 75 |
{"role": "user", "content": user_input},
|
|
|
|
| 78 |
model="llama-3.2-11b-vision-preview",
|
| 79 |
messages=messages,
|
| 80 |
)
|
| 81 |
+
logger.info("Text processed successfully.")
|
| 82 |
|
| 83 |
# Extract the reply
|
| 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 |
return [(user_input or "Image uploaded", LLM_reply)], audio_file
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
# Handle errors gracefully
|
| 97 |
+
logger.error(f"Error in customLLMBot function: {e}")
|
| 98 |
+
return [(user_input or "Image uploaded", f"An error occurred: {e}")], None
|
| 99 |
|
| 100 |
|
| 101 |
# Gradio Interface
|
|
|
|
| 121 |
|
| 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 |
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
gtts
|
| 2 |
gradio
|
| 3 |
groq
|
|
|
|
|
|
| 1 |
gtts
|
| 2 |
gradio
|
| 3 |
groq
|
| 4 |
+
loguru
|