File size: 11,658 Bytes
5a4c895
0580c15
f53076b
 
1913949
0580c15
5a4c895
 
 
f53076b
 
 
 
 
5a4c895
0580c15
1913949
0580c15
1913949
0580c15
 
1913949
1ffcfa5
 
 
 
 
 
 
1913949
 
 
 
 
 
0580c15
 
 
 
 
 
 
 
 
 
 
1913949
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0580c15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1913949
5a4c895
1913949
5a4c895
f53076b
 
 
 
 
 
 
 
 
 
 
 
5a4c895
f53076b
5a4c895
1913949
 
 
f53076b
 
5a4c895
 
1913949
 
 
 
 
 
 
 
 
 
 
 
0580c15
 
 
5a4c895
f53076b
 
0580c15
5a4c895
0580c15
5a4c895
 
 
 
1913949
 
 
0580c15
 
1913949
 
0580c15
 
1913949
 
0580c15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1913949
0580c15
 
1913949
 
 
 
 
 
 
 
 
 
 
 
 
 
0580c15
 
1913949
0580c15
5a4c895
1ffcfa5
 
 
 
 
 
 
5fedfb2
 
 
 
 
 
 
 
0580c15
 
 
 
 
5fedfb2
 
 
 
1913949
5fedfb2
 
0580c15
 
 
5fedfb2
 
0580c15
1913949
0580c15
5fedfb2
 
 
1913949
0580c15
 
 
 
 
 
1913949
 
 
 
 
 
 
 
0580c15
1913949
 
 
 
 
0580c15
5fedfb2
 
 
 
 
 
0580c15
5fedfb2
 
 
 
0580c15
5fedfb2
5a4c895
f53076b
1ffcfa5
f53076b
5fedfb2
1
2
3
4
5
6
7
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
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
import gradio as gr
from transformers import pipeline, AutoTokenizer
from gtts import gTTS
import os
import random
import re

# Initialize NLP pipelines
qa = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
try:
    summarizer = pipeline("summarization", model="t5-small")
except Exception as e:
    print(f"Error loading summarizer: {e}")
    summarizer = None

# Load tokenizer for rule-based question generation
try:
    tokenizer = AutoTokenizer.from_pretrained("t5-small")
except Exception as e:
    print(f"Error loading tokenizer: {e}")
    tokenizer = None

# Initialize user stats and log files
if not os.path.exists("decision_log.txt"):
    with open("decision_log.txt", "w") as f:
        f.write("Decision Log Initialized\n")
if not os.path.exists("feedback.txt"):
    with open("feedback.txt", "w") as f:
        f.write("Feedback Log Initialized\n")
if not os.path.exists("user_score.txt"):
    with open("user_score.txt", "w") as f:
        f.write("0")
if not os.path.exists("questions_answered.txt"):
    with open("questions_answered.txt", "w") as f:
        f.write("0")
if not os.path.exists("avatar.txt"):
    with open("avatar.txt", "w") as f:
        f.write("default")

# Avatar options
avatars = {
    "default": "https://cdn.pixabay.com/photo/2016/08/08/09/17/avatar-1577909_1280.png",
    "robot": "https://cdn.pixabay.com/photo/2017/01/31/21/23/robot-2027195_1280.png",
    "cat": "https://cdn.pixabay.com/photo/2017/01/31/21/23/cat-2027196_1280.png",
    "dog": "https://cdn.pixabay.com/photo/2017/01/31/21/23/dog-2027197_1280.png"
}

def update_score(points):
    with open("user_score.txt", "r") as f:
        score = int(f.read())
    score += points
    with open("user_score.txt", "w") as f:
        f.write(str(score))
    return score

def update_questions_answered():
    with open("questions_answered.txt", "r") as f:
        count = int(f.read())
    count += 1
    with open("questions_answered.txt", "w") as f:
        f.write(str(count))
    return count

def get_progress():
    with open("questions_answered.txt", "r") as f:
        count = int(f.read())
    progress = min(count * 10, 100)  # 10% per question, max 100%
    return f"Progress: {progress}%"

def get_motivational_message():
    messages = [
        "Great job! Keep learning!",
        "You're doing awesome!",
        "Amazing effort! Keep it up!",
        "You're making great progress!"
    ]
    return random.choice(messages)

