Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# impoprt packages
|
2 |
+
import torch
|
3 |
+
import requests
|
4 |
+
from PIL import Image
|
5 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration, AutoTokenizer, pipeline
|
6 |
+
import sentencepiece
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
# Image captioning model
|
10 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
11 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
12 |
+
|
13 |
+
# Translate en to ar
|
14 |
+
model_translater = pipeline("translation", model="Helsinki-NLP/opus-mt-tc-big-en-ar")
|
15 |
+
|
16 |
+
# conditional image captioning (with prefix-)
|
17 |
+
def image_captioning(image_url, prefix="a "):
|
18 |
+
""" Return text (As str) to describe an image """
|
19 |
+
# Get the image by image_url and convert it to RGB
|
20 |
+
raw_image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB')
|
21 |
+
|
22 |
+
# Process the image
|
23 |
+
inputs = processor(raw_image, prefix, return_tensors="pt")
|
24 |
+
|
25 |
+
# Generate text to describe the image
|
26 |
+
output = model.generate(**inputs)
|
27 |
+
|
28 |
+
# Decode the output
|
29 |
+
output = processor.decode(output[0], skip_special_tokens=True, max_length=80)
|
30 |
+
return output
|
31 |
+
|
32 |
+
def translate_text(text, to="ar"):
|
33 |
+
""" Return translated text """
|
34 |
+
translated_text = model_translater(str(text))
|
35 |
+
return translated_text[0]['translation_text']
|
36 |
+
|
37 |
+
def image_captioning_ar(image_url, prefix = "a "):
|
38 |
+
if image_url:
|
39 |
+
text = image_captioning(image_url, prefix=prefix)
|
40 |
+
return translate_text(text)
|
41 |
+
return null
|
42 |
+
|
43 |
+
imageCaptioning_interface = gr.Interface(
|
44 |
+
fn = image_captioning_ar,
|
45 |
+
inputs=gr.inputs.Textbox(lines = 7, label = 'Image url'),
|
46 |
+
outputs=gr.outputs.Textbox(label="Caption"),
|
47 |
+
title = 'Image captioning',
|
48 |
+
)
|
49 |
+
imageCaptioning_interface.launch()
|