Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -43,32 +43,53 @@ model_vq = blip_vqa(pretrained=model_url_vq, image_size=480, vit='base')
|
|
43 |
model_vq.eval()
|
44 |
model_vq = model_vq.to(device)
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
if model_n == 'Image Captioning':
|
50 |
image = transform(raw_image).unsqueeze(0).to(device)
|
51 |
with torch.no_grad():
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
63 |
|
64 |
-
|
65 |
-
outputs = gr.outputs.Textbox(label="Output")
|
66 |
|
67 |
-
title = "BLIP"
|
68 |
|
69 |
-
|
70 |
|
71 |
-
|
72 |
|
|
|
73 |
|
74 |
-
|
|
|
43 |
model_vq.eval()
|
44 |
model_vq = model_vq.to(device)
|
45 |
|
46 |
+
def getModelPath(language):
|
47 |
+
if language == 'English':
|
48 |
+
path = None
|
49 |
+
elif language == 'German':
|
50 |
+
path = "Helsinki-NLP/opus-mt-en-de"
|
51 |
+
elif language == 'French':
|
52 |
+
path = "Helsinki-NLP/opus-mt-en-fr"
|
53 |
+
elif language == 'Spanish':
|
54 |
+
path = "Helsinki-NLP/opus-mt-en-es"
|
55 |
+
elif language == 'Chinese':
|
56 |
+
path = "Helsinki-NLP/opus-mt-en-zh"
|
57 |
+
elif language == 'Ukranian':
|
58 |
+
path = "Helsinki-NLP/opus-mt-en-uk"
|
59 |
+
elif language == 'Swedish':
|
60 |
+
path = "Helsinki-NLP/opus-mt-en-sv"
|
61 |
+
elif language == 'Arabic':
|
62 |
+
path = "Helsinki-NLP/opus-mt-en-ar"
|
63 |
+
elif language == 'Italian':
|
64 |
+
path = "Helsinki-NLP/opus-mt-en-it"
|
65 |
+
elif language == 'Hindi':
|
66 |
+
path = "Helsinki-NLP/opus-mt-en-hi"
|
67 |
+
return(path)
|
68 |
+
|
69 |
+
def inference(input_img,strategy,language):
|
70 |
if model_n == 'Image Captioning':
|
71 |
image = transform(raw_image).unsqueeze(0).to(device)
|
72 |
with torch.no_grad():
|
73 |
+
if strategy == "Beam search":
|
74 |
+
cap = model.generate(image, sample=False, num_beams=3, max_length=20, min_length=5)
|
75 |
+
else:
|
76 |
+
cap = model.generate(image, sample=True, top_p=0.9, max_length=20, min_length=5)
|
77 |
+
if modelpath:
|
78 |
+
translator = pipeline("translation", model=modelpath)
|
79 |
+
trans_cap = translator(cap[0])
|
80 |
+
tc = trans_cap[0]['translation_text']
|
81 |
+
return str(tc)
|
82 |
+
else:
|
83 |
+
return str(cap[0])
|
84 |
+
|
85 |
|
86 |
+
description = "A pipeline of BLIP image captioning and Helsinki translation in order to generate image captions in a language of your choice either with beam search (deterministic) or nucleus sampling (stochastic). Enjoy! Is the language you want to use missing? Let me know and I'll integrate it."
|
|
|
87 |
|
|
|
88 |
|
89 |
+
inputs_ = [gr.inputs.Image(type='filepath', label="Input Image"),gr.inputs.Radio(choices=['Beam search','Nucleus sampling'], type="value", default="Nucleus sampling", label="Mode"), gr.inputs.Radio(choices=['English','German', 'French', 'Spanish', 'Chinese', 'Ukranian', 'Swedish', 'Arabic', 'Italian', 'Hindi'],type="value", default = 'German',label="Language")]
|
90 |
|
91 |
+
outputs_ = gr.outputs.Textbox(label="Output")
|
92 |
|
93 |
+
iface = gr.Interface(inference, inputs_, outputs_, description=description)
|
94 |
|
95 |
+
iface.launch(debug=True,show_error=True)
|