Spaces:
Runtime error
Runtime error
Shaffique Aljoofri commited on
Commit ·
5977d60
1
Parent(s): 99bd8b0
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,54 @@
|
|
| 1 |
import os
|
| 2 |
import io
|
| 3 |
-
#from PIL import Image
|
| 4 |
import base64
|
| 5 |
|
| 6 |
hf_api_key = os.environ['HF_API_KEY']
|
| 7 |
|
| 8 |
# Helper functions
|
| 9 |
-
import requests,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
#Image-to-text endpoint
|
| 12 |
def get_completion(inputs, parameters = None, ENDPOINT_URL = os.environ['HF_API_ITT_BASE']):
|
|
|
|
| 13 |
headers = {
|
| 14 |
"Authorization": f"Bearer {hf_api_key}",
|
| 15 |
"Content-Type": "application/json"
|
| 16 |
}
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
if parameters is not None:
|
| 19 |
-
data.update({"parameters": parameters})
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
return json.loads(response.content.decode("utf-8"))
|
| 25 |
|
|
|
|
| 26 |
# Gradio App
|
| 27 |
|
| 28 |
import gradio as gr
|
| 29 |
-
|
| 30 |
-
def image_to_base64_str(pil_image):
|
| 31 |
-
byte_arr = io.BytesIO()
|
| 32 |
-
pil_image.save(byte_arr, format='PNG')
|
| 33 |
-
byte_arr = byte_arr.getvalue()
|
| 34 |
-
return str(base64.b64encode(byte_arr).decode('utf-8'))
|
| 35 |
|
| 36 |
def captioner(image):
|
| 37 |
-
|
|
|
|
| 38 |
result = get_completion(base64_image)
|
| 39 |
return result[0]['generated_text']
|
| 40 |
|
| 41 |
gr.close_all()
|
|
|
|
| 42 |
demo = gr.Interface(fn = captioner,
|
| 43 |
inputs = [gr.Image(label="Upload image", type="pil")],
|
| 44 |
outputs = [gr.Textbox(label="Caption")],
|
|
@@ -48,5 +58,4 @@ demo = gr.Interface(fn = captioner,
|
|
| 48 |
examples = ["tank-aerial-view.jpg"]
|
| 49 |
)
|
| 50 |
|
| 51 |
-
demo.launch()
|
| 52 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import io
|
|
|
|
| 3 |
import base64
|
| 4 |
|
| 5 |
hf_api_key = os.environ['HF_API_KEY']
|
| 6 |
|
| 7 |
# Helper functions
|
| 8 |
+
import requests, js
|
| 9 |
+
|
| 10 |
+
#Image-to-text endpoint
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def image_to_base64_str(pil_image):
|
| 14 |
+
byte_arr = io.BytesIO()
|
| 15 |
+
pil_image.save(byte_arr, format='PNG')
|
| 16 |
+
byte_arr = byte_arr.getvalue()
|
| 17 |
+
return str(base64.b64encode(byte_arr).decode('utf-8'))
|
| 18 |
|
| 19 |
#Image-to-text endpoint
|
| 20 |
def get_completion(inputs, parameters = None, ENDPOINT_URL = os.environ['HF_API_ITT_BASE']):
|
| 21 |
+
|
| 22 |
headers = {
|
| 23 |
"Authorization": f"Bearer {hf_api_key}",
|
| 24 |
"Content-Type": "application/json"
|
| 25 |
}
|
| 26 |
+
|
| 27 |
+
data = { "inputs" : inputs }
|
| 28 |
+
|
| 29 |
if parameters is not None:
|
| 30 |
+
data.update({"parameters" : parameters})
|
| 31 |
+
|
| 32 |
+
response = requests.post(ENDPOINT_URL,
|
| 33 |
+
headers = headers,
|
| 34 |
+
data = json.dumps(data))
|
| 35 |
+
|
| 36 |
return json.loads(response.content.decode("utf-8"))
|
| 37 |
|
| 38 |
+
|
| 39 |
# Gradio App
|
| 40 |
|
| 41 |
import gradio as gr
|
| 42 |
+
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
def captioner(image):
|
| 45 |
+
pil_image = Image.open(image)
|
| 46 |
+
base64_image = image_to_base64_str(pil_image)
|
| 47 |
result = get_completion(base64_image)
|
| 48 |
return result[0]['generated_text']
|
| 49 |
|
| 50 |
gr.close_all()
|
| 51 |
+
|
| 52 |
demo = gr.Interface(fn = captioner,
|
| 53 |
inputs = [gr.Image(label="Upload image", type="pil")],
|
| 54 |
outputs = [gr.Textbox(label="Caption")],
|
|
|
|
| 58 |
examples = ["tank-aerial-view.jpg"]
|
| 59 |
)
|
| 60 |
|
| 61 |
+
demo.launch()
|
|
|