Rename file_module.py to file_study_guide_panel.py
Browse files- file_module.py +0 -168
- file_study_guide_panel.py +84 -0
file_module.py
DELETED
|
@@ -1,168 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
-
import json
|
| 4 |
-
from file_utils import (
|
| 5 |
-
extract_text_from_file,
|
| 6 |
-
call_llm
|
| 7 |
-
)
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def file_dashboard():
|
| 11 |
-
uploaded_files = gr.State([])
|
| 12 |
-
file_sections = gr.State({})
|
| 13 |
-
file_texts = gr.State({})
|
| 14 |
-
chat_history = gr.State({})
|
| 15 |
-
quiz_data = gr.State({})
|
| 16 |
-
total_score = gr.State({})
|
| 17 |
-
|
| 18 |
-
with gr.Column(scale=4, elem_id="main-column") as file_content:
|
| 19 |
-
file_guide_list = gr.Radio(label="Your Study Guides", choices=[], interactive=True)
|
| 20 |
-
btn_file_view = gr.Button("π Files")
|
| 21 |
-
|
| 22 |
-
file_upload = gr.File(label="π€ Upload File", file_types=[".pdf", ".txt", ".md", ".pptx"])
|
| 23 |
-
study_guide_output = gr.Markdown("Upload a file to generate a study guide.")
|
| 24 |
-
file_chat_output = gr.Row(visible=False)
|
| 25 |
-
|
| 26 |
-
with file_chat_output:
|
| 27 |
-
file_preview = gr.Textbox(label="π File Content Preview", lines=15, interactive=False)
|
| 28 |
-
chat_output = gr.Chatbot(label="π¬ Chat with Document")
|
| 29 |
-
|
| 30 |
-
chat_input = gr.Textbox(label="Ask something about this file")
|
| 31 |
-
generate_quiz_btn = gr.Button("π§ Generate Quiz from File")
|
| 32 |
-
quiz_container = gr.Column(visible=False)
|
| 33 |
-
quiz_score = gr.Textbox(label="Your Score", interactive=False, visible=False)
|
| 34 |
-
|
| 35 |
-
def store_file_and_generate_guide(file, current_files, section_map, text_map):
|
| 36 |
-
if file is None:
|
| 37 |
-
return current_files, section_map, text_map, "No file uploaded.", gr.update(choices=[])
|
| 38 |
-
text = extract_text_from_file(file)
|
| 39 |
-
filename = os.path.basename(file.name)
|
| 40 |
-
prompt = f"Break the content into 4β6 logical learning sections. Format: 'Section Title: Short summary'\n\n{text}"
|
| 41 |
-
response = call_llm(prompt, system_message="You are a helpful assistant for students studying documents.")
|
| 42 |
-
updated_files = current_files + [filename] if filename not in current_files else current_files
|
| 43 |
-
section_map[filename] = response
|
| 44 |
-
text_map[filename] = text
|
| 45 |
-
return updated_files, section_map, text_map, f"### {filename} Study Guide\n\n{response}", gr.update(choices=updated_files)
|
| 46 |
-
|
| 47 |
-
def load_study_guide(selected_filename, section_map):
|
| 48 |
-
if not selected_filename or selected_filename not in section_map:
|
| 49 |
-
return "No study guide available."
|
| 50 |
-
return f"### {selected_filename} Study Guide\n\n{section_map[selected_filename]}"
|
| 51 |
-
|
| 52 |
-
def open_file_viewer(selected_file, text_map):
|
| 53 |
-
content = text_map.get(selected_file, "No file content available.")
|
| 54 |
-
return gr.update(visible=True), content, []
|
| 55 |
-
|
| 56 |
-
def chat_with_file(question, selected_file, text_map, history_map):
|
| 57 |
-
if not selected_file or selected_file not in text_map:
|
| 58 |
-
return [], history_map
|
| 59 |
-
text = text_map[selected_file]
|
| 60 |
-
history = history_map.get(selected_file, [])
|
| 61 |
-
prompt = f"You are a helpful tutor. Answer this question using the document content below.\n\n{text}\n\nQuestion: {question}"
|
| 62 |
-
answer = call_llm(prompt)
|
| 63 |
-
new_history = history + [(question, answer)]
|
| 64 |
-
history_map[selected_file] = new_history
|
| 65 |
-
return new_history, history_map
|
| 66 |
-
|
| 67 |
-
def generate_quiz_ui(selected_file, text_map):
|
| 68 |
-
if not selected_file or selected_file not in text_map:
|
| 69 |
-
return gr.update(visible=True), gr.update(visible=False, value="")
|
| 70 |
-
|
| 71 |
-
text = text_map[selected_file]
|
| 72 |
-
prompt = f"Create 5 multiple-choice questions based on the content below.\nReturn as JSON list with 'question', 'options' (list), and 'answer'.\n\n{text}"
|
| 73 |
-
response = call_llm(prompt)
|
| 74 |
-
|
| 75 |
-
try:
|
| 76 |
-
questions = json.loads(response)
|
| 77 |
-
quiz_elements = []
|
| 78 |
-
score = {"correct": 0, "total": len(questions)}
|
| 79 |
-
|
| 80 |
-
for i, q in enumerate(questions):
|
| 81 |
-
question_md = gr.Markdown(f"**Q{i+1}. {q['question']}**")
|
| 82 |
-
options = gr.Radio(choices=q['options'], label=f"Your Answer to Q{i+1}", interactive=True)
|
| 83 |
-
feedback = gr.Textbox(label="Result", interactive=False)
|
| 84 |
-
|
| 85 |
-
def evaluate(ans):
|
| 86 |
-
def check(choice):
|
| 87 |
-
correct = choice == ans
|
| 88 |
-
return "β
Correct!" if correct else f"β Incorrect. Correct answer: {ans}"
|
| 89 |
-
return check
|
| 90 |
-
|
| 91 |
-
options.change(fn=evaluate(q['answer']), inputs=[options], outputs=[feedback])
|
| 92 |
-
quiz_elements.extend([question_md, options, feedback])
|
| 93 |
-
|
| 94 |
-
return gr.update(visible=True), gr.update(visible=True, value=f"Score: 0/{score['total']}")
|
| 95 |
-
|
| 96 |
-
except Exception as e:
|
| 97 |
-
return gr.update(visible=True), gr.update(visible=True, value="Error generating quiz.")
|
| 98 |
-
|
| 99 |
-
file_upload.change(
|
| 100 |
-
fn=store_file_and_generate_guide,
|
| 101 |
-
inputs=[file_upload, uploaded_files, file_sections, file_texts],
|
| 102 |
-
outputs=[uploaded_files, file_sections, file_texts, study_guide_output, file_guide_list],
|
| 103 |
-
show_progress="minimal"
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
file_guide_list.change(
|
| 107 |
-
fn=load_study_guide,
|
| 108 |
-
inputs=[file_guide_list, file_sections],
|
| 109 |
-
outputs=[study_guide_output],
|
| 110 |
-
show_progress="minimal"
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
btn_file_view.click(
|
| 114 |
-
fn=open_file_viewer,
|
| 115 |
-
inputs=[file_guide_list, file_texts],
|
| 116 |
-
outputs=[file_chat_output, file_preview, chat_output]
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
chat_input.submit(
|
| 120 |
-
fn=chat_with_file,
|
| 121 |
-
inputs=[chat_input, file_guide_list, file_texts, chat_history],
|
| 122 |
-
outputs=[chat_output, chat_history],
|
| 123 |
-
show_progress="minimal"
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
generate_quiz_btn.click(
|
| 127 |
-
fn=generate_quiz_ui,
|
| 128 |
-
inputs=[file_guide_list, file_texts],
|
| 129 |
-
outputs=[quiz_container, quiz_score],
|
| 130 |
-
show_progress="minimal"
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
return file_content
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
def file_study_guide_dashboard():
|
| 137 |
-
uploaded_files = gr.State([])
|
| 138 |
-
file_sections = gr.State({})
|
| 139 |
-
file_texts = gr.State({})
|
| 140 |
-
|
| 141 |
-
with gr.Column(scale=4, elem_id="main-column") as guide_content:
|
| 142 |
-
file_upload = gr.File(label="π€ Upload File for Study Guide", file_types=[".pdf", ".txt", ".md", ".pptx"])
|
| 143 |
-
study_guide_output = gr.Markdown("Upload a file to generate a personalized study guide.")
|
| 144 |
-
|
| 145 |
-
def generate_study_guide(file, current_files, section_map, text_map):
|
| 146 |
-
if file is None:
|
| 147 |
-
return current_files, section_map, text_map, "No file uploaded."
|
| 148 |
-
|
| 149 |
-
text = extract_text_from_file(file)
|
| 150 |
-
filename = os.path.basename(file.name)
|
| 151 |
-
|
| 152 |
-
prompt = f"Divide this document into 4-6 detailed learning sections. For each section, include a bold title and detailed explanation.\n\n{text}"
|
| 153 |
-
response = call_llm(prompt, system_message="You are an AI tutor creating study guides.")
|
| 154 |
-
|
| 155 |
-
updated_files = current_files + [filename] if filename not in current_files else current_files
|
| 156 |
-
section_map[filename] = response
|
| 157 |
-
text_map[filename] = text
|
| 158 |
-
|
| 159 |
-
return updated_files, section_map, text_map, f"### {filename} Study Guide\n\n{response}"
|
| 160 |
-
|
| 161 |
-
file_upload.change(
|
| 162 |
-
fn=generate_study_guide,
|
| 163 |
-
inputs=[file_upload, uploaded_files, file_sections, file_texts],
|
| 164 |
-
outputs=[uploaded_files, file_sections, file_texts, study_guide_output],
|
| 165 |
-
show_progress="minimal"
|
| 166 |
-
)
|
| 167 |
-
|
| 168 |
-
return guide_content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
file_study_guide_panel.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
from file_utils import (
|
| 5 |
+
extract_text_from_file,
|
| 6 |
+
call_llm,
|
| 7 |
+
save_study_guide,
|
| 8 |
+
fetch_study_guides
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
def file_study_guide_dashboard():
|
| 12 |
+
uploaded_files = gr.State([])
|
| 13 |
+
file_sections = gr.State({})
|
| 14 |
+
file_texts = gr.State({})
|
| 15 |
+
|
| 16 |
+
with gr.Column(scale=4, elem_id="main-column") as guide_content:
|
| 17 |
+
nickname_input = gr.Textbox(label="π€ Your Nickname", placeholder="Enter your name")
|
| 18 |
+
file_upload = gr.File(label="π€ Upload File for Study Guide", file_types=[".pdf", ".txt", ".md", ".pptx"])
|
| 19 |
+
study_guide_output = gr.Textbox(label="π Generated Study Guide", lines=10, interactive=False)
|
| 20 |
+
save_guide_btn = gr.Button("π Save Study Guide")
|
| 21 |
+
|
| 22 |
+
saved_guides_dropdown = gr.Dropdown(label="π Your Saved Guides", choices=[], interactive=True)
|
| 23 |
+
saved_guide_display = gr.Textbox(label="π Selected Guide Content", lines=10, interactive=False)
|
| 24 |
+
|
| 25 |
+
def generate_study_guide(file, current_files, section_map, text_map):
|
| 26 |
+
if file is None:
|
| 27 |
+
return current_files, section_map, text_map, "No file uploaded."
|
| 28 |
+
|
| 29 |
+
text = extract_text_from_file(file)
|
| 30 |
+
filename = os.path.basename(file.name)
|
| 31 |
+
|
| 32 |
+
prompt = f"Divide this document into 4β6 learning sections. For each section, include a bold title and 2β3 line explanation.\n\n{text}"
|
| 33 |
+
response = call_llm(prompt, system_message="You are an AI tutor creating study guides.")
|
| 34 |
+
|
| 35 |
+
updated_files = current_files + [filename] if filename not in current_files else current_files
|
| 36 |
+
section_map[filename] = response
|
| 37 |
+
text_map[filename] = text
|
| 38 |
+
|
| 39 |
+
return updated_files, section_map, text_map, response
|
| 40 |
+
|
| 41 |
+
def save_guide_to_db(user, uploaded_files, section_map):
|
| 42 |
+
if not uploaded_files or user.strip() == "":
|
| 43 |
+
return
|
| 44 |
+
filename = uploaded_files[-1]
|
| 45 |
+
guide = section_map.get(filename, "")
|
| 46 |
+
if guide:
|
| 47 |
+
save_study_guide(user=user, filename=filename, guide=guide)
|
| 48 |
+
|
| 49 |
+
def load_user_guides(user):
|
| 50 |
+
guides = fetch_study_guides(user)
|
| 51 |
+
return [f"{g.filename} ({g.timestamp})" for g in guides]
|
| 52 |
+
|
| 53 |
+
def show_saved_guide(user, label):
|
| 54 |
+
guides = fetch_study_guides(user)
|
| 55 |
+
for g in guides:
|
| 56 |
+
if label.startswith(g.filename):
|
| 57 |
+
return g.guide
|
| 58 |
+
return ""
|
| 59 |
+
|
| 60 |
+
file_upload.change(
|
| 61 |
+
fn=generate_study_guide,
|
| 62 |
+
inputs=[file_upload, uploaded_files, file_sections, file_texts],
|
| 63 |
+
outputs=[uploaded_files, file_sections, file_texts, study_guide_output]
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
save_guide_btn.click(
|
| 67 |
+
fn=save_guide_to_db,
|
| 68 |
+
inputs=[nickname_input, uploaded_files, file_sections],
|
| 69 |
+
outputs=[]
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
nickname_input.change(
|
| 73 |
+
fn=load_user_guides,
|
| 74 |
+
inputs=[nickname_input],
|
| 75 |
+
outputs=[saved_guides_dropdown]
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
saved_guides_dropdown.change(
|
| 79 |
+
fn=show_saved_guide,
|
| 80 |
+
inputs=[nickname_input, saved_guides_dropdown],
|
| 81 |
+
outputs=[saved_guide_display]
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
return guide_content
|