ShanzaGull commited on
Commit
1d5751f
·
verified ·
1 Parent(s): 7edd494

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +92 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import requests
4
+
5
+ # =========================
6
+ # CONFIG
7
+ # =========================
8
+ GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
9
+ GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
10
+ MODEL_NAME = "llama3-8b-8192"
11
+
12
+ SYSTEM_PROMPT = """
13
+ You are StudyBuddy, a smart and friendly study companion for students.
14
+ Your job is to help with study planning, exam preparation, time management,
15
+ and staying motivated.
16
+
17
+ Your style:
18
+ - Clear and structured explanations
19
+ - Simple language
20
+ - Practical study tips
21
+ - Motivational and supportive tone
22
+
23
+ You help with:
24
+ - Making study schedules
25
+ - Exam preparation strategies
26
+ - Time management (Pomodoro, focus tips)
27
+ - Reducing exam stress
28
+ - Improving learning habits
29
+ """
30
+
31
+ # =========================
32
+ # GROQ QUERY FUNCTION
33
+ # =========================
34
+ def query_groq(user_message, chat_history):
35
+ headers = {
36
+ "Authorization": f"Bearer {GROQ_API_KEY}",
37
+ "Content-Type": "application/json"
38
+ }
39
+
40
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
41
+
42
+ for user, bot in chat_history:
43
+ messages.append({"role": "user", "content": user})
44
+ messages.append({"role": "assistant", "content": bot})
45
+
46
+ messages.append({"role": "user", "content": user_message})
47
+
48
+ response = requests.post(
49
+ GROQ_API_URL,
50
+ headers=headers,
51
+ json={
52
+ "model": MODEL_NAME,
53
+ "messages": messages,
54
+ "temperature": 0.7
55
+ }
56
+ )
57
+
58
+ if response.status_code == 200:
59
+ return response.json()["choices"][0]["message"]["content"]
60
+ else:
61
+ return f"Error {response.status_code}: {response.text}"
62
+
63
+ # =========================
64
+ # RESPONSE FUNCTION
65
+ # =========================
66
+ def respond(message, chat_history):
67
+ reply = query_groq(message, chat_history)
68
+ chat_history.append((message, reply))
69
+ return "", chat_history
70
+
71
+ # =========================
72
+ # UI
73
+ # =========================
74
+ with gr.Blocks() as demo:
75
+ gr.Markdown(
76
+ """
77
+ # 📘 StudyBuddy – Smart Study Companion
78
+ Your personal assistant for study planning, exams, and motivation.
79
+ """
80
+ )
81
+
82
+ chatbot = gr.Chatbot()
83
+ msg = gr.Textbox(
84
+ label="Ask StudyBuddy",
85
+ placeholder="e.g. Make a study plan for exams"
86
+ )
87
+ clear = gr.Button("Clear Chat")
88
+
89
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
90
+ clear.click(lambda: [], None, chatbot)
91
+
92
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ requests