jdesiree commited on
Commit
88f3203
·
verified ·
1 Parent(s): 59e0f26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -329
app.py CHANGED
@@ -1,331 +1,102 @@
1
- import gradio as gr
2
- from langchain.prompts import ChatPromptTemplate
3
- from langchain_huggingface import HuggingFaceEndpoint
4
- from langchain.schema import HumanMessage, SystemMessage
5
  import os
6
- import time
7
- import logging
8
- import re
9
-
10
- logging.basicConfig(level=logging.INFO)
11
- logger = logging.getLogger(__name__)
12
-
13
- llm = HuggingFaceEndpoint(
14
- repo_id="HuggingFaceH4/zephyr-7b-beta",
15
- temperature=0.7,
16
- top_p=0.9,
17
- repetition_penalty=1.1,
18
- model_kwargs={"max_length": 1024},
19
- huggingfacehub_api_token=os.getenv("HUGGINGFACEHUB_API_TOKEN")
20
- )
21
-
22
- math_template = ChatPromptTemplate.from_messages([
23
- ("system", """{system_message}
24
-
25
- You are an expert math tutor. For every math problem:
26
- 1. Break it down into key concepts
27
- 2. Breifly explain concepts
28
- 3. Outline the process for solving a similar problem
29
-
30
- Be comprehensive and educational. Structure your response clearly."""),
31
- ("human", "{question}")
32
- ])
33
-
34
- research_template = ChatPromptTemplate.from_messages([
35
- ("system", """{system_message}
36
-
37
- You are a research skills mentor. Help students with:
38
- - Determining the validity of sources
39
- - Evaluating source credibility and bias if a source is mentioned
40
- - Proper citation formats (APA, MLA, Chicago, etc.)
41
- - Research strategies and methodologies
42
- - Academic writing techniques and structure
43
- - Database navigation and search strategies
44
-
45
- Provide detailed, actionable advice with specific examples."""),
46
- ("human", "{question}")
47
- ])
48
-
49
- study_template = ChatPromptTemplate.from_messages([
50
- ("system", """{system_message}
51
-
52
- You are a study skills coach. Help students with:
53
- - Effective study methods for different learning styles
54
- - Time management and scheduling techniques
55
- - Memory techniques and retention strategies
56
- - Test preparation and exam strategies
57
- - Note-taking methods and organization
58
- - Learning style optimization
59
- - Offer short quiz sessions where you pose one to two questions at a time, then provide feedback on the students answers.
60
-
61
- Provide comprehensive, personalized advice with practical examples."""),
62
- ("human", "{question}")
63
- ])
64
-
65
- general_template = ChatPromptTemplate.from_messages([
66
- ("system", """{system_message}
67
-
68
- You are EduBot, a comprehensive AI learning assistant. You help students with:
69
- 📐 Mathematics (Concise explainations rooted in understanding the concepts and process rather than answering the math problem directly)
70
- 🔍 Research skills (source guidance, research advice, evaluation, and citation)
71
- 📚 Study strategies (effective learning techniques and exam preparation)
72
- 🛠️ Educational tools (guidance on learning resources and technologies)
73
-
74
- Always be encouraging, patient, thorough, and comprehensive."""),
75
- ("human", "{question}")
76
- ])
77
-
78
- def detect_subject(message):
79
- message_lower = message.lower()
80
-
81
- math_keywords = ['math', 'solve', 'calculate', 'equation', 'formula', 'algebra', 'geometry', 'calculus', 'derivative', 'integral', 'theorem', 'proof']
82
- research_keywords = ['research', 'source', 'citation', 'bibliography', 'reference', 'academic', 'paper', 'essay', 'thesis', 'database', 'journal']
83
- study_keywords = ['study', 'memorize', 'exam', 'test', 'quiz', 'review', 'learn', 'remember', 'focus', 'motivation', 'notes']
84
-
85
- if any(keyword in message_lower for keyword in math_keywords):
86
- return math_template, "🧮 Math Mode"
87
- elif any(keyword in message_lower for keyword in research_keywords):
88
- return research_template, "🔍 Research Mode"
89
- elif any(keyword in message_lower for keyword in study_keywords):
90
- return study_template, "📚 Study Mode"
91
- else:
92
- return general_template, "🎓 General Mode"
93
-
94
- def smart_truncate(text, max_length=3000):
95
- if len(text) <= max_length:
96
- return text
97
-
98
- sentences = re.split(r'(?<=[.!?]) +', text[:max_length])
99
- if len(sentences) > 1:
100
- return ' '.join(sentences[:-1]) + "... [Response truncated - ask for continuation]"
101
- else:
102
- words = text[:max_length].split()
103
- return ' '.join(words[:-1]) + "... [Response truncated - ask for continuation]"
104
-
105
- def respond_with_enhanced_streaming(
106
- message,
107
- history,
108
- max_tokens=600,
109
- temperature=0.7,
110
- top_p=0.9,
111
- ):
112
- try:
113
- template, mode = detect_subject(message)
114
- chain = template | llm
115
-
116
- yield f"*{mode}*\n\nGenerating response..."
117
-
118
- logger.info(f"Processing {mode} query: {message[:50]}...")
119
-
120
- response = chain.invoke({
121
- "question": message,
122
- "system_message": "You are EduBot, an expert AI learning assistant. Provide comprehensive, educational responses that help students truly understand concepts."
123
- })
124
-
125
- response = smart_truncate(response, max_length=3000)
126
-
127
- words = response.split()
128
- partial_response = f"*{mode}*\n\n"
129
-
130
- for i, word in enumerate(words):
131
- partial_response += word + " "
132
-
133
- if i % 4 == 0:
134
- yield partial_response
135
- time.sleep(0.03)
136
-
137
- final_response = f"*{mode}*\n\n{response}"
138
- logger.info(f"Response completed. Length: {len(response)} characters")
139
- yield final_response
140
-
141
- except Exception as e:
142
- logger.exception("Error in LangChain response generation")
143
- yield f"Sorry, I encountered an error: {str(e)[:150]}"
144
-
145
- theme = gr.themes.Glass(
146
- primary_hue="amber",
147
- text_size="md",
148
- radius_size="md",
149
- ).set(
150
- shadow_drop='*shadow_drop_lg',
151
- shadow_drop_lg='*shadow_drop',
152
- shadow_spread='*shadow_drop_lg',
153
- block_info_text_weight='500',
154
- button_border_width='*block_border_width'
155
- )
156
-
157
- with gr.Blocks(theme=theme, css="""
158
- .main-container {
159
- max-width: 900px;
160
- margin: 0 auto;
161
- padding: 1rem;
162
- height: 100vh;
163
- display: flex;
164
- flex-direction: column;
165
- }
166
- .title {
167
- text-align: center;
168
- font-size: clamp(1.8rem, 4vw, 2.5rem);
169
- font-weight: bold;
170
- color: #d97706;
171
- margin-bottom: 1rem;
172
- flex-shrink: 0;
173
- }
174
- .chat-container {
175
- flex: 1;
176
- display: flex;
177
- flex-direction: column;
178
- min-height: 0;
179
- margin-bottom: 1rem;
180
- }
181
- .gradio-chatbot {
182
- flex: 1 !important;
183
- height: auto !important;
184
- min-height: 400px;
185
- max-height: calc(100vh - 200px);
186
- }
187
- .chat-bot .message-wrap .avatar-container {
188
- display: none !important;
189
- }
190
- .gradio-chatbot .message-wrap .avatar-container {
191
- display: none !important;
192
- }
193
- .gradio-chatbot .message.bot .avatar-container,
194
- .gradio-chatbot .message.user .avatar-container {
195
- display: none !important;
196
- }
197
- .input-row {
198
- flex-shrink: 0;
199
- margin-top: 0.5rem;
200
- display: flex;
201
- justify-content: center;
202
- align-items: center;
203
- gap: 10px;
204
- max-width: 700px;
205
- margin-left: auto;
206
- margin-right: auto;
207
- }
208
- .custom-textbox {
209
- flex: 1;
210
- min-width: 400px;
211
- }
212
- .custom-textbox > div {
213
- width: 100% !important;
214
- }
215
- .custom-textbox textarea {
216
- border-radius: 12px !important;
217
- border: 1pt solid #64748b !important;
218
- padding: 15px 20px !important;
219
- font-size: 16px !important;
220
- background: white !important;
221
- outline: none !important;
222
- box-shadow: 0 2px 4px rgba(0,0,0,0.1) !important;
223
- transition: all 0.3s ease !important;
224
- min-height: 100px !important;
225
- resize: vertical !important;
226
- width: 100% !important;
227
- }
228
- .custom-textbox textarea:focus {
229
- border-color: #475569 !important;
230
- box-shadow: 0 0 0 3px rgba(100, 116, 139, 0.1) !important;
231
- }
232
- .custom-textbox label {
233
- display: none !important;
234
- }
235
- .send-button {
236
- border-radius: 50% !important;
237
- width: 50px !important;
238
- height: 50px !important;
239
- display: flex !important;
240
- align-items: center !important;
241
- justify-content: center !important;
242
- flex-shrink: 0 !important;
243
- }
244
- @media (max-width: 768px) {
245
- .main-container {
246
- padding: 0.5rem;
247
- }
248
- .title {
249
- font-size: 1.5rem;
250
- margin-bottom: 0.5rem;
251
- }
252
- .gradio-chatbot {
253
- min-height: 300px;
254
- max-height: calc(100vh - 150px);
255
- }
256
- .input-row {
257
- margin-top: 0.25rem;
258
- max-width: 100%;
259
- padding: 0 0.5rem;
260
- }
261
- .custom-textbox {
262
- min-width: 250px;
263
- }
264
- .custom-textbox textarea {
265
- min-height: 80px !important;
266
- font-size: 14px !important;
267
- }
268
- }
269
- @media (min-width: 769px) and (max-width: 1024px) {
270
- .gradio-chatbot {
271
- min-height: 450px;
272
- max-height: calc(100vh - 180px);
273
- }
274
- }
275
- @media (min-width: 1025px) {
276
- .gradio-chatbot {
277
- min-height: 500px;
278
- max-height: calc(100vh - 200px);
279
- }
280
- }
281
- @media (max-height: 500px) and (orientation: landscape) {
282
- .title {
283
- font-size: 1.2rem;
284
- margin-bottom: 0.25rem;
285
- }
286
- .gradio-chatbot {
287
- min-height: 200px;
288
- max-height: calc(100vh - 100px);
289
- }
290
- }
291
- """) as demo:
292
- with gr.Column(elem_classes=["main-container"]):
293
- gr.HTML("""
294
- <div class='title'>🎓 EduBot</div>
295
- """)
296
- with gr.Column(elem_classes=["chat-container"]):
297
- chatbot = gr.Chatbot(
298
- show_copy_button=True,
299
- avatar_images=[None, None],
300
- placeholder="Hi! I'm EduBot. What would you like to learn today? 📚",
301
- container=True,
302
- elem_classes=["gradio-chatbot"]
303
- )
304
- with gr.Row(elem_classes=["input-row"]):
305
- msg = gr.Textbox(
306
- placeholder="Ask me about math, research, study strategies, or any educational topic...",
307
- container=False,
308
- show_label=False,
309
- elem_classes=["custom-textbox"],
310
- lines=4,
311
- max_lines=8
312
- )
313
- send_btn = gr.Button("📤", variant="primary", elem_classes=["send-button"])
314
- def respond_and_update(message, history):
315
- for response in respond_with_enhanced_streaming(message, history):
316
- history.append([message, response])
317
- yield history, ""
318
- msg.submit(
319
- respond_and_update,
320
- [msg, chatbot],
321
- [chatbot, msg]
322
- )
323
- send_btn.click(
324
- respond_and_update,
325
- [msg, chatbot],
326
- [chatbot, msg]
327
- )
328
 
