ankban commited on
Commit
7a0a3a3
Β·
verified Β·
1 Parent(s): 0a0c7fa

Update file_module.py

Browse files
Files changed (1) hide show
  1. 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
- generate_summary,
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("""### πŸ“‘ Section Overview
33
- Upload a file to generate section outline...""", elem_id="section-overview")
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.Markdown("""### πŸ—‚ Sections
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 placeholder_file_parse(file):
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
- return (
56
- f"**File:** {name} | **Progress:** 3%",
57
- "### πŸ“‘ Section Overview\n1. Introduction\n2. Key Topics\n3. Summary\n4. Conclusion",
58
- "#### πŸ“˜ Introduction\n\nThis is a placeholder section content. More details coming soon.",
59
- "### πŸ—‚ Sections\n- Introduction\n- Key Topics\n- Summary\n- Conclusion"
60
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  file_upload.change(
63
- fn=placeholder_file_parse,
64
  inputs=[file_upload],
65
- outputs=[uploaded_file_label, section_overview, section_content_box, section_list],
 
 
 
 
 
 
 
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