Update file_utils.py
Browse files- file_utils.py +75 -104
file_utils.py
CHANGED
|
@@ -1,105 +1,76 @@
|
|
| 1 |
-
import
|
| 2 |
-
from pptx import Presentation
|
| 3 |
-
from openai import OpenAI
|
| 4 |
import os
|
| 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 |
-
if
|
| 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 |
-
Q: Question?
|
| 80 |
-
A: Answer.
|
| 81 |
-
|
| 82 |
-
{text}
|
| 83 |
-
"""
|
| 84 |
-
return call_llm(prompt, system_message="You are a flashcard generator assistant.")
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
def generate_quiz(text):
|
| 88 |
-
prompt = f"""Create a short quiz (3-5 questions) based on the content below.
|
| 89 |
-
Include a mix of multiple choice and short answer questions.
|
| 90 |
-
|
| 91 |
-
{text}
|
| 92 |
-
"""
|
| 93 |
-
return call_llm(prompt, system_message="You are a quiz generator assistant.")
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
def answer_question(text, question):
|
| 97 |
-
prompt = f"""You are an AI tutor. Answer the following question based only on the content below.
|
| 98 |
-
|
| 99 |
-
Content:
|
| 100 |
-
{text}
|
| 101 |
-
|
| 102 |
-
Question:
|
| 103 |
-
{question}
|
| 104 |
-
"""
|
| 105 |
-
return call_llm(prompt, system_message="You are a helpful and accurate tutor that stays grounded in provided content.")
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
+
from file_utils import (
|
| 4 |
+
extract_text_from_file,
|
| 5 |
+
generate_summary,
|
| 6 |
+
generate_flashcards,
|
| 7 |
+
generate_quiz,
|
| 8 |
+
answer_question
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def file_dashboard():
|
| 13 |
+
with gr.Column(elem_classes="file-panel") as file_panel:
|
| 14 |
+
gr.Markdown("""
|
| 15 |
+
<div style='text-align: center; margin-bottom: 1rem;'>
|
| 16 |
+
<img src='file/images/chatter_owl.png' width='100'>
|
| 17 |
+
<h2 style='margin-top: 0.5rem;'>π File-Based Learning with <strong>Chatter the Owl</strong></h2>
|
| 18 |
+
<p style='margin-bottom: 1rem;'>Upload your notes, textbooks, or slides, and let me turn them into summaries, quizzes, and flashcards!</p>
|
| 19 |
+
</div>
|
| 20 |
+
""")
|
| 21 |
+
|
| 22 |
+
file_upload = gr.File(label="π Upload Your Study Material", file_types=[".pdf", ".txt", ".md", ".pptx"])
|
| 23 |
+
question_box = gr.Textbox(label="π¬ Ask a question about the uploaded file", lines=2)
|
| 24 |
+
|
| 25 |
+
with gr.Tabs():
|
| 26 |
+
with gr.Tab("π Summary"):
|
| 27 |
+
summary_output = gr.Textbox(label="Generated Summary", lines=6, interactive=False)
|
| 28 |
+
|
| 29 |
+
with gr.Tab("π§ Flashcards"):
|
| 30 |
+
with gr.Accordion("Click to view generated flashcards", open=False):
|
| 31 |
+
flashcard_output = gr.Textbox(label=None, lines=6, interactive=False, show_label=False)
|
| 32 |
+
|
| 33 |
+
with gr.Tab("β Quiz"):
|
| 34 |
+
with gr.Accordion("Click to view generated quiz questions", open=False):
|
| 35 |
+
quiz_output = gr.Textbox(label=None, lines=6, interactive=False, show_label=False)
|
| 36 |
+
|
| 37 |
+
with gr.Tab("π¬ Answer to Your Question"):
|
| 38 |
+
answer_output = gr.Textbox(label="Answer", lines=4, interactive=False)
|
| 39 |
+
|
| 40 |
+
text_state = gr.State("")
|
| 41 |
+
|
| 42 |
+
def handle_file_upload(file):
|
| 43 |
+
if file is None:
|
| 44 |
+
return "", "No file uploaded.", "", "", ""
|
| 45 |
+
|
| 46 |
+
text = extract_text_from_file(file)
|
| 47 |
+
if not text or "Unsupported file type" in text:
|
| 48 |
+
return "", text, "", "", ""
|
| 49 |
+
|
| 50 |
+
summary = generate_summary(text)
|
| 51 |
+
flashcards = generate_flashcards(text)
|
| 52 |
+
quiz = generate_quiz(text)
|
| 53 |
+
return text, summary, flashcards, quiz, ""
|
| 54 |
+
|
| 55 |
+
def handle_question(text, question):
|
| 56 |
+
if not text.strip():
|
| 57 |
+
return "Please upload a file first."
|
| 58 |
+
if not question.strip():
|
| 59 |
+
return "Please enter a question."
|
| 60 |
+
return answer_question(text, question)
|
| 61 |
+
|
| 62 |
+
file_upload.change(
|
| 63 |
+
fn=handle_file_upload,
|
| 64 |
+
inputs=[file_upload],
|
| 65 |
+
outputs=[text_state, summary_output, flashcard_output, quiz_output, answer_output],
|
| 66 |
+
show_progress="minimal"
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
question_box.change(
|
| 70 |
+
fn=handle_question,
|
| 71 |
+
inputs=[text_state, question_box],
|
| 72 |
+
outputs=[answer_output],
|
| 73 |
+
show_progress="minimal"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
return file_panel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|