Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
GROQ_API_KEY = "
|
|
|
|
|
|
|
| 6 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 7 |
|
| 8 |
def chat_with_groq(user_input):
|
|
|
|
|
|
|
|
|
|
| 9 |
headers = {
|
| 10 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 11 |
"Content-Type": "application/json"
|
| 12 |
}
|
|
|
|
| 13 |
data = {
|
| 14 |
"model": "llama3-8b-8192", # ✅ Using only LLaMA 3-8B model
|
| 15 |
"messages": [{"role": "user", "content": user_input}]
|
| 16 |
}
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Create Gradio UI
|
| 25 |
iface = gr.Interface(
|
|
@@ -32,3 +46,4 @@ iface = gr.Interface(
|
|
| 32 |
|
| 33 |
# Launch the chatbot
|
| 34 |
iface.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Load API key from environment variables (Recommended for security)
|
| 6 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
+
|
| 8 |
+
# Groq API URL
|
| 9 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 10 |
|
| 11 |
def chat_with_groq(user_input):
|
| 12 |
+
if not GROQ_API_KEY:
|
| 13 |
+
return "Error: API key is missing. Set the GROQ_API_KEY environment variable."
|
| 14 |
+
|
| 15 |
headers = {
|
| 16 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 17 |
"Content-Type": "application/json"
|
| 18 |
}
|
| 19 |
+
|
| 20 |
data = {
|
| 21 |
"model": "llama3-8b-8192", # ✅ Using only LLaMA 3-8B model
|
| 22 |
"messages": [{"role": "user", "content": user_input}]
|
| 23 |
}
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
response = requests.post(GROQ_API_URL, json=data, headers=headers)
|
| 27 |
+
response_json = response.json()
|
| 28 |
+
|
| 29 |
+
if response.status_code == 200:
|
| 30 |
+
return response_json.get("choices", [{}])[0].get("message", {}).get("content", "No response")
|
| 31 |
+
else:
|
| 32 |
+
error_message = response_json.get("error", {}).get("message", "Unknown error")
|
| 33 |
+
return f"Error {response.status_code}: {error_message}"
|
| 34 |
+
|
| 35 |
+
except requests.exceptions.RequestException as e:
|
| 36 |
+
return f"Request failed: {e}"
|
| 37 |
|
| 38 |
# Create Gradio UI
|
| 39 |
iface = gr.Interface(
|
|
|
|
| 46 |
|
| 47 |
# Launch the chatbot
|
| 48 |
iface.launch()
|
| 49 |
+
|