Spaces:
Runtime error
Runtime error
Commit
·
7a513d5
1
Parent(s):
b56b22d
Update app.py
Browse files
app.py
CHANGED
@@ -3,14 +3,15 @@ import requests, json
|
|
3 |
import os
|
4 |
import io
|
5 |
import IPython.display
|
6 |
-
from PIL import Image
|
7 |
import base64
|
8 |
import torch
|
9 |
-
from transformers import pipeline
|
10 |
|
|
|
|
|
11 |
|
|
|
|
|
12 |
|
13 |
-
completion_obj = pipeline("image-to-text",model="Salesforce/blip-image-captioning-base")
|
14 |
|
15 |
|
16 |
|
@@ -28,10 +29,14 @@ completion_obj = pipeline("image-to-text",model="Salesforce/blip-image-captionin
|
|
28 |
#gr.Textbox(os.environ['HF_TOKENS'])
|
29 |
|
30 |
#Image-to-text endpoint
|
31 |
-
def get_completion(
|
|
|
32 |
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
35 |
|
36 |
|
37 |
# headers = {
|
@@ -57,16 +62,6 @@ def get_completion(inputs):
|
|
57 |
#image_url = "https://free-images.com/sm/9596/dog_animal_greyhound_983023.jpg"
|
58 |
#demo = gr.get_completion(image_url)
|
59 |
|
60 |
-
def image_to_base64_str(pil_image):
|
61 |
-
byte_arr = io.BytesIO()
|
62 |
-
pil_image.save(byte_arr, format='PNG')
|
63 |
-
byte_arr = byte_arr.getvalue()
|
64 |
-
return str(base64.b64encode(byte_arr).decode('utf-8'))
|
65 |
-
|
66 |
-
def captioner(image):
|
67 |
-
base64_image = image_to_base64_str(image)
|
68 |
-
result = get_completion(base64_image)
|
69 |
-
return result[0]['generated_text']
|
70 |
|
71 |
gr.close_all()
|
72 |
demo = gr.Interface(fn=get_completion,
|
|
|
3 |
import os
|
4 |
import io
|
5 |
import IPython.display
|
|
|
6 |
import base64
|
7 |
import torch
|
|
|
8 |
|
9 |
+
from PIL import Image
|
10 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
11 |
|
12 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
13 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
14 |
|
|
|
15 |
|
16 |
|
17 |
|
|
|
29 |
#gr.Textbox(os.environ['HF_TOKENS'])
|
30 |
|
31 |
#Image-to-text endpoint
|
32 |
+
def get_completion(image):
|
33 |
+
raw_image = Image.open(image).convert('RGB')
|
34 |
|
35 |
+
text = "a photography of"
|
36 |
+
inputs = processor(raw_image, text, return_tensors="pt")
|
37 |
+
|
38 |
+
out = model.generate(**inputs)
|
39 |
+
return processor.decode(out[0], skip_special_tokens=True)
|
40 |
|
41 |
|
42 |
# headers = {
|
|
|
62 |
#image_url = "https://free-images.com/sm/9596/dog_animal_greyhound_983023.jpg"
|
63 |
#demo = gr.get_completion(image_url)
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
gr.close_all()
|
67 |
demo = gr.Interface(fn=get_completion,
|