Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,66 +2,52 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
|
| 5 |
-
#
|
| 6 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 7 |
-
GROQ_API_URL = "https://api.groq.com/
|
| 8 |
MODEL_NAME = "llama3-8b-8192"
|
| 9 |
|
| 10 |
-
|
| 11 |
-
SYSTEM_PROMPT = "You are a Programming Tutor. Explain programming concepts clearly and concisely with examples if needed."
|
| 12 |
|
| 13 |
-
# Sanitize chat history into proper dicts
|
| 14 |
-
def sanitize_history(chat_history):
|
| 15 |
-
sanitized = []
|
| 16 |
-
for item in chat_history or []:
|
| 17 |
-
if isinstance(item, tuple) and len(item) == 2:
|
| 18 |
-
user_msg = str(item[0]) if item[0] is not None else ""
|
| 19 |
-
bot_msg = str(item[1]) if item[1] is not None else ""
|
| 20 |
-
sanitized.append({"role": "user", "content": user_msg})
|
| 21 |
-
sanitized.append({"role": "assistant", "content": bot_msg})
|
| 22 |
-
return sanitized
|
| 23 |
-
|
| 24 |
-
# Respond function for Gradio
|
| 25 |
def respond(user_input, chat_history, temperature):
|
| 26 |
-
#
|
| 27 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
messages.append({"role": "user", "content": str(user_input)})
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
headers = {
|
| 33 |
-
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 34 |
-
"Content-Type": "application/json"
|
| 35 |
-
}
|
| 36 |
-
|
| 37 |
payload = {
|
| 38 |
"model": MODEL_NAME,
|
| 39 |
"messages": messages,
|
| 40 |
"temperature": float(temperature)
|
| 41 |
}
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
else:
|
| 48 |
-
bot_reply = f"Error {
|
| 49 |
|
| 50 |
-
# Keep Gradio history as tuples for display
|
| 51 |
new_history = (chat_history or []) + [(user_input, bot_reply)]
|
| 52 |
return "", new_history
|
| 53 |
|
| 54 |
# Gradio UI
|
| 55 |
with gr.Blocks() as demo:
|
| 56 |
-
gr.Markdown("##
|
| 57 |
-
|
| 58 |
chatbot = gr.Chatbot()
|
| 59 |
state = gr.State([])
|
| 60 |
-
|
| 61 |
msg = gr.Textbox(label="Ask a programming question")
|
| 62 |
-
temperature = gr.Slider(0.1, 1.0, value=0.7, step=0.1
|
| 63 |
clear = gr.Button("Clear Chat")
|
| 64 |
-
|
| 65 |
msg.submit(respond, [msg, state, temperature], [msg, chatbot])
|
| 66 |
clear.click(lambda: ([], []), None, [chatbot, state])
|
| 67 |
|
|
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
|
| 5 |
+
# API setup
|
| 6 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 7 |
+
GROQ_API_URL = "https://api.groq.com/v1/chat/completions" # Latest URL
|
| 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 |
+
# Payload
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
payload = {
|
| 23 |
"model": MODEL_NAME,
|
| 24 |
"messages": messages,
|
| 25 |
"temperature": float(temperature)
|
| 26 |
}
|
| 27 |
|
| 28 |
+
# POST request
|
| 29 |
+
headers = {
|
| 30 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 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 |
new_history = (chat_history or []) + [(user_input, bot_reply)]
|
| 41 |
return "", new_history
|
| 42 |
|
| 43 |
# Gradio UI
|
| 44 |
with gr.Blocks() as demo:
|
| 45 |
+
gr.Markdown("## Programming Tutor Chatbot")
|
|
|
|
| 46 |
chatbot = gr.Chatbot()
|
| 47 |
state = gr.State([])
|
|
|
|
| 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 |
|