Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,93 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
+
import tempfile
|
4 |
+
import shutil
|
5 |
+
import pandas as pd
|
6 |
+
from PIL import Image
|
7 |
+
from preprocess import convert_pdf_to_images, preprocess_image
|
8 |
+
from llm_utils import load_image, generate_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Global temporary directory for image processing
|
11 |
+
temp_dir = tempfile.mkdtemp()
|
12 |
+
|
13 |
+
pdf_image_map = {} # Map from PDF to its image list
|
14 |
+
image_preview_map = {} # Map from image name to full path
|
15 |
+
|
16 |
+
|
17 |
+
def extract_images_from_pdfs(pdf_files):
|
18 |
+
global pdf_image_map, image_preview_map
|
19 |
+
pdf_image_map.clear()
|
20 |
+
image_preview_map.clear()
|
21 |
+
|
22 |
+
previews = []
|
23 |
+
|
24 |
+
for pdf in pdf_files:
|
25 |
+
image_paths = convert_pdf_to_images(pdf.name, temp_dir)
|
26 |
+
pdf_image_map[pdf.name] = image_paths
|
27 |
+
|
28 |
+
for img_path in image_paths:
|
29 |
+
img_name = os.path.basename(img_path)
|
30 |
+
image_preview_map[img_name] = img_path
|
31 |
+
previews.append((img_name, img_path))
|
32 |
+
|
33 |
+
# Return preview (tuples: filename, image_path)
|
34 |
+
return [img for _, img in previews]
|
35 |
+
|
36 |
+
|
37 |
+
def process_selected_images(selected_images, output_excel_name):
|
38 |
+
results = []
|
39 |
+
|
40 |
+
for img_name in selected_images:
|
41 |
+
img_path = image_preview_map.get(img_name)
|
42 |
+
if img_path is None:
|
43 |
+
continue
|
44 |
+
|
45 |
+
processed = preprocess_image(img_path)
|
46 |
+
processed_path = os.path.join(temp_dir, f"processed_{img_name}")
|
47 |
+
Image.fromarray(processed).save(processed_path)
|
48 |
+
|
49 |
+
# Run LLM
|
50 |
+
pixel_values = load_image(processed_path)
|
51 |
+
response = generate_response(pixel_values)
|
52 |
+
|
53 |
+
# Clean filename to find source pdf
|
54 |
+
base_name = img_name.rsplit("_page_", 1)[0] + ".pdf"
|
55 |
+
results.append({"Source PDF": base_name, "Page Image": img_name, "LLM Output": response})
|
56 |
+
|
57 |
+
df = pd.DataFrame(results)
|
58 |
+
output_excel = os.path.join(temp_dir, output_excel_name)
|
59 |
+
df.to_excel(output_excel, index=False)
|
60 |
+
return output_excel
|
61 |
+
|
62 |
+
|
63 |
+
def reset_all():
|
64 |
+
shutil.rmtree(temp_dir)
|
65 |
+
os.makedirs(temp_dir, exist_ok=True)
|
66 |
+
|
67 |
+
|
68 |
+
with gr.Blocks() as demo:
|
69 |
+
gr.Markdown("## π§ PDF β Image β LLM β Excel Output")
|
70 |
+
|
71 |
+
with gr.Row():
|
72 |
+
pdf_input = gr.File(file_types=[".pdf"], file_count="multiple", label="π Upload multiple PDF files")
|
73 |
+
extract_btn = gr.Button("π Extract Images")
|
74 |
+
|
75 |
+
gallery = gr.Gallery(label="πΌοΈ Choose images to process", columns=4, allow_preview=True, interactive=True, show_label=True).style(grid=4)
|
76 |
+
selected_images = gr.CheckboxGroup(choices=[], label="Select image filenames for processing")
|
77 |
+
|
78 |
+
with gr.Row():
|
79 |
+
output_name = gr.Textbox(label="π Output Excel filename", value="output.xlsx")
|
80 |
+
generate_btn = gr.Button("π Generate Excel")
|
81 |
+
|
82 |
+
excel_output = gr.File(label="π₯ Download Excel")
|
83 |
+
|
84 |
+
def update_gallery(pdf_files):
|
85 |
+
previews = extract_images_from_pdfs(pdf_files)
|
86 |
+
choices = list(image_preview_map.keys())
|
87 |
+
return gr.update(value=previews), gr.update(choices=choices, value=[])
|
88 |
+
|
89 |
+
extract_btn.click(update_gallery, inputs=[pdf_input], outputs=[gallery, selected_images])
|
90 |
+
generate_btn.click(fn=process_selected_images, inputs=[selected_images, output_name], outputs=[excel_output])
|
91 |
|
92 |
if __name__ == "__main__":
|
93 |
demo.launch()
|