Spaces:
Running
Running
Update appImage.py
Browse files- appImage.py +24 -2
appImage.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from fastapi import FastAPI, UploadFile, File
|
2 |
from fastapi.responses import RedirectResponse, JSONResponse
|
3 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
4 |
from PIL import Image
|
@@ -42,4 +42,26 @@ async def caption_from_frontend(file: UploadFile = File(...)):
|
|
42 |
|
43 |
@app.get("/")
|
44 |
def home():
|
45 |
-
return RedirectResponse(url="/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""from fastapi import FastAPI, UploadFile, File
|
2 |
from fastapi.responses import RedirectResponse, JSONResponse
|
3 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
4 |
from PIL import Image
|
|
|
42 |
|
43 |
@app.get("/")
|
44 |
def home():
|
45 |
+
return RedirectResponse(url="/")"""
|
46 |
+
# app_image_logic.py
|
47 |
+
from transformers import AutoProcessor, AutoModelForCausalLM, pipeline
|
48 |
+
from PIL import Image
|
49 |
+
import torch
|
50 |
+
|
51 |
+
try:
|
52 |
+
processor = AutoProcessor.from_pretrained("microsoft/git-large-coco")
|
53 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/git-large-coco")
|
54 |
+
USE_GIT = True
|
55 |
+
except:
|
56 |
+
captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
57 |
+
USE_GIT = False
|
58 |
+
|
59 |
+
def generate_caption(image_path):
|
60 |
+
if USE_GIT:
|
61 |
+
image = Image.open(image_path)
|
62 |
+
inputs = processor(images=image, return_tensors="pt")
|
63 |
+
outputs = model.generate(**inputs, max_length=50)
|
64 |
+
return processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
65 |
+
else:
|
66 |
+
result = captioner(image_path)
|
67 |
+
return result[0]['generated_text']
|