Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,41 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
from gradio_client import Client # ๊ฐ์ : gradio_client ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# ์ด๋ฏธ์ง ์ธ์ ํ์ดํ๋ผ์ธ ๋ก๋
|
6 |
image_model = pipeline("image-classification", model="google/vit-base-patch16-224")
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
from gradio_client import Client # ๊ฐ์ : gradio_client ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
|
4 |
+
import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
|
5 |
+
import torch
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
9 |
+
feature_extractor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
11 |
+
|
12 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
+
model.to(device)
|
14 |
+
|
15 |
+
max_length = 16
|
16 |
+
num_beams = 4
|
17 |
+
gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
|
18 |
+
def predict_step(image_paths):
|
19 |
+
images = []
|
20 |
+
for image_path in image_paths:
|
21 |
+
i_image = Image.open(image_path)
|
22 |
+
if i_image.mode != "RGB":
|
23 |
+
i_image = i_image.convert(mode="RGB")
|
24 |
+
|
25 |
+
images.append(i_image)
|
26 |
+
|
27 |
+
pixel_values = feature_extractor(images=images, return_tensors="pt").pixel_values
|
28 |
+
pixel_values = pixel_values.to(device)
|
29 |
+
|
30 |
+
output_ids = model.generate(pixel_values, **gen_kwargs)
|
31 |
+
|
32 |
+
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
33 |
+
preds = [pred.strip() for pred in preds]
|
34 |
+
return preds
|
35 |
+
|
36 |
+
|
37 |
+
predict_step(pipeline)
|
38 |
+
|
39 |
|
40 |
# ์ด๋ฏธ์ง ์ธ์ ํ์ดํ๋ผ์ธ ๋ก๋
|
41 |
image_model = pipeline("image-classification", model="google/vit-base-patch16-224")
|