Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,32 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
| 3 |
import requests
|
|
|
|
| 4 |
|
| 5 |
-
# API
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
MODEL_NAME = "llama3-8b-8192"
|
| 9 |
-
|
| 10 |
-
SYSTEM_PROMPT = "You are a Programming Tutor. Explain programming concepts clearly and concisely."
|
| 11 |
-
|
| 12 |
-
def respond(user_input, chat_history, temperature):
|
| 13 |
-
# Convert Gradio history into list of dicts for GROQ
|
| 14 |
-
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 15 |
-
for item in chat_history or []:
|
| 16 |
-
if isinstance(item, tuple) and len(item) == 2:
|
| 17 |
-
messages.append({"role": "user", "content": str(item[0])})
|
| 18 |
-
messages.append({"role": "assistant", "content": str(item[1])})
|
| 19 |
-
messages.append({"role": "user", "content": str(user_input)})
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
"model": MODEL_NAME,
|
| 24 |
-
"messages": messages,
|
| 25 |
-
"temperature": float(temperature)
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
# POST request
|
| 29 |
headers = {
|
| 30 |
-
"Authorization": f"Bearer {
|
| 31 |
"Content-Type": "application/json"
|
| 32 |
}
|
| 33 |
-
r = requests.post(GROQ_API_URL, headers=headers, json=payload)
|
| 34 |
-
|
| 35 |
-
if r.status_code == 200:
|
| 36 |
-
bot_reply = r.json()["choices"][0]["message"]["content"]
|
| 37 |
-
else:
|
| 38 |
-
bot_reply = f"Error {r.status_code}: {r.text}"
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
msg = gr.Textbox(label="Ask a programming question")
|
| 49 |
-
temperature = gr.Slider(0.1, 1.0, value=0.7, step=0.1)
|
| 50 |
-
clear = gr.Button("Clear Chat")
|
| 51 |
-
msg.submit(respond, [msg, state, temperature], [msg, chatbot])
|
| 52 |
-
clear.click(lambda: ([], []), None, [chatbot, state])
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
|
| 5 |
+
# Get API key from environment variable
|
| 6 |
+
API_KEY = os.environ.get("GROQ_API_KEY")
|
| 7 |
+
API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Define a function to call the Groq API
|
| 10 |
+
def query_groq(prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
headers = {
|
| 12 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 13 |
"Content-Type": "application/json"
|
| 14 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
data = {
|
| 17 |
+
"model": "llama3-8b-8192",
|
| 18 |
+
"messages": [
|
| 19 |
+
{"role": "system", "content": "You are an expert Programming Tutor."},
|
| 20 |
+
{"role": "user", "content": prompt}
|
| 21 |
+
]
|
| 22 |
+
}
|
| 23 |
|
| 24 |
+
response = requests.post(API_URL, headers=headers, json=data)
|
| 25 |
+
if response.status_code == 200:
|
| 26 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 27 |
+
else:
|
| 28 |
+
return f"Error: {response.status_code} - {response.text}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Gradio interface
|
| 31 |
+
iface = gr.Interface(fn=query_groq, inputs="text", outputs="text", title="Groq Chat")
|
| 32 |
+
iface.launch()
|