Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,83 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
-
import requests
|
| 4 |
-
|
| 5 |
-
# Load GROQ 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 defines chatbot personality
|
| 11 |
-
SYSTEM_PROMPT = """
|
| 12 |
-
You are an expert Programming Tutor.
|
| 13 |
-
Explain programming concepts in a simple, beginner-friendly way.
|
| 14 |
-
Provide examples when helpful and keep answers concise.
|
| 15 |
-
"""
|
| 16 |
-
|
| 17 |
-
# Function to call GROQ API safely
|
| 18 |
-
def query_groq(message, chat_history, temperature):
|
| 19 |
-
headers = {
|
| 20 |
-
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 21 |
-
"Content-Type": "application/json"
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
-
# Ensure all messages are strings and in correct format
|
| 25 |
-
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 26 |
-
for user, bot in chat_history:
|
| 27 |
-
messages.append({"role": "user", "content": str(user)})
|
| 28 |
-
messages.append({"role": "assistant", "content": str(bot)})
|
| 29 |
-
|
| 30 |
-
# Append the latest user message
|
| 31 |
-
messages.append({"role": "user", "content": str(message)})
|
| 32 |
-
|
| 33 |
-
response = requests.post(
|
| 34 |
-
GROQ_API_URL,
|
| 35 |
-
headers=headers,
|
| 36 |
-
json={
|
| 37 |
-
"model": MODEL_NAME,
|
| 38 |
-
"messages": messages,
|
| 39 |
-
"temperature": temperature
|
| 40 |
-
}
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
if response.status_code == 200:
|
| 44 |
-
return response.json()["choices"][0]["message"]["content"]
|
| 45 |
-
else:
|
| 46 |
-
return f"Error {response.status_code}: {response.text}"
|
| 47 |
-
|
| 48 |
-
# Respond function for Gradio
|
| 49 |
-
def respond(message, chat_history, temperature):
|
| 50 |
-
# Ensure chat_history is list of tuples
|
| 51 |
-
safe_history = []
|
| 52 |
-
if chat_history is not None:
|
| 53 |
-
for item in chat_history:
|
| 54 |
-
if isinstance(item, tuple) and len(item) == 2:
|
| 55 |
-
safe_history.append((str(item[0]), str(item[1])))
|
| 56 |
-
|
| 57 |
-
# Call GROQ
|
| 58 |
-
bot_reply = query_groq(message, safe_history, temperature)
|
| 59 |
-
safe_history.append((str(message), str(bot_reply)))
|
| 60 |
-
return "", safe_history
|
| 61 |
-
|
| 62 |
-
# Gradio UI
|
| 63 |
-
with gr.Blocks() as demo:
|
| 64 |
-
gr.Markdown("## 💻 Programming Tutor Chatbot (Powered by GROQ)")
|
| 65 |
-
|
| 66 |
-
chatbot = gr.Chatbot()
|
| 67 |
-
state = gr.State([])
|
| 68 |
-
|
| 69 |
-
msg = gr.Textbox(label="Ask a programming question")
|
| 70 |
-
temperature = gr.Slider(
|
| 71 |
-
minimum=0.1,
|
| 72 |
-
maximum=1.0,
|
| 73 |
-
value=0.7,
|
| 74 |
-
step=0.1,
|
| 75 |
-
label="Response Creativity"
|
| 76 |
-
)
|
| 77 |
-
clear = gr.Button("Clear Chat")
|
| 78 |
-
|
| 79 |
-
# Connect submit and clear button
|
| 80 |
-
msg.submit(respond, [msg, state, temperature], [msg, chatbot])
|
| 81 |
-
clear.click(lambda: ([], []), None, [chatbot, state])
|
| 82 |
-
|
| 83 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|