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