Update file_viewer_panel.py
Browse files- file_viewer_panel.py +22 -9
file_viewer_panel.py
CHANGED
|
@@ -2,6 +2,8 @@ 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, save_user_file, fetch_user_files
|
| 7 |
|
|
@@ -23,20 +25,31 @@ def file_viewer_dashboard(nickname):
|
|
| 23 |
|
| 24 |
file_text_state = gr.State("")
|
| 25 |
chat_state = gr.State([])
|
|
|
|
| 26 |
|
| 27 |
def render_file_as_html(filename, text):
|
| 28 |
ext = os.path.splitext(filename)[1].lower()
|
|
|
|
| 29 |
if ext == ".pdf":
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
for i, img in enumerate(images):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def handle_file_upload(file, nickname):
|
| 42 |
if not file or not nickname:
|
|
|
|
| 2 |
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 |
|
|
|
|
| 25 |
|
| 26 |
file_text_state = gr.State("")
|
| 27 |
chat_state = gr.State([])
|
| 28 |
+
|
| 29 |
|
| 30 |
def render_file_as_html(filename, text):
|
| 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:
|