Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,79 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
-
import requests
|
| 4 |
-
|
| 5 |
-
# Load
|
| 6 |
-
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 7 |
-
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 8 |
-
MODEL_NAME = "llama3-8b-8192"
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
"
|
| 43 |
-
"
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
clear = gr.Button("Clear Chat")
|
| 80 |
-
|
| 81 |
-
msg.submit(respond, [msg, state, temperature], [msg, chatbot])
|
| 82 |
-
clear.click(lambda: ([], []), None, [chatbot, state])
|
| 83 |
-
|
| 84 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
# Load API key from Hugging Face Secrets
|
| 6 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 7 |
+
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 8 |
+
MODEL_NAME = "llama3-8b-8192"
|
| 9 |
+
|
| 10 |
+
SYSTEM_PROMPT = """
|
| 11 |
+
You are an expert Programming Tutor.
|
| 12 |
+
Explain programming concepts in a simple, beginner-friendly way.
|
| 13 |
+
Provide examples when helpful and keep answers concise.
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
# Build messages in proper format
|
| 17 |
+
def build_messages(user_input, chat_history):
|
| 18 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 19 |
+
|
| 20 |
+
# Add previous chat history safely
|
| 21 |
+
for item in chat_history or []:
|
| 22 |
+
if isinstance(item, tuple) and len(item) == 2:
|
| 23 |
+
user_msg = str(item[0])
|
| 24 |
+
bot_msg = str(item[1])
|
| 25 |
+
messages.append({"role": "user", "content": user_msg})
|
| 26 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 27 |
+
|
| 28 |
+
# Add latest user input
|
| 29 |
+
messages.append({"role": "user", "content": str(user_input)})
|
| 30 |
+
return messages
|
| 31 |
+
|
| 32 |
+
# Call GROQ API
|
| 33 |
+
def query_groq(user_input, chat_history, temperature):
|
| 34 |
+
messages = build_messages(user_input, chat_history)
|
| 35 |
+
|
| 36 |
+
headers = {
|
| 37 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 38 |
+
"Content-Type": "application/json"
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
payload = {
|
| 42 |
+
"model": MODEL_NAME,
|
| 43 |
+
"messages": messages,
|
| 44 |
+
"temperature": temperature
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
response = requests.post(GROQ_API_URL, headers=headers, json=payload)
|
| 48 |
+
|
| 49 |
+
if response.status_code == 200:
|
| 50 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 51 |
+
else:
|
| 52 |
+
return f"Error {response.status_code}: {response.text}"
|
| 53 |
+
|
| 54 |
+
# Gradio respond function
|
| 55 |
+
def respond(user_input, chat_history, temperature):
|
| 56 |
+
safe_history = []
|
| 57 |
+
for item in chat_history or []:
|
| 58 |
+
if isinstance(item, tuple) and len(item) == 2:
|
| 59 |
+
safe_history.append((str(item[0]), str(item[1])))
|
| 60 |
+
|
| 61 |
+
bot_reply = query_groq(user_input, safe_history, temperature)
|
| 62 |
+
safe_history.append((str(user_input), str(bot_reply)))
|
| 63 |
+
return "", safe_history
|
| 64 |
+
|
| 65 |
+
# Gradio UI
|
| 66 |
+
with gr.Blocks() as demo:
|
| 67 |
+
gr.Markdown("## 💻 Programming Tutor Chatbot (Powered by GROQ)")
|
| 68 |
+
|
| 69 |
+
chatbot = gr.Chatbot()
|
| 70 |
+
state = gr.State([])
|
| 71 |
+
|
| 72 |
+
msg = gr.Textbox(label="Ask a programming question")
|
| 73 |
+
temperature = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Response Creativity")
|
| 74 |
+
clear = gr.Button("Clear Chat")
|
| 75 |
+
|
| 76 |
+
msg.submit(respond, [msg, state, temperature], [msg, chatbot])
|
| 77 |
+
clear.click(lambda: ([], []), None, [chatbot, state])
|
| 78 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
demo.launch()
|