Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,20 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
title = "Document Image Transformer"
|
4 |
description = "Gradio Demo for DiT, the Document Image Transformer pre-trained on IIT-CDIP, a dataset that includes 42 million document images and fine-tuned on RVL-CDIP, a dataset consisting of 400,000 grayscale images in 16 classes, with 25,000 images per class. To use it, simply add your image, or click one of the examples to load them. Read more at the links below."
|
@@ -9,9 +25,4 @@ examples = [
|
|
9 |
["coca_cola_advertisement.png"]
|
10 |
]
|
11 |
|
12 |
-
gr.Interface.
|
13 |
-
title=title,
|
14 |
-
description=description,
|
15 |
-
article=article,
|
16 |
-
examples=examples,
|
17 |
-
enable_queue=True).launch()
|
|
|
1 |
+
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
|
2 |
import gradio as gr
|
3 |
+
import torch
|
4 |
+
|
5 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained("microsoft/beit-base-finetuned-rvlcdip")
|
6 |
+
model = AutoModelForImageClassification.from_pretrained("microsoft/beit-base-finetuned-rvlcdip")
|
7 |
+
|
8 |
+
def classify_image(image):
|
9 |
+
pixel_values = feature_extractor(image, return_tensors="pt")
|
10 |
+
|
11 |
+
with torch.no_grad():
|
12 |
+
outputs = model(pixel_values)
|
13 |
+
logits = outputs.logits
|
14 |
+
|
15 |
+
predicted_class = model.config.id2label[logits.argmax(-1).item()]
|
16 |
+
|
17 |
+
return predicted_class
|
18 |
|
19 |
title = "Document Image Transformer"
|
20 |
description = "Gradio Demo for DiT, the Document Image Transformer pre-trained on IIT-CDIP, a dataset that includes 42 million document images and fine-tuned on RVL-CDIP, a dataset consisting of 400,000 grayscale images in 16 classes, with 25,000 images per class. To use it, simply add your image, or click one of the examples to load them. Read more at the links below."
|
|
|
25 |
["coca_cola_advertisement.png"]
|
26 |
]
|
27 |
|
28 |
+
gr.Interface(fn=classify_image, inputs=image, outputs=label, title=title, description=description, examples=examples, enable_queue=True).launch(debug=True)
|
|
|
|
|
|
|
|
|
|