Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,26 @@
|
|
1 |
-
|
2 |
-
|
3 |
import os
|
4 |
import requests
|
5 |
import gradio as gr
|
6 |
|
7 |
-
|
|
|
8 |
|
9 |
-
|
|
|
10 |
|
11 |
-
# Define the
|
12 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
13 |
-
|
14 |
-
# Set the headers for the API request
|
15 |
-
headers = {
|
16 |
-
"Authorization": f"Bearer {groq_api_key}"
|
17 |
-
}
|
18 |
|
19 |
# Function to interact with Groq API
|
20 |
def chat_with_groq(user_input):
|
21 |
body = {
|
22 |
-
"model": "llama-3.
|
23 |
-
"messages": [
|
24 |
-
{"role": "user", "content": user_input}
|
25 |
-
]
|
26 |
}
|
27 |
-
|
28 |
response = requests.post(url, headers=headers, json=body)
|
29 |
-
|
30 |
if response.status_code == 200:
|
31 |
return response.json()['choices'][0]['message']['content']
|
32 |
else:
|
@@ -37,9 +31,10 @@ interface = gr.Interface(
|
|
37 |
fn=chat_with_groq,
|
38 |
inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."),
|
39 |
outputs=gr.Textbox(),
|
40 |
-
title="
|
41 |
description="Type your question below and get a response powered by Groq's Llama 3.1-8B model."
|
42 |
)
|
43 |
|
44 |
# Launch Gradio app
|
45 |
-
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import requests
|
3 |
import gradio as gr
|
4 |
|
5 |
+
# Retrieve the API key from the environment variable
|
6 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
7 |
|
8 |
+
if not groq_api_key:
|
9 |
+
raise ValueError("GROQ_API_KEY is missing! Set it in the Hugging Face Spaces 'Secrets'.")
|
10 |
|
11 |
+
# Define the API endpoint and headers
|
12 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
13 |
+
headers = {"Authorization": f"Bearer {groq_api_key}"}
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Function to interact with Groq API
|
16 |
def chat_with_groq(user_input):
|
17 |
body = {
|
18 |
+
"model": "llama-3.1-8b-instant",
|
19 |
+
"messages": [{"role": "user", "content": user_input}]
|
|
|
|
|
20 |
}
|
21 |
+
|
22 |
response = requests.post(url, headers=headers, json=body)
|
23 |
+
|
24 |
if response.status_code == 200:
|
25 |
return response.json()['choices'][0]['message']['content']
|
26 |
else:
|
|
|
31 |
fn=chat_with_groq,
|
32 |
inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."),
|
33 |
outputs=gr.Textbox(),
|
34 |
+
title="Chat with Groq AI (Llama 3.1-8B)",
|
35 |
description="Type your question below and get a response powered by Groq's Llama 3.1-8B model."
|
36 |
)
|
37 |
|
38 |
# Launch Gradio app
|
39 |
+
if __name__ == "__main__":
|
40 |
+
interface.launch()
|