Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,37 +4,25 @@ from gradio_client import Client # κ°μ : gradio_client λΌμ΄λΈλ¬λ¦¬κ° μ¬
|
|
4 |
import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
|
5 |
import torch
|
6 |
from PIL import Image
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
11 |
|
12 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
model.to(device)
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
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 |
-
|
26 |
-
|
27 |
-
|
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 |
# μ΄λ―Έμ§ μΈμ νμ΄νλΌμΈ λ‘λ
|
@@ -81,13 +69,13 @@ def classify_and_generate_voice(uploaded_image):
|
|
81 |
|
82 |
# λ°νλ μμ± λ° μμ
κ²°κ³Όλ₯Ό Gradio μΈν°νμ΄μ€λ‘ μ λ¬
|
83 |
# μ: voice_result['url'] λλ voice_result['audio_data'] λ±
|
84 |
-
return top_prediction, voice_result, music_result
|
85 |
|
86 |
# Gradio μΈν°νμ΄μ€ μμ±
|
87 |
iface = gr.Interface(
|
88 |
fn=classify_and_generate_voice,
|
89 |
inputs=gr.Image(type="pil"),
|
90 |
-
outputs=[gr.Label(), gr.Audio(), gr.Audio()],
|
91 |
title="msVision_3",
|
92 |
description="μ΄λ―Έμ§λ₯Ό μ
λ‘λνλ©΄, μ¬λ¬Όμ μΈμνκ³ ν΄λΉνλ μμ± λ° μμ
μ μμ±ν©λλ€.(recognizes object and generate Voice&Music)",
|
93 |
examples=["dog.jpg", "cat.png", "cafe.jpg"]
|
|
|
4 |
import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
|
5 |
import torch
|
6 |
from PIL import Image
|
7 |
+
import requests
|
8 |
|
9 |
+
# λͺ¨λΈκ³Ό ν ν¬λμ΄μ λ‘λ
|
10 |
+
model_id = "nlpconnect/vit-gpt2-image-captioning"
|
11 |
+
model = VisionEncoderDecoderModel.from_pretrained(model_id)
|
12 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
14 |
|
15 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
16 |
model.to(device)
|
17 |
|
18 |
+
def predict_caption(image):
|
19 |
+
image = Image.open(image)
|
20 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
21 |
+
pixel_values = inputs["pixel_values"].to(device)
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
output_ids = model.generate(pixel_values)
|
24 |
+
caption = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
25 |
+
return caption
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
|
28 |
# μ΄λ―Έμ§ μΈμ νμ΄νλΌμΈ λ‘λ
|
|
|
69 |
|
70 |
# λ°νλ μμ± λ° μμ
κ²°κ³Όλ₯Ό Gradio μΈν°νμ΄μ€λ‘ μ λ¬
|
71 |
# μ: voice_result['url'] λλ voice_result['audio_data'] λ±
|
72 |
+
return caption, top_prediction, voice_result, music_result
|
73 |
|
74 |
# Gradio μΈν°νμ΄μ€ μμ±
|
75 |
iface = gr.Interface(
|
76 |
fn=classify_and_generate_voice,
|
77 |
inputs=gr.Image(type="pil"),
|
78 |
+
outputs=[gr.Textbox(label="Caption"), gr.Label(), gr.Audio(), gr.Audio()],
|
79 |
title="msVision_3",
|
80 |
description="μ΄λ―Έμ§λ₯Ό μ
λ‘λνλ©΄, μ¬λ¬Όμ μΈμνκ³ ν΄λΉνλ μμ± λ° μμ
μ μμ±ν©λλ€.(recognizes object and generate Voice&Music)",
|
81 |
examples=["dog.jpg", "cat.png", "cafe.jpg"]
|