Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,46 +13,37 @@ Explain programming concepts in a simple, beginner-friendly way.
|
|
| 13 |
Provide examples when helpful and keep answers concise.
|
| 14 |
"""
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# Convert Gradio history tuples into dicts for API
|
| 21 |
for item in chat_history or []:
|
| 22 |
if isinstance(item, tuple) and len(item) == 2:
|
| 23 |
user_msg, bot_msg = item
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# Latest user input
|
| 28 |
-
messages.append({"role": "user", "content": str(user_input)})
|
| 29 |
-
return messages
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
messages = build_groq_messages(user_input, chat_history)
|
| 34 |
|
|
|
|
| 35 |
headers = {
|
| 36 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 37 |
"Content-Type": "application/json"
|
| 38 |
}
|
| 39 |
-
|
| 40 |
payload = {
|
| 41 |
"model": MODEL_NAME,
|
| 42 |
-
"messages":
|
| 43 |
"temperature": temperature
|
| 44 |
}
|
| 45 |
|
| 46 |
response = requests.post(GROQ_API_URL, headers=headers, json=payload)
|
| 47 |
-
|
| 48 |
if response.status_code == 200:
|
| 49 |
-
|
| 50 |
else:
|
| 51 |
-
|
| 52 |
|
| 53 |
-
# Gradio
|
| 54 |
-
def respond(user_input, chat_history, temperature):
|
| 55 |
-
bot_reply = query_groq(user_input, chat_history, temperature)
|
| 56 |
new_history = (chat_history or []) + [(user_input, bot_reply)]
|
| 57 |
return "", new_history
|
| 58 |
|
|
|
|
| 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 |
+
# Convert chat history tuples to dicts
|
| 19 |
+
groq_messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
|
|
|
| 20 |
for item in chat_history or []:
|
| 21 |
if isinstance(item, tuple) and len(item) == 2:
|
| 22 |
user_msg, bot_msg = item
|
| 23 |
+
groq_messages.append({"role": "user", "content": str(user_msg)})
|
| 24 |
+
groq_messages.append({"role": "assistant", "content": str(bot_msg)})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Append latest user input
|
| 27 |
+
groq_messages.append({"role": "user", "content": str(user_input)})
|
|
|
|
| 28 |
|
| 29 |
+
# Call GROQ API
|
| 30 |
headers = {
|
| 31 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 32 |
"Content-Type": "application/json"
|
| 33 |
}
|
|
|
|
| 34 |
payload = {
|
| 35 |
"model": MODEL_NAME,
|
| 36 |
+
"messages": groq_messages,
|
| 37 |
"temperature": temperature
|
| 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 Gradio chat history as tuples for display
|
|
|
|
|
|
|
| 47 |
new_history = (chat_history or []) + [(user_input, bot_reply)]
|
| 48 |
return "", new_history
|
| 49 |
|