Spaces:
Runtime error
Runtime error
Commit
·
3eb994e
1
Parent(s):
7bd48de
add files
Browse files- app.py +54 -0
- bird.jpg +0 -0
- dogs.jpg +0 -0
- girl.jpg +0 -0
- requirements.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import base64
|
| 5 |
+
from dotenv import load_dotenv, find_dotenv
|
| 6 |
+
import requests, json
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# read local .env file
|
| 11 |
+
_ = load_dotenv(find_dotenv())
|
| 12 |
+
hf_api_key = os.environ['HF_API_KEY']
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Helper functions
|
| 16 |
+
#Image-to-text endpoint
|
| 17 |
+
def get_completion(inputs, parameters=None, ENDPOINT_URL=os.environ['HF_API_ITT_BASE']):
|
| 18 |
+
headers = {
|
| 19 |
+
"Authorization": f"Bearer {hf_api_key}",
|
| 20 |
+
"Content-Type": "application/json"
|
| 21 |
+
}
|
| 22 |
+
data = { "inputs": inputs }
|
| 23 |
+
if parameters is not None:
|
| 24 |
+
data.update({"parameters": parameters})
|
| 25 |
+
response = requests.request("POST",
|
| 26 |
+
ENDPOINT_URL,
|
| 27 |
+
headers=headers,
|
| 28 |
+
data=json.dumps(data))
|
| 29 |
+
return json.loads(response.content.decode("utf-8"))
|
| 30 |
+
|
| 31 |
+
#Image-to-text endpoint
|
| 32 |
+
def image_to_base64_str(pil_image):
|
| 33 |
+
byte_arr = io.BytesIO()
|
| 34 |
+
pil_image.save(byte_arr, format='PNG')
|
| 35 |
+
byte_arr = byte_arr.getvalue()
|
| 36 |
+
return str(base64.b64encode(byte_arr).decode('utf-8'))
|
| 37 |
+
|
| 38 |
+
# Captioning function
|
| 39 |
+
def captioner(image):
|
| 40 |
+
base64_image = image_to_base64_str(image)
|
| 41 |
+
result = get_completion(base64_image)
|
| 42 |
+
return result[0]['generated_text']
|
| 43 |
+
|
| 44 |
+
# Interface
|
| 45 |
+
gr.close_all()
|
| 46 |
+
demo = gr.Interface(fn=captioner,
|
| 47 |
+
inputs=[gr.Image(label="Upload image", type="pil")],
|
| 48 |
+
outputs=[gr.Textbox(label="Caption")],
|
| 49 |
+
title="Image Captioning with BLIP",
|
| 50 |
+
description="Caption any image using the BLIP model",
|
| 51 |
+
allow_flagging="never",
|
| 52 |
+
examples=["bird.jpg", "dogs.jpg", "girl.jpg"])
|
| 53 |
+
|
| 54 |
+
demo.launch(share=True)
|
bird.jpg
ADDED
|
dogs.jpg
ADDED
|
girl.jpg
ADDED
|
requirements.txt
ADDED
|
Binary file (146 Bytes). View file
|
|
|