Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
from gtts import gTTS
|
| 4 |
import os
|
| 5 |
import random
|
|
|
|
| 6 |
|
| 7 |
# Initialize NLP pipelines
|
| 8 |
qa = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
|
@@ -12,11 +13,12 @@ except Exception as e:
|
|
| 12 |
print(f"Error loading summarizer: {e}")
|
| 13 |
summarizer = None
|
| 14 |
|
|
|
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
except Exception as e:
|
| 18 |
-
print(f"Error loading
|
| 19 |
-
|
| 20 |
|
| 21 |
# Initialize user stats and log files
|
| 22 |
if not os.path.exists("decision_log.txt"):
|
|
@@ -31,6 +33,17 @@ if not os.path.exists("user_score.txt"):
|
|
| 31 |
if not os.path.exists("questions_answered.txt"):
|
| 32 |
with open("questions_answered.txt", "w") as f:
|
| 33 |
f.write("0")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def update_score(points):
|
| 36 |
with open("user_score.txt", "r") as f:
|
|
@@ -63,6 +76,25 @@ def get_motivational_message():
|
|
| 63 |
]
|
| 64 |
return random.choice(messages)
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
def study_aid(question, context, font_size=16, audio_output=False, simplify_text=False, theme="dark"):
|
| 67 |
with open("decision_log.txt", "a") as f:
|
| 68 |
f.write(f"Question: {question}, Simplified: {simplify_text}, Audio: {audio_output}, Font: {font_size}, Theme: {theme}\n")
|
|
@@ -101,12 +133,15 @@ def study_aid(question, context, font_size=16, audio_output=False, simplify_text
|
|
| 101 |
progress = get_progress()
|
| 102 |
motivation = get_motivational_message()
|
| 103 |
|
|
|
|
|
|
|
|
|
|
| 104 |
if audio_output:
|
| 105 |
tts = gTTS(text=answer, lang='en')
|
| 106 |
tts.save("answer_audio.mp3")
|
| 107 |
-
return output, "answer_audio.mp3", f"Your Score: {score} | {progress} | {motivation}"
|
| 108 |
|
| 109 |
-
return output, None, f"Your Score: {score} | {progress} | {motivation}"
|
| 110 |
|
| 111 |
def submit_feedback(feedback):
|
| 112 |
with open("feedback.txt", "a") as f:
|
|
@@ -114,19 +149,36 @@ def submit_feedback(feedback):
|
|
| 114 |
score = update_score(5)
|
| 115 |
progress = get_progress()
|
| 116 |
motivation = get_motivational_message()
|
| 117 |
-
|
|
|
|
| 118 |
|
| 119 |
def generate_quiz(context, theme="dark"):
|
| 120 |
-
if
|
| 121 |
-
return "Question generation not available.", None, None
|
| 122 |
|
| 123 |
try:
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
|
|
|
|
| 130 |
answer = qa(question=quiz_question, context=context)["answer"]
|
| 131 |
|
| 132 |
bg_color = "black" if theme == "dark" else "white"
|
|
@@ -141,9 +193,10 @@ def generate_quiz(context, theme="dark"):
|
|
| 141 |
questions_answered = update_questions_answered()
|
| 142 |
progress = get_progress()
|
| 143 |
motivation = get_motivational_message()
|
| 144 |
-
|
|
|
|
| 145 |
except Exception as e:
|
| 146 |
-
return f"Error generating quiz: {str(e)}", None, None
|
| 147 |
|
| 148 |
def read_logs():
|
| 149 |
try:
|
|
@@ -160,6 +213,11 @@ with gr.Blocks(title="StudyBuddy: Accessible Study Aid for Neurodiverse Students
|
|
| 160 |
"""
|
| 161 |
)
|
| 162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
with gr.Tab("Ask a Question"):
|
| 164 |
question_input = gr.Textbox(label="Question", placeholder="e.g., What is machine learning?")
|
| 165 |
context_input = gr.Textbox(label="Context (Lecture Notes)", placeholder="Paste your notes here...")
|
|
@@ -167,15 +225,24 @@ with gr.Blocks(title="StudyBuddy: Accessible Study Aid for Neurodiverse Students
|
|
| 167 |
theme_input = gr.Dropdown(choices=["dark", "light"], value="dark", label="Theme")
|
| 168 |
audio_output_input = gr.Checkbox(label="Generate Audio Output")
|
| 169 |
simplify_text_input = gr.Checkbox(label="Simplify Text")
|
| 170 |
-
|
|
|
|
|
|
|
| 171 |
study_output_text = gr.HTML(label="Answer")
|
| 172 |
study_output_audio = gr.Audio(label="Audio Narration")
|
|
|
|
| 173 |
score_output = gr.Text(label="Score & Progress")
|
|
|
|
| 174 |
|
| 175 |
study_submit_btn.click(
|
| 176 |
fn=study_aid,
|
| 177 |
inputs=[question_input, context_input, font_size_input, audio_output_input, simplify_text_input, theme_input],
|
| 178 |
-
outputs=[study_output_text, study_output_audio, score_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
)
|
| 180 |
|
| 181 |
with gr.Tab("Quiz Me"):
|
|
@@ -184,23 +251,25 @@ with gr.Blocks(title="StudyBuddy: Accessible Study Aid for Neurodiverse Students
|
|
| 184 |
quiz_submit_btn = gr.Button("Generate Quiz Question")
|
| 185 |
quiz_output_text = gr.HTML(label="Quiz Question and Answer")
|
| 186 |
quiz_output_audio = gr.Audio(label="Audio Narration")
|
|
|
|
| 187 |
quiz_score_output = gr.Text(label="Score & Progress")
|
| 188 |
|
| 189 |
quiz_submit_btn.click(
|
| 190 |
fn=generate_quiz,
|
| 191 |
inputs=[quiz_context_input, quiz_theme_input],
|
| 192 |
-
outputs=[quiz_output_text, quiz_output_audio, quiz_score_output]
|
| 193 |
)
|
| 194 |
|
| 195 |
with gr.Tab("Submit Feedback"):
|
| 196 |
feedback_input = gr.Textbox(label="Feedback", placeholder="Report issues or suggestions...")
|
| 197 |
feedback_submit_btn = gr.Button("Submit Feedback")
|
| 198 |
feedback_output = gr.Text(label="Feedback Status")
|
|
|
|
| 199 |
|
| 200 |
feedback_submit_btn.click(
|
| 201 |
fn=submit_feedback,
|
| 202 |
inputs=feedback_input,
|
| 203 |
-
outputs=feedback_output
|
| 204 |
)
|
| 205 |
|
| 206 |
with gr.Tab("View Logs"):
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline, AutoTokenizer
|
| 3 |
from gtts import gTTS
|
| 4 |
import os
|
| 5 |
import random
|
| 6 |
+
import re
|
| 7 |
|
| 8 |
# Initialize NLP pipelines
|
| 9 |
qa = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
|
|
|
| 13 |
print(f"Error loading summarizer: {e}")
|
| 14 |
summarizer = None
|
| 15 |
|
| 16 |
+
# Load tokenizer for rule-based question generation
|
| 17 |
try:
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained("t5-small")
|
| 19 |
except Exception as e:
|
| 20 |
+
print(f"Error loading tokenizer: {e}")
|
| 21 |
+
tokenizer = None
|
| 22 |
|
| 23 |
# Initialize user stats and log files
|
| 24 |
if not os.path.exists("decision_log.txt"):
|
|
|
|
| 33 |
if not os.path.exists("questions_answered.txt"):
|
| 34 |
with open("questions_answered.txt", "w") as f:
|
| 35 |
f.write("0")
|
| 36 |
+
if not os.path.exists("avatar.txt"):
|
| 37 |
+
with open("avatar.txt", "w") as f:
|
| 38 |
+
f.write("default")
|
| 39 |
+
|
| 40 |
+
# Avatar options
|
| 41 |
+
avatars = {
|
| 42 |
+
"default": "https://cdn.pixabay.com/photo/2016/08/08/09/17/avatar-1577909_1280.png",
|
| 43 |
+
"robot": "https://cdn.pixabay.com/photo/2017/01/31/21/23/robot-2027195_1280.png",
|
| 44 |
+
"cat": "https://cdn.pixabay.com/photo/2017/01/31/21/23/cat-2027196_1280.png",
|
| 45 |
+
"dog": "https://cdn.pixabay.com/photo/2017/01/31/21/23/dog-2027197_1280.png"
|
| 46 |
+
}
|
| 47 |
|
| 48 |
def update_score(points):
|
| 49 |
with open("user_score.txt", "r") as f:
|
|
|
|
| 76 |
]
|
| 77 |
return random.choice(messages)
|
| 78 |
|
| 79 |
+
def get_hint(context):
|
| 80 |
+
try:
|
| 81 |
+
if len(context.split()) < 10:
|
| 82 |
+
return "Hint: The answer is in the context, but it's too short to summarize."
|
| 83 |
+
summary = summarizer(context, max_length=30, min_length=10)[0]["summary_text"]
|
| 84 |
+
return f"Hint: {summary}"
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return f"Hint unavailable: {str(e)}"
|
| 87 |
+
|
| 88 |
+
def set_avatar(avatar_choice):
|
| 89 |
+
with open("avatar.txt", "w") as f:
|
| 90 |
+
f.write(avatar_choice)
|
| 91 |
+
return avatars[avatar_choice]
|
| 92 |
+
|
| 93 |
+
def get_avatar():
|
| 94 |
+
with open("avatar.txt", "r") as f:
|
| 95 |
+
avatar_choice = f.read().strip()
|
| 96 |
+
return avatars.get(avatar_choice, avatars["default"])
|
| 97 |
+
|
| 98 |
def study_aid(question, context, font_size=16, audio_output=False, simplify_text=False, theme="dark"):
|
| 99 |
with open("decision_log.txt", "a") as f:
|
| 100 |
f.write(f"Question: {question}, Simplified: {simplify_text}, Audio: {audio_output}, Font: {font_size}, Theme: {theme}\n")
|
|
|
|
| 133 |
progress = get_progress()
|
| 134 |
motivation = get_motivational_message()
|
| 135 |
|
| 136 |
+
# Sound effect for points earned
|
| 137 |
+
sound_effect = "https://www.soundjay.com/buttons/sounds/beep-01a.mp3"
|
| 138 |
+
|
| 139 |
if audio_output:
|
| 140 |
tts = gTTS(text=answer, lang='en')
|
| 141 |
tts.save("answer_audio.mp3")
|
| 142 |
+
return output, "answer_audio.mp3", sound_effect, f"Your Score: {score} | {progress} | {motivation}"
|
| 143 |
|
| 144 |
+
return output, None, sound_effect, f"Your Score: {score} | {progress} | {motivation}"
|
| 145 |
|
| 146 |
def submit_feedback(feedback):
|
| 147 |
with open("feedback.txt", "a") as f:
|
|
|
|
| 149 |
score = update_score(5)
|
| 150 |
progress = get_progress()
|
| 151 |
motivation = get_motivational_message()
|
| 152 |
+
sound_effect = "https://www.soundjay.com/buttons/sounds/beep-01a.mp3"
|
| 153 |
+
return f"Feedback submitted! Your Score: {score} | {progress} | {motivation}", sound_effect
|
| 154 |
|
| 155 |
def generate_quiz(context, theme="dark"):
|
| 156 |
+
if tokenizer is None:
|
| 157 |
+
return "Question generation not available: Tokenizer failed to load.", None, None, None
|
| 158 |
|
| 159 |
try:
|
| 160 |
+
# Simple rule-based question generation
|
| 161 |
+
# Tokenize the context and extract key phrases (e.g., noun phrases)
|
| 162 |
+
tokens = context.split()
|
| 163 |
+
key_phrases = []
|
| 164 |
+
current_phrase = []
|
| 165 |
+
for token in tokens:
|
| 166 |
+
if token in [",", ".", ":", ";", "!", "?", "and", "or", "but"]:
|
| 167 |
+
if current_phrase:
|
| 168 |
+
key_phrases.append(" ".join(current_phrase))
|
| 169 |
+
current_phrase = []
|
| 170 |
+
else:
|
| 171 |
+
current_phrase.append(token)
|
| 172 |
+
if current_phrase:
|
| 173 |
+
key_phrases.append(" ".join(current_phrase))
|
| 174 |
+
|
| 175 |
+
# Filter phrases that are likely to be meaningful (e.g., longer than 2 words)
|
| 176 |
+
key_phrases = [phrase for phrase in key_phrases if len(phrase.split()) > 2]
|
| 177 |
+
if not key_phrases:
|
| 178 |
+
return "No suitable phrases found for question generation.", None, None, None
|
| 179 |
|
| 180 |
+
# Generate a question using the first key phrase
|
| 181 |
+
quiz_question = f"What is {key_phrases[0]}?"
|
| 182 |
answer = qa(question=quiz_question, context=context)["answer"]
|
| 183 |
|
| 184 |
bg_color = "black" if theme == "dark" else "white"
|
|
|
|
| 193 |
questions_answered = update_questions_answered()
|
| 194 |
progress = get_progress()
|
| 195 |
motivation = get_motivational_message()
|
| 196 |
+
sound_effect = "https://www.soundjay.com/buttons/sounds/beep-01a.mp3"
|
| 197 |
+
return output, "quiz_audio.mp3", sound_effect, f"Your Score: {score} | {progress} | {motivation}"
|
| 198 |
except Exception as e:
|
| 199 |
+
return f"Error generating quiz: {str(e)}", None, None, None
|
| 200 |
|
| 201 |
def read_logs():
|
| 202 |
try:
|
|
|
|
| 213 |
"""
|
| 214 |
)
|
| 215 |
|
| 216 |
+
with gr.Row():
|
| 217 |
+
gr.Image(value=get_avatar(), label="Your Avatar", width=100, height=100)
|
| 218 |
+
avatar_input = gr.Dropdown(choices=list(avatars.keys()), value="default", label="Choose Avatar")
|
| 219 |
+
avatar_input.change(fn=set_avatar, inputs=avatar_input, outputs=gr.Image(label="Your Avatar", width=100, height=100))
|
| 220 |
+
|
| 221 |
with gr.Tab("Ask a Question"):
|
| 222 |
question_input = gr.Textbox(label="Question", placeholder="e.g., What is machine learning?")
|
| 223 |
context_input = gr.Textbox(label="Context (Lecture Notes)", placeholder="Paste your notes here...")
|
|
|
|
| 225 |
theme_input = gr.Dropdown(choices=["dark", "light"], value="dark", label="Theme")
|
| 226 |
audio_output_input = gr.Checkbox(label="Generate Audio Output")
|
| 227 |
simplify_text_input = gr.Checkbox(label="Simplify Text")
|
| 228 |
+
with gr.Row():
|
| 229 |
+
study_submit_btn = gr.Button("Get Answer")
|
| 230 |
+
hint_btn = gr.Button("Get Hint")
|
| 231 |
study_output_text = gr.HTML(label="Answer")
|
| 232 |
study_output_audio = gr.Audio(label="Audio Narration")
|
| 233 |
+
study_output_sound = gr.Audio(label="Sound Effect", visible=False)
|
| 234 |
score_output = gr.Text(label="Score & Progress")
|
| 235 |
+
hint_output = gr.Text(label="Hint")
|
| 236 |
|
| 237 |
study_submit_btn.click(
|
| 238 |
fn=study_aid,
|
| 239 |
inputs=[question_input, context_input, font_size_input, audio_output_input, simplify_text_input, theme_input],
|
| 240 |
+
outputs=[study_output_text, study_output_audio, study_output_sound, score_output]
|
| 241 |
+
)
|
| 242 |
+
hint_btn.click(
|
| 243 |
+
fn=get_hint,
|
| 244 |
+
inputs=context_input,
|
| 245 |
+
outputs=hint_output
|
| 246 |
)
|
| 247 |
|
| 248 |
with gr.Tab("Quiz Me"):
|
|
|
|
| 251 |
quiz_submit_btn = gr.Button("Generate Quiz Question")
|
| 252 |
quiz_output_text = gr.HTML(label="Quiz Question and Answer")
|
| 253 |
quiz_output_audio = gr.Audio(label="Audio Narration")
|
| 254 |
+
quiz_output_sound = gr.Audio(label="Sound Effect", visible=False)
|
| 255 |
quiz_score_output = gr.Text(label="Score & Progress")
|
| 256 |
|
| 257 |
quiz_submit_btn.click(
|
| 258 |
fn=generate_quiz,
|
| 259 |
inputs=[quiz_context_input, quiz_theme_input],
|
| 260 |
+
outputs=[quiz_output_text, quiz_output_audio, quiz_output_sound, quiz_score_output]
|
| 261 |
)
|
| 262 |
|
| 263 |
with gr.Tab("Submit Feedback"):
|
| 264 |
feedback_input = gr.Textbox(label="Feedback", placeholder="Report issues or suggestions...")
|
| 265 |
feedback_submit_btn = gr.Button("Submit Feedback")
|
| 266 |
feedback_output = gr.Text(label="Feedback Status")
|
| 267 |
+
feedback_sound = gr.Audio(label="Sound Effect", visible=False)
|
| 268 |
|
| 269 |
feedback_submit_btn.click(
|
| 270 |
fn=submit_feedback,
|
| 271 |
inputs=feedback_input,
|
| 272 |
+
outputs=[feedback_output, feedback_sound]
|
| 273 |
)
|
| 274 |
|
| 275 |
with gr.Tab("View Logs"):
|