Create file_viewer_panel.py
Browse files- file_viewer_panel.py +53 -0
file_viewer_panel.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
from file_utils import extract_text_from_file, call_llm
|
| 5 |
+
|
| 6 |
+
def file_viewer_dashboard():
|
| 7 |
+
with gr.Column(scale=4, elem_id="main-column") as file_panel:
|
| 8 |
+
file_upload = gr.File(label="π Upload a File", file_types=[".pdf", ".txt", ".md", ".pptx"])
|
| 9 |
+
preview = gr.Textbox(label="π File Content Preview", lines=10, interactive=False)
|
| 10 |
+
chat_input = gr.Textbox(label="π¬ Ask about the file")
|
| 11 |
+
chat_output = gr.Chatbot()
|
| 12 |
+
quiz_box = gr.Column(visible=False)
|
| 13 |
+
quiz_score = gr.Textbox(label="Your Score", interactive=False, visible=False)
|
| 14 |
+
|
| 15 |
+
file_text_state = gr.State("")
|
| 16 |
+
|
| 17 |
+
def load_and_preview(file):
|
| 18 |
+
if not file: return "", ""
|
| 19 |
+
text = extract_text_from_file(file)
|
| 20 |
+
return text, text
|
| 21 |
+
|
| 22 |
+
def handle_chat(query, text):
|
| 23 |
+
prompt = f"Using the document below, answer this question:\n\n{text}\n\nQuestion: {query}"
|
| 24 |
+
return [(query, call_llm(prompt))]
|
| 25 |
+
|
| 26 |
+
def generate_quiz(text):
|
| 27 |
+
prompt = f"Create 5 multiple-choice questions from this text. Return as JSON list."
|
| 28 |
+
response = call_llm(prompt)
|
| 29 |
+
try:
|
| 30 |
+
questions = json.loads(response)
|
| 31 |
+
score_state = {"score": 0, "total": len(questions)}
|
| 32 |
+
elements = []
|
| 33 |
+
|
| 34 |
+
for i, q in enumerate(questions):
|
| 35 |
+
q_md = gr.Markdown(f"**Q{i+1}. {q['question']}**")
|
| 36 |
+
q_radio = gr.Radio(q["options"], label="Select an answer")
|
| 37 |
+
q_feedback = gr.Textbox(label="Result", interactive=False)
|
| 38 |
+
|
| 39 |
+
def grade(choice, correct=q["answer"]):
|
| 40 |
+
return "β
Correct!" if choice == correct else f"β Correct: {correct}"
|
| 41 |
+
|
| 42 |
+
q_radio.change(fn=grade, inputs=[q_radio], outputs=[q_feedback])
|
| 43 |
+
elements.extend([q_md, q_radio, q_feedback])
|
| 44 |
+
|
| 45 |
+
return gr.update(visible=True), elements, gr.update(visible=True, value=f"Score: 0/{score_state['total']}")
|
| 46 |
+
except:
|
| 47 |
+
return gr.update(visible=False), [], gr.update(visible=True, value="Error generating quiz.")
|
| 48 |
+
|
| 49 |
+
file_upload.change(fn=load_and_preview, inputs=[file_upload], outputs=[preview, file_text_state])
|
| 50 |
+
chat_input.submit(fn=handle_chat, inputs=[chat_input, file_text_state], outputs=[chat_output])
|
| 51 |
+
gr.Button("π§ Generate Quiz").click(fn=generate_quiz, inputs=[file_text_state], outputs=[quiz_box, quiz_box, quiz_score])
|
| 52 |
+
|
| 53 |
+
return file_panel
|