Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,19 +28,16 @@ llm = HuggingFaceEndpoint(
|
|
| 28 |
|
| 29 |
math_template = ChatPromptTemplate.from_messages([
|
| 30 |
("system", """{system_message}
|
| 31 |
-
|
| 32 |
You are an expert math tutor. For every math problem:
|
| 33 |
1. Break it down into key concepts
|
| 34 |
2. Briefly explain concepts
|
| 35 |
3. Outline the process for solving a similar problem
|
| 36 |
-
|
| 37 |
Be comprehensive and educational. Structure your response clearly."""),
|
| 38 |
("human", "{question}")
|
| 39 |
])
|
| 40 |
|
| 41 |
research_template = ChatPromptTemplate.from_messages([
|
| 42 |
("system", """{system_message}
|
| 43 |
-
|
| 44 |
You are a research skills mentor. Help students with:
|
| 45 |
- Determining the validity of sources
|
| 46 |
- Evaluating source credibility and bias if a source is mentioned
|
|
@@ -48,14 +45,12 @@ You are a research skills mentor. Help students with:
|
|
| 48 |
- Research strategies and methodologies
|
| 49 |
- Academic writing techniques and structure
|
| 50 |
- Database navigation and search strategies
|
| 51 |
-
|
| 52 |
Provide detailed, actionable advice with specific examples."""),
|
| 53 |
("human", "{question}")
|
| 54 |
])
|
| 55 |
|
| 56 |
study_template = ChatPromptTemplate.from_messages([
|
| 57 |
("system", """{system_message}
|
| 58 |
-
|
| 59 |
You are a study skills coach. Help students with:
|
| 60 |
- Effective study methods for different learning styles
|
| 61 |
- Time management and scheduling techniques
|
|
@@ -64,20 +59,17 @@ You are a study skills coach. Help students with:
|
|
| 64 |
- Note-taking methods and organization
|
| 65 |
- Learning style optimization
|
| 66 |
- Offer short quiz sessions where you pose one to two questions at a time, then provide feedback on the students answers.
|
| 67 |
-
|
| 68 |
Provide comprehensive, personalized advice with practical examples."""),
|
| 69 |
("human", "{question}")
|
| 70 |
])
|
| 71 |
|
| 72 |
general_template = ChatPromptTemplate.from_messages([
|
| 73 |
("system", """{system_message}
|
| 74 |
-
|
| 75 |
You are EduBot, a comprehensive AI learning assistant. You help students with:
|
| 76 |
π Mathematics (Concise explanations rooted in understanding the concepts and process rather than answering the math problem directly)
|
| 77 |
π Research skills (source guidance, research advice, evaluation, and citation)
|
| 78 |
π Study strategies (effective learning techniques and exam preparation)
|
| 79 |
π οΈ Educational tools (guidance on learning resources and technologies)
|
| 80 |
-
|
| 81 |
Always be encouraging, patient, thorough, and comprehensive."""),
|
| 82 |
("human", "{question}")
|
| 83 |
])
|
|
@@ -170,83 +162,58 @@ def respond_with_enhanced_streaming(message, history):
|
|
| 170 |
logger.exception("Error in LangChain response generation")
|
| 171 |
yield f"Sorry, I encountered an error: {str(e)[:150]}"
|
| 172 |
|
| 173 |
-
# --- Gradio UI and CSS ---
|
| 174 |
custom_css = """
|
| 175 |
-
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
.gradio-container {
|
| 177 |
-
height: 100vh;
|
| 178 |
background-color: rgb(240, 236, 230) !important;
|
| 179 |
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 180 |
-
|
| 181 |
-
|
| 182 |
margin: 0 !important;
|
| 183 |
padding: 0 !important;
|
| 184 |
-
max-width: none !important;
|
| 185 |
-
}
|
| 186 |
-
|
| 187 |
-
/* Main layout container - full width, sectioned heights */
|
| 188 |
-
.main-container {
|
| 189 |
-
width: 98vw;
|
| 190 |
-
height: 100vh;
|
| 191 |
-
margin: 0 auto;
|
| 192 |
-
display: flex;
|
| 193 |
-
flex-direction: column;
|
| 194 |
}
|
| 195 |
|
| 196 |
-
/*
|
| 197 |
-
.title-
|
| 198 |
-
flex: 0 0 15vh;
|
| 199 |
-
padding: 1rem 2rem;
|
| 200 |
background-color: rgb(240, 236, 230);
|
|
|
|
| 201 |
border-bottom: 2px solid rgba(28, 18, 5, 0.1);
|
| 202 |
-
|
| 203 |
-
align-items: center;
|
| 204 |
-
box-sizing: border-box;
|
| 205 |
-
flex-shrink: 0;
|
| 206 |
}
|
| 207 |
|
| 208 |
-
|
| 209 |
-
.model-name {
|
| 210 |
font-size: 1.8rem;
|
| 211 |
font-weight: bold;
|
| 212 |
color: black;
|
| 213 |
-
text-align: left;
|
| 214 |
margin: 0;
|
| 215 |
-
width: 100%;
|
| 216 |
}
|
| 217 |
|
| 218 |
-
/*
|
| 219 |
-
.chat-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
flex-direction: column;
|
| 223 |
-
overflow: auto;
|
| 224 |
-
max-width: none !important;
|
| 225 |
-
margin: 0 !important;
|
| 226 |
-
padding: 0 !important;
|
| 227 |
background-color: rgb(240, 236, 230);
|
| 228 |
}
|
| 229 |
|
| 230 |
-
/* Chatbot styling */
|
| 231 |
-
.gradio-chatbot {
|
| 232 |
height: 100% !important;
|
| 233 |
-
|
| 234 |
-
|
| 235 |
background-color: transparent !important;
|
| 236 |
-
|
| 237 |
-
padding:
|
| 238 |
-
width: 100% !important;
|
| 239 |
-
max-width: none !important;
|
| 240 |
-
box-sizing: border-box;
|
| 241 |
-
}
|
| 242 |
-
|
| 243 |
-
/* Message container styling */
|
| 244 |
-
.gradio-chatbot .message-wrap {
|
| 245 |
-
margin: 0.5rem 0 !important;
|
| 246 |
-
max-width: none !important;
|
| 247 |
}
|
| 248 |
|
| 249 |
-
/*
|
| 250 |
.gradio-chatbot .message.bot .markdown {
|
| 251 |
background-color: rgb(240, 185, 103) !important;
|
| 252 |
color: black !important;
|
|
@@ -257,9 +224,9 @@ custom_css = """
|
|
| 257 |
max-width: 70%;
|
| 258 |
margin-left: 0;
|
| 259 |
margin-right: auto;
|
|
|
|
| 260 |
}
|
| 261 |
|
| 262 |
-
/* User message styling */
|
| 263 |
.gradio-chatbot .message.user .markdown {
|
| 264 |
background-color: rgb(242, 238, 233) !important;
|
| 265 |
color: black !important;
|
|
@@ -270,6 +237,7 @@ custom_css = """
|
|
| 270 |
max-width: 70%;
|
| 271 |
margin-left: auto;
|
| 272 |
margin-right: 0;
|
|
|
|
| 273 |
}
|
| 274 |
|
| 275 |
/* Hide avatars */
|
|
@@ -277,119 +245,91 @@ custom_css = """
|
|
| 277 |
display: none !important;
|
| 278 |
}
|
| 279 |
|
| 280 |
-
/*
|
| 281 |
.input-section {
|
| 282 |
-
flex: 0 0 25vh;
|
| 283 |
-
width: 100vw;
|
| 284 |
background-color: rgb(240, 236, 230);
|
| 285 |
border-top: 2px solid rgba(28, 18, 5, 0.1);
|
| 286 |
-
padding:
|
| 287 |
-
|
| 288 |
-
display: flex;
|
| 289 |
-
flex-direction: column;
|
| 290 |
-
justify-content: center;
|
| 291 |
-
position: relative;
|
| 292 |
-
flex-shrink: 0;
|
| 293 |
}
|
| 294 |
|
| 295 |
/* Input textbox styling */
|
| 296 |
-
.input-
|
| 297 |
-
width: 100% !important;
|
| 298 |
-
max-width: none !important;
|
| 299 |
-
flex: 1;
|
| 300 |
-
display: flex;
|
| 301 |
-
align-items: center;
|
| 302 |
background-color: rgb(242, 238, 233) !important;
|
| 303 |
border: 2px solid rgb(28, 18, 5) !important;
|
| 304 |
border-radius: 20px !important;
|
| 305 |
-
|
| 306 |
-
min-height: 60px;
|
| 307 |
-
max-height: calc(25vh - 4rem);
|
| 308 |
}
|
| 309 |
|
| 310 |
-
.input-
|
| 311 |
background-color: transparent !important;
|
| 312 |
border: none !important;
|
| 313 |
color: black !important;
|
| 314 |
padding: 15px !important;
|
| 315 |
font-size: 16px !important;
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
width: 100% !important;
|
| 319 |
-
resize: none !important;
|
| 320 |
-
min-height: 30px;
|
| 321 |
}
|
| 322 |
|
| 323 |
-
.input-
|
| 324 |
-
border: none !important;
|
| 325 |
-
box-shadow: none !important;
|
| 326 |
outline: none !important;
|
|
|
|
| 327 |
}
|
| 328 |
|
| 329 |
-
/*
|
| 330 |
-
.
|
| 331 |
-
display: none !important;
|
| 332 |
-
}
|
| 333 |
-
|
| 334 |
-
/* Clear button styling - positioned in input section */
|
| 335 |
-
.clear-btn {
|
| 336 |
-
position: absolute;
|
| 337 |
-
top: 1rem;
|
| 338 |
-
right: 2rem;
|
| 339 |
background-color: rgb(28, 18, 5) !important;
|
| 340 |
color: white !important;
|
| 341 |
border: none !important;
|
| 342 |
border-radius: 10px !important;
|
| 343 |
padding: 8px 16px !important;
|
| 344 |
-
cursor: pointer;
|
| 345 |
-
|
| 346 |
}
|
| 347 |
|
| 348 |
-
.clear-
|
| 349 |
background-color: rgba(28, 18, 5, 0.8) !important;
|
| 350 |
}
|
| 351 |
|
| 352 |
-
/*
|
| 353 |
-
.gradio-container
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
width: 100% !important;
|
| 358 |
-
height: auto !important;
|
| 359 |
}
|
| 360 |
|
| 361 |
-
/* Ensure
|
| 362 |
-
.
|
| 363 |
-
overflow:
|
| 364 |
}
|
| 365 |
"""
|
| 366 |
|
|
|
|
| 367 |
with gr.Blocks(css=custom_css, title="EduBot") as demo:
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
)
|
| 393 |
|
| 394 |
def respond_and_update(message, history):
|
| 395 |
"""Main function to handle user submission."""
|
|
|
|
| 28 |
|
| 29 |
math_template = ChatPromptTemplate.from_messages([
|
| 30 |
("system", """{system_message}
|
|
|
|
| 31 |
You are an expert math tutor. For every math problem:
|
| 32 |
1. Break it down into key concepts
|
| 33 |
2. Briefly explain concepts
|
| 34 |
3. Outline the process for solving a similar problem
|
|
|
|
| 35 |
Be comprehensive and educational. Structure your response clearly."""),
|
| 36 |
("human", "{question}")
|
| 37 |
])
|
| 38 |
|
| 39 |
research_template = ChatPromptTemplate.from_messages([
|
| 40 |
("system", """{system_message}
|
|
|
|
| 41 |
You are a research skills mentor. Help students with:
|
| 42 |
- Determining the validity of sources
|
| 43 |
- Evaluating source credibility and bias if a source is mentioned
|
|
|
|
| 45 |
- Research strategies and methodologies
|
| 46 |
- Academic writing techniques and structure
|
| 47 |
- Database navigation and search strategies
|
|
|
|
| 48 |
Provide detailed, actionable advice with specific examples."""),
|
| 49 |
("human", "{question}")
|
| 50 |
])
|
| 51 |
|
| 52 |
study_template = ChatPromptTemplate.from_messages([
|
| 53 |
("system", """{system_message}
|
|
|
|
| 54 |
You are a study skills coach. Help students with:
|
| 55 |
- Effective study methods for different learning styles
|
| 56 |
- Time management and scheduling techniques
|
|
|
|
| 59 |
- Note-taking methods and organization
|
| 60 |
- Learning style optimization
|
| 61 |
- Offer short quiz sessions where you pose one to two questions at a time, then provide feedback on the students answers.
|
|
|
|
| 62 |
Provide comprehensive, personalized advice with practical examples."""),
|
| 63 |
("human", "{question}")
|
| 64 |
])
|
| 65 |
|
| 66 |
general_template = ChatPromptTemplate.from_messages([
|
| 67 |
("system", """{system_message}
|
|
|
|
| 68 |
You are EduBot, a comprehensive AI learning assistant. You help students with:
|
| 69 |
π Mathematics (Concise explanations 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 |
Always be encouraging, patient, thorough, and comprehensive."""),
|
| 74 |
("human", "{question}")
|
| 75 |
])
|
|
|
|
| 162 |
logger.exception("Error in LangChain response generation")
|
| 163 |
yield f"Sorry, I encountered an error: {str(e)[:150]}"
|
| 164 |
|
| 165 |
+
# --- Fixed Gradio UI and CSS ---
|
| 166 |
custom_css = """
|
| 167 |
+
/* Reset and base styling */
|
| 168 |
+
html, body {
|
| 169 |
+
margin: 0;
|
| 170 |
+
padding: 0;
|
| 171 |
+
height: 100%;
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
/* Main container styling */
|
| 175 |
.gradio-container {
|
|
|
|
| 176 |
background-color: rgb(240, 236, 230) !important;
|
| 177 |
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 178 |
+
height: 100vh !important;
|
| 179 |
+
max-width: none !important;
|
| 180 |
margin: 0 !important;
|
| 181 |
padding: 0 !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
}
|
| 183 |
|
| 184 |
+
/* Title styling */
|
| 185 |
+
.title-header {
|
|
|
|
|
|
|
| 186 |
background-color: rgb(240, 236, 230);
|
| 187 |
+
padding: 20px;
|
| 188 |
border-bottom: 2px solid rgba(28, 18, 5, 0.1);
|
| 189 |
+
text-align: left;
|
|
|
|
|
|
|
|
|
|
| 190 |
}
|
| 191 |
|
| 192 |
+
.title-header h1 {
|
|
|
|
| 193 |
font-size: 1.8rem;
|
| 194 |
font-weight: bold;
|
| 195 |
color: black;
|
|
|
|
| 196 |
margin: 0;
|
|
|
|
| 197 |
}
|
| 198 |
|
| 199 |
+
/* Chat container - give it proper height */
|
| 200 |
+
.chat-container {
|
| 201 |
+
height: calc(100vh - 200px) !important;
|
| 202 |
+
min-height: 400px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
background-color: rgb(240, 236, 230);
|
| 204 |
}
|
| 205 |
|
| 206 |
+
/* Chatbot specific styling */
|
| 207 |
+
.chat-container .gradio-chatbot {
|
| 208 |
height: 100% !important;
|
| 209 |
+
min-height: 400px !important;
|
| 210 |
+
max-height: none !important;
|
| 211 |
background-color: transparent !important;
|
| 212 |
+
border: none !important;
|
| 213 |
+
padding: 20px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
}
|
| 215 |
|
| 216 |
+
/* Fix message styling */
|
| 217 |
.gradio-chatbot .message.bot .markdown {
|
| 218 |
background-color: rgb(240, 185, 103) !important;
|
| 219 |
color: black !important;
|
|
|
|
| 224 |
max-width: 70%;
|
| 225 |
margin-left: 0;
|
| 226 |
margin-right: auto;
|
| 227 |
+
word-wrap: break-word;
|
| 228 |
}
|
| 229 |
|
|
|
|
| 230 |
.gradio-chatbot .message.user .markdown {
|
| 231 |
background-color: rgb(242, 238, 233) !important;
|
| 232 |
color: black !important;
|
|
|
|
| 237 |
max-width: 70%;
|
| 238 |
margin-left: auto;
|
| 239 |
margin-right: 0;
|
| 240 |
+
word-wrap: break-word;
|
| 241 |
}
|
| 242 |
|
| 243 |
/* Hide avatars */
|
|
|
|
| 245 |
display: none !important;
|
| 246 |
}
|
| 247 |
|
| 248 |
+
/* Input section styling */
|
| 249 |
.input-section {
|
|
|
|
|
|
|
| 250 |
background-color: rgb(240, 236, 230);
|
| 251 |
border-top: 2px solid rgba(28, 18, 5, 0.1);
|
| 252 |
+
padding: 20px;
|
| 253 |
+
min-height: 80px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
}
|
| 255 |
|
| 256 |
/* Input textbox styling */
|
| 257 |
+
.input-section .gradio-textbox {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
background-color: rgb(242, 238, 233) !important;
|
| 259 |
border: 2px solid rgb(28, 18, 5) !important;
|
| 260 |
border-radius: 20px !important;
|
| 261 |
+
margin-bottom: 10px !important;
|
|
|
|
|
|
|
| 262 |
}
|
| 263 |
|
| 264 |
+
.input-section .gradio-textbox textarea {
|
| 265 |
background-color: transparent !important;
|
| 266 |
border: none !important;
|
| 267 |
color: black !important;
|
| 268 |
padding: 15px !important;
|
| 269 |
font-size: 16px !important;
|
| 270 |
+
min-height: 50px !important;
|
| 271 |
+
resize: vertical !important;
|
|
|
|
|
|
|
|
|
|
| 272 |
}
|
| 273 |
|
| 274 |
+
.input-section .gradio-textbox textarea:focus {
|
|
|
|
|
|
|
| 275 |
outline: none !important;
|
| 276 |
+
box-shadow: none !important;
|
| 277 |
}
|
| 278 |
|
| 279 |
+
/* Clear button styling */
|
| 280 |
+
.clear-button {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
background-color: rgb(28, 18, 5) !important;
|
| 282 |
color: white !important;
|
| 283 |
border: none !important;
|
| 284 |
border-radius: 10px !important;
|
| 285 |
padding: 8px 16px !important;
|
| 286 |
+
cursor: pointer !important;
|
| 287 |
+
float: right;
|
| 288 |
}
|
| 289 |
|
| 290 |
+
.clear-button:hover {
|
| 291 |
background-color: rgba(28, 18, 5, 0.8) !important;
|
| 292 |
}
|
| 293 |
|
| 294 |
+
/* Remove default margins and padding that might interfere */
|
| 295 |
+
.gradio-container .block {
|
| 296 |
+
margin: 0 !important;
|
| 297 |
+
padding: 0 !important;
|
| 298 |
+
border: none !important;
|
|
|
|
|
|
|
| 299 |
}
|
| 300 |
|
| 301 |
+
/* Ensure proper scrolling */
|
| 302 |
+
.gradio-chatbot {
|
| 303 |
+
overflow-y: auto !important;
|
| 304 |
}
|
| 305 |
"""
|
| 306 |
|
| 307 |
+
# Create the interface with proper structure
|
| 308 |
with gr.Blocks(css=custom_css, title="EduBot") as demo:
|
| 309 |
+
# Title Section
|
| 310 |
+
gr.HTML('<div class="title-header"><h1>π EduBot</h1></div>')
|
| 311 |
+
|
| 312 |
+
# Chat Section - with explicit height
|
| 313 |
+
with gr.Row(elem_classes=["chat-container"]):
|
| 314 |
+
chatbot = gr.Chatbot(
|
| 315 |
+
type="messages",
|
| 316 |
+
show_copy_button=True,
|
| 317 |
+
show_share_button=False,
|
| 318 |
+
avatar_images=None,
|
| 319 |
+
height="100%"
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
# Input Section
|
| 323 |
+
with gr.Column(elem_classes=["input-section"]):
|
| 324 |
+
with gr.Row():
|
| 325 |
+
clear = gr.Button("Clear", elem_classes=["clear-button"])
|
| 326 |
+
msg = gr.Textbox(
|
| 327 |
+
placeholder="Ask me about math, research, study strategies, or any educational topic...",
|
| 328 |
+
show_label=False,
|
| 329 |
+
lines=2,
|
| 330 |
+
max_lines=6,
|
| 331 |
+
interactive=True
|
| 332 |
+
)
|
|
|
|
| 333 |
|
| 334 |
def respond_and_update(message, history):
|
| 335 |
"""Main function to handle user submission."""
|