Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,37 +5,39 @@ import torch
|
|
5 |
# Step 1: Set device
|
6 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
7 |
|
8 |
-
# Step 2: Load
|
9 |
try:
|
10 |
model_name = "ibm-granite/granite-3.3-2b-instruct"
|
11 |
-
|
12 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
13 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
14 |
-
|
15 |
generator = pipeline(
|
16 |
"text-generation",
|
17 |
model=model,
|
18 |
tokenizer=tokenizer,
|
19 |
device=0 if device == "cuda" else -1,
|
20 |
-
max_new_tokens=
|
21 |
)
|
22 |
print("β
Model and tokenizer loaded successfully.")
|
23 |
except Exception as e:
|
24 |
print(f"β Error loading model/tokenizer: {e}")
|
25 |
generator = None
|
26 |
|
27 |
-
#
|
28 |
-
def
|
29 |
if generator is None:
|
30 |
return "β Error: Model not loaded."
|
|
|
|
|
31 |
|
|
|
|
|
32 |
prompt = f"""
|
33 |
You are an expert tutor.
|
34 |
|
35 |
Topic: {subject}
|
36 |
Student Score: {score}/10
|
37 |
|
38 |
-
Generate {num_questions} multiple-choice questions to help the student
|
39 |
|
40 |
Each question must:
|
41 |
- Be relevant and based only on the topic: '{subject}'
|
@@ -54,40 +56,122 @@ C. <option C>
|
|
54 |
D. <option D>
|
55 |
Correct Answer: <correct option letter>
|
56 |
"""
|
|
|
57 |
|
58 |
-
|
59 |
-
return output[0]["generated_text"]
|
60 |
-
|
61 |
def generate_feedback(score):
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
|
|
|
|
65 |
prompt = f"""
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
quiz = generate_quiz(subject, score, num_questions)
|
75 |
feedback = generate_feedback(score)
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
interface = gr.Interface(
|
79 |
fn=run_all,
|
80 |
inputs=[
|
81 |
-
gr.Textbox(label="
|
82 |
gr.Slider(0, 10, step=1, label="Score (out of 10)"),
|
83 |
-
gr.Slider(1, 10, step=1, label="Number of Questions")
|
|
|
|
|
84 |
],
|
85 |
outputs=[
|
86 |
gr.Textbox(label="Generated Quiz", show_copy_button=True),
|
87 |
-
gr.Textbox(label="Personalized Feedback", show_copy_button=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
],
|
89 |
title="EduTutor AI β Personalized Learning & Assessment System",
|
90 |
-
description="
|
91 |
)
|
92 |
|
93 |
interface.launch(debug=True)
|
|
|
5 |
# Step 1: Set device
|
6 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
7 |
|
8 |
+
# Step 2: Load model & tokenizer
|
9 |
try:
|
10 |
model_name = "ibm-granite/granite-3.3-2b-instruct"
|
|
|
11 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
12 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
13 |
generator = pipeline(
|
14 |
"text-generation",
|
15 |
model=model,
|
16 |
tokenizer=tokenizer,
|
17 |
device=0 if device == "cuda" else -1,
|
18 |
+
max_new_tokens=700
|
19 |
)
|
20 |
print("β
Model and tokenizer loaded successfully.")
|
21 |
except Exception as e:
|
22 |
print(f"β Error loading model/tokenizer: {e}")
|
23 |
generator = None
|
24 |
|
25 |
+
# Utility function to generate text
|
26 |
+
def generate_response(prompt):
|
27 |
if generator is None:
|
28 |
return "β Error: Model not loaded."
|
29 |
+
response = generator(prompt)
|
30 |
+
return response[0]["generated_text"]
|
31 |
|
32 |
+
# Functionality 1: Generate Quiz
|
33 |
+
def generate_quiz(subject: str, score: int, num_questions: int):
|
34 |
prompt = f"""
|
35 |
You are an expert tutor.
|
36 |
|
37 |
Topic: {subject}
|
38 |
Student Score: {score}/10
|
39 |
|
40 |
+
Generate {num_questions} multiple-choice questions to help the student's understanding of the topic '{subject}'.
|
41 |
|
42 |
Each question must:
|
43 |
- Be relevant and based only on the topic: '{subject}'
|
|
|
56 |
D. <option D>
|
57 |
Correct Answer: <correct option letter>
|
58 |
"""
|
59 |
+
return generate_response(prompt)
|
60 |
|
61 |
+
# Functionality 2: Feedback Generator
|
|
|
|
|
62 |
def generate_feedback(score):
|
63 |
+
prompt = f"A student scored {score}/10. Provide a friendly, personalized feedback message with suggestions to improve."
|
64 |
+
return generate_response(prompt)
|
65 |
+
|
66 |
+
# Functionality 3: Recommended Resources
|
67 |
+
def generate_resources(subject):
|
68 |
+
prompt = f"Provide 5 free, high-quality online learning resources (websites, YouTube, courses) to study the topic: {subject}."
|
69 |
+
return generate_response(prompt)
|
70 |
+
|
71 |
+
# Functionality 4: Summary Notes
|
72 |
+
def generate_summary_notes(subject):
|
73 |
+
prompt = f"Give a beginner-friendly summary of the topic '{subject}' with clear and simple explanation."
|
74 |
+
return generate_response(prompt)
|
75 |
+
|
76 |
+
# Functionality 5: Adaptive Question Suggestion
|
77 |
+
def generate_adaptive_question(subject, score):
|
78 |
+
difficulty = "easy" if score <= 4 else "medium" if score <= 7 else "hard"
|
79 |
+
prompt = f"Generate one {difficulty}-level multiple choice question on the topic: {subject}."
|
80 |
+
return generate_response(prompt)
|
81 |
+
|
82 |
+
# Functionality 6: Concept-wise MCQ Generation
|
83 |
+
def generate_concept_questions(subject, concept):
|
84 |
+
prompt = f"Generate 3 multiple-choice questions focused on the sub-topic '{concept}' under '{subject}'."
|
85 |
+
return generate_response(prompt)
|
86 |
+
|
87 |
+
# Functionality 7: Fill in the Blanks
|
88 |
+
def generate_fill_in_the_blanks(subject):
|
89 |
+
prompt = f"""
|
90 |
+
Generate 5 fill-in-the-blank questions with answers on the topic: '{subject}'.
|
91 |
+
|
92 |
+
Format:
|
93 |
+
Q1: <question with blank>
|
94 |
+
Answer: <correct word or phrase>
|
95 |
+
|
96 |
+
Ensure each blank tests an important concept from the topic.
|
97 |
+
"""
|
98 |
+
return generate_response(prompt)
|
99 |
+
|
100 |
+
# Functionality 8: Important Points
|
101 |
+
def generate_important_points(subject):
|
102 |
+
prompt = f"""
|
103 |
+
List the 7 most important points a beginner should remember when studying the topic: '{subject}'. Use short, clear bullet points.
|
104 |
+
"""
|
105 |
+
return generate_response(prompt)
|
106 |
+
|
107 |
+
# Functionality 9: Flashcard Format Output
|
108 |
+
def generate_flashcards(subject, num_flashcards):
|
109 |
+
prompt = f"Generate {num_flashcards} flashcards for the topic '{subject}'. Format each as: Q: <question> A: <answer>"
|
110 |
+
return generate_response(prompt)
|
111 |
|
112 |
+
# Functionality 10: Misconception Correction
|
113 |
+
def generate_misconceptions(subject):
|
114 |
prompt = f"""
|
115 |
+
List common misconceptions students have when learning the topic: '{subject}'. For each one, provide a correct explanation.
|
116 |
+
|
117 |
+
Format:
|
118 |
+
Misconception: <wrong idea>
|
119 |
+
Correction: <correct understanding>
|
120 |
+
"""
|
121 |
+
return generate_response(prompt)
|
122 |
+
|
123 |
+
# Functionality 11: Confidence Score Explanation
|
124 |
+
def confidence_analysis(score):
|
125 |
+
prompt = f"A student scored {score}/10. Analyze their confidence level and suggest how to build stronger understanding in weak areas."
|
126 |
+
return generate_response(prompt)
|
127 |
+
|
128 |
+
# Functionality 12: Weekly Learning Plan Generator
|
129 |
+
def generate_study_plan(subject, score):
|
130 |
+
prompt = f"A student scored {score}/10 on the topic '{subject}'. Create a personalized 5-day learning plan to improve their understanding."
|
131 |
+
return generate_response(prompt)
|
132 |
+
|
133 |
+
# Gradio App
|
134 |
+
def run_all(subject, score, num_questions, concept, flashcard_count):
|
135 |
quiz = generate_quiz(subject, score, num_questions)
|
136 |
feedback = generate_feedback(score)
|
137 |
+
resources = generate_resources(subject)
|
138 |
+
notes = generate_summary_notes(subject)
|
139 |
+
adaptive = generate_adaptive_question(subject, score)
|
140 |
+
concept_questions = generate_concept_questions(subject, concept)
|
141 |
+
fill_blanks = generate_fill_in_the_blanks(subject)
|
142 |
+
important_points = generate_important_points(subject)
|
143 |
+
flashcards = generate_flashcards(subject, flashcard_count)
|
144 |
+
misconceptions = generate_misconceptions(subject)
|
145 |
+
confidence = confidence_analysis(score)
|
146 |
+
study_plan = generate_study_plan(subject, score)
|
147 |
+
|
148 |
+
return quiz, feedback, resources, notes, adaptive, concept_questions, fill_blanks, important_points, misconceptions, confidence, flashcards, study_plan
|
149 |
|
150 |
interface = gr.Interface(
|
151 |
fn=run_all,
|
152 |
inputs=[
|
153 |
+
gr.Textbox(label="Topic (e.g., Algebra)"),
|
154 |
gr.Slider(0, 10, step=1, label="Score (out of 10)"),
|
155 |
+
gr.Slider(1, 10, step=1, label="Number of Questions"),
|
156 |
+
gr.Textbox(label="Concept Name (e.g., Linear Equations)"),
|
157 |
+
gr.Slider(1, 10, step=1, label="Number of Flashcards")
|
158 |
],
|
159 |
outputs=[
|
160 |
gr.Textbox(label="Generated Quiz", show_copy_button=True),
|
161 |
+
gr.Textbox(label="Personalized Feedback", show_copy_button=True),
|
162 |
+
gr.Textbox(label="Learning Resources", show_copy_button=True),
|
163 |
+
gr.Textbox(label="Summary Notes", show_copy_button=True),
|
164 |
+
gr.Textbox(label="Adaptive Question", show_copy_button=True),
|
165 |
+
gr.Textbox(label="Concept-Based Questions", show_copy_button=True),
|
166 |
+
gr.Textbox(label="Fill in the Blanks", show_copy_button=True),
|
167 |
+
gr.Textbox(label="Important Points", show_copy_button=True),
|
168 |
+
gr.Textbox(label="Flashcards", show_copy_button=True),
|
169 |
+
gr.Textbox(label="Misconception Correction", show_copy_button=True),
|
170 |
+
gr.Textbox(label="Confidence Analysis", show_copy_button=True),
|
171 |
+
gr.Textbox(label="Weekly Study Plan", show_copy_button=True)
|
172 |
],
|
173 |
title="EduTutor AI β Personalized Learning & Assessment System",
|
174 |
+
description="π Generate quizzes, feedback, flashcards, study plans, and more using IBM Granite LLM"
|
175 |
)
|
176 |
|
177 |
interface.launch(debug=True)
|