Spaces:
Runtime error
Runtime error
Commit ·
eb950ec
1
Parent(s): bcdc97a
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import requests, json
|
|
| 3 |
import os
|
| 4 |
import io
|
| 5 |
import IPython.display
|
| 6 |
-
|
| 7 |
#import base64
|
| 8 |
|
| 9 |
|
|
@@ -24,12 +24,25 @@ def get_completion(inputs, parameters=None, ENDPOINT_URL="http://internal-aws-pr
|
|
| 24 |
return json.loads(response.content.decode("utf-8"))
|
| 25 |
|
| 26 |
|
| 27 |
-
image_url = "https://free-images.com/sm/9596/dog_animal_greyhound_983023.jpg"
|
| 28 |
-
display(IPython.display.Image(url=image_url))
|
| 29 |
-
get_completion(image_url)
|
| 30 |
|
| 31 |
-
def
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
import io
|
| 5 |
import IPython.display
|
| 6 |
+
from PIL import Image
|
| 7 |
#import base64
|
| 8 |
|
| 9 |
|
|
|
|
| 24 |
return json.loads(response.content.decode("utf-8"))
|
| 25 |
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
def image_to_base64_str(pil_image):
|
| 29 |
+
byte_arr = io.BytesIO()
|
| 30 |
+
pil_image.save(byte_arr, format='PNG')
|
| 31 |
+
byte_arr = byte_arr.getvalue()
|
| 32 |
+
return str(base64.b64encode(byte_arr).decode('utf-8'))
|
| 33 |
|
| 34 |
+
def captioner(image):
|
| 35 |
+
base64_image = image_to_base64_str(image)
|
| 36 |
+
result = get_completion(base64_image)
|
| 37 |
+
return result[0]['generated_text']
|
| 38 |
+
|
| 39 |
+
gr.close_all()
|
| 40 |
+
demo = gr.Interface(fn=captioner,
|
| 41 |
+
inputs=[gr.Image(label="Upload image", type="pil")],
|
| 42 |
+
outputs=[gr.Textbox(label="Caption")],
|
| 43 |
+
title="Image Captioning with BLIP",
|
| 44 |
+
description="Caption any image using the BLIP model",
|
| 45 |
+
allow_flagging="never",
|
| 46 |
+
examples=["christmas_dog.jpeg", "bird_flight.jpeg", "cow.jpeg"])
|
| 47 |
+
|
| 48 |
+
demo.launch(share=True, server_port=int(os.environ['PORT1']))
|