Update file_utils.py
Browse files- file_utils.py +60 -118
file_utils.py
CHANGED
|
@@ -1,119 +1,61 @@
|
|
| 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 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
]
|
| 65 |
-
)
|
| 66 |
-
return response.choices[0].message.content
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
def generate_flashcards(text):
|
| 70 |
-
prompt = f"""Based on the content below, create a set of helpful flashcards.
|
| 71 |
-
Each flashcard should be formatted as:
|
| 72 |
-
Q: Question?
|
| 73 |
-
A: Answer.
|
| 74 |
-
|
| 75 |
-
{text}
|
| 76 |
-
"""
|
| 77 |
-
response = client.chat.completions.create(
|
| 78 |
-
model="gpt-4",
|
| 79 |
-
messages=[
|
| 80 |
-
{"role": "system", "content": "You are a flashcard generator assistant."},
|
| 81 |
-
{"role": "user", "content": prompt}
|
| 82 |
-
]
|
| 83 |
-
)
|
| 84 |
-
return response.choices[0].message.content
|
| 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 |
-
response = client.chat.completions.create(
|
| 94 |
-
model="gpt-4",
|
| 95 |
-
messages=[
|
| 96 |
-
{"role": "system", "content": "You are a quiz generator assistant."},
|
| 97 |
-
{"role": "user", "content": prompt}
|
| 98 |
-
]
|
| 99 |
-
)
|
| 100 |
-
return response.choices[0].message.content
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
def answer_question(text, question):
|
| 104 |
-
prompt = f"""You are an AI tutor. Answer the following question based only on the content below.
|
| 105 |
-
|
| 106 |
-
Content:
|
| 107 |
-
{text}
|
| 108 |
-
|
| 109 |
-
Question:
|
| 110 |
-
{question}
|
| 111 |
-
"""
|
| 112 |
-
response = client.chat.completions.create(
|
| 113 |
-
model="gpt-4",
|
| 114 |
-
messages=[
|
| 115 |
-
{"role": "system", "content": "You are a helpful and accurate tutor that stays grounded in provided content."},
|
| 116 |
-
{"role": "user", "content": prompt}
|
| 117 |
-
]
|
| 118 |
-
)
|
| 119 |
-
return response.choices[0].message.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() as file_panel:
|
| 14 |
+
gr.Markdown("""
|
| 15 |
+
<div style='text-align: center;'>
|
| 16 |
+
<img src='file/images/chatter_owl.png' width='120'>
|
| 17 |
+
<h2>π File-Based Learning with <strong>Chatter the Owl</strong></h2>
|
| 18 |
+
<p>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")
|
| 24 |
+
|
| 25 |
+
with gr.Tabs():
|
| 26 |
+
with gr.Tab("π Summary"):
|
| 27 |
+
summary_output = gr.Textbox(label="Generated Summary", lines=8, interactive=False)
|
| 28 |
+
with gr.Tab("π§ Flashcards"):
|
| 29 |
+
flashcard_output = gr.Textbox(label="Generated Flashcards", lines=8, interactive=False)
|
| 30 |
+
with gr.Tab("β Quiz"):
|
| 31 |
+
quiz_output = gr.Textbox(label="Generated Quiz Questions", lines=8, interactive=False)
|
| 32 |
+
with gr.Tab("π¬ Answer to Your Question"):
|
| 33 |
+
answer_output = gr.Textbox(label="Answer", lines=4, interactive=False)
|
| 34 |
+
|
| 35 |
+
text_state = gr.State("")
|
| 36 |
+
|
| 37 |
+
def handle_file_upload(file):
|
| 38 |
+
text = extract_text_from_file(file)
|
| 39 |
+
summary = generate_summary(text)
|
| 40 |
+
flashcards = generate_flashcards(text)
|
| 41 |
+
quiz = generate_quiz(text)
|
| 42 |
+
return text, summary, flashcards, quiz
|
| 43 |
+
|
| 44 |
+
def handle_question(text, question):
|
| 45 |
+
if not text.strip() or not question.strip():
|
| 46 |
+
return "Please upload a file and enter a question."
|
| 47 |
+
return answer_question(text, question)
|
| 48 |
+
|
| 49 |
+
file_upload.change(
|
| 50 |
+
fn=handle_file_upload,
|
| 51 |
+
inputs=[file_upload],
|
| 52 |
+
outputs=[text_state, summary_output, flashcard_output, quiz_output]
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
question_box.change(
|
| 56 |
+
fn=handle_question,
|
| 57 |
+
inputs=[text_state, question_box],
|
| 58 |
+
outputs=[answer_output]
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
return file_panel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|