Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,18 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
"""Untitled13.ipynb
|
| 3 |
-
|
| 4 |
-
Automatically generated by Colab.
|
| 5 |
-
|
| 6 |
-
Original file is located at
|
| 7 |
-
https://colab.research.google.com/drive/1glmxSLUi1Bpo3p91wMqTNmzipbGn_j-j
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
from groq import Groq
|
| 11 |
import gradio as gr
|
| 12 |
import os
|
| 13 |
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
# Ensure the API key exists
|
| 17 |
if not groqkey:
|
| 18 |
-
raise ValueError("Error: 'groqkey' is missing! Make sure it is set in Hugging Face Secrets.")
|
| 19 |
|
| 20 |
-
# Initialize the Groq client
|
| 21 |
client = Groq(api_key=groqkey)
|
| 22 |
|
| 23 |
-
# Define chatbot response function
|
| 24 |
def get_chatbot_response(user_message, insurance_type, country, conversation_history):
|
| 25 |
"""Fetch insurance-related responses from the AI."""
|
| 26 |
|
|
@@ -41,7 +33,7 @@ def get_chatbot_response(user_message, insurance_type, country, conversation_his
|
|
| 41 |
|
| 42 |
conversation_history.append({"role": "user", "content": user_message})
|
| 43 |
|
| 44 |
-
# API call to Groq
|
| 45 |
completion = client.chat.completions.create(
|
| 46 |
model="mixtral-8x7b",
|
| 47 |
messages=conversation_history,
|
|
@@ -62,12 +54,13 @@ def get_chatbot_response(user_message, insurance_type, country, conversation_his
|
|
| 62 |
|
| 63 |
return conversation_history, chat_display
|
| 64 |
|
| 65 |
-
# Styling and Theme
|
| 66 |
theme = gr.themes.Base().set(
|
| 67 |
body_background_fill="linear-gradient(to right, #0052CC, #00AFFF)",
|
| 68 |
button_primary_background_fill="#0052CC",
|
| 69 |
button_primary_text_color="white"
|
| 70 |
)
|
|
|
|
| 71 |
custom_css = """
|
| 72 |
.title-text {
|
| 73 |
background: #0052CC;
|
|
@@ -98,7 +91,7 @@ custom_css = """
|
|
| 98 |
def clear_history():
|
| 99 |
return []
|
| 100 |
|
| 101 |
-
# Build Gradio Interface
|
| 102 |
with gr.Blocks(theme=theme, css=custom_css) as demo:
|
| 103 |
gr.HTML("<h2 class='title-text'>π‘οΈ AI Insurance Chatbot</h2>")
|
| 104 |
gr.Markdown("### Select your insurance type, country, and ask your question!")
|
|
@@ -119,6 +112,10 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
|
|
| 119 |
|
| 120 |
conversation_state = gr.State([])
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
clear_button = gr.Button("Clear Chat History")
|
| 123 |
clear_button.click(lambda: [], outputs=[conversation_state, chatbot])
|
| 124 |
|
|
@@ -167,4 +164,4 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
|
|
| 167 |
outputs=[conversation_state, chatbot, question_input]
|
| 168 |
)
|
| 169 |
|
| 170 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from groq import Groq
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# β
Securely retrieve the Groq API key from Hugging Face Secrets
|
| 6 |
+
groqkey = os.getenv("groqkey") # Make sure this matches the secret name in Hugging Face
|
| 7 |
|
| 8 |
+
# β
Ensure the API key exists
|
| 9 |
if not groqkey:
|
| 10 |
+
raise ValueError("π¨ Error: 'groqkey' is missing! Make sure it is set in Hugging Face Secrets.")
|
| 11 |
|
| 12 |
+
# β
Initialize the Groq client
|
| 13 |
client = Groq(api_key=groqkey)
|
| 14 |
|
| 15 |
+
# β
Define chatbot response function
|
| 16 |
def get_chatbot_response(user_message, insurance_type, country, conversation_history):
|
| 17 |
"""Fetch insurance-related responses from the AI."""
|
| 18 |
|
|
|
|
| 33 |
|
| 34 |
conversation_history.append({"role": "user", "content": user_message})
|
| 35 |
|
| 36 |
+
# β
API call to Groq
|
| 37 |
completion = client.chat.completions.create(
|
| 38 |
model="mixtral-8x7b",
|
| 39 |
messages=conversation_history,
|
|
|
|
| 54 |
|
| 55 |
return conversation_history, chat_display
|
| 56 |
|
| 57 |
+
# β
Styling and Theme
|
| 58 |
theme = gr.themes.Base().set(
|
| 59 |
body_background_fill="linear-gradient(to right, #0052CC, #00AFFF)",
|
| 60 |
button_primary_background_fill="#0052CC",
|
| 61 |
button_primary_text_color="white"
|
| 62 |
)
|
| 63 |
+
|
| 64 |
custom_css = """
|
| 65 |
.title-text {
|
| 66 |
background: #0052CC;
|
|
|
|
| 91 |
def clear_history():
|
| 92 |
return []
|
| 93 |
|
| 94 |
+
# β
Build Gradio Interface
|
| 95 |
with gr.Blocks(theme=theme, css=custom_css) as demo:
|
| 96 |
gr.HTML("<h2 class='title-text'>π‘οΈ AI Insurance Chatbot</h2>")
|
| 97 |
gr.Markdown("### Select your insurance type, country, and ask your question!")
|
|
|
|
| 112 |
|
| 113 |
conversation_state = gr.State([])
|
| 114 |
|
| 115 |
+
# β
Define chatbot BEFORE using it in clear button
|
| 116 |
+
chatbot = gr.Chatbot(label="π¬ Chat History")
|
| 117 |
+
|
| 118 |
+
# β
Define the clear chat button AFTER chatbot
|
| 119 |
clear_button = gr.Button("Clear Chat History")
|
| 120 |
clear_button.click(lambda: [], outputs=[conversation_state, chatbot])
|
| 121 |
|
|
|
|
| 164 |
outputs=[conversation_state, chatbot, question_input]
|
| 165 |
)
|
| 166 |
|
| 167 |
+
demo.launch()
|