Sumit404 commited on
Commit
1913949
·
verified ·
1 Parent(s): e8adaee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -9
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from transformers import pipeline
3
  from gtts import gTTS
4
  import os
 
5
 
6
  # Initialize NLP pipelines
7
  qa = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
@@ -11,14 +12,58 @@ except Exception as e:
11
  print(f"Error loading summarizer: {e}")
12
  summarizer = None
13
 
14
- def study_aid(question, context, font_size=16, audio_output=False, simplify_text=False):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  with open("decision_log.txt", "a") as f:
16
- f.write(f"Question: {question}, Simplified: {simplify_text}, Audio: {audio_output}, Font: {font_size}\n")
17
 
18
  simplified_context = context
19
  if simplify_text and summarizer is not None:
20
  try:
21
- # Ensure input length is suitable
22
  if len(context.split()) < 10:
23
  simplified_context = "Input too short to simplify."
24
  elif len(context.split()) > 512:
@@ -31,22 +76,68 @@ def study_aid(question, context, font_size=16, audio_output=False, simplify_text
31
 
32
  answer = qa(question=question, context=simplified_context)["answer"]
33
 
34
- output = f"<div style='font-size:{font_size}px'>"
 
 
35
  if simplify_text and simplified_context != context:
36
  output += f"<b>Simplified Context:</b> {simplified_context}<br>"
37
  output += f"<b>Answer:</b> {answer}</div>"
38
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  if audio_output:
40
  tts = gTTS(text=answer, lang='en')
41
  tts.save("answer_audio.mp3")
42
- return output, "answer_audio.mp3"
43
 
44
- return output, None
45
 
46
  def submit_feedback(feedback):
47
  with open("feedback.txt", "a") as f:
48
  f.write(feedback + "\n")
49
- return "Feedback submitted!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  with gr.Blocks(title="StudyBuddy: Accessible Study Aid for Neurodiverse Students") as app:
52
  gr.Markdown(
@@ -60,16 +151,32 @@ with gr.Blocks(title="StudyBuddy: Accessible Study Aid for Neurodiverse Students
60
  question_input = gr.Textbox(label="Question", placeholder="e.g., What is machine learning?")
61
  context_input = gr.Textbox(label="Context (Lecture Notes)", placeholder="Paste your notes here...")
62
  font_size_input = gr.Slider(12, 24, value=16, label="Font Size (px)")
 
63
  audio_output_input = gr.Checkbox(label="Generate Audio Output")
64
  simplify_text_input = gr.Checkbox(label="Simplify Text")
65
  study_submit_btn = gr.Button("Get Answer")
66
  study_output_text = gr.HTML(label="Answer")
67
  study_output_audio = gr.Audio(label="Audio Narration")
 
68
 
69
  study_submit_btn.click(
70
  fn=study_aid,
71
- inputs=[question_input, context_input, font_size_input, audio_output_input, simplify_text_input],
72
- outputs=[study_output_text, study_output_audio]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  )
74
 
75
  with gr.Tab("Submit Feedback"):
 
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
  print(f"Error loading summarizer: {e}")
13
  summarizer = None
14
 
15
+ try:
16
+ question_generator = pipeline("text2text-generation", model="valhalla/t5-small-qg-hl")
17
+ except Exception as e:
18
+ print(f"Error loading question generator: {e}")
19
+ question_generator = None
20
+
21
+ # Initialize user stats
22
+ if not os.path.exists("user_score.txt"):
23
+ with open("user_score.txt", "w") as f:
24
+ f.write("0")
25
+ if not os.path.exists("questions_answered.txt"):
26
+ with open("questions_answered.txt", "w") as f:
27
+ f.write("0")
28
+
29
+ def update_score(points):
30
+ with open("user_score.txt", "r") as f:
31
+ score = int(f.read())
32
+ score += points
33
+ with open("user_score.txt", "w") as f:
34
+ f.write(str(score))
35
+ return score
36
+
37
+ def update_questions_answered():
38
+ with open("questions_answered.txt", "r") as f:
39
+ count = int(f.read())
40
+ count += 1
41
+ with open("questions_answered.txt", "w") as f:
42
+ f.write(str(count))
43
+ return count
44
+
45
+ def get_progress():
46
+ with open("questions_answered.txt", "r") as f:
47
+ count = int(f.read())
48
+ progress = min(count * 10, 100) # 10% per question, max 100%
49
+ return f"Progress: {progress}%"
50
+
51
+ def get_motivational_message():
52
+ messages = [
53
+ "Great job! Keep learning!",
54
+ "You're doing awesome!",
55
+ "Amazing effort! Keep it up!",
56
+ "You're making great progress!"
57
+ ]
58
+ return random.choice(messages)
59
+
60
+ def study_aid(question, context, font_size=16, audio_output=False, simplify_text=False, theme="dark"):
61
  with open("decision_log.txt", "a") as f:
62
+ f.write(f"Question: {question}, Simplified: {simplify_text}, Audio: {audio_output}, Font: {font_size}, Theme: {theme}\n")
63
 
64
  simplified_context = context
65
  if simplify_text and summarizer is not None:
66
  try:
 
67
  if len(context.split()) < 10:
68
  simplified_context = "Input too short to simplify."
69
  elif len(context.split()) > 512:
 
76
 
77
  answer = qa(question=question, context=simplified_context)["answer"]
78
 
79
+ bg_color = "black" if theme == "dark" else "white"
80
+ text_color = "white" if theme == "dark" else "black"
81
+ output = f"<div style='font-size:{font_size}px; color:{text_color}; background-color:{bg_color}; padding:10px;'>"
82
  if simplify_text and simplified_context != context:
83
  output += f"<b>Simplified Context:</b> {simplified_context}<br>"
84
  output += f"<b>Answer:</b> {answer}</div>"
85
 
86
+ # Add visual diagram for neural network questions
87
+ diagram = None
88
+ if "neural network" in question.lower():
89
+ diagram = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/46/Colored_neural_network.svg/300px-Colored_neural_network.svg.png"
90
+ output += f"<br><img src='{diagram}' alt='Neural Network Diagram' style='width:300px; height:auto;'>"
91
+
92
+ # Update stats
93
+ score = update_score(10)
94
+ questions_answered = update_questions_answered()
95
+ progress = get_progress()
96
+ motivation = get_motivational_message()
97
+
98
  if audio_output:
99
  tts = gTTS(text=answer, lang='en')
100
  tts.save("answer_audio.mp3")
101
+ return output, "answer_audio.mp3", f"Your Score: {score} | {progress} | {motivation}"
102
 
103
+ return output, None, f"Your Score: {score} | {progress} | {motivation}"
104
 
105
  def submit_feedback(feedback):
106
  with open("feedback.txt", "a") as f:
107
  f.write(feedback + "\n")
108
+ score = update_score(5)
109
+ progress = get_progress()
110
+ motivation = get_motivational_message()
111
+ return f"Feedback submitted! Your Score: {score} | {progress} | {motivation}"
112
+
113
+ def generate_quiz(context, theme="dark"):
114
+ if question_generator is None:
115
+ return "Question generation not available.", None, None
116
+
117
+ try:
118
+ generated = question_generator(f"generate questions: {context}", max_length=50)
119
+ questions = generated[0]["generated_text"].split(" | ")
120
+ if not questions:
121
+ return "No questions generated.", None, None
122
+
123
+ quiz_question = questions[0]
124
+ answer = qa(question=quiz_question, context=context)["answer"]
125
+
126
+ bg_color = "black" if theme == "dark" else "white"
127
+ text_color = "white" if theme == "dark" else "black"
128
+ output = f"<div style='color:{text_color}; background-color:{bg_color}; padding:10px;'>"
129
+ output += f"<b>Quiz Question:</b> {quiz_question}<br><b>Answer:</b> {answer}</div>"
130
+
131
+ tts = gTTS(text=answer, lang='en')
132
+ tts.save("quiz_audio.mp3")
133
+
134
+ score = update_score(20)
135
+ questions_answered = update_questions_answered()
136
+ progress = get_progress()
137
+ motivation = get_motivational_message()
138
+ return output, "quiz_audio.mp3", f"Your Score: {score} | {progress} | {motivation}"
139
+ except Exception as e:
140
+ return f"Error generating quiz: {str(e)}", None, None
141
 
142
  with gr.Blocks(title="StudyBuddy: Accessible Study Aid for Neurodiverse Students") as app:
143
  gr.Markdown(
 
151
  question_input = gr.Textbox(label="Question", placeholder="e.g., What is machine learning?")
152
  context_input = gr.Textbox(label="Context (Lecture Notes)", placeholder="Paste your notes here...")
153
  font_size_input = gr.Slider(12, 24, value=16, label="Font Size (px)")
154
+ theme_input = gr.Dropdown(choices=["dark", "light"], value="dark", label="Theme")
155
  audio_output_input = gr.Checkbox(label="Generate Audio Output")
156
  simplify_text_input = gr.Checkbox(label="Simplify Text")
157
  study_submit_btn = gr.Button("Get Answer")
158
  study_output_text = gr.HTML(label="Answer")
159
  study_output_audio = gr.Audio(label="Audio Narration")
160
+ score_output = gr.Text(label="Score & Progress")
161
 
162
  study_submit_btn.click(
163
  fn=study_aid,
164
+ inputs=[question_input, context_input, font_size_input, audio_output_input, simplify_text_input, theme_input],
165
+ outputs=[study_output_text, study_output_audio, score_output]
166
+ )
167
+
168
+ with gr.Tab("Quiz Me"):
169
+ quiz_context_input = gr.Textbox(label="Context (Lecture Notes)", placeholder="Paste your notes here...")
170
+ quiz_theme_input = gr.Dropdown(choices=["dark", "light"], value="dark", label="Theme")
171
+ quiz_submit_btn = gr.Button("Generate Quiz Question")
172
+ quiz_output_text = gr.HTML(label="Quiz Question and Answer")
173
+ quiz_output_audio = gr.Audio(label="Audio Narration")
174
+ quiz_score_output = gr.Text(label="Score & Progress")
175
+
176
+ quiz_submit_btn.click(
177
+ fn=generate_quiz,
178
+ inputs=[quiz_context_input, quiz_theme_input],
179
+ outputs=[quiz_output_text, quiz_output_audio, quiz_score_output]
180
  )
181
 
182
  with gr.Tab("Submit Feedback"):