Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from langchain.prompts import ChatPromptTemplate
|
| 3 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 4 |
-
from langchain.schema import
|
| 5 |
import os
|
| 6 |
import time
|
| 7 |
import logging
|
|
@@ -11,6 +11,8 @@ import re
|
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
|
|
|
|
|
|
| 14 |
if "HUGGINGFACEHUB_API_TOKEN" not in os.environ:
|
| 15 |
logger.warning("HUGGINGFACEHUB_API_TOKEN not set, the application may not work.")
|
| 16 |
|
|
@@ -54,7 +56,7 @@ Provide detailed, actionable advice with specific examples."""),
|
|
| 54 |
study_template = ChatPromptTemplate.from_messages([
|
| 55 |
("system", """{system_message}
|
| 56 |
|
| 57 |
-
You are a study skills coach. Help students with
|
| 58 |
- Effective study methods for different learning styles
|
| 59 |
- Time management and scheduling techniques
|
| 60 |
- Memory techniques and retention strategies
|
|
@@ -76,8 +78,8 @@ You are EduBot, a comprehensive AI learning assistant. You help students with:
|
|
| 76 |
π Study strategies (effective learning techniques and exam preparation)
|
| 77 |
π οΈ Educational tools (guidance on learning resources and technologies)
|
| 78 |
|
| 79 |
-
Always be encouraging, patient, thorough, and comprehensive.
|
| 80 |
-
("human", "
|
| 81 |
])
|
| 82 |
|
| 83 |
# --- Core Logic Functions ---
|
|
@@ -155,16 +157,30 @@ custom_css = """
|
|
| 155 |
.gradio-container {
|
| 156 |
background-color: rgb(240, 236, 230) !important;
|
| 157 |
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
}
|
| 159 |
|
| 160 |
-
/*
|
| 161 |
-
.chat-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
max-width: 800px;
|
| 163 |
margin: 0 auto;
|
| 164 |
padding: 1rem;
|
| 165 |
-
|
| 166 |
-
display: flex;
|
| 167 |
-
flex-direction: column;
|
| 168 |
}
|
| 169 |
|
| 170 |
/* Model name title */
|
|
@@ -182,6 +198,7 @@ custom_css = """
|
|
| 182 |
overflow-y: auto;
|
| 183 |
border: none !important;
|
| 184 |
background-color: transparent !important;
|
|
|
|
| 185 |
}
|
| 186 |
|
| 187 |
/* Message container styling */
|
|
@@ -220,12 +237,29 @@ custom_css = """
|
|
| 220 |
display: none !important;
|
| 221 |
}
|
| 222 |
|
| 223 |
-
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
.input-container .wrap {
|
| 225 |
background-color: rgb(242, 238, 233) !important;
|
| 226 |
border: 2px solid rgb(28, 18, 5) !important;
|
| 227 |
border-radius: 20px !important;
|
| 228 |
box-shadow: none !important;
|
|
|
|
| 229 |
}
|
| 230 |
|
| 231 |
.input-container textarea {
|
|
@@ -236,6 +270,8 @@ custom_css = """
|
|
| 236 |
font-size: 16px !important;
|
| 237 |
box-shadow: none !important;
|
| 238 |
outline: none !important;
|
|
|
|
|
|
|
| 239 |
}
|
| 240 |
|
| 241 |
.input-container textarea:focus {
|
|
@@ -249,20 +285,18 @@ custom_css = """
|
|
| 249 |
display: none !important;
|
| 250 |
}
|
| 251 |
|
| 252 |
-
/*
|
| 253 |
-
.input-container {
|
| 254 |
-
margin-top: 1rem;
|
| 255 |
-
}
|
| 256 |
-
|
| 257 |
-
/* Clear button styling */
|
| 258 |
.clear-btn {
|
|
|
|
|
|
|
|
|
|
| 259 |
background-color: rgb(28, 18, 5) !important;
|
| 260 |
color: white !important;
|
| 261 |
border: none !important;
|
| 262 |
border-radius: 10px !important;
|
| 263 |
padding: 8px 16px !important;
|
| 264 |
-
margin-top: 0.5rem;
|
| 265 |
cursor: pointer;
|
|
|
|
| 266 |
}
|
| 267 |
|
| 268 |
.clear-btn:hover {
|
|
@@ -271,29 +305,33 @@ custom_css = """
|
|
| 271 |
"""
|
| 272 |
|
| 273 |
with gr.Blocks(css=custom_css, title="EduBot") as demo:
|
| 274 |
-
with gr.Column(elem_classes=["
|
| 275 |
-
#
|
| 276 |
-
gr.
|
| 277 |
|
| 278 |
-
#
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
-
#
|
| 288 |
-
with gr.
|
| 289 |
msg = gr.Textbox(
|
| 290 |
placeholder="Ask me about math, research, study strategies, or any educational topic...",
|
| 291 |
show_label=False,
|
| 292 |
-
lines=
|
| 293 |
-
|
| 294 |
-
|
| 295 |
)
|
| 296 |
-
clear = gr.Button("Clear", elem_classes=["clear-btn"], scale=1)
|
| 297 |
|
| 298 |
def respond_and_update(message, history):
|
| 299 |
"""Main function to handle user submission."""
|
|
|
|
| 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
|
|
|
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
+
# Make sure to set your Hugging Face API token in your environment variables
|
| 15 |
+
# For example: export HUGGINGFACEHUB_API_TOKEN='your_token_here'
|
| 16 |
if "HUGGINGFACEHUB_API_TOKEN" not in os.environ:
|
| 17 |
logger.warning("HUGGINGFACEHUB_API_TOKEN not set, the application may not work.")
|
| 18 |
|
|
|
|
| 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
|
| 62 |
- Memory techniques and retention strategies
|
|
|
|
| 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 |
])
|
| 84 |
|
| 85 |
# --- Core Logic Functions ---
|
|
|
|
| 157 |
.gradio-container {
|
| 158 |
background-color: rgb(240, 236, 230) !important;
|
| 159 |
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 160 |
+
height: 100vh !important;
|
| 161 |
+
overflow: hidden;
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
/* Main layout container */
|
| 165 |
+
.main-container {
|
| 166 |
+
height: 100vh;
|
| 167 |
+
display: flex;
|
| 168 |
+
flex-direction: column;
|
| 169 |
+
max-width: none !important;
|
| 170 |
+
margin: 0 !important;
|
| 171 |
+
padding: 0 !important;
|
| 172 |
}
|
| 173 |
|
| 174 |
+
/* Chat content area */
|
| 175 |
+
.chat-content {
|
| 176 |
+
flex: 1;
|
| 177 |
+
display: flex;
|
| 178 |
+
flex-direction: column;
|
| 179 |
+
overflow: hidden;
|
| 180 |
max-width: 800px;
|
| 181 |
margin: 0 auto;
|
| 182 |
padding: 1rem;
|
| 183 |
+
padding-bottom: 0;
|
|
|
|
|
|
|
| 184 |
}
|
| 185 |
|
| 186 |
/* Model name title */
|
|
|
|
| 198 |
overflow-y: auto;
|
| 199 |
border: none !important;
|
| 200 |
background-color: transparent !important;
|
| 201 |
+
margin-bottom: 0 !important;
|
| 202 |
}
|
| 203 |
|
| 204 |
/* Message container styling */
|
|
|
|
| 237 |
display: none !important;
|
| 238 |
}
|
| 239 |
|
| 240 |
+
/* Full-width input container at bottom */
|
| 241 |
+
.input-bottom {
|
| 242 |
+
position: sticky;
|
| 243 |
+
bottom: 0;
|
| 244 |
+
left: 0;
|
| 245 |
+
right: 0;
|
| 246 |
+
background-color: rgb(240, 236, 230);
|
| 247 |
+
padding: 1rem;
|
| 248 |
+
border-top: 1px solid rgba(28, 18, 5, 0.1);
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
/* Input textbox styling - full width */
|
| 252 |
+
.input-container {
|
| 253 |
+
width: 100% !important;
|
| 254 |
+
max-width: none !important;
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
.input-container .wrap {
|
| 258 |
background-color: rgb(242, 238, 233) !important;
|
| 259 |
border: 2px solid rgb(28, 18, 5) !important;
|
| 260 |
border-radius: 20px !important;
|
| 261 |
box-shadow: none !important;
|
| 262 |
+
width: 100% !important;
|
| 263 |
}
|
| 264 |
|
| 265 |
.input-container textarea {
|
|
|
|
| 270 |
font-size: 16px !important;
|
| 271 |
box-shadow: none !important;
|
| 272 |
outline: none !important;
|
| 273 |
+
width: 100% !important;
|
| 274 |
+
resize: none !important;
|
| 275 |
}
|
| 276 |
|
| 277 |
.input-container textarea:focus {
|
|
|
|
| 285 |
display: none !important;
|
| 286 |
}
|
| 287 |
|
| 288 |
+
/* Clear button styling - positioned in top right */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
.clear-btn {
|
| 290 |
+
position: absolute;
|
| 291 |
+
top: 1rem;
|
| 292 |
+
right: 1rem;
|
| 293 |
background-color: rgb(28, 18, 5) !important;
|
| 294 |
color: white !important;
|
| 295 |
border: none !important;
|
| 296 |
border-radius: 10px !important;
|
| 297 |
padding: 8px 16px !important;
|
|
|
|
| 298 |
cursor: pointer;
|
| 299 |
+
z-index: 1000;
|
| 300 |
}
|
| 301 |
|
| 302 |
.clear-btn:hover {
|
|
|
|
| 305 |
"""
|
| 306 |
|
| 307 |
with gr.Blocks(css=custom_css, title="EduBot") as demo:
|
| 308 |
+
with gr.Column(elem_classes=["main-container"]):
|
| 309 |
+
# Clear button in top right
|
| 310 |
+
clear = gr.Button("Clear", elem_classes=["clear-btn"])
|
| 311 |
|
| 312 |
+
# Chat content area
|
| 313 |
+
with gr.Column(elem_classes=["chat-content"]):
|
| 314 |
+
# Model Name in the top-left corner
|
| 315 |
+
gr.HTML('<div class="model-name">π EduBot</div>')
|
| 316 |
+
|
| 317 |
+
# The main chatbot interface
|
| 318 |
+
chatbot = gr.Chatbot(
|
| 319 |
+
type="messages",
|
| 320 |
+
height="100%",
|
| 321 |
+
show_copy_button=False,
|
| 322 |
+
show_share_button=False,
|
| 323 |
+
avatar_images=None
|
| 324 |
+
)
|
| 325 |
|
| 326 |
+
# Full-width input at bottom
|
| 327 |
+
with gr.Column(elem_classes=["input-bottom"]):
|
| 328 |
msg = gr.Textbox(
|
| 329 |
placeholder="Ask me about math, research, study strategies, or any educational topic...",
|
| 330 |
show_label=False,
|
| 331 |
+
lines=1,
|
| 332 |
+
max_lines=4,
|
| 333 |
+
elem_classes=["input-container"]
|
| 334 |
)
|
|
|
|
| 335 |
|
| 336 |
def respond_and_update(message, history):
|
| 337 |
"""Main function to handle user submission."""
|