def get_hint(context):
    try:
        if len(context.split()) < 10:
            return "Hint: The answer is in the context, but it's too short to summarize."
        summary = summarizer(context, max_length=30, min_length=10)[0]["summary_text"]
        return f"Hint: {summary}"
    except Exception as e:
        return f"Hint unavailable: {str(e)}"

def set_avatar(avatar_choice):
    with open("avatar.txt", "w") as f:
        f.write(avatar_choice)
    return avatars[avatar_choice]

def get_avatar():
    with open("avatar.txt", "r") as f:
        avatar_choice = f.read().strip()
    return avatars.get(avatar_choice, avatars["default"])

def study_aid(question, context, font_size=16, audio_output=False, simplify_text=False, theme="dark"):
    with open("decision_log.txt", "a") as f:
        f.write(f"Question: {question}, Simplified: {simplify_text}, Audio: {audio_output}, Font: {font_size}, Theme: {theme}\n")
    
    simplified_context = context
    if simplify_text and summarizer is not None:
        try:
            if len(context.split()) < 10:
                simplified_context = "Input too short to simplify."
            elif len(context.split()) > 512:
                simplified_context = "Input too long to simplify."
            else:
                summary = summarizer(context, max_length=100, min_length=50)[0]["summary_text"]
                simplified_context = summary
        except Exception as e:
            simplified_context = f"Error simplifying text: {str(e)}"
    
    answer = qa(question=question, context=simplified_context)["answer"]
    
    bg_color = "black" if theme == "dark" else "white"
    text_color = "white" if theme == "dark" else "black"
    output = f"<div style='font-size:{font_size}px; color:{text_color}; background-color:{bg_color}; padding:10px;'>"
    if simplify_text and simplified_context != context:
        output += f"<b>Simplified Context:</b> {simplified_context}<br>"
    output += f"<b>Answer:</b> {answer}</div>"
    
    # Add visual diagram for neural network questions
    diagram = None
    if "neural network" in question.lower():
        diagram = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/46/Colored_neural_network.svg/300px-Colored_neural_network.svg.png"
        output += f"<br><img src='{diagram}' alt='Neural Network Diagram' style='width:300px; height:auto;'>"
    
    # Update stats
    score = update_score(10)
    questions_answered = update_questions_answered()
    progress = get_progress()
    motivation = get_motivational_message()
    
    # Sound effect for points earned
    sound_effect = "https://www.soundjay.com/buttons/sounds/beep-01a.mp3"
    
    if audio_output:
        tts = gTTS(text=answer, lang='en')
        tts.save("answer_audio.mp3")
        return output, "answer_audio.mp3", sound_effect, f"Your Score: {score} | {progress} | {motivation}"
    
    return output, None, sound_effect, f"Your Score: {score} | {progress} | {motivation}"

def submit_feedback(feedback):
    with open("feedback.txt", "a") as f:
        f.write(feedback + "\n")
    score = update_score(5)
    progress = get_progress()
    motivation = get_motivational_message()
    sound_effect = "https://www.soundjay.com/buttons/sounds/beep-01a.mp3"
    return f"Feedback submitted! Your Score: {score} | {progress} | {motivation}", sound_effect

def generate_quiz(context, theme="dark"):
    if tokenizer is None:
        return "Question generation not available: Tokenizer failed to load.", None, None, None
    
    try:
        # Simple rule-based question generation
        # Tokenize the context and extract key phrases (e.g., noun phrases)
        tokens = context.split()
        key_phrases = []
        current_phrase = []
        for token in tokens:
            if token in [",", ".", ":", ";", "!", "?", "and", "or", "but"]:
                if current_phrase:
                    key_phrases.append(" ".join(current_phrase))
                    current_phrase = []
            else:
                current_phrase.append(token)
        if current_phrase:
            key_phrases.append(" ".join(current_phrase))
        
        # Filter phrases that are likely to be meaningful (e.g., longer than 2 words)
        key_phrases = [phrase for phrase in key_phrases if len(phrase.split()) > 2]
        if not key_phrases:
            return "No suitable phrases found for question generation.", None, None, None
        
        # Generate a question using the first key phrase
        quiz_question = f"What is {key_phrases[0]}?"
        answer = qa(question=quiz_question, context=context)["answer"]
        
        bg_color = "black" if theme == "dark" else "white"
        text_color = "white" if theme == "dark" else "black"
        output = f"<div style='color:{text_color}; background-color:{bg_color}; padding:10px;'>"
        output += f"<b>Quiz Question:</b> {quiz_question}<br><b>Answer:</b> {answer}</div>"
        
        tts = gTTS(text=answer, lang='en')
        tts.save("quiz_audio.mp3")
        
        score = update_score(20)
        questions_answered = update_questions_answered()
        progress = get_progress()
        motivation = get_motivational_message()
        sound_effect = "https://www.soundjay.com/buttons/sounds/beep-01a.mp3"
        return output, "quiz_audio.mp3", sound_effect, f"Your Score: {score} | {progress} | {motivation}"
    except Exception as e:
        return f"Error generating quiz: {str(e)}", None, None, None

