Spaces:
Runtime error
Runtime error
Commit
·
c6d3ca9
1
Parent(s):
d246a8f
upload all the files
Browse files
app.py
CHANGED
@@ -1,52 +1,66 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
examples=
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
def prompt_user_acceptance():
|
4 |
+
response = input("Do you accept the execution of remote code from the model repository? (y/N): ").strip().lower()
|
5 |
+
if response != 'y':
|
6 |
+
print("You must accept to continue.")
|
7 |
+
exit(1)
|
8 |
+
|
9 |
+
def main():
|
10 |
+
prompt_user_acceptance()
|
11 |
+
|
12 |
+
import gradio as gr
|
13 |
+
from transformers import AutoModelForCausalLM, AutoProcessor
|
14 |
+
from PIL import Image
|
15 |
+
import torch
|
16 |
+
|
17 |
+
# Load model and processor
|
18 |
+
model = AutoModelForCausalLM.from_pretrained("mynkchaudhry/Florence-2-FT-DocVQA", force_download=True)
|
19 |
+
processor = AutoProcessor.from_pretrained("mynkchaudhry/Florence-2-FT-DocVQA", force_download=True)
|
20 |
+
|
21 |
+
def generate_response(image, question):
|
22 |
+
try:
|
23 |
+
if image.mode != "RGB":
|
24 |
+
image = image.convert("RGB")
|
25 |
+
|
26 |
+
inputs = processor(text=question, images=image, return_tensors="pt")
|
27 |
+
|
28 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
29 |
+
model.to(device)
|
30 |
+
inputs = {key: value.to(device) for key, value in inputs.items()}
|
31 |
+
|
32 |
+
generated_ids = model.generate(
|
33 |
+
input_ids=inputs["input_ids"],
|
34 |
+
pixel_values=inputs["pixel_values"],
|
35 |
+
max_length=1024,
|
36 |
+
num_beams=3,
|
37 |
+
early_stopping=True
|
38 |
+
)
|
39 |
+
|
40 |
+
response = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
41 |
+
return response
|
42 |
+
except Exception as e:
|
43 |
+
return f"Error processing image: {e}"
|
44 |
+
|
45 |
+
# Example images for demonstration (update paths as needed)
|
46 |
+
examples = [
|
47 |
+
["demo.png", "what is the address in the page?"],
|
48 |
+
["demo2.jpg", "what is the date in the page?"],
|
49 |
+
["demo.png", "what is the name in the page?"]
|
50 |
+
]
|
51 |
+
|
52 |
+
# Gradio interface
|
53 |
+
iface = gr.Interface(
|
54 |
+
fn=generate_response,
|
55 |
+
inputs=[gr.Image(type="pil"), gr.Textbox(label="Question")],
|
56 |
+
outputs=gr.Textbox(label="Response"),
|
57 |
+
examples=examples,
|
58 |
+
title="Image to Text Extractor",
|
59 |
+
description="Upload an image and provide a question. This tool will extract the relevant information from the image based on your question."
|
60 |
+
)
|
61 |
+
|
62 |
+
# Launch the interface
|
63 |
+
iface.launch()
|
64 |
+
|
65 |
+
if __name__ == "__main__":
|
66 |
+
main()
|