Update app.py
Browse files
app.py
CHANGED
|
@@ -3,17 +3,17 @@ import gradio as gr
|
|
| 3 |
import os
|
| 4 |
import speech_recognition as sr
|
| 5 |
|
| 6 |
-
#
|
| 7 |
groqkey = os.getenv("groqkey")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
if not groqkey:
|
| 11 |
raise ValueError("π¨ Error: 'groqkey' is missing! Make sure it is set in Hugging Face Secrets.")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
client = Groq(api_key=groqkey)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
def get_chatbot_response(user_message, insurance_type, country, claim_number, image, language, conversation_history):
|
| 18 |
"""Fetch insurance-related responses from the AI."""
|
| 19 |
|
|
@@ -44,7 +44,7 @@ def get_chatbot_response(user_message, insurance_type, country, claim_number, im
|
|
| 44 |
if image:
|
| 45 |
conversation_history.append({"role": "user", "content": "[User uploaded an image]"})
|
| 46 |
|
| 47 |
-
#
|
| 48 |
completion = client.chat.completions.create(
|
| 49 |
model="mixtral-8x7b",
|
| 50 |
messages=conversation_history,
|
|
@@ -65,7 +65,7 @@ def get_chatbot_response(user_message, insurance_type, country, claim_number, im
|
|
| 65 |
|
| 66 |
return conversation_history, chat_display
|
| 67 |
|
| 68 |
-
#
|
| 69 |
def transcribe_audio(audio):
|
| 70 |
recognizer = sr.Recognizer()
|
| 71 |
with sr.AudioFile(audio) as source:
|
|
@@ -78,7 +78,7 @@ def transcribe_audio(audio):
|
|
| 78 |
except sr.RequestError:
|
| 79 |
return "Error processing the audio."
|
| 80 |
|
| 81 |
-
#
|
| 82 |
theme = gr.themes.Base().set(
|
| 83 |
body_background_fill="linear-gradient(to right, #001F3F, #0052CC)", # Dark navy to royal blue
|
| 84 |
button_primary_background_fill="linear-gradient(135deg, #FFD700, #FFAA00)", # Golden gradient
|
|
@@ -134,9 +134,9 @@ button.primary:hover {
|
|
| 134 |
}
|
| 135 |
"""
|
| 136 |
|
| 137 |
-
#
|
| 138 |
with gr.Blocks(theme=theme, css=custom_css) as demo:
|
| 139 |
-
gr.HTML("<h2 class='title-text'
|
| 140 |
gr.Markdown("### Select your insurance type, country, and ask your question.")
|
| 141 |
|
| 142 |
with gr.Row():
|
|
@@ -162,13 +162,13 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
|
|
| 162 |
|
| 163 |
conversation_state = gr.State([])
|
| 164 |
|
| 165 |
-
chatbot = gr.Chatbot(label="Chat History", type="messages") #
|
| 166 |
|
| 167 |
clear_button = gr.Button("Clear Chat History")
|
| 168 |
clear_button.click(lambda: [], outputs=[conversation_state, chatbot])
|
| 169 |
|
| 170 |
question_input = gr.Textbox(label="Ask your question...", placeholder="Describe your insurance question...", interactive=True)
|
| 171 |
-
image_input = gr.File(label="Upload an image (optional)", type="filepath") #
|
| 172 |
voice_input = gr.Audio(label="Speak your question (optional)", type="filepath")
|
| 173 |
|
| 174 |
submit_btn = gr.Button("Send")
|
|
|
|
| 3 |
import os
|
| 4 |
import speech_recognition as sr
|
| 5 |
|
| 6 |
+
# Securely retrieve the Groq API key from Hugging Face Secrets
|
| 7 |
groqkey = os.getenv("groqkey")
|
| 8 |
|
| 9 |
+
# Ensure the API key exists
|
| 10 |
if not groqkey:
|
| 11 |
raise ValueError("π¨ Error: 'groqkey' is missing! Make sure it is set in Hugging Face Secrets.")
|
| 12 |
|
| 13 |
+
# Initialize the Groq client
|
| 14 |
client = Groq(api_key=groqkey)
|
| 15 |
|
| 16 |
+
# Define chatbot response function
|
| 17 |
def get_chatbot_response(user_message, insurance_type, country, claim_number, image, language, conversation_history):
|
| 18 |
"""Fetch insurance-related responses from the AI."""
|
| 19 |
|
|
|
|
| 44 |
if image:
|
| 45 |
conversation_history.append({"role": "user", "content": "[User uploaded an image]"})
|
| 46 |
|
| 47 |
+
# API call to Groq
|
| 48 |
completion = client.chat.completions.create(
|
| 49 |
model="mixtral-8x7b",
|
| 50 |
messages=conversation_history,
|
|
|
|
| 65 |
|
| 66 |
return conversation_history, chat_display
|
| 67 |
|
| 68 |
+
# Convert voice input to text
|
| 69 |
def transcribe_audio(audio):
|
| 70 |
recognizer = sr.Recognizer()
|
| 71 |
with sr.AudioFile(audio) as source:
|
|
|
|
| 78 |
except sr.RequestError:
|
| 79 |
return "Error processing the audio."
|
| 80 |
|
| 81 |
+
# Styling and Theme
|
| 82 |
theme = gr.themes.Base().set(
|
| 83 |
body_background_fill="linear-gradient(to right, #001F3F, #0052CC)", # Dark navy to royal blue
|
| 84 |
button_primary_background_fill="linear-gradient(135deg, #FFD700, #FFAA00)", # Golden gradient
|
|
|
|
| 134 |
}
|
| 135 |
"""
|
| 136 |
|
| 137 |
+
# Build Gradio Interface
|
| 138 |
with gr.Blocks(theme=theme, css=custom_css) as demo:
|
| 139 |
+
gr.HTML("<h2 class='title-text'>π‘οΈ AI Insurance Chatbot</h2>")
|
| 140 |
gr.Markdown("### Select your insurance type, country, and ask your question.")
|
| 141 |
|
| 142 |
with gr.Row():
|
|
|
|
| 162 |
|
| 163 |
conversation_state = gr.State([])
|
| 164 |
|
| 165 |
+
chatbot = gr.Chatbot(label="Chat History", type="messages") # Fix: Set type to "messages"
|
| 166 |
|
| 167 |
clear_button = gr.Button("Clear Chat History")
|
| 168 |
clear_button.click(lambda: [], outputs=[conversation_state, chatbot])
|
| 169 |
|
| 170 |
question_input = gr.Textbox(label="Ask your question...", placeholder="Describe your insurance question...", interactive=True)
|
| 171 |
+
image_input = gr.File(label="Upload an image (optional)", type="filepath") # Fix: Change type to "filepath"
|
| 172 |
voice_input = gr.Audio(label="Speak your question (optional)", type="filepath")
|
| 173 |
|
| 174 |
submit_btn = gr.Button("Send")
|