def read_logs():
    try:
        with open("decision_log.txt", "r") as f:
            return f.read()
    except FileNotFoundError:
        return "Decision log not found. It will be created once you start using the app."

with gr.Blocks(title="StudyBuddy: Accessible Study Aid for Neurodiverse Students") as app:
    gr.Markdown(
        """
        # StudyBuddy: Accessible Study Aid for Neurodiverse Students
        Ask questions about your college lecture notes with accessible text and audio outputs. No data is stored.
        """
    )
    
    with gr.Row():
        gr.Image(value=get_avatar(), label="Your Avatar", width=100, height=100)
        avatar_input = gr.Dropdown(choices=list(avatars.keys()), value="default", label="Choose Avatar")
        avatar_input.change(fn=set_avatar, inputs=avatar_input, outputs=gr.Image(label="Your Avatar", width=100, height=100))
    
    with gr.Tab("Ask a Question"):
        question_input = gr.Textbox(label="Question", placeholder="e.g., What is machine learning?")
        context_input = gr.Textbox(label="Context (Lecture Notes)", placeholder="Paste your notes here...")
        font_size_input = gr.Slider(12, 24, value=16, label="Font Size (px)")
        theme_input = gr.Dropdown(choices=["dark", "light"], value="dark", label="Theme")
        audio_output_input = gr.Checkbox(label="Generate Audio Output")
        simplify_text_input = gr.Checkbox(label="Simplify Text")
        with gr.Row():
            study_submit_btn = gr.Button("Get Answer")
            hint_btn = gr.Button("Get Hint")
        study_output_text = gr.HTML(label="Answer")
        study_output_audio = gr.Audio(label="Audio Narration")
        study_output_sound = gr.Audio(label="Sound Effect", visible=False)
        score_output = gr.Text(label="Score & Progress")
        hint_output = gr.Text(label="Hint")
        
        study_submit_btn.click(
            fn=study_aid,
            inputs=[question_input, context_input, font_size_input, audio_output_input, simplify_text_input, theme_input],
            outputs=[study_output_text, study_output_audio, study_output_sound, score_output]
        )
        hint_btn.click(
            fn=get_hint,
            inputs=context_input,
            outputs=hint_output
        )
    
    with gr.Tab("Quiz Me"):
        quiz_context_input = gr.Textbox(label="Context (Lecture Notes)", placeholder="Paste your notes here...")
        quiz_theme_input = gr.Dropdown(choices=["dark", "light"], value="dark", label="Theme")
        quiz_submit_btn = gr.Button("Generate Quiz Question")
        quiz_output_text = gr.HTML(label="Quiz Question and Answer")
        quiz_output_audio = gr.Audio(label="Audio Narration")
        quiz_output_sound = gr.Audio(label="Sound Effect", visible=False)
        quiz_score_output = gr.Text(label="Score & Progress")
        
        quiz_submit_btn.click(
            fn=generate_quiz,
            inputs=[quiz_context_input, quiz_theme_input],
            outputs=[quiz_output_text, quiz_output_audio, quiz_output_sound, quiz_score_output]
        )
    
    with gr.Tab("Submit Feedback"):
        feedback_input = gr.Textbox(label="Feedback", placeholder="Report issues or suggestions...")
        feedback_submit_btn = gr.Button("Submit Feedback")
        feedback_output = gr.Text(label="Feedback Status")
        feedback_sound = gr.Audio(label="Sound Effect", visible=False)
        
        feedback_submit_btn.click(
            fn=submit_feedback,
            inputs=feedback_input,
            outputs=[feedback_output, feedback_sound]
        )

    with gr.Tab("View Logs"):
        logs_output = gr.Textbox(label="Decision Logs", value=read_logs())

app.launch()