Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,69 +2,51 @@ 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 |
-
# Gradio respond function with safe GROQ formatting
|
| 17 |
def respond(user_input, chat_history, temperature):
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
for item in chat_history or []:
|
| 21 |
if isinstance(item, tuple) and len(item) == 2:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
response = requests.post(GROQ_API_URL, headers=headers, json=payload)
|
| 41 |
if response.status_code == 200:
|
| 42 |
bot_reply = response.json()["choices"][0]["message"]["content"]
|
| 43 |
else:
|
| 44 |
bot_reply = f"Error {response.status_code}: {response.text}"
|
| 45 |
|
| 46 |
-
# Return
|
| 47 |
new_history = (chat_history or []) + [(user_input, bot_reply)]
|
| 48 |
return "", new_history
|
| 49 |
|
| 50 |
-
# Gradio UI
|
| 51 |
with gr.Blocks() as demo:
|
| 52 |
-
gr.Markdown("##
|
| 53 |
-
|
| 54 |
chatbot = gr.Chatbot()
|
| 55 |
state = gr.State([])
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
temperature = gr.Slider(
|
| 59 |
-
minimum=0.1,
|
| 60 |
-
maximum=1.0,
|
| 61 |
-
value=0.7,
|
| 62 |
-
step=0.1,
|
| 63 |
-
label="Response Creativity"
|
| 64 |
-
)
|
| 65 |
clear = gr.Button("Clear Chat")
|
| 66 |
-
|
| 67 |
msg.submit(respond, [msg, state, temperature], [msg, chatbot])
|
| 68 |
clear.click(lambda: ([], []), None, [chatbot, state])
|
| 69 |
-
|
| 70 |
demo.launch()
|
|
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
|
|
|
|
| 5 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 6 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 7 |
MODEL_NAME = "llama3-8b-8192"
|
| 8 |
|
| 9 |
+
SYSTEM_PROMPT = "You are a Programming Tutor. Answer programming questions clearly."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
| 11 |
def respond(user_input, chat_history, temperature):
|
| 12 |
+
# Build messages list properly
|
| 13 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 14 |
for item in chat_history or []:
|
| 15 |
if isinstance(item, tuple) and len(item) == 2:
|
| 16 |
+
messages.append({"role": "user", "content": str(item[0])})
|
| 17 |
+
messages.append({"role": "assistant", "content": str(item[1])})
|
| 18 |
+
messages.append({"role": "user", "content": str(user_input)})
|
| 19 |
+
|
| 20 |
+
# Make API request
|
| 21 |
+
response = requests.post(
|
| 22 |
+
GROQ_API_URL,
|
| 23 |
+
headers={
|
| 24 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 25 |
+
"Content-Type": "application/json"
|
| 26 |
+
},
|
| 27 |
+
json={
|
| 28 |
+
"model": MODEL_NAME,
|
| 29 |
+
"messages": messages,
|
| 30 |
+
"temperature": float(temperature)
|
| 31 |
+
}
|
| 32 |
+
)
|
| 33 |
|
|
|
|
| 34 |
if response.status_code == 200:
|
| 35 |
bot_reply = response.json()["choices"][0]["message"]["content"]
|
| 36 |
else:
|
| 37 |
bot_reply = f"Error {response.status_code}: {response.text}"
|
| 38 |
|
| 39 |
+
# Return tuples for Gradio display
|
| 40 |
new_history = (chat_history or []) + [(user_input, bot_reply)]
|
| 41 |
return "", new_history
|
| 42 |
|
|
|
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown("## Programming Tutor Chatbot")
|
|
|
|
| 45 |
chatbot = gr.Chatbot()
|
| 46 |
state = gr.State([])
|
| 47 |
+
msg = gr.Textbox(label="Ask a question")
|
| 48 |
+
temperature = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Creativity")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
clear = gr.Button("Clear Chat")
|
|
|
|
| 50 |
msg.submit(respond, [msg, state, temperature], [msg, chatbot])
|
| 51 |
clear.click(lambda: ([], []), None, [chatbot, state])
|
|
|
|
| 52 |
demo.launch()
|