Update file_viewer_panel.py
Browse files- file_viewer_panel.py +33 -20
file_viewer_panel.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import shutil
|
| 4 |
import json
|
| 5 |
from pdf2image import convert_from_path
|
| 6 |
from file_utils import extract_text_from_file, call_llm
|
|
@@ -15,40 +14,53 @@ def file_viewer_dashboard():
|
|
| 15 |
generate_quiz_btn = gr.Button("🧠 Practice Questions")
|
| 16 |
quiz_container = gr.Column(visible=False)
|
| 17 |
chat_output = gr.Chatbot(label="Ask a question to learn more about the file")
|
| 18 |
-
chat_input = gr.Textbox(label="", placeholder="Ask
|
| 19 |
|
| 20 |
file_text_state = gr.State("")
|
|
|
|
| 21 |
|
| 22 |
def render_file(file):
|
| 23 |
if not file:
|
| 24 |
-
return "", ""
|
| 25 |
|
| 26 |
ext = os.path.splitext(file.name)[1].lower()
|
| 27 |
text = extract_text_from_file(file)
|
| 28 |
|
| 29 |
if ext == ".pdf":
|
| 30 |
-
# Convert to images
|
| 31 |
output_dir = "/tmp/pdf_images"
|
| 32 |
os.makedirs(output_dir, exist_ok=True)
|
| 33 |
images = convert_from_path(file.name, dpi=150, fmt="png", output_folder=output_dir)
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
for i, img in enumerate(images):
|
| 37 |
-
|
| 38 |
-
img.save(img_path, "PNG")
|
| 39 |
-
img_tags.append(f"<img src='/file={img_path}' style='width:100%; margin-bottom: 10px; border-radius: 8px;'/>")
|
| 40 |
-
|
| 41 |
viewer_html = "".join(img_tags)
|
| 42 |
-
|
| 43 |
else:
|
| 44 |
-
# Fallback for text/markdown/pptx
|
| 45 |
viewer_html = f"<pre style='padding:12px; background:#f9f9f9; white-space:pre-wrap;'>{text}</pre>"
|
| 46 |
|
| 47 |
-
return viewer_html, text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
return [(q, call_llm(prompt))]
|
| 52 |
|
| 53 |
def generate_quiz(text):
|
| 54 |
prompt = (
|
|
@@ -56,7 +68,7 @@ def file_viewer_dashboard():
|
|
| 56 |
"Return a JSON list where each item has:\n- question\n- options (list)\n- answer (correct one)"
|
| 57 |
)
|
| 58 |
try:
|
| 59 |
-
result = call_llm(prompt)
|
| 60 |
questions = json.loads(result)
|
| 61 |
|
| 62 |
components = []
|
|
@@ -72,11 +84,12 @@ def file_viewer_dashboard():
|
|
| 72 |
components.extend([q_md, radio, feedback])
|
| 73 |
|
| 74 |
return gr.update(visible=True), components
|
| 75 |
-
except
|
| 76 |
return gr.update(visible=False), []
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
| 80 |
generate_quiz_btn.click(fn=generate_quiz, inputs=[file_text_state], outputs=[quiz_container])
|
| 81 |
|
| 82 |
return file_panel
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
| 3 |
import json
|
| 4 |
from pdf2image import convert_from_path
|
| 5 |
from file_utils import extract_text_from_file, call_llm
|
|
|
|
| 14 |
generate_quiz_btn = gr.Button("🧠 Practice Questions")
|
| 15 |
quiz_container = gr.Column(visible=False)
|
| 16 |
chat_output = gr.Chatbot(label="Ask a question to learn more about the file")
|
| 17 |
+
chat_input = gr.Textbox(label="", placeholder="Ask ChatEDU a question...")
|
| 18 |
|
| 19 |
file_text_state = gr.State("")
|
| 20 |
+
chat_state = gr.State([])
|
| 21 |
|
| 22 |
def render_file(file):
|
| 23 |
if not file:
|
| 24 |
+
return "", "", []
|
| 25 |
|
| 26 |
ext = os.path.splitext(file.name)[1].lower()
|
| 27 |
text = extract_text_from_file(file)
|
| 28 |
|
| 29 |
if ext == ".pdf":
|
|
|
|
| 30 |
output_dir = "/tmp/pdf_images"
|
| 31 |
os.makedirs(output_dir, exist_ok=True)
|
| 32 |
images = convert_from_path(file.name, dpi=150, fmt="png", output_folder=output_dir)
|
| 33 |
+
img_tags = [
|
| 34 |
+
f"<img src='/file={os.path.join(output_dir, f'page_{i}.png')}' style='width:100%; margin-bottom: 10px; border-radius: 8px;'/>"
|
| 35 |
+
for i in range(len(images))
|
| 36 |
+
]
|
| 37 |
for i, img in enumerate(images):
|
| 38 |
+
img.save(os.path.join(output_dir, f"page_{i}.png"), "PNG")
|
|
|
|
|
|
|
|
|
|
| 39 |
viewer_html = "".join(img_tags)
|
|
|
|
| 40 |
else:
|
|
|
|
| 41 |
viewer_html = f"<pre style='padding:12px; background:#f9f9f9; white-space:pre-wrap;'>{text}</pre>"
|
| 42 |
|
| 43 |
+
return viewer_html, text, [] # Reset chat on new file
|
| 44 |
+
|
| 45 |
+
def ask_question_with_context(user_input, text, history):
|
| 46 |
+
if not user_input.strip():
|
| 47 |
+
return history, history
|
| 48 |
+
|
| 49 |
+
messages = [{"role": "system", "content": "You are a helpful tutor. Answer only using the content provided."}]
|
| 50 |
+
if len(text) > 8000:
|
| 51 |
+
text = text[:8000] + "\n\n[Truncated due to token limits]"
|
| 52 |
+
|
| 53 |
+
messages.append({"role": "user", "content": f"Document Content:\n{text}"})
|
| 54 |
+
|
| 55 |
+
for user_msg, bot_reply in history:
|
| 56 |
+
messages.append({"role": "user", "content": user_msg})
|
| 57 |
+
messages.append({"role": "assistant", "content": bot_reply})
|
| 58 |
+
|
| 59 |
+
messages.append({"role": "user", "content": user_input})
|
| 60 |
+
response = call_llm(messages=messages)
|
| 61 |
|
| 62 |
+
history.append((user_input, response))
|
| 63 |
+
return history, history
|
|
|
|
| 64 |
|
| 65 |
def generate_quiz(text):
|
| 66 |
prompt = (
|
|
|
|
| 68 |
"Return a JSON list where each item has:\n- question\n- options (list)\n- answer (correct one)"
|
| 69 |
)
|
| 70 |
try:
|
| 71 |
+
result = call_llm(prompt=prompt)
|
| 72 |
questions = json.loads(result)
|
| 73 |
|
| 74 |
components = []
|
|
|
|
| 84 |
components.extend([q_md, radio, feedback])
|
| 85 |
|
| 86 |
return gr.update(visible=True), components
|
| 87 |
+
except:
|
| 88 |
return gr.update(visible=False), []
|
| 89 |
|
| 90 |
+
# Hook up logic
|
| 91 |
+
file_upload.change(fn=render_file, inputs=[file_upload], outputs=[file_viewer, file_text_state, chat_output])
|
| 92 |
+
chat_input.submit(fn=ask_question_with_context, inputs=[chat_input, file_text_state, chat_state], outputs=[chat_output, chat_state])
|
| 93 |
generate_quiz_btn.click(fn=generate_quiz, inputs=[file_text_state], outputs=[quiz_container])
|
| 94 |
|
| 95 |
return file_panel
|