Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,13 +14,10 @@ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-capt
|
|
| 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(
|
| 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(
|
| 24 |
|
| 25 |
# Generate text to describe the image
|
| 26 |
output = model.generate(**inputs)
|
|
@@ -34,9 +31,9 @@ def translate_text(text, to="ar"):
|
|
| 34 |
translated_text = model_translater(str(text))
|
| 35 |
return translated_text[0]['translation_text']
|
| 36 |
|
| 37 |
-
def image_captioning_ar(
|
| 38 |
-
if
|
| 39 |
-
text = image_captioning(
|
| 40 |
return translate_text(text)
|
| 41 |
return null
|
| 42 |
|
|
|
|
| 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, prefix="a "):
|
| 18 |
""" Return text (As str) to describe an image """
|
|
|
|
|
|
|
|
|
|
| 19 |
# Process the image
|
| 20 |
+
inputs = processor(image, prefix, return_tensors="pt")
|
| 21 |
|
| 22 |
# Generate text to describe the image
|
| 23 |
output = model.generate(**inputs)
|
|
|
|
| 31 |
translated_text = model_translater(str(text))
|
| 32 |
return translated_text[0]['translation_text']
|
| 33 |
|
| 34 |
+
def image_captioning_ar(image, prefix = "a "):
|
| 35 |
+
if image:
|
| 36 |
+
text = image_captioning(image, prefix=prefix)
|
| 37 |
return translate_text(text)
|
| 38 |
return null
|
| 39 |
|