Baptiste Canton
commited on
Commit
·
c95d497
1
Parent(s):
a6aa908
init
Browse files- app.py +40 -0
- requirements.txt +10 -0
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from pillow_heif import register_heif_opener
|
| 6 |
+
|
| 7 |
+
register_heif_opener()
|
| 8 |
+
|
| 9 |
+
import gradio as gr
|
| 10 |
+
from transformers import pipeline
|
| 11 |
+
|
| 12 |
+
LOG_LEVEL = os.getenv("LOG_LEVEL", "DEBUG")
|
| 13 |
+
MAX_NEW_TOKENS = int(os.getenv("MAX_NEW_TOKENS", 200))
|
| 14 |
+
# https://huggingface.co/models?pipeline_tag=image-to-text&sort=likes
|
| 15 |
+
MODEL = os.getenv("MODEL", "Salesforce/blip-image-captioning-large")
|
| 16 |
+
|
| 17 |
+
logging.basicConfig(level=LOG_LEVEL)
|
| 18 |
+
logger = logging.getLogger(__name__)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
logger.info("Loading model...")
|
| 22 |
+
# simpler model: "ydshieh/vit-gpt2-coco-en"
|
| 23 |
+
captioner = pipeline(
|
| 24 |
+
"image-to-text",
|
| 25 |
+
model=MODEL,
|
| 26 |
+
max_new_tokens=MAX_NEW_TOKENS,
|
| 27 |
+
)
|
| 28 |
+
logger.info("Done loading model.")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def graptioner(image_url):
|
| 32 |
+
global captioner
|
| 33 |
+
result = captioner(image_url)
|
| 34 |
+
caption = result[0]["generated_text"]
|
| 35 |
+
return caption
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# add gradio interface
|
| 39 |
+
iface = gr.Interface(fn=graptioner, inputs="text", outputs=["text"])
|
| 40 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
pillow
|
| 3 |
+
transformers
|
| 4 |
+
torch
|
| 5 |
+
accelerate
|
| 6 |
+
pillow-heif
|
| 7 |
+
gradio
|
| 8 |
+
|
| 9 |
+
uvicorn
|
| 10 |
+
|