JenetGhumman commited on
Commit
6215013
Β·
verified Β·
1 Parent(s): 0aa524d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -18
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
- groqkey = os.getenv("groqkey") # Must match the exact key name
 
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()