Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
from google.colab import userdata # Secure storage for API keys in Colab
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
|
7 |
+
# Get the Groq API key securely (use userdata in Colab)
|
8 |
+
groq_api_key = userdata.get("GROQ_API_KEY") # Store using userdata.set("GROQ_API_KEY", "your_api_key")
|
9 |
+
|
10 |
+
if not groq_api_key:
|
11 |
+
raise ValueError("GROQ_API_KEY not found! Set it using userdata.set('GROQ_API_KEY', 'your_api_key')")
|
12 |
+
|
13 |
+
# Define the URL for the Groq API endpoint
|
14 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
15 |
+
|
16 |
+
# Set the headers for the API request
|
17 |
+
headers = {
|
18 |
+
"Authorization": f"Bearer {groq_api_key}"
|
19 |
+
}
|
20 |
+
|
21 |
+
def chat_with_groq(user_input):
|
22 |
+
body = {
|
23 |
+
"model": "llama-3.1-8b-instant",
|
24 |
+
"messages": [
|
25 |
+
{"role": "user", "content": user_input}
|
26 |
+
]
|
27 |
+
}
|
28 |
+
|
29 |
+
# Send a POST request to the Groq API
|
30 |
+
response = requests.post(url, headers=headers, json=body)
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
# Check if the request was successful
|
35 |
+
if response.status_code == 200:
|
36 |
+
return response.json()['choices'][0]['message']['content']
|
37 |
+
else:
|
38 |
+
return "Error:", response.json()
|
39 |
+
|
40 |
+
|
41 |
+
interface=gr.Interface(
|
42 |
+
fn=chat_with_groq,
|
43 |
+
inputs=gr.Textbox("Ask me anything..."),
|
44 |
+
outputs=gr.Textbox(),
|
45 |
+
title="Chat bow uwing groq (Llama 3.1-8B)",
|
46 |
+
description="Type your question below and get a response powered by Groq's Llama 3.1-8B model"
|
47 |
+
)
|
48 |
+
|
49 |
+
interface.launch()
|