Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Initialize NLP pipelines
|
| 5 |
qa = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def study_aid(question, context, font_size=16, audio_output=False, simplify_text=False):
|
| 10 |
-
# Log decisions for transparency
|
| 11 |
with open("decision_log.txt", "a") as f:
|
| 12 |
f.write(f"Question: {question}, Simplified: {simplify_text}, Audio: {audio_output}, Font: {font_size}\n")
|
| 13 |
|
| 14 |
-
|
| 15 |
-
if simplify_text:
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
answer = qa(question=question, context=context)["answer"]
|
| 20 |
|
| 21 |
-
# Format output with adjustable font size
|
| 22 |
output = f"<div style='font-size:{font_size}px'>"
|
| 23 |
-
if simplify_text:
|
| 24 |
-
output += f"<b>Simplified Context:</b> {
|
| 25 |
output += f"<b>Answer:</b> {answer}</div>"
|
| 26 |
|
| 27 |
-
# Generate audio if requested
|
| 28 |
if audio_output:
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
return output, None
|
| 33 |
|
|
@@ -36,7 +48,6 @@ def submit_feedback(feedback):
|
|
| 36 |
f.write(feedback + "\n")
|
| 37 |
return "Feedback submitted!"
|
| 38 |
|
| 39 |
-
# Create Gradio app with Blocks
|
| 40 |
with gr.Blocks(title="StudyBuddy: Accessible Study Aid for Neurodiverse Students") as app:
|
| 41 |
gr.Markdown(
|
| 42 |
"""
|
|
@@ -45,7 +56,6 @@ with gr.Blocks(title="StudyBuddy: Accessible Study Aid for Neurodiverse Students
|
|
| 45 |
"""
|
| 46 |
)
|
| 47 |
|
| 48 |
-
# Tab 1: Study Aid
|
| 49 |
with gr.Tab("Ask a Question"):
|
| 50 |
question_input = gr.Textbox(label="Question", placeholder="e.g., What is machine learning?")
|
| 51 |
context_input = gr.Textbox(label="Context (Lecture Notes)", placeholder="Paste your notes here...")
|
|
@@ -62,7 +72,6 @@ with gr.Blocks(title="StudyBuddy: Accessible Study Aid for Neurodiverse Students
|
|
| 62 |
outputs=[study_output_text, study_output_audio]
|
| 63 |
)
|
| 64 |
|
| 65 |
-
# Tab 2: Feedback
|
| 66 |
with gr.Tab("Submit Feedback"):
|
| 67 |
feedback_input = gr.Textbox(label="Feedback", placeholder="Report issues or suggestions...")
|
| 68 |
feedback_submit_btn = gr.Button("Submit Feedback")
|
|
@@ -74,4 +83,7 @@ with gr.Blocks(title="StudyBuddy: Accessible Study Aid for Neurodiverse Students
|
|
| 74 |
outputs=feedback_output
|
| 75 |
)
|
| 76 |
|
|
|
|
|
|
|
|
|
|
| 77 |
app.launch()
|
|
|
|
| 1 |
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")
|
| 8 |
+
try:
|
| 9 |
+
summarizer = pipeline("summarization", model="t5-small")
|
| 10 |
+
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:
|
| 25 |
+
simplified_context = "Input too long to simplify."
|
| 26 |
+
else:
|
| 27 |
+
summary = summarizer(context, max_length=100, min_length=50)[0]["summary_text"]
|
| 28 |
+
simplified_context = summary
|
| 29 |
+
except Exception as e:
|
| 30 |
+
simplified_context = f"Error simplifying text: {str(e)}"
|
| 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 |
|
|
|
|
| 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(
|
| 53 |
"""
|
|
|
|
| 56 |
"""
|
| 57 |
)
|
| 58 |
|
|
|
|
| 59 |
with gr.Tab("Ask a Question"):
|
| 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...")
|
|
|
|
| 72 |
outputs=[study_output_text, study_output_audio]
|
| 73 |
)
|
| 74 |
|
|
|
|
| 75 |
with gr.Tab("Submit Feedback"):
|
| 76 |
feedback_input = gr.Textbox(label="Feedback", placeholder="Report issues or suggestions...")
|
| 77 |
feedback_submit_btn = gr.Button("Submit Feedback")
|
|
|
|
| 83 |
outputs=feedback_output
|
| 84 |
)
|
| 85 |
|
| 86 |
+
with gr.Tab("View Logs"):
|
| 87 |
+
logs_output = gr.Textbox(label="Decision Logs", value=lambda: open("decision_log.txt", "r").read())
|
| 88 |
+
|
| 89 |
app.launch()
|