Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,42 @@
|
|
| 1 |
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
|
| 2 |
-
|
| 3 |
-
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
-
|
| 6 |
import sys, os
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_1.2B")
|
| 12 |
tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_1.2B")
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
tokenizer.src_lang = "en"
|
| 21 |
-
encodedText = tokenizer(
|
| 22 |
generatedTokens = model.generate(**encodedText, forced_bos_token_id=tokenizer.get_lang_id("ru"))
|
| 23 |
|
| 24 |
return tokenizer.batch_decode(generatedTokens, skip_special_tokens=True)[0]
|
| 25 |
|
| 26 |
-
demoApp = gr.Interface(extractAndTranslate, "image", "text")
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
|
| 2 |
+
from turtle import title
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import numpy as np
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
import sys, os
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
+
|
| 10 |
+
|
| 11 |
|
| 12 |
model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_1.2B")
|
| 13 |
tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_1.2B")
|
| 14 |
+
pipe = pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch32")
|
| 15 |
+
images="dog.jpg"
|
| 16 |
|
| 17 |
+
def shot(image, labels_text):
|
| 18 |
+
PIL_image = Image.fromarray(np.uint8(image)).convert('RGB')
|
| 19 |
+
labels = labels_text.split(",")
|
| 20 |
+
res = pipe(images=PIL_image,
|
| 21 |
+
candidate_labels=labels,
|
| 22 |
+
hypothesis_template= "This is a photo of a {}")
|
| 23 |
+
return {dic["label"]: dic["score"] for dic in res}
|
| 24 |
+
# Translate
|
| 25 |
tokenizer.src_lang = "en"
|
| 26 |
+
encodedText = tokenizer(candidate_labels, return_tensors="pt")
|
| 27 |
generatedTokens = model.generate(**encodedText, forced_bos_token_id=tokenizer.get_lang_id("ru"))
|
| 28 |
|
| 29 |
return tokenizer.batch_decode(generatedTokens, skip_special_tokens=True)[0]
|
| 30 |
|
|
|
|
| 31 |
|
| 32 |
+
|
| 33 |
+
iface = gr.Interface(shot,
|
| 34 |
+
["image", "text"],
|
| 35 |
+
"label",
|
| 36 |
+
examples=[["dog.jpg", "dog,cat,bird"],
|
| 37 |
+
["germany.jpg", "germany,belgium,colombia"],
|
| 38 |
+
["colombia.jpg", "germany,belgium,colombia"]],
|
| 39 |
+
description="Add a picture and a list of labels separated by commas",
|
| 40 |
+
title="Zero-shot Image Classification")
|
| 41 |
+
|
| 42 |
+
iface.launch()
|