StudyBuddy / app.py
ShanzaGull's picture
Upload 2 files
1d5751f verified
import gradio as gr
import os
import requests
# =========================
# CONFIG
# =========================
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
MODEL_NAME = "llama3-8b-8192"
SYSTEM_PROMPT = """
You are StudyBuddy, a smart and friendly study companion for students.
Your job is to help with study planning, exam preparation, time management,
and staying motivated.
Your style:
- Clear and structured explanations
- Simple language
- Practical study tips
- Motivational and supportive tone
You help with:
- Making study schedules
- Exam preparation strategies
- Time management (Pomodoro, focus tips)
- Reducing exam stress
- Improving learning habits
"""
# =========================
# GROQ QUERY FUNCTION
# =========================
def query_groq(user_message, chat_history):
headers = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
for user, bot in chat_history:
messages.append({"role": "user", "content": user})
messages.append({"role": "assistant", "content": bot})
messages.append({"role": "user", "content": user_message})
response = requests.post(
GROQ_API_URL,
headers=headers,
json={
"model": MODEL_NAME,
"messages": messages,
"temperature": 0.7
}
)
if response.status_code == 200:
return response.json()["choices"][0]["message"]["content"]
else:
return f"Error {response.status_code}: {response.text}"
# =========================
# RESPONSE FUNCTION
# =========================
def respond(message, chat_history):
reply = query_groq(message, chat_history)
chat_history.append((message, reply))
return "", chat_history
# =========================
# UI
# =========================
with gr.Blocks() as demo:
gr.Markdown(
"""
# 📘 StudyBuddy – Smart Study Companion
Your personal assistant for study planning, exams, and motivation.
"""
)
chatbot = gr.Chatbot()
msg = gr.Textbox(
label="Ask StudyBuddy",
placeholder="e.g. Make a study plan for exams"
)
clear = gr.Button("Clear Chat")
msg.submit(respond, [msg, chatbot], [msg, chatbot])
clear.click(lambda: [], None, chatbot)
demo.launch()