Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,304 +1,372 @@
|
|
1 |
-
# EDUTUTOR AI
|
2 |
-
#
|
3 |
-
|
4 |
-
import gradio as gr
|
5 |
-
import torch
|
6 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
7 |
-
import warnings
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
)
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
#
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
return
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
demo.launch()
|
|
|
1 |
+
# EDUTUTOR AI with Google Authentication
|
2 |
+
# Note: This collects user data - ensure you have proper privacy policy and user consent
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
import torch
|
6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
7 |
+
import warnings
|
8 |
+
import datetime
|
9 |
+
import json
|
10 |
+
warnings.filterwarnings("ignore")
|
11 |
+
|
12 |
+
# User session storage (in-memory for demo - use database in production)
|
13 |
+
user_sessions = {}
|
14 |
+
usage_analytics = []
|
15 |
+
|
16 |
+
class EduTutorAI:
|
17 |
+
def __init__(self):
|
18 |
+
self.model_name = "ibm-granite/granite-3.3-2b-instruct"
|
19 |
+
self.tokenizer = None
|
20 |
+
self.model = None
|
21 |
+
self.pipe = None
|
22 |
+
self.conversation_history = []
|
23 |
+
|
24 |
+
def load_model(self):
|
25 |
+
"""Load the Granite model and tokenizer"""
|
26 |
+
try:
|
27 |
+
print("Loading EDUTUTOR AI model...")
|
28 |
+
|
29 |
+
self.tokenizer = AutoTokenizer.from_pretrained(
|
30 |
+
self.model_name,
|
31 |
+
trust_remote_code=True
|
32 |
+
)
|
33 |
+
|
34 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
35 |
+
self.model_name,
|
36 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
37 |
+
device_map="auto" if torch.cuda.is_available() else None,
|
38 |
+
trust_remote_code=True,
|
39 |
+
low_cpu_mem_usage=True
|
40 |
+
)
|
41 |
+
|
42 |
+
self.pipe = pipeline(
|
43 |
+
"text-generation",
|
44 |
+
model=self.model,
|
45 |
+
tokenizer=self.tokenizer,
|
46 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
47 |
+
device_map="auto" if torch.cuda.is_available() else None
|
48 |
+
)
|
49 |
+
|
50 |
+
print("β
Model loaded successfully!")
|
51 |
+
return True
|
52 |
+
|
53 |
+
except Exception as e:
|
54 |
+
print(f"β Error loading model: {str(e)}")
|
55 |
+
return False
|
56 |
+
|
57 |
+
def create_educational_prompt(self, user_question, subject="General", difficulty="Intermediate"):
|
58 |
+
"""Create an educational prompt template"""
|
59 |
+
system_prompt = f"""You are EDUTUTOR AI, an expert educational tutor specializing in {subject}.
|
60 |
+
Your role is to:
|
61 |
+
1. Provide clear, accurate explanations at {difficulty} level
|
62 |
+
2. Break down complex concepts into digestible parts
|
63 |
+
3. Use examples and analogies when helpful
|
64 |
+
4. Encourage learning through questions
|
65 |
+
5. Be patient and supportive
|
66 |
+
|
67 |
+
Student Question: {user_question}
|
68 |
+
|
69 |
+
Please provide a comprehensive yet accessible explanation:"""
|
70 |
+
|
71 |
+
return system_prompt
|
72 |
+
|
73 |
+
def generate_response(self, question, subject, difficulty, max_length, user_info=None):
|
74 |
+
"""Generate educational response with user tracking"""
|
75 |
+
if not self.pipe:
|
76 |
+
return "β Model not loaded. Please wait for initialization."
|
77 |
+
|
78 |
+
try:
|
79 |
+
# Log user interaction
|
80 |
+
if user_info:
|
81 |
+
self.log_user_interaction(user_info, question, subject, difficulty)
|
82 |
+
|
83 |
+
# Create educational prompt
|
84 |
+
prompt = self.create_educational_prompt(question, subject, difficulty)
|
85 |
+
|
86 |
+
# Generate response
|
87 |
+
response = self.pipe(
|
88 |
+
prompt,
|
89 |
+
max_length=max_length,
|
90 |
+
num_return_sequences=1,
|
91 |
+
temperature=0.7,
|
92 |
+
do_sample=True,
|
93 |
+
pad_token_id=self.tokenizer.eos_token_id,
|
94 |
+
truncation=True
|
95 |
+
)
|
96 |
+
|
97 |
+
# Extract the generated text
|
98 |
+
full_response = response[0]['generated_text']
|
99 |
+
ai_response = full_response.replace(prompt, "").strip()
|
100 |
+
|
101 |
+
# Store in conversation history
|
102 |
+
self.conversation_history.append({
|
103 |
+
"user": user_info.get("name", "Anonymous") if user_info else "Anonymous",
|
104 |
+
"question": question,
|
105 |
+
"subject": subject,
|
106 |
+
"difficulty": difficulty,
|
107 |
+
"response": ai_response,
|
108 |
+
"timestamp": datetime.datetime.now().isoformat()
|
109 |
+
})
|
110 |
+
|
111 |
+
return ai_response
|
112 |
+
|
113 |
+
except Exception as e:
|
114 |
+
return f"β Error generating response: {str(e)}"
|
115 |
+
|
116 |
+
def log_user_interaction(self, user_info, question, subject, difficulty):
|
117 |
+
"""Log user interaction for analytics"""
|
118 |
+
interaction = {
|
119 |
+
"timestamp": datetime.datetime.now().isoformat(),
|
120 |
+
"user_name": user_info.get("name", "Unknown"),
|
121 |
+
"user_email": user_info.get("email", "Unknown"),
|
122 |
+
"question": question,
|
123 |
+
"subject": subject,
|
124 |
+
"difficulty": difficulty
|
125 |
+
}
|
126 |
+
usage_analytics.append(interaction)
|
127 |
+
print(f"π Logged interaction: {user_info.get('name', 'Anonymous')} asked about {subject}")
|
128 |
+
|
129 |
+
# Initialize the EduTutor AI
|
130 |
+
edututor = EduTutorAI()
|
131 |
+
|
132 |
+
# Authentication functions
|
133 |
+
def authenticate_user(username, password):
|
134 |
+
"""Simple authentication (replace with Google OAuth in production)"""
|
135 |
+
# This is a demo - in production, use proper Google OAuth
|
136 |
+
if username and password:
|
137 |
+
user_info = {
|
138 |
+
"name": username,
|
139 |
+
"email": f"{username}@example.com",
|
140 |
+
"login_time": datetime.datetime.now().isoformat()
|
141 |
+
}
|
142 |
+
user_sessions[username] = user_info
|
143 |
+
return True, f"β
Welcome {username}! You are now logged in.", user_info
|
144 |
+
return False, "β Please enter valid credentials.", None
|
145 |
+
|
146 |
+
def initialize_model():
|
147 |
+
"""Initialize the model and return status"""
|
148 |
+
success = edututor.load_model()
|
149 |
+
if success:
|
150 |
+
return "β
EDUTUTOR AI is ready! You can now start asking questions."
|
151 |
+
else:
|
152 |
+
return "β Failed to load model. Please try again."
|
153 |
+
|
154 |
+
def chat_with_edututor(question, subject, difficulty, max_length, user_info):
|
155 |
+
"""Main chat interface function with user tracking"""
|
156 |
+
if not question.strip():
|
157 |
+
return "Please enter a question to get started!"
|
158 |
+
|
159 |
+
# Parse user_info if it's a string
|
160 |
+
if isinstance(user_info, str) and user_info.startswith("{"):
|
161 |
+
try:
|
162 |
+
user_info = json.loads(user_info)
|
163 |
+
except:
|
164 |
+
user_info = None
|
165 |
+
|
166 |
+
response = edututor.generate_response(question, subject, difficulty, max_length, user_info)
|
167 |
+
return response
|
168 |
+
|
169 |
+
def get_user_analytics():
|
170 |
+
"""Get analytics dashboard"""
|
171 |
+
if not usage_analytics:
|
172 |
+
return "No user interactions logged yet."
|
173 |
+
|
174 |
+
total_users = len(set([interaction["user_email"] for interaction in usage_analytics]))
|
175 |
+
total_questions = len(usage_analytics)
|
176 |
+
|
177 |
+
subjects = {}
|
178 |
+
for interaction in usage_analytics:
|
179 |
+
subject = interaction["subject"]
|
180 |
+
subjects[subject] = subjects.get(subject, 0) + 1
|
181 |
+
|
182 |
+
analytics = f"""
|
183 |
+
π **EDUTUTOR AI Analytics Dashboard**
|
184 |
+
|
185 |
+
π₯ **Total Unique Users**: {total_users}
|
186 |
+
β **Total Questions Asked**: {total_questions}
|
187 |
+
|
188 |
+
π **Popular Subjects**:
|
189 |
+
"""
|
190 |
+
for subject, count in sorted(subjects.items(), key=lambda x: x[1], reverse=True):
|
191 |
+
analytics += f"β’ {subject}: {count} questions\n"
|
192 |
+
|
193 |
+
analytics += f"\nπ **Recent Activity** (Last 5):\n"
|
194 |
+
for interaction in usage_analytics[-5:]:
|
195 |
+
analytics += f"β’ {interaction['user_name']} asked about {interaction['subject']} ({interaction['timestamp'][:19]})\n"
|
196 |
+
|
197 |
+
return analytics
|
198 |
+
|
199 |
+
# Create Gradio interface with authentication
|
200 |
+
def create_interface():
|
201 |
+
"""Create the EDUTUTOR AI Gradio interface with authentication"""
|
202 |
+
|
203 |
+
with gr.Blocks(
|
204 |
+
title="π EDUTUTOR AI - Your Personal Learning Assistant",
|
205 |
+
theme=gr.themes.Soft(),
|
206 |
+
css="""
|
207 |
+
.gradio-container {
|
208 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
209 |
+
}
|
210 |
+
.main-header {
|
211 |
+
text-align: center;
|
212 |
+
background: linear-gradient(45deg, #667eea 0%, #764ba2 100%);
|
213 |
+
color: white;
|
214 |
+
padding: 20px;
|
215 |
+
border-radius: 10px;
|
216 |
+
margin-bottom: 20px;
|
217 |
+
}
|
218 |
+
.privacy-notice {
|
219 |
+
background: #f0f0f0;
|
220 |
+
padding: 15px;
|
221 |
+
border-radius: 5px;
|
222 |
+
margin: 10px 0;
|
223 |
+
border-left: 4px solid #ff6b6b;
|
224 |
+
}
|
225 |
+
"""
|
226 |
+
) as interface:
|
227 |
+
|
228 |
+
# Header
|
229 |
+
gr.HTML("""
|
230 |
+
<div class="main-header">
|
231 |
+
<h1>π EDUTUTOR AI</h1>
|
232 |
+
<p>Your Intelligent Educational Tutor powered by IBM Granite 3.3-2B</p>
|
233 |
+
<p><em>Ask questions, learn concepts, and expand your knowledge!</em></p>
|
234 |
+
</div>
|
235 |
+
""")
|
236 |
+
|
237 |
+
# Privacy Notice
|
238 |
+
gr.HTML("""
|
239 |
+
<div class="privacy-notice">
|
240 |
+
<h3>π Privacy Notice</h3>
|
241 |
+
<p><strong>Data Collection:</strong> This app collects user names, questions, and usage analytics to improve the learning experience.</p>
|
242 |
+
<p><strong>By using this app, you consent to this data collection.</strong></p>
|
243 |
+
<p>π§ Contact: For privacy concerns, contact the administrator.</p>
|
244 |
+
</div>
|
245 |
+
""")
|
246 |
+
|
247 |
+
# Authentication Section
|
248 |
+
with gr.Row():
|
249 |
+
with gr.Column(scale=1):
|
250 |
+
gr.Markdown("### π Login to EDUTUTOR AI")
|
251 |
+
username_input = gr.Textbox(label="Username", placeholder="Enter your username")
|
252 |
+
password_input = gr.Textbox(label="Password", type="password", placeholder="Enter your password")
|
253 |
+
login_button = gr.Button("π Login", variant="primary")
|
254 |
+
login_status = gr.Textbox(label="Login Status", value="Please login to continue", interactive=False)
|
255 |
+
user_info_state = gr.State(value=None)
|
256 |
+
|
257 |
+
# Model initialization section
|
258 |
+
with gr.Row():
|
259 |
+
with gr.Column():
|
260 |
+
init_button = gr.Button("π Initialize EDUTUTOR AI", variant="primary", size="lg")
|
261 |
+
init_status = gr.Textbox(
|
262 |
+
label="Initialization Status",
|
263 |
+
value="Click 'Initialize EDUTUTOR AI' to start",
|
264 |
+
interactive=False
|
265 |
+
)
|
266 |
+
|
267 |
+
# Main interface
|
268 |
+
with gr.Row():
|
269 |
+
with gr.Column(scale=2):
|
270 |
+
# Input section
|
271 |
+
with gr.Group():
|
272 |
+
gr.Markdown("### π Ask Your Question")
|
273 |
+
question_input = gr.Textbox(
|
274 |
+
label="Your Question",
|
275 |
+
placeholder="e.g., Explain quantum physics, How does photosynthesis work?, What is machine learning?",
|
276 |
+
lines=3
|
277 |
+
)
|
278 |
+
|
279 |
+
with gr.Row():
|
280 |
+
subject_dropdown = gr.Dropdown(
|
281 |
+
choices=[
|
282 |
+
"General", "Mathematics", "Physics", "Chemistry",
|
283 |
+
"Biology", "Computer Science", "History", "Literature",
|
284 |
+
"Geography", "Economics", "Philosophy"
|
285 |
+
],
|
286 |
+
value="General",
|
287 |
+
label="Subject Area"
|
288 |
+
)
|
289 |
+
|
290 |
+
difficulty_dropdown = gr.Dropdown(
|
291 |
+
choices=["Beginner", "Intermediate", "Advanced"],
|
292 |
+
value="Intermediate",
|
293 |
+
label="Difficulty Level"
|
294 |
+
)
|
295 |
+
|
296 |
+
max_length_slider = gr.Slider(
|
297 |
+
minimum=100,
|
298 |
+
maximum=1000,
|
299 |
+
value=512,
|
300 |
+
step=50,
|
301 |
+
label="Response Length (tokens)"
|
302 |
+
)
|
303 |
+
|
304 |
+
ask_button = gr.Button("π€ Ask EDUTUTOR AI", variant="primary")
|
305 |
+
|
306 |
+
with gr.Column(scale=1):
|
307 |
+
# Quick actions
|
308 |
+
with gr.Group():
|
309 |
+
gr.Markdown("### β‘ Quick Actions")
|
310 |
+
analytics_button = gr.Button("π View Analytics")
|
311 |
+
|
312 |
+
gr.Markdown("### π‘ Tips")
|
313 |
+
gr.Markdown("""
|
314 |
+
- Login to track your learning progress
|
315 |
+
- Be specific with your questions
|
316 |
+
- Select appropriate subject and difficulty
|
317 |
+
- Use follow-up questions for deeper understanding
|
318 |
+
""")
|
319 |
+
|
320 |
+
# Response section
|
321 |
+
with gr.Row():
|
322 |
+
response_output = gr.Textbox(
|
323 |
+
label="π EDUTUTOR AI Response",
|
324 |
+
lines=15,
|
325 |
+
max_lines=20,
|
326 |
+
interactive=False
|
327 |
+
)
|
328 |
+
|
329 |
+
# Analytics section
|
330 |
+
with gr.Row():
|
331 |
+
analytics_output = gr.Textbox(
|
332 |
+
label="π Usage Analytics",
|
333 |
+
lines=10,
|
334 |
+
interactive=False,
|
335 |
+
visible=False
|
336 |
+
)
|
337 |
+
|
338 |
+
# Event handlers
|
339 |
+
login_button.click(
|
340 |
+
fn=authenticate_user,
|
341 |
+
inputs=[username_input, password_input],
|
342 |
+
outputs=[gr.State(), login_status, user_info_state]
|
343 |
+
)
|
344 |
+
|
345 |
+
init_button.click(
|
346 |
+
fn=initialize_model,
|
347 |
+
outputs=init_status
|
348 |
+
)
|
349 |
+
|
350 |
+
ask_button.click(
|
351 |
+
fn=chat_with_edututor,
|
352 |
+
inputs=[question_input, subject_dropdown, difficulty_dropdown, max_length_slider, user_info_state],
|
353 |
+
outputs=response_output
|
354 |
+
)
|
355 |
+
|
356 |
+
analytics_button.click(
|
357 |
+
fn=get_user_analytics,
|
358 |
+
outputs=analytics_output
|
359 |
+
).then(
|
360 |
+
fn=lambda: gr.update(visible=True),
|
361 |
+
outputs=analytics_output
|
362 |
+
)
|
363 |
+
|
364 |
+
return interface
|
365 |
+
|
366 |
+
# Launch the application
|
367 |
+
if __name__ == "__main__":
|
368 |
+
print("π Starting EDUTUTOR AI with User Authentication...")
|
369 |
+
print("=" * 50)
|
370 |
+
|
371 |
+
demo = create_interface()
|
372 |
demo.launch()
|