Update file_viewer_panel.py
Browse files- file_viewer_panel.py +51 -35
file_viewer_panel.py
CHANGED
|
@@ -1,53 +1,69 @@
|
|
| 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.
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
file_text_state = gr.State("")
|
| 16 |
|
| 17 |
-
def
|
| 18 |
-
if not file:
|
|
|
|
|
|
|
|
|
|
| 19 |
text = extract_text_from_file(file)
|
| 20 |
-
return text, text
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def generate_quiz(text):
|
| 27 |
-
prompt =
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
q_radio = gr.Radio(q["options"], label="Select an answer")
|
| 37 |
-
q_feedback = gr.Textbox(label="Result", interactive=False)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
elements.extend([q_md, q_radio, q_feedback])
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
return gr.update(visible=False), [], gr.update(visible=True, value="Error generating quiz.")
|
| 48 |
|
| 49 |
-
file_upload.change(fn=
|
| 50 |
-
chat_input.submit(fn=
|
| 51 |
-
|
| 52 |
|
| 53 |
-
return
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from file_utils import extract_text_from_file, call_llm
|
| 3 |
+
import os, json
|
| 4 |
|
| 5 |
def file_viewer_dashboard():
|
| 6 |
+
with gr.Row():
|
| 7 |
+
with gr.Column(scale=3): # Main viewer
|
| 8 |
+
file_upload = gr.File(label="π Upload a File", file_types=[".pdf", ".txt", ".md", ".pptx"])
|
| 9 |
+
file_viewer = gr.HTML(label="π File Preview")
|
| 10 |
+
|
| 11 |
+
with gr.Column(scale=2): # Right: Chat + Quiz
|
| 12 |
+
generate_quiz_btn = gr.Button("π§ Practice Questions")
|
| 13 |
+
quiz_container = gr.Column(visible=False)
|
| 14 |
+
chat_output = gr.Chatbot(label="Ask a question to learn more about the file")
|
| 15 |
+
chat_input = gr.Textbox(label="", placeholder="Ask ChatEDU a question...")
|
| 16 |
|
| 17 |
file_text_state = gr.State("")
|
| 18 |
|
| 19 |
+
def render_file(file):
|
| 20 |
+
if not file:
|
| 21 |
+
return "", ""
|
| 22 |
+
ext = os.path.splitext(file.name)[1].lower()
|
| 23 |
+
file_path = file.name
|
| 24 |
text = extract_text_from_file(file)
|
|
|
|
| 25 |
|
| 26 |
+
if ext == ".pdf":
|
| 27 |
+
viewer_html = f"<iframe src='/file={file_path}' width='100%' height='700px' style='border:none; border-radius:8px;'></iframe>"
|
| 28 |
+
elif ext in [".txt", ".md", ".pptx"]:
|
| 29 |
+
viewer_html = f"<pre style='padding:12px; background:#f9f9f9;'>{text}</pre>"
|
| 30 |
+
else:
|
| 31 |
+
viewer_html = "<p>Unsupported file type.</p>"
|
| 32 |
+
|
| 33 |
+
return viewer_html, text
|
| 34 |
+
|
| 35 |
+
def ask_question(q, text):
|
| 36 |
+
prompt = f"Answer the following question based only on this document:\n\n{text}\n\nQuestion: {q}"
|
| 37 |
+
return [(q, call_llm(prompt))]
|
| 38 |
|
| 39 |
def generate_quiz(text):
|
| 40 |
+
prompt = (
|
| 41 |
+
"Generate 5 multiple choice questions from this document. "
|
| 42 |
+
"Return a JSON array of questions, each with:\n- question\n- options (list)\n- answer (string)"
|
| 43 |
+
)
|
| 44 |
try:
|
| 45 |
+
result = call_llm(prompt)
|
| 46 |
+
questions = json.loads(result)
|
| 47 |
+
|
| 48 |
+
components = []
|
| 49 |
+
for idx, q in enumerate(questions):
|
| 50 |
+
q_md = gr.Markdown(f"**Q{idx+1}. {q['question']}**")
|
| 51 |
+
radio = gr.Radio(choices=q["options"], label="Choose an answer")
|
| 52 |
+
feedback = gr.Textbox(interactive=False)
|
| 53 |
|
| 54 |
+
def check(ans, correct=q["answer"]):
|
| 55 |
+
return "β
Correct!" if ans == correct else f"β Correct answer: {correct}"
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
radio.change(fn=check, inputs=[radio], outputs=[feedback])
|
| 58 |
+
components.extend([q_md, radio, feedback])
|
| 59 |
|
| 60 |
+
return gr.update(visible=True), components
|
|
|
|
| 61 |
|
| 62 |
+
except Exception as e:
|
| 63 |
+
return gr.update(visible=False), []
|
|
|
|
| 64 |
|
| 65 |
+
file_upload.change(fn=render_file, inputs=[file_upload], outputs=[file_viewer, file_text_state])
|
| 66 |
+
chat_input.submit(fn=ask_question, inputs=[chat_input, file_text_state], outputs=[chat_output])
|
| 67 |
+
generate_quiz_btn.click(fn=generate_quiz, inputs=[file_text_state], outputs=[quiz_container])
|
| 68 |
|
| 69 |
+
return file_viewer_dashboard
|