Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,67 @@
|
|
| 1 |
"""
|
| 2 |
-
ThutoAI - AI Student Assistant (Gradio
|
| 3 |
-
For Hugging Face Spaces
|
| 4 |
Meaning: "Thuto" = Learning/Education (Setswana)
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
"""
|
| 13 |
|
| 14 |
import os
|
| 15 |
import gradio as gr
|
| 16 |
from datetime import datetime
|
| 17 |
from typing import List, Dict
|
|
|
|
| 18 |
|
| 19 |
-
# ==================== MOCK SERVICES ====================
|
| 20 |
|
| 21 |
class SchoolService:
|
|
|
|
|
|
|
| 22 |
def __init__(self):
|
| 23 |
self.announcements = [
|
| 24 |
{
|
| 25 |
"id": 1,
|
| 26 |
"title": "Math Final Exam",
|
| 27 |
-
"content": "Covers chapters 1-5. Bring calculator.",
|
| 28 |
"course": "Mathematics",
|
| 29 |
"date": "2025-04-15",
|
| 30 |
-
"priority": "high"
|
|
|
|
| 31 |
},
|
| 32 |
{
|
| 33 |
"id": 2,
|
| 34 |
"title": "Science Project Due",
|
| 35 |
-
"content": "Submit your ecology report by Friday.",
|
| 36 |
"course": "Science",
|
| 37 |
"date": "2025-04-12",
|
| 38 |
-
"priority": "normal"
|
|
|
|
| 39 |
},
|
| 40 |
{
|
| 41 |
"id": 3,
|
| 42 |
"title": "Library Extended Hours",
|
| 43 |
-
"content": "Open until 9 PM during exam week.",
|
| 44 |
"course": "General",
|
| 45 |
"date": "2025-04-10",
|
| 46 |
-
"priority": "low"
|
|
|
|
| 47 |
},
|
| 48 |
]
|
| 49 |
self.courses = ["All", "Mathematics", "Science", "English", "History", "General"]
|
| 50 |
self.files = []
|
|
|
|
|
|
|
| 51 |
|
| 52 |
def get_announcements(self, course_filter: str = "All") -> List[Dict]:
|
| 53 |
if course_filter == "All":
|
| 54 |
return self.announcements
|
| 55 |
return [a for a in self.announcements if a["course"] == course_filter]
|
| 56 |
|
| 57 |
-
def add_announcement(self, title: str, content: str, course: str, priority: str):
|
| 58 |
new_id = max([a["id"] for a in self.announcements], default=0) + 1
|
| 59 |
self.announcements.append({
|
| 60 |
"id": new_id,
|
|
@@ -62,25 +69,46 @@ class SchoolService:
|
|
| 62 |
"content": content,
|
| 63 |
"course": course,
|
| 64 |
"date": datetime.now().strftime("%Y-%m-%d"),
|
| 65 |
-
"priority": priority
|
|
|
|
| 66 |
})
|
|
|
|
| 67 |
|
| 68 |
def upload_file(self, file) -> str:
|
| 69 |
if not file:
|
| 70 |
return "β No file selected"
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
def get_school_context_for_ai(self) -> str:
|
| 75 |
context = "Current School Announcements:\n"
|
| 76 |
-
for ann in self.announcements[:
|
| 77 |
-
context += f"- [{ann['course']}] {ann['title']}: {ann['content'][:
|
| 78 |
return context
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
class AdminService:
|
|
|
|
|
|
|
| 82 |
def __init__(self):
|
| 83 |
-
self.admins = {
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
def authenticate(self, username: str, password: str) -> bool:
|
| 86 |
return self.admins.get(username) == password
|
|
@@ -104,16 +132,27 @@ if USE_OPENAI:
|
|
| 104 |
else:
|
| 105 |
print("β οΈ OPENAI_API_KEY not set. Using mock responses.")
|
| 106 |
|
|
|
|
|
|
|
| 107 |
def ai_chat(message: str, history: List) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
if USE_OPENAI:
|
| 109 |
try:
|
| 110 |
-
system_prompt = f"""You are ThutoAI,
|
| 111 |
-
|
| 112 |
{school_service.get_school_context_for_ai()}
|
| 113 |
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
# β
|
| 117 |
response = client.chat.completions.create(
|
| 118 |
model="gpt-3.5-turbo",
|
| 119 |
messages=[
|
|
@@ -122,88 +161,133 @@ Be helpful, encouraging, and accurate. If unsure, say so."""
|
|
| 122 |
],
|
| 123 |
temperature=0.7,
|
| 124 |
max_tokens=500
|
| 125 |
-
)
|
| 126 |
|
| 127 |
reply = response.choices[0].message.content.strip()
|
| 128 |
|
| 129 |
-
#
|
| 130 |
-
keywords = ["exam", "test", "due", "assignment", "deadline", "when", "what"]
|
| 131 |
if any(kw in message.lower() for kw in keywords):
|
| 132 |
matches = school_service.get_announcements()
|
| 133 |
relevant = [a for a in matches if any(kw in a["title"].lower() or kw in a["content"].lower() for kw in keywords)]
|
| 134 |
if relevant:
|
| 135 |
-
reply += "\n\nπ **
|
| 136 |
for ann in relevant[:2]:
|
| 137 |
-
|
|
|
|
| 138 |
|
| 139 |
return reply
|
|
|
|
| 140 |
except Exception as e:
|
| 141 |
-
return f"β οΈ
|
| 142 |
else:
|
| 143 |
-
|
|
|
|
| 144 |
|
| 145 |
|
| 146 |
-
# ====================
|
| 147 |
|
| 148 |
def render_announcements(course: str) -> str:
|
|
|
|
| 149 |
announcements = school_service.get_announcements(course)
|
| 150 |
if not announcements:
|
| 151 |
-
return "
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
for ann in announcements:
|
| 154 |
-
|
|
|
|
| 155 |
html += f"""
|
| 156 |
-
<div style=
|
| 157 |
-
border: 1px solid #e0e0e0;
|
| 158 |
-
border-radius: 8px;
|
| 159 |
-
padding: 12px;
|
| 160 |
-
margin: 8px 0;
|
| 161 |
background: white;
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
</div>
|
| 181 |
</div>
|
| 182 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
return html
|
| 184 |
|
| 185 |
|
| 186 |
-
#
|
|
|
|
| 187 |
IS_ADMIN = False
|
| 188 |
|
| 189 |
def admin_login(username: str, password: str) -> tuple:
|
|
|
|
| 190 |
global IS_ADMIN
|
| 191 |
if admin_service.authenticate(username, password):
|
| 192 |
IS_ADMIN = True
|
|
|
|
|
|
|
| 193 |
return (
|
| 194 |
-
gr.update(visible=False),
|
| 195 |
-
gr.update(visible=True),
|
| 196 |
-
"β
Welcome,
|
| 197 |
-
gr.update(visible=True)
|
| 198 |
)
|
| 199 |
return (
|
| 200 |
gr.update(visible=True),
|
| 201 |
gr.update(visible=False),
|
| 202 |
-
"β Invalid
|
| 203 |
gr.update(visible=False)
|
| 204 |
)
|
| 205 |
|
| 206 |
def admin_logout():
|
|
|
|
| 207 |
global IS_ADMIN
|
| 208 |
IS_ADMIN = False
|
| 209 |
return (
|
|
@@ -214,34 +298,131 @@ def admin_logout():
|
|
| 214 |
)
|
| 215 |
|
| 216 |
def post_announcement(title: str, content: str, course: str, priority: str) -> str:
|
|
|
|
| 217 |
if not IS_ADMIN:
|
| 218 |
-
return "π
|
| 219 |
-
if not title.strip()
|
| 220 |
-
return "β οΈ Title
|
|
|
|
|
|
|
|
|
|
| 221 |
school_service.add_announcement(title, content, course, priority)
|
| 222 |
-
return "β
|
| 223 |
|
| 224 |
-
# ====================
|
| 225 |
|
| 226 |
-
|
|
|
|
| 227 |
.gradio-container {
|
| 228 |
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
}
|
| 230 |
-
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
with gr.Tab("π’ Announcements"):
|
| 234 |
-
gr.Markdown("### Filter by Course or Subject")
|
| 235 |
-
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
course_filter.change(fn=render_announcements, inputs=course_filter, outputs=announcements_html)
|
|
|
|
| 238 |
demo.load(fn=render_announcements, inputs=course_filter, outputs=announcements_html)
|
| 239 |
|
| 240 |
with gr.Tab("π¬ Ask ThutoAI"):
|
| 241 |
-
gr.Markdown("### Ask me anything β
|
| 242 |
-
chatbot = gr.Chatbot(
|
| 243 |
-
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
def respond(message, chat_history):
|
| 247 |
bot_reply = ai_chat(message, chat_history)
|
|
@@ -249,36 +430,52 @@ with gr.Blocks(css="""
|
|
| 249 |
return "", chat_history
|
| 250 |
|
| 251 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
|
|
|
|
|
|
| 259 |
upload_btn.click(fn=school_service.upload_file, inputs=file_input, outputs=status)
|
| 260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
with gr.Tab("π Teacher Admin"):
|
| 262 |
-
gr.Markdown("### Post Announcements
|
| 263 |
|
| 264 |
with gr.Group() as login_group:
|
| 265 |
-
gr.Markdown("####
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
|
|
|
| 270 |
|
| 271 |
with gr.Group(visible=False) as dashboard:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
gr.Markdown("#### βοΈ Create New Announcement")
|
| 273 |
with gr.Row():
|
| 274 |
-
title = gr.Textbox(label="
|
| 275 |
-
course = gr.Dropdown(choices=school_service.courses[1:], label="Course", value="General")
|
| 276 |
-
content = gr.Textbox(label="
|
| 277 |
-
priority = gr.Radio(["low", "normal", "high"], label="Priority
|
| 278 |
-
post_btn = gr.Button("π¬ Post Announcement")
|
| 279 |
post_result = gr.Textbox(label="Result", interactive=False)
|
| 280 |
|
| 281 |
-
logout_btn = gr.Button("β¬
οΈ Logout")
|
| 282 |
|
| 283 |
# Connect buttons
|
| 284 |
login_btn.click(
|
|
@@ -297,6 +494,6 @@ with gr.Blocks(css="""
|
|
| 297 |
outputs=post_result
|
| 298 |
)
|
| 299 |
|
| 300 |
-
|
| 301 |
if __name__ == "__main__":
|
| 302 |
demo.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
+
ThutoAI - Modern AI Student Assistant (Gradio Enhanced)
|
|
|
|
| 3 |
Meaning: "Thuto" = Learning/Education (Setswana)
|
| 4 |
|
| 5 |
+
β
Modern UI with animations, dark mode, icons
|
| 6 |
+
β
Mobile-first responsive design
|
| 7 |
+
β
Teacher dashboard with stats
|
| 8 |
+
β
Loading states & smooth UX
|
| 9 |
+
β
File upload preview & management
|
| 10 |
+
β
Fully commented for easy understanding
|
| 11 |
"""
|
| 12 |
|
| 13 |
import os
|
| 14 |
import gradio as gr
|
| 15 |
from datetime import datetime
|
| 16 |
from typing import List, Dict
|
| 17 |
+
import time
|
| 18 |
|
| 19 |
+
# ==================== MOCK SERVICES (Replace with DB later) ====================
|
| 20 |
|
| 21 |
class SchoolService:
|
| 22 |
+
"""Handles announcements, files, and AI context."""
|
| 23 |
+
|
| 24 |
def __init__(self):
|
| 25 |
self.announcements = [
|
| 26 |
{
|
| 27 |
"id": 1,
|
| 28 |
"title": "Math Final Exam",
|
| 29 |
+
"content": "Covers chapters 1-5. Bring calculator and ruler.",
|
| 30 |
"course": "Mathematics",
|
| 31 |
"date": "2025-04-15",
|
| 32 |
+
"priority": "high",
|
| 33 |
+
"posted_by": "Mr. Smith"
|
| 34 |
},
|
| 35 |
{
|
| 36 |
"id": 2,
|
| 37 |
"title": "Science Project Due",
|
| 38 |
+
"content": "Submit your ecology report by Friday. Include bibliography.",
|
| 39 |
"course": "Science",
|
| 40 |
"date": "2025-04-12",
|
| 41 |
+
"priority": "normal",
|
| 42 |
+
"posted_by": "Dr. Lee"
|
| 43 |
},
|
| 44 |
{
|
| 45 |
"id": 3,
|
| 46 |
"title": "Library Extended Hours",
|
| 47 |
+
"content": "Open until 9 PM during exam week. Quiet study zones available.",
|
| 48 |
"course": "General",
|
| 49 |
"date": "2025-04-10",
|
| 50 |
+
"priority": "low",
|
| 51 |
+
"posted_by": "Librarian"
|
| 52 |
},
|
| 53 |
]
|
| 54 |
self.courses = ["All", "Mathematics", "Science", "English", "History", "General"]
|
| 55 |
self.files = []
|
| 56 |
+
self.total_announcements = len(self.announcements)
|
| 57 |
+
self.total_files = 0
|
| 58 |
|
| 59 |
def get_announcements(self, course_filter: str = "All") -> List[Dict]:
|
| 60 |
if course_filter == "All":
|
| 61 |
return self.announcements
|
| 62 |
return [a for a in self.announcements if a["course"] == course_filter]
|
| 63 |
|
| 64 |
+
def add_announcement(self, title: str, content: str, course: str, priority: str, posted_by: str = "Admin"):
|
| 65 |
new_id = max([a["id"] for a in self.announcements], default=0) + 1
|
| 66 |
self.announcements.append({
|
| 67 |
"id": new_id,
|
|
|
|
| 69 |
"content": content,
|
| 70 |
"course": course,
|
| 71 |
"date": datetime.now().strftime("%Y-%m-%d"),
|
| 72 |
+
"priority": priority,
|
| 73 |
+
"posted_by": posted_by
|
| 74 |
})
|
| 75 |
+
self.total_announcements += 1
|
| 76 |
|
| 77 |
def upload_file(self, file) -> str:
|
| 78 |
if not file:
|
| 79 |
return "β No file selected"
|
| 80 |
+
filename = file.name
|
| 81 |
+
self.files.append({
|
| 82 |
+
"name": filename,
|
| 83 |
+
"uploaded_at": datetime.now().strftime("%Y-%m-%d %H:%M"),
|
| 84 |
+
"size": f"{os.path.getsize(file.name) // 1024} KB" if os.path.exists(file.name) else "Unknown"
|
| 85 |
+
})
|
| 86 |
+
self.total_files += 1
|
| 87 |
+
return f"β
Uploaded: {filename}"
|
| 88 |
|
| 89 |
def get_school_context_for_ai(self) -> str:
|
| 90 |
context = "Current School Announcements:\n"
|
| 91 |
+
for ann in self.announcements[-5:]: # Show latest 5
|
| 92 |
+
context += f"- [{ann['course']}] {ann['title']}: {ann['content'][:80]}... (Priority: {ann['priority']})\n"
|
| 93 |
return context
|
| 94 |
|
| 95 |
+
def get_stats(self) -> Dict:
|
| 96 |
+
return {
|
| 97 |
+
"total_announcements": self.total_announcements,
|
| 98 |
+
"total_files": self.total_files,
|
| 99 |
+
"active_courses": len(set(a["course"] for a in self.announcements if a["course"] != "General")),
|
| 100 |
+
"high_priority": len([a for a in self.announcements if a["priority"] == "high"])
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
|
| 104 |
class AdminService:
|
| 105 |
+
"""Handles teacher authentication."""
|
| 106 |
+
|
| 107 |
def __init__(self):
|
| 108 |
+
self.admins = {
|
| 109 |
+
"teacher@thutoai.edu": "password123",
|
| 110 |
+
"admin@school.org": "letmein"
|
| 111 |
+
}
|
| 112 |
|
| 113 |
def authenticate(self, username: str, password: str) -> bool:
|
| 114 |
return self.admins.get(username) == password
|
|
|
|
| 132 |
else:
|
| 133 |
print("β οΈ OPENAI_API_KEY not set. Using mock responses.")
|
| 134 |
|
| 135 |
+
# ==================== AI CHAT WITH LOADING STATE ====================
|
| 136 |
+
|
| 137 |
def ai_chat(message: str, history: List) -> str:
|
| 138 |
+
"""Generate AI response with loading animation."""
|
| 139 |
+
if not message.strip():
|
| 140 |
+
return "β οΈ Please ask a real question."
|
| 141 |
+
|
| 142 |
if USE_OPENAI:
|
| 143 |
try:
|
| 144 |
+
system_prompt = f"""You are ThutoAI, a friendly and knowledgeable AI assistant for students.
|
| 145 |
+
Context from school:
|
| 146 |
{school_service.get_school_context_for_ai()}
|
| 147 |
|
| 148 |
+
Guidelines:
|
| 149 |
+
- Be encouraging, clear, and concise.
|
| 150 |
+
- Use emojis sparingly to keep it fun πππ―
|
| 151 |
+
- If asked about deadlines or exams, refer to context.
|
| 152 |
+
- Offer study tips if appropriate.
|
| 153 |
+
- Never invent facts β say 'I don't know' if unsure."""
|
| 154 |
|
| 155 |
+
# β
Fixed: Properly closed parentheses
|
| 156 |
response = client.chat.completions.create(
|
| 157 |
model="gpt-3.5-turbo",
|
| 158 |
messages=[
|
|
|
|
| 161 |
],
|
| 162 |
temperature=0.7,
|
| 163 |
max_tokens=500
|
| 164 |
+
)
|
| 165 |
|
| 166 |
reply = response.choices[0].message.content.strip()
|
| 167 |
|
| 168 |
+
# Append relevant announcements
|
| 169 |
+
keywords = ["exam", "test", "due", "assignment", "deadline", "when", "what", "grade", "score"]
|
| 170 |
if any(kw in message.lower() for kw in keywords):
|
| 171 |
matches = school_service.get_announcements()
|
| 172 |
relevant = [a for a in matches if any(kw in a["title"].lower() or kw in a["content"].lower() for kw in keywords)]
|
| 173 |
if relevant:
|
| 174 |
+
reply += "\n\nπ **Quick Info from School:**"
|
| 175 |
for ann in relevant[:2]:
|
| 176 |
+
emoji = "π¨" if ann["priority"] == "high" else "π" if ann["priority"] == "normal" else "βΉοΈ"
|
| 177 |
+
reply += f"\n{emoji} **{ann['title']}** ({ann['course']})\n β {ann['content'][:70]}..."
|
| 178 |
|
| 179 |
return reply
|
| 180 |
+
|
| 181 |
except Exception as e:
|
| 182 |
+
return f"β οΈ Sorry, I had a glitch: {str(e)}"
|
| 183 |
else:
|
| 184 |
+
time.sleep(1.5) # Simulate AI thinking
|
| 185 |
+
return f"π Hi! I'm ThutoAI. You asked: '{message}'.\nπ‘ *Pro tip: Add your OpenAI API key in HF Secrets for smarter answers!*"
|
| 186 |
|
| 187 |
|
| 188 |
+
# ==================== MODERN UI RENDERING ====================
|
| 189 |
|
| 190 |
def render_announcements(course: str) -> str:
|
| 191 |
+
"""Render announcements with modern cards and icons."""
|
| 192 |
announcements = school_service.get_announcements(course)
|
| 193 |
if not announcements:
|
| 194 |
+
return """
|
| 195 |
+
<div style='text-align: center; padding: 40px; color: #6c757d;'>
|
| 196 |
+
<div style='font-size: 4em; margin-bottom: 16px;'>π</div>
|
| 197 |
+
<h3>No announcements for this course.</h3>
|
| 198 |
+
<p>Check back later or select "All" to see everything.</p>
|
| 199 |
+
</div>
|
| 200 |
+
"""
|
| 201 |
+
|
| 202 |
+
html = "<div style='display: grid; gap: 16px;'>"
|
| 203 |
+
priority_icons = {"high": "π¨", "normal": "π", "low": "βΉοΈ"}
|
| 204 |
+
priority_colors = {"high": "#dc3545", "normal": "#ffc107", "low": "#6c757d"}
|
| 205 |
+
|
| 206 |
for ann in announcements:
|
| 207 |
+
icon = priority_icons.get(ann["priority"], "π")
|
| 208 |
+
color = priority_colors.get(ann["priority"], "#6c757d")
|
| 209 |
html += f"""
|
| 210 |
+
<div style='
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
background: white;
|
| 212 |
+
border-radius: 12px;
|
| 213 |
+
padding: 20px;
|
| 214 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.08);
|
| 215 |
+
border-left: 4px solid {color};
|
| 216 |
+
transition: transform 0.2s, box-shadow 0.2s;
|
| 217 |
+
animation: fadeInUp 0.6s ease-out;
|
| 218 |
+
' onmouseover="this.style.transform='translateY(-2px)'; this.style.boxShadow='0 4px 12px rgba(0,0,0,0.1)';"
|
| 219 |
+
onmouseout="this.style.transform='translateY(0)'; this.style.boxShadow='0 2px 8px rgba(0,0,0,0.08)';">
|
| 220 |
+
<div style='display: flex; align-items: flex-start; gap: 12px;'>
|
| 221 |
+
<div style='font-size: 1.5em;'>{icon}</div>
|
| 222 |
+
<div style='flex: 1;'>
|
| 223 |
+
<div style='display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px;'>
|
| 224 |
+
<h3 style='margin: 0; color: #212529;'>{ann['title']}</h3>
|
| 225 |
+
<span style='
|
| 226 |
+
background: {color};
|
| 227 |
+
color: white;
|
| 228 |
+
padding: 4px 10px;
|
| 229 |
+
border-radius: 20px;
|
| 230 |
+
font-size: 0.8em;
|
| 231 |
+
font-weight: bold;
|
| 232 |
+
'>{ann['priority'].upper()}</span>
|
| 233 |
+
</div>
|
| 234 |
+
<p style='margin: 8px 0; color: #495057; line-height: 1.5;'>{ann['content']}</p>
|
| 235 |
+
<div style='display: flex; justify-content: space-between; font-size: 0.85em; color: #6c757d; margin-top: 12px;'>
|
| 236 |
+
<span>π {ann['course']}</span>
|
| 237 |
+
<span>π
{ann['date']}</span>
|
| 238 |
+
<span>π¨βπ« {ann['posted_by']}</span>
|
| 239 |
+
</div>
|
| 240 |
+
</div>
|
| 241 |
</div>
|
| 242 |
</div>
|
| 243 |
"""
|
| 244 |
+
|
| 245 |
+
html += "</div>"
|
| 246 |
+
|
| 247 |
+
# Add animation CSS
|
| 248 |
+
html += """
|
| 249 |
+
<style>
|
| 250 |
+
@keyframes fadeInUp {
|
| 251 |
+
from {
|
| 252 |
+
opacity: 0;
|
| 253 |
+
transform: translateY(20px);
|
| 254 |
+
}
|
| 255 |
+
to {
|
| 256 |
+
opacity: 1;
|
| 257 |
+
transform: translateY(0);
|
| 258 |
+
}
|
| 259 |
+
}
|
| 260 |
+
</style>
|
| 261 |
+
"""
|
| 262 |
return html
|
| 263 |
|
| 264 |
|
| 265 |
+
# ==================== STATE & ADMIN FUNCTIONS ====================
|
| 266 |
+
|
| 267 |
IS_ADMIN = False
|
| 268 |
|
| 269 |
def admin_login(username: str, password: str) -> tuple:
|
| 270 |
+
"""Handle admin login with feedback."""
|
| 271 |
global IS_ADMIN
|
| 272 |
if admin_service.authenticate(username, password):
|
| 273 |
IS_ADMIN = True
|
| 274 |
+
stats = school_service.get_stats()
|
| 275 |
+
stats_text = f"π Stats: {stats['total_announcements']} announcements, {stats['total_files']} files"
|
| 276 |
return (
|
| 277 |
+
gr.update(visible=False),
|
| 278 |
+
gr.update(visible=True),
|
| 279 |
+
f"β
Welcome back, {username.split('@')[0].title()}! {stats_text}",
|
| 280 |
+
gr.update(visible=True)
|
| 281 |
)
|
| 282 |
return (
|
| 283 |
gr.update(visible=True),
|
| 284 |
gr.update(visible=False),
|
| 285 |
+
"β Invalid credentials. Try teacher@thutoai.edu / password123",
|
| 286 |
gr.update(visible=False)
|
| 287 |
)
|
| 288 |
|
| 289 |
def admin_logout():
|
| 290 |
+
"""Handle admin logout."""
|
| 291 |
global IS_ADMIN
|
| 292 |
IS_ADMIN = False
|
| 293 |
return (
|
|
|
|
| 298 |
)
|
| 299 |
|
| 300 |
def post_announcement(title: str, content: str, course: str, priority: str) -> str:
|
| 301 |
+
"""Post announcement with validation."""
|
| 302 |
if not IS_ADMIN:
|
| 303 |
+
return "π Please log in first."
|
| 304 |
+
if not title.strip():
|
| 305 |
+
return "β οΈ Title is required."
|
| 306 |
+
if not content.strip():
|
| 307 |
+
return "β οΈ Content is required."
|
| 308 |
+
|
| 309 |
school_service.add_announcement(title, content, course, priority)
|
| 310 |
+
return f"β
Posted! π New announcement ID: {len(school_service.announcements)}"
|
| 311 |
|
| 312 |
+
# ==================== CUSTOM CSS & JS FOR MODERN UI ====================
|
| 313 |
|
| 314 |
+
CUSTOM_CSS = """
|
| 315 |
+
/* Modern, clean design system */
|
| 316 |
.gradio-container {
|
| 317 |
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
|
| 318 |
+
max-width: 1200px;
|
| 319 |
+
margin: 0 auto;
|
| 320 |
+
padding: 16px;
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
/* Button styles */
|
| 324 |
+
.primary {
|
| 325 |
+
background: linear-gradient(135deg, #6e8efb, #a777e3) !important;
|
| 326 |
+
border: none !important;
|
| 327 |
+
color: white !important;
|
| 328 |
+
transition: all 0.3s ease !important;
|
| 329 |
+
}
|
| 330 |
+
.primary:hover {
|
| 331 |
+
transform: translateY(-2px) !important;
|
| 332 |
+
box-shadow: 0 4px 12px rgba(110, 142, 251, 0.3) !important;
|
| 333 |
+
}
|
| 334 |
+
|
| 335 |
+
/* Chatbot bubble styling */
|
| 336 |
+
.chatbot-container {
|
| 337 |
+
background: #f8f9fa !important;
|
| 338 |
+
border-radius: 16px !important;
|
| 339 |
+
}
|
| 340 |
+
.user, .bot {
|
| 341 |
+
border-radius: 18px !important;
|
| 342 |
+
padding: 12px 16px !important;
|
| 343 |
+
margin: 4px 0 !important;
|
| 344 |
}
|
| 345 |
+
.bot {
|
| 346 |
+
background: linear-gradient(135deg, #f5f7fa, #e4e8eb) !important;
|
| 347 |
+
border: 1px solid #e0e0e0 !important;
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
/* File upload area */
|
| 351 |
+
.file-upload {
|
| 352 |
+
border: 2px dashed #6e8efb !important;
|
| 353 |
+
border-radius: 12px !important;
|
| 354 |
+
background: #f8f9ff !important;
|
| 355 |
+
}
|
| 356 |
+
|
| 357 |
+
/* Dark mode toggle */
|
| 358 |
+
.dark-mode {
|
| 359 |
+
background: #1e1e1e !important;
|
| 360 |
+
color: #f0f0f0 !important;
|
| 361 |
+
}
|
| 362 |
+
.dark-mode .gr-box {
|
| 363 |
+
background: #2d2d2d !important;
|
| 364 |
+
border-color: #444 !important;
|
| 365 |
+
}
|
| 366 |
+
.dark-mode .gr-button {
|
| 367 |
+
background: #444 !important;
|
| 368 |
+
border-color: #555 !important;
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
/* Animation for page transitions */
|
| 372 |
+
.fade-in {
|
| 373 |
+
animation: fadeIn 0.5s ease-in;
|
| 374 |
+
}
|
| 375 |
+
@keyframes fadeIn {
|
| 376 |
+
from { opacity: 0; }
|
| 377 |
+
to { opacity: 1; }
|
| 378 |
+
}
|
| 379 |
+
"""
|
| 380 |
+
|
| 381 |
+
# ==================== BUILD MODERN UI ====================
|
| 382 |
+
|
| 383 |
+
with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Soft(primary_hue="indigo")) as demo:
|
| 384 |
+
gr.Markdown(
|
| 385 |
+
"""<div style='text-align: center; padding: 20px 0;'>
|
| 386 |
+
<h1 style='margin: 0; font-size: 2.5em;'>π ThutoAI</h1>
|
| 387 |
+
<p style='margin: 8px 0 0 0; font-size: 1.1em; color: #6c757d;'>Your Modern AI Learning Assistant</p>
|
| 388 |
+
</div>"""
|
| 389 |
+
)
|
| 390 |
+
|
| 391 |
+
# Dark mode toggle (simulated with CSS class toggle)
|
| 392 |
+
with gr.Row():
|
| 393 |
+
dark_mode_btn = gr.Button("π Toggle Dark Mode", variant="secondary")
|
| 394 |
+
# Note: Full dark mode requires JS β we simulate with CSS classes
|
| 395 |
|
| 396 |
with gr.Tab("π’ Announcements"):
|
| 397 |
+
gr.Markdown("### π Filter by Course or Subject")
|
| 398 |
+
with gr.Row():
|
| 399 |
+
course_filter = gr.Dropdown(
|
| 400 |
+
choices=school_service.courses,
|
| 401 |
+
value="All",
|
| 402 |
+
label="Select Course",
|
| 403 |
+
interactive=True
|
| 404 |
+
)
|
| 405 |
+
refresh_btn = gr.Button("π Refresh", variant="secondary")
|
| 406 |
+
announcements_html = gr.HTML(elem_classes=["fade-in"])
|
| 407 |
course_filter.change(fn=render_announcements, inputs=course_filter, outputs=announcements_html)
|
| 408 |
+
refresh_btn.click(fn=render_announcements, inputs=course_filter, outputs=announcements_html)
|
| 409 |
demo.load(fn=render_announcements, inputs=course_filter, outputs=announcements_html)
|
| 410 |
|
| 411 |
with gr.Tab("π¬ Ask ThutoAI"):
|
| 412 |
+
gr.Markdown("### π‘ Ask me anything β I'm here to help!")
|
| 413 |
+
chatbot = gr.Chatbot(
|
| 414 |
+
height=480,
|
| 415 |
+
bubble_full_width=False,
|
| 416 |
+
elem_classes=["chatbot-container"]
|
| 417 |
+
)
|
| 418 |
+
with gr.Row():
|
| 419 |
+
msg = gr.Textbox(
|
| 420 |
+
label="Type your question",
|
| 421 |
+
placeholder="E.g., How do I prepare for the Math exam?",
|
| 422 |
+
scale=8
|
| 423 |
+
)
|
| 424 |
+
submit_btn = gr.Button("β€ Send", variant="primary", scale=1)
|
| 425 |
+
clear_btn = gr.Button("ποΈ Clear Chat", variant="secondary")
|
| 426 |
|
| 427 |
def respond(message, chat_history):
|
| 428 |
bot_reply = ai_chat(message, chat_history)
|
|
|
|
| 430 |
return "", chat_history
|
| 431 |
|
| 432 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 433 |
+
submit_btn.click(respond, [msg, chatbot], [msg, chatbot])
|
| 434 |
+
clear_btn.click(lambda: None, None, chatbot, queue=False)
|
| 435 |
+
|
| 436 |
+
with gr.Tab("π My Files"):
|
| 437 |
+
gr.Markdown("### π Upload & Manage Study Materials")
|
| 438 |
+
with gr.Row():
|
| 439 |
+
file_input = gr.File(label="Drag & drop or click to upload", elem_classes=["file-upload"])
|
| 440 |
+
upload_btn = gr.Button("π€ Upload", variant="primary")
|
| 441 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 442 |
upload_btn.click(fn=school_service.upload_file, inputs=file_input, outputs=status)
|
| 443 |
|
| 444 |
+
# File list preview (simulated)
|
| 445 |
+
gr.Markdown("### π Recently Uploaded")
|
| 446 |
+
file_list = gr.JSON(value=school_service.files[-5:] if school_service.files else [], label="Last 5 Files")
|
| 447 |
+
|
| 448 |
with gr.Tab("π Teacher Admin"):
|
| 449 |
+
gr.Markdown("### π©βπ« Post Announcements & View Stats")
|
| 450 |
|
| 451 |
with gr.Group() as login_group:
|
| 452 |
+
gr.Markdown("#### π Teacher Login")
|
| 453 |
+
with gr.Row():
|
| 454 |
+
username = gr.Textbox(label="Username", placeholder="e.g., teacher@thutoai.edu", scale=3)
|
| 455 |
+
password = gr.Textbox(label="Password", type="password", placeholder="β’β’β’β’β’β’β’β’", scale=3)
|
| 456 |
+
login_btn = gr.Button("π Login", variant="primary")
|
| 457 |
+
login_status = gr.Textbox(label="Status", interactive=False)
|
| 458 |
|
| 459 |
with gr.Group(visible=False) as dashboard:
|
| 460 |
+
gr.Markdown("#### π Dashboard Stats")
|
| 461 |
+
stats = school_service.get_stats()
|
| 462 |
+
gr.Markdown(f"""
|
| 463 |
+
- π’ Total Announcements: **{stats['total_announcements']}**
|
| 464 |
+
- π Total Files Uploaded: **{stats['total_files']}**
|
| 465 |
+
- π― Active Courses: **{stats['active_courses']}**
|
| 466 |
+
- π¨ High Priority Posts: **{stats['high_priority']}**
|
| 467 |
+
""")
|
| 468 |
+
|
| 469 |
gr.Markdown("#### βοΈ Create New Announcement")
|
| 470 |
with gr.Row():
|
| 471 |
+
title = gr.Textbox(label="Title", placeholder="e.g., Quiz Moved to Friday", scale=3)
|
| 472 |
+
course = gr.Dropdown(choices=school_service.courses[1:], label="Course", value="General", scale=2)
|
| 473 |
+
content = gr.Textbox(label="Content", placeholder="Details for students...", lines=3)
|
| 474 |
+
priority = gr.Radio(["low", "normal", "high"], label="Priority", value="normal", inline=True)
|
| 475 |
+
post_btn = gr.Button("π¬ Post Announcement", variant="primary")
|
| 476 |
post_result = gr.Textbox(label="Result", interactive=False)
|
| 477 |
|
| 478 |
+
logout_btn = gr.Button("β¬
οΈ Logout", variant="secondary")
|
| 479 |
|
| 480 |
# Connect buttons
|
| 481 |
login_btn.click(
|
|
|
|
| 494 |
outputs=post_result
|
| 495 |
)
|
| 496 |
|
| 497 |
+
# Launch app
|
| 498 |
if __name__ == "__main__":
|
| 499 |
demo.launch()
|