mfraz commited on
Commit
a3e9a25
·
verified ·
1 Parent(s): 672c29b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -1,11 +1,15 @@
1
-
2
  import gradio as gr
3
  import requests
 
 
 
 
4
 
5
- # Groq API Key (Replace with your actual key)
6
- GROQ_API_KEY = "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"
 
7
 
8
- # Correct Groq API URL for chat completion
9
  GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
10
 
11
  # Function to interact with Groq API
@@ -13,7 +17,7 @@ def chat_with_groq(prompt, history):
13
  headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
14
 
15
  # System role for chatbot identity
16
- messages = [{"role": "system", "content": "You are Faraz-Chatbot, an AI assistant created by Muhammad Faraz to provide helpful responses."}]
17
 
18
  # Ensure history is in the correct format
19
  for entry in history:
@@ -24,13 +28,13 @@ def chat_with_groq(prompt, history):
24
 
25
  # Add the latest user query
26
  messages.append({"role": "user", "content": prompt})
27
-
28
  # Custom response when asked about the owner
29
  if "who is your owner" in prompt.lower() or "who created you" in prompt.lower():
30
  return "I was created by Muhammad Faraz, a passionate developer!"
31
 
32
- payload = {"model": "llama3-8b-8192", "messages": messages} # Groq's LLaMA 3 model
33
-
34
  try:
35
  response = requests.post(GROQ_API_URL, headers=headers, json=payload)
36
  response_data = response.json()
@@ -46,14 +50,12 @@ def chat_with_groq(prompt, history):
46
  # Function to handle chatbot responses
47
  def chatbot(query, history):
48
  if not query.strip():
49
- return history, "" # Return previous history and clear input box
50
-
51
- response = chat_with_groq(query, history)
52
 
53
- # Append user query and bot response as a list of lists
54
  history = history + [[query, response]]
55
 
56
- return history, "" # Clear the input box after response
57
 
58
  # Gradio Interface
59
  with gr.Blocks() as demo:
@@ -63,10 +65,12 @@ with gr.Blocks() as demo:
63
  chatbot_interface = gr.Chatbot()
64
  user_input = gr.Textbox(placeholder="Ask me anything...", show_label=False)
65
 
66
- history = gr.State([]) # Stores conversation history
67
 
68
- # Automatically trigger response when Enter is pressed
69
  user_input.submit(fn=chatbot, inputs=[user_input, history], outputs=[chatbot_interface, user_input])
70
 
71
  # Launch the chatbot
72
  demo.launch()
 
 
 
 
 
1
  import gradio as gr
2
  import requests
3
+ import os # Import os to access environment variables
4
+
5
+ # Retrieve the API key securely from Hugging Face Secrets
6
+ GROQ_API_KEY = os.getenv("myapi")
7
 
8
+ # Ensure API key is set
9
+ if not GROQ_API_KEY:
10
+ raise ValueError("Error: GROQ_API_KEY is missing! Check your Hugging Face secret settings.")
11
 
12
+ # Groq API URL
13
  GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
14
 
15
  # Function to interact with Groq API
 
17
  headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
18
 
19
  # System role for chatbot identity
20
+ messages = [{"role": "system", "content": "You are Faraz-Chatbot, an AI assistant created by Muhammad Faraz."}]
21
 
22
  # Ensure history is in the correct format
23
  for entry in history:
 
28
 
29
  # Add the latest user query
30
  messages.append({"role": "user", "content": prompt})
31
+
32
  # Custom response when asked about the owner
33
  if "who is your owner" in prompt.lower() or "who created you" in prompt.lower():
34
  return "I was created by Muhammad Faraz, a passionate developer!"
35
 
36
+ payload = {"model": "llama3-8b-8192", "messages": messages}
37
+
38
  try:
39
  response = requests.post(GROQ_API_URL, headers=headers, json=payload)
40
  response_data = response.json()
 
50
  # Function to handle chatbot responses
51
  def chatbot(query, history):
52
  if not query.strip():
53
+ return history, ""
 
 
54
 
55
+ response = chat_with_groq(query, history)
56
  history = history + [[query, response]]
57
 
58
+ return history, ""
59
 
60
  # Gradio Interface
61
  with gr.Blocks() as demo:
 
65
  chatbot_interface = gr.Chatbot()
66
  user_input = gr.Textbox(placeholder="Ask me anything...", show_label=False)
67
 
68
+ history = gr.State([])
69
 
 
70
  user_input.submit(fn=chatbot, inputs=[user_input, history], outputs=[chatbot_interface, user_input])
71
 
72
  # Launch the chatbot
73
  demo.launch()
74
+
75
+
76
+