Update file_viewer_panel.py
Browse files- file_viewer_panel.py +58 -48
file_viewer_panel.py
CHANGED
|
@@ -3,19 +3,17 @@ import os
|
|
| 3 |
import shutil
|
| 4 |
import json
|
| 5 |
import tempfile
|
| 6 |
-
from gradio import processing_utils
|
| 7 |
from pdf2image import convert_from_path
|
|
|
|
| 8 |
from file_utils import extract_text_from_file, call_llm, save_user_file, fetch_user_files
|
| 9 |
|
| 10 |
def file_viewer_dashboard(nickname):
|
| 11 |
with gr.Column(scale=4, elem_id="main-column") as file_panel:
|
| 12 |
-
nickname_input = nickname
|
| 13 |
-
|
| 14 |
with gr.Row():
|
| 15 |
with gr.Column(scale=3):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
with gr.Column(scale=2):
|
| 21 |
generate_quiz_btn = gr.Button("π§ Practice Questions")
|
|
@@ -25,54 +23,60 @@ def file_viewer_dashboard(nickname):
|
|
| 25 |
|
| 26 |
file_text_state = gr.State("")
|
| 27 |
chat_state = gr.State([])
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
def
|
| 31 |
-
ext = os.path.splitext(filename)[1].lower()
|
| 32 |
-
|
| 33 |
-
if ext == ".pdf":
|
| 34 |
-
from pdf2image import convert_from_path
|
| 35 |
-
|
| 36 |
-
temp_dir = tempfile.mkdtemp()
|
| 37 |
-
images = convert_from_path(filename, dpi=150, fmt="png", output_folder=temp_dir)
|
| 38 |
-
|
| 39 |
-
img_tags = []
|
| 40 |
-
for i, img in enumerate(images):
|
| 41 |
-
img_path = os.path.join(temp_dir, f"page_{i}.png")
|
| 42 |
-
img.save(img_path, "PNG")
|
| 43 |
-
|
| 44 |
-
# Copy to Gradio's accessible location and get file URL
|
| 45 |
-
gradio_img_path = processing_utils.save_temp_file(img_path)
|
| 46 |
-
img_tags.append(f"<img src='file={gradio_img_path}' style='width:100%; margin-bottom:10px;'/>")
|
| 47 |
-
|
| 48 |
-
return "".join(img_tags)
|
| 49 |
-
|
| 50 |
-
else:
|
| 51 |
-
return f"<pre style='padding:12px; background:#f9f9f9; white-space:pre-wrap;'>{text}</pre>"
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
def handle_file_upload(file, nickname):
|
| 55 |
if not file or not nickname:
|
| 56 |
-
return "",
|
| 57 |
|
| 58 |
filename = os.path.basename(file.name)
|
| 59 |
saved_path = f"/tmp/{filename}"
|
| 60 |
shutil.copy(file.name, saved_path)
|
| 61 |
-
|
| 62 |
save_user_file(nickname, filename, saved_path)
|
| 63 |
-
text = extract_text_from_file(file)
|
| 64 |
-
html = render_file_as_html(file.name, text)
|
| 65 |
|
| 66 |
-
|
| 67 |
-
file_choices = [f"{f.filename} ({f.timestamp})" for f in user_files]
|
| 68 |
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
def ask_question(user_input, text, history):
|
| 72 |
if not user_input.strip():
|
| 73 |
return history, history
|
| 74 |
-
messages = [
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
for user_msg, bot_reply in history:
|
| 77 |
messages.append({"role": "user", "content": user_msg})
|
| 78 |
messages.append({"role": "assistant", "content": bot_reply})
|
|
@@ -104,15 +108,21 @@ def file_viewer_dashboard(nickname):
|
|
| 104 |
except:
|
| 105 |
return gr.update(visible=False), []
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
nickname_input.change(fn=load_user_files, inputs=[nickname_input], outputs=[user_file_list])
|
| 112 |
-
file_upload.change(fn=handle_file_upload, inputs=[file_upload, nickname_input],
|
| 113 |
-
outputs=[file_viewer, file_text_state, user_file_list, chat_output])
|
| 114 |
chat_input.submit(fn=ask_question, inputs=[chat_input, file_text_state, chat_state],
|
| 115 |
outputs=[chat_output, chat_state])
|
|
|
|
| 116 |
generate_quiz_btn.click(fn=generate_quiz, inputs=[file_text_state], outputs=[quiz_container])
|
| 117 |
|
| 118 |
-
return file_panel
|
|
|
|
| 3 |
import shutil
|
| 4 |
import json
|
| 5 |
import tempfile
|
|
|
|
| 6 |
from pdf2image import convert_from_path
|
| 7 |
+
from gradio.processing_utils import save_temp_file
|
| 8 |
from file_utils import extract_text_from_file, call_llm, save_user_file, fetch_user_files
|
| 9 |
|
| 10 |
def file_viewer_dashboard(nickname):
|
| 11 |
with gr.Column(scale=4, elem_id="main-column") as file_panel:
|
|
|
|
|
|
|
| 12 |
with gr.Row():
|
| 13 |
with gr.Column(scale=3):
|
| 14 |
+
file_upload = gr.File(label="π Upload a File", file_types=[".pdf", ".txt", ".md", ".pptx"])
|
| 15 |
+
image_viewer = gr.HTML(label="π PDF Viewer")
|
| 16 |
+
load_more_btn = gr.Button("β Load More Pages", visible=False)
|
| 17 |
|
| 18 |
with gr.Column(scale=2):
|
| 19 |
generate_quiz_btn = gr.Button("π§ Practice Questions")
|
|
|
|
| 23 |
|
| 24 |
file_text_state = gr.State("")
|
| 25 |
chat_state = gr.State([])
|
| 26 |
+
all_image_paths = gr.State([])
|
| 27 |
+
loaded_page_count = gr.State(0)
|
| 28 |
|
| 29 |
+
def prepare_pdf_images(file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
if not file or not nickname:
|
| 31 |
+
return "", [], 0, gr.update(visible=False)
|
| 32 |
|
| 33 |
filename = os.path.basename(file.name)
|
| 34 |
saved_path = f"/tmp/{filename}"
|
| 35 |
shutil.copy(file.name, saved_path)
|
|
|
|
| 36 |
save_user_file(nickname, filename, saved_path)
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
text = extract_text_from_file(file)
|
|
|
|
| 39 |
|
| 40 |
+
ext = os.path.splitext(filename)[1].lower()
|
| 41 |
+
if ext != ".pdf":
|
| 42 |
+
html = f"<pre style='padding:12px; background:#f9f9f9; white-space:pre-wrap;'>{text}</pre>"
|
| 43 |
+
return html, [], 0, gr.update(visible=False)
|
| 44 |
+
|
| 45 |
+
temp_dir = tempfile.mkdtemp()
|
| 46 |
+
images = convert_from_path(saved_path, dpi=150, fmt="png", output_folder=temp_dir)
|
| 47 |
+
|
| 48 |
+
paths = []
|
| 49 |
+
for i, img in enumerate(images):
|
| 50 |
+
img_path = os.path.join(temp_dir, f"page_{i}.png")
|
| 51 |
+
img.save(img_path, "PNG")
|
| 52 |
+
gr_img_path = save_temp_file(img_path)
|
| 53 |
+
paths.append(gr_img_path)
|
| 54 |
+
|
| 55 |
+
initial_html = "".join([
|
| 56 |
+
f"<img src='file={paths[i]}' style='width:100%; margin-bottom:12px; border-radius:8px;'/>"
|
| 57 |
+
for i in range(min(3, len(paths)))
|
| 58 |
+
])
|
| 59 |
+
return initial_html, paths, min(3, len(paths)), gr.update(visible=(len(paths) > 3))
|
| 60 |
+
|
| 61 |
+
def load_more_pages(paths, current_count):
|
| 62 |
+
next_count = current_count + 3
|
| 63 |
+
total = len(paths)
|
| 64 |
+
|
| 65 |
+
new_html = "".join([
|
| 66 |
+
f"<img src='file={paths[i]}' style='width:100%; margin-bottom:12px; border-radius:8px;'/>"
|
| 67 |
+
for i in range(current_count, min(next_count, total))
|
| 68 |
+
])
|
| 69 |
+
|
| 70 |
+
show_more = next_count < total
|
| 71 |
+
return gr.update(value=new_html, append=True), next_count, gr.update(visible=show_more)
|
| 72 |
|
| 73 |
def ask_question(user_input, text, history):
|
| 74 |
if not user_input.strip():
|
| 75 |
return history, history
|
| 76 |
+
messages = [
|
| 77 |
+
{"role": "system", "content": "You are a tutor. Only use the document below."},
|
| 78 |
+
{"role": "user", "content": f"Document:\n{text}"}
|
| 79 |
+
]
|
| 80 |
for user_msg, bot_reply in history:
|
| 81 |
messages.append({"role": "user", "content": user_msg})
|
| 82 |
messages.append({"role": "assistant", "content": bot_reply})
|
|
|
|
| 108 |
except:
|
| 109 |
return gr.update(visible=False), []
|
| 110 |
|
| 111 |
+
file_upload.change(
|
| 112 |
+
fn=prepare_pdf_images,
|
| 113 |
+
inputs=[file_upload],
|
| 114 |
+
outputs=[image_viewer, all_image_paths, loaded_page_count, load_more_btn]
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
load_more_btn.click(
|
| 118 |
+
fn=load_more_pages,
|
| 119 |
+
inputs=[all_image_paths, loaded_page_count],
|
| 120 |
+
outputs=[image_viewer, loaded_page_count, load_more_btn]
|
| 121 |
+
)
|
| 122 |
|
|
|
|
|
|
|
|
|
|
| 123 |
chat_input.submit(fn=ask_question, inputs=[chat_input, file_text_state, chat_state],
|
| 124 |
outputs=[chat_output, chat_state])
|
| 125 |
+
|
| 126 |
generate_quiz_btn.click(fn=generate_quiz, inputs=[file_text_state], outputs=[quiz_container])
|
| 127 |
|
| 128 |
+
return file_panel
|