Spaces:
Runtime error
Runtime error
Sophia Zell
commited on
Commit
·
647139c
1
Parent(s):
79a95f1
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import requests
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-de")
|
7 |
+
|
8 |
+
def blipofasinki(input_img):
|
9 |
+
b64_string = gr.processing_utils.encode_url_or_file_to_base64(input_img)
|
10 |
+
#blip-Nucleus
|
11 |
+
responsen = requests.post(url='https://hf.space/embed/Salesforce/BLIP/+/api/predict/', json={"data": [ b64_string,"Image Captioning","None",str('Nucleus sampling')]})
|
12 |
+
jresn = responsen.json()
|
13 |
+
capn = jresn["data"][0]
|
14 |
+
offset = len(str("caption:"))
|
15 |
+
capn = capn[offset:]
|
16 |
+
trans_capn = translator(capn)
|
17 |
+
tcn = trans_capn[0]['translation_text']
|
18 |
+
#blip-beam
|
19 |
+
responseb = requests.post(url='https://hf.space/embed/Salesforce/BLIP/+/api/predict/', json={"data": [ b64_string,"Image Captioning","None",str('Beam search')]})
|
20 |
+
jresb = responseb.json()
|
21 |
+
capb = jresb["data"][0]
|
22 |
+
capb = capb[offset:]
|
23 |
+
trans_capb = translator(capb)
|
24 |
+
tcb = trans_capb[0]['translation_text']
|
25 |
+
#ofa
|
26 |
+
responseo = requests.post(url='https://hf.space/embed/OFA-Sys/OFA-Image_Caption/+/api/predict/', json={"data": [b64_string]})
|
27 |
+
jreso = responseo.json()
|
28 |
+
capo = jreso["data"][0]
|
29 |
+
trans_capo = translator(capo)
|
30 |
+
tco = trans_capo[0]['translation_text']
|
31 |
+
return [tcn, tcb, tco]
|
32 |
+
|
33 |
+
|
34 |
+
description = "A direct comparison in image captioning between BLIP and OFA (in German translated with Helsinki)."
|
35 |
+
|
36 |
+
input_ = [gr.inputs.Image(type='filepath', label="Input Image")]
|
37 |
+
|
38 |
+
output_ = [gr.outputs.Textbox(label="BLIP Nucleus sampling output"),gr.outputs.Textbox(label="BLIP Beam search output"), gr.outputs.Textbox(label="OFA output")]
|
39 |
+
|
40 |
+
iface = gr.Interface(blipofasinki, input_, output_, description=description)
|
41 |
+
|
42 |
+
iface.launch(debug=True,show_error=True)
|