329
- if __name__ == "__main__":
330
- logger.info("Starting EduBot with custom styling...")
331
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import openai
3
+ import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ openai.api_key = os.environ.get("OPENAI_API_KEY")
6
+
7
+ # Gradio + OpenAI Chatbot
8
+ def chat_with_openai(message, history):
9
+ history_openai_format = [
10
+ {"role": "user" if h["role"] == "user" else "assistant", "content": h["content"]}
11
+ for h in history
12
+ ]
13
+
14
+ history_openai_format.append({"role": "user", "content": message})
15
+
16
+ response = openai.chat.completions.create(
17
+ model="gpt-4o-mini",
18
+ messages=history_openai_format,
19
+ )
20
+
21
+ return response.choices[0].message["content"]
22
+
23
+ # Custom CSS
24
+ css = """
25
+ /* Chat container */
26
+ #chatbot {
27
+ height: 600px !important;
28
+ overflow-y: auto !important;
29
+ border-radius: 16px !important;
30
+ border: 1pt solid #d1d5db !important;
31
+ padding: 15px !important;
32
+ background: white !important;
33
+ box-shadow: 0 2px 6px rgba(0,0,0,0.05) !important;
34
+ }
35
+
36
+ /* Messages */
37
+ .message {
38
+ border-radius: 12px !important;
39
+ padding: 10px 15px !important;
40
+ margin: 8px 0 !important;
41
+ }
42
+ .message.user {
43
+ background: #f1f5f9 !important; /* light slate */
44
+ color: #0f172a !important;
45
+ }
46
+ .message.assistant {
47
+ background: #e0f2fe !important; /* light blue */
48
+ color: #1e3a8a !important;
49
+ }
50
+
51
+ /* Textbox (force textarea style) */
52
+ .custom-textbox textarea {
53
+ border-radius: 12px !important;
54
+ border: 1pt solid #64748b !important; /* slate border */
55
+ padding: 15px 20px !important;
56
+ font-size: 16px !important;
57
+ background: white !important;
58
+ outline: none !important;
59
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1) !important;
60
+ transition: all 0.3s ease !important;
61
+ min-height: 100px !important; /* rectangle instead of flat line */
62
+ resize: vertical !important;
63
+ }
64
+ .custom-textbox textarea:focus {
65
+ border-color: #3b82f6 !important; /* blue focus */
66
+ box-shadow: 0 4px 8px rgba(59,130,246,0.2) !important;
67
+ }
68
+
69
+ /* Send button */
70
+ button {
71
+ border-radius: 12px !important;
72
+ background: #3b82f6 !important;
73
+ color: white !important;
74
+ padding: 12px 18px !important;
75
+ font-size: 16px !important;
76
+ border: none !important;
77
+ transition: background 0.3s ease !important;
78
+ }
79
+ button:hover {
80
+ background: #2563eb !important;
81
+ }
82
+ """
83
+
84
+ # Gradio UI
85
+ with gr.Blocks(css=css) as demo:
86
+ chatbot = gr.Chatbot(elem_id="chatbot", bubble_full_width=False, height=600)
87
+
88
+ with gr.Row():
89
+ msg = gr.Textbox(
90
+ placeholder="Ask me about math, research, study strategies, or any educational topic...",
91
+ container=False,
92
+ show_label=False,
93
+ elem_classes=["custom-textbox"],
94
+ lines=3,
95
+ max_lines=6
96
+ )
97
+ submit = gr.Button("Send", scale=0)
98
+
99
+ msg.submit(chat_with_openai, [msg, chatbot], chatbot)
100
+ submit.click(chat_with_openai, [msg, chatbot], chatbot)
101
+
102
+ demo.launch()