Reshmarb commited on
Commit
d32256a
·
1 Parent(s): 03876cc
Files changed (1) hide show
  1. app.py +42 -48
app.py CHANGED
@@ -6,9 +6,8 @@ import base64
6
  from io import BytesIO
7
  import os
8
  import logging
9
- import tempfile
10
 
11
- # Logging Setup
12
  logger = logging.getLogger(__name__)
13
  logger.setLevel(logging.DEBUG)
14
  console_handler = logging.StreamHandler()
@@ -20,97 +19,90 @@ logger.addHandler(console_handler)
20
  logger.addHandler(file_handler)
21
 
22
  # Initialize Groq Client
23
- api_key = os.getenv("GROQ_API_KEY_2")
24
- if not api_key:
25
- logger.error("GROQ_API_KEY_2 environment variable is not set. Exiting...")
26
- raise EnvironmentError("Missing GROQ_API_KEY_2 environment variable.")
27
- client = Groq(api_key=api_key)
28
 
29
- # Function to encode image
30
  def encode_image(uploaded_image):
31
  try:
32
- if not uploaded_image:
33
- raise ValueError("No image provided.")
34
  logger.debug("Encoding image...")
35
  buffered = BytesIO()
36
- uploaded_image.save(buffered, format="PNG")
 
37
  return base64.b64encode(buffered.getvalue()).decode("utf-8")
38
  except Exception as e:
39
  logger.error(f"Error encoding image: {e}")
40
  raise
41
 
42
- # Function to handle user input
43
  def customLLMBot(user_input, uploaded_image, chat_history):
44
  try:
45
- # Check for valid inputs
46
- if not user_input.strip() and uploaded_image is None:
47
- raise ValueError("Either text input or an image is required.")
48
 
49
  # Append user input to the chat history
50
  chat_history.append(("User", user_input))
51
 
52
- # Process image if provided
53
  if uploaded_image is not None:
 
54
  base64_image = encode_image(uploaded_image)
55
- logger.debug(f"Encoded image size: {len(base64_image)} bytes")
56
 
 
 
 
 
57
  messages = [
58
  {
59
  "role": "user",
60
  "content": [
61
  {"type": "text", "text": "What's in this image?"},
62
- {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_image}"}}
63
- ]
64
  }
65
  ]
66
- logger.info("Sending image to Groq API...")
 
 
67
  response = client.chat.completions.create(
68
  model="llama-3.2-11b-vision-preview",
69
  messages=messages,
70
  )
 
71
  else:
72
  # Process text input
 
73
  messages = [
74
  {"role": "system", "content": "You are Dr. HealthBuddy, a professional virtual doctor chatbot."},
75
  {"role": "user", "content": user_input},
76
  ]
77
- logger.info("Sending text to Groq API...")
78
  response = client.chat.completions.create(
79
  model="llama-3.2-11b-vision-preview",
80
  messages=messages,
81
  )
 
82
 
83
- # Extract response
84
  LLM_reply = response.choices[0].message.content
85
  logger.debug(f"LLM reply: {LLM_reply}")
 
 
86
  chat_history.append(("Bot", LLM_reply))
87
 
88
- # Generate audio response
89
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_audio:
90
- tts = gTTS(LLM_reply, lang='en')
91
- tts.save(tmp_audio.name)
92
- audio_file = tmp_audio.name
93
- logger.info(f"Audio response saved: {audio_file}")
94
 
95
- # Return chat history and audio file
96
- return [(entry[0], entry[1]) for entry in chat_history], audio_file
97
 
98
  except Exception as e:
99
- logger.error(f"Error in customLLMBot: {e}")
100
- return [("User", f"An error occurred: {e}")], None
 
101
 
102
- # Cleanup Function for Audio
103
- def cleanup_audio(audio_file):
104
- try:
105
- if os.path.exists(audio_file):
106
- os.remove(audio_file)
107
- logger.info(f"Cleaned up audio file: {audio_file}")
108
- except Exception as e:
109
- logger.warning(f"Failed to delete audio file {audio_file}: {e}")
110
 
111
  # Gradio Interface
112
  def chatbot_ui():
113
- chat_history = [] # Initialize empty chat history for the session
114
 
115
  with gr.Blocks() as demo:
116
  gr.Markdown("# Healthcare Chatbot Doctor")
@@ -151,15 +143,17 @@ def chatbot_ui():
151
  outputs=[chatbot, audio_output, user_input],
152
  )
153
 
154
- # Action for clearing all fields and resetting chat history
155
- def clear_chat():
156
- nonlocal chat_history
157
- chat_history = [] # Reset chat history for new session
158
- return [], "", None, None
159
-
160
- clear_btn.click(clear_chat, inputs=[], outputs=[chatbot, user_input, uploaded_image, audio_output])
161
 
162
  return demo
163
 
 
164
  # Launch the interface
165
  chatbot_ui().launch(server_name="0.0.0.0", server_port=7860)
 
 
 
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()
 
19
  logger.addHandler(file_handler)
20
 
21
  # Initialize Groq Client
22
+ client = Groq(api_key=os.getenv("GROQ_API_KEY_2"))
 
 
 
 
23
 
24
+ # Function to encode the image
25
  def encode_image(uploaded_image):
26
  try:
 
 
27
  logger.debug("Encoding image...")
28
  buffered = BytesIO()
29
+ uploaded_image.save(buffered, format="PNG") # Ensure the correct format
30
+ logger.debug("Image encoding complete.")
31
  return base64.b64encode(buffered.getvalue()).decode("utf-8")
32
  except Exception as e:
33
  logger.error(f"Error encoding image: {e}")
34
  raise
35
 
36
+ # Function to handle text and image inputs
37
  def customLLMBot(user_input, uploaded_image, chat_history):
38
  try:
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
46
  base64_image = encode_image(uploaded_image)
 
47
 
48
+ # Log the image size and type
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
83
  logger.debug(f"LLM reply: {LLM_reply}")
84
+
85
+ # Append the bot's response to the chat history
86
  chat_history.append(("Bot", LLM_reply))
87
 
88
+ # Generate audio for response
89
+ audio_file = f"response_{uuid.uuid4().hex}.mp3"
90
+ tts = gTTS(LLM_reply, lang='en')
91
+ tts.save(audio_file)
92
+ logger.info(f"Audio response saved as {audio_file}")
 
93
 
94
+ # Return chat history as a list of tuples (sender, message)
95
+ return [(entry[1], entry[0]) for entry in chat_history], audio_file
96
 
97
  except Exception as e:
98
+ # Handle errors gracefully
99
+ logger.error(f"Error in customLLMBot function: {e}")
100
+ return [(user_input or "Image uploaded", f"An error occurred: {e}")], None
101
 
 
 
 
 
 
 
 
 
102
 
103
  # Gradio Interface
104
  def chatbot_ui():
105
+ chat_history = [] # Initialize empty chat history
106
 
107
  with gr.Blocks() as demo:
108
  gr.Markdown("# Healthcare Chatbot Doctor")
 
143
  outputs=[chatbot, audio_output, user_input],
144
  )
145
 
146
+ # Action for clearing all fields
147
+ clear_btn.click(
148
+ lambda: ([], "", None, None),
149
+ inputs=[],
150
+ outputs=[chatbot, user_input, uploaded_image, audio_output],
151
+ )
 
152
 
153
  return demo
154
 
155
+
156
  # Launch the interface
157
  chatbot_ui().launch(server_name="0.0.0.0", server_port=7860)
158
+
159
+ #chatbot_ui().launch(server_name="localhost", server_port=7860)