File size: 2,522 Bytes
1d5751f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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()