Update file_module.py
Browse files- file_module.py +53 -27
file_module.py
CHANGED
|
@@ -2,10 +2,7 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
from file_utils import (
|
| 4 |
extract_text_from_file,
|
| 5 |
-
|
| 6 |
-
generate_flashcards,
|
| 7 |
-
generate_quiz,
|
| 8 |
-
answer_question
|
| 9 |
)
|
| 10 |
|
| 11 |
|
|
@@ -29,40 +26,69 @@ def file_dashboard():
|
|
| 29 |
|
| 30 |
with gr.Row():
|
| 31 |
with gr.Column(scale=4):
|
| 32 |
-
section_overview = gr.Markdown("
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
section_content_box = gr.Markdown("""
|
| 36 |
-
> Select a section to view content
|
| 37 |
-
|
| 38 |
-
*No section selected yet.*
|
| 39 |
-
""", elem_id="section-content")
|
| 40 |
|
| 41 |
with gr.Column(scale=1, min_width=200):
|
| 42 |
-
section_list = gr.
|
| 43 |
-
_(auto-generated)_""", elem_id="section-sidebar")
|
| 44 |
|
| 45 |
file_upload = gr.File(label="π Upload PDF/Text File", file_types=[".pdf", ".txt", ".md", ".pptx"])
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
def
|
| 48 |
if file is None:
|
| 49 |
-
return "**File:** _No file uploaded_ | **Progress:** 0%",
|
| 50 |
-
"### π Section Overview\nNo file uploaded.", \
|
| 51 |
-
"*No section selected yet.*", \
|
| 52 |
-
"### π Sections\n_(none)_"
|
| 53 |
|
|
|
|
| 54 |
name = os.path.basename(file.name)
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
file_upload.change(
|
| 63 |
-
fn=
|
| 64 |
inputs=[file_upload],
|
| 65 |
-
outputs=[uploaded_file_label, section_overview,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
show_progress="minimal"
|
| 67 |
)
|
| 68 |
|
|
|
|
| 2 |
import os
|
| 3 |
from file_utils import (
|
| 4 |
extract_text_from_file,
|
| 5 |
+
call_llm
|
|
|
|
|
|
|
|
|
|
| 6 |
)
|
| 7 |
|
| 8 |
|
|
|
|
| 26 |
|
| 27 |
with gr.Row():
|
| 28 |
with gr.Column(scale=4):
|
| 29 |
+
section_overview = gr.Markdown("### π Section Overview\nUpload a file to generate section outline...", elem_id="section-overview")
|
| 30 |
+
section_content_box = gr.Markdown("> Select a section to view content\n\n*No section selected yet.*", elem_id="section-content")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
with gr.Column(scale=1, min_width=200):
|
| 33 |
+
section_list = gr.Radio(choices=[], label="π Sections", interactive=True, elem_id="section-sidebar")
|
|
|
|
| 34 |
|
| 35 |
file_upload = gr.File(label="π Upload PDF/Text File", file_types=[".pdf", ".txt", ".md", ".pptx"])
|
| 36 |
+
sections_state = gr.State([])
|
| 37 |
+
text_state = gr.State("")
|
| 38 |
|
| 39 |
+
def parse_sections(file):
|
| 40 |
if file is None:
|
| 41 |
+
return "**File:** _No file uploaded_ | **Progress:** 0%", "No file uploaded.", [], [], ""
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
text = extract_text_from_file(file)
|
| 44 |
name = os.path.basename(file.name)
|
| 45 |
+
|
| 46 |
+
prompt = f"""You are an education assistant. Read the following content and break it into 4β6 clear sections.
|
| 47 |
+
Return them in this format:
|
| 48 |
+
|
| 49 |
+
1. Title One: Short summary
|
| 50 |
+
2. Title Two: Short summary
|
| 51 |
+
...
|
| 52 |
+
|
| 53 |
+
Content:
|
| 54 |
+
{text}
|
| 55 |
+
"""
|
| 56 |
+
response = call_llm(prompt, system_message="You break study material into logical learning sections.")
|
| 57 |
+
|
| 58 |
+
lines = response.strip().split("\n")
|
| 59 |
+
section_titles = []
|
| 60 |
+
section_summaries = []
|
| 61 |
+
|
| 62 |
+
for line in lines:
|
| 63 |
+
if ":" in line:
|
| 64 |
+
title, summary = line.split(":", 1)
|
| 65 |
+
section_titles.append(title.strip())
|
| 66 |
+
section_summaries.append(summary.strip())
|
| 67 |
+
|
| 68 |
+
overview_md = "### π Section Overview\n" + "\n".join([f"{i+1}. {title}" for i, title in enumerate(section_titles)])
|
| 69 |
+
section_list = section_titles
|
| 70 |
+
initial_content = f"### {section_titles[0]}\n\n{section_summaries[0]}" if section_titles else "*No sections returned.*"
|
| 71 |
+
file_header = f"**File:** {name} | **Progress:** 30%"
|
| 72 |
+
|
| 73 |
+
return file_header, overview_md, section_list, section_summaries, text
|
| 74 |
+
|
| 75 |
+
def show_selected_section(index, summaries):
|
| 76 |
+
try:
|
| 77 |
+
return f"### {index}\n\n{summaries[index]}"
|
| 78 |
+
except:
|
| 79 |
+
return "Error showing section."
|
| 80 |
|
| 81 |
file_upload.change(
|
| 82 |
+
fn=parse_sections,
|
| 83 |
inputs=[file_upload],
|
| 84 |
+
outputs=[uploaded_file_label, section_overview, section_list, sections_state, text_state],
|
| 85 |
+
show_progress="minimal"
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
section_list.change(
|
| 89 |
+
fn=show_selected_section,
|
| 90 |
+
inputs=[section_list, sections_state],
|
| 91 |
+
outputs=section_content_box,
|
| 92 |
show_progress="minimal"
|
| 93 |
)
|
| 94 |
|