Spaces:
Running
Running
Update appImage.py
Browse files- appImage.py +19 -47
appImage.py
CHANGED
@@ -1,73 +1,45 @@
|
|
1 |
-
import
|
|
|
2 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
3 |
from PIL import Image
|
|
|
4 |
import torch
|
5 |
-
from fastapi import FastAPI
|
6 |
-
from fastapi.responses import RedirectResponse
|
7 |
|
8 |
-
# Initialize FastAPI
|
9 |
app = FastAPI()
|
10 |
|
11 |
-
# Load
|
12 |
try:
|
13 |
-
# Load the better model
|
14 |
processor = AutoProcessor.from_pretrained("microsoft/git-large-coco")
|
15 |
-
|
16 |
-
print("Successfully loaded microsoft/git-large-coco model")
|
17 |
USE_GIT = True
|
18 |
-
except Exception
|
19 |
-
|
20 |
captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
21 |
USE_GIT = False
|
22 |
|
23 |
def generate_caption(image_path):
|
24 |
-
"""Generate caption using the best available model"""
|
25 |
try:
|
26 |
if USE_GIT:
|
27 |
image = Image.open(image_path)
|
28 |
inputs = processor(images=image, return_tensors="pt")
|
29 |
-
outputs =
|
30 |
return processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
31 |
else:
|
32 |
result = captioner(image_path)
|
33 |
return result[0]['generated_text']
|
34 |
except Exception as e:
|
35 |
-
|
36 |
-
return "Could not generate caption"
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
caption = generate_caption(file_path)
|
45 |
-
return f"📷 Image Caption:\n{caption}"
|
46 |
-
except Exception as e:
|
47 |
-
return f"Error processing image: {str(e)}"
|
48 |
-
|
49 |
-
# Gradio Interface
|
50 |
-
with gr.Blocks(title="Image Captioning Service", theme=gr.themes.Soft()) as demo:
|
51 |
-
gr.Markdown("# 🖼️ Image Captioning Service")
|
52 |
-
gr.Markdown("Upload an image to get automatic captioning")
|
53 |
-
|
54 |
-
with gr.Row():
|
55 |
-
with gr.Column():
|
56 |
-
image_input = gr.Image(label="Upload Image", type="filepath")
|
57 |
-
analyze_btn = gr.Button("Generate Caption", variant="primary")
|
58 |
-
|
59 |
-
with gr.Column():
|
60 |
-
output = gr.Textbox(label="Caption Result", lines=5)
|
61 |
-
|
62 |
-
analyze_btn.click(
|
63 |
-
fn=process_image,
|
64 |
-
inputs=[image_input],
|
65 |
-
outputs=[output]
|
66 |
-
)
|
67 |
|
68 |
-
|
69 |
-
|
70 |
|
71 |
@app.get("/")
|
72 |
-
def
|
73 |
-
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
|
5 |
+
import tempfile
|
6 |
import torch
|
|
|
|
|
7 |
|
|
|
8 |
app = FastAPI()
|
9 |
|
10 |
+
# Load model
|
11 |
try:
|
|
|
12 |
processor = AutoProcessor.from_pretrained("microsoft/git-large-coco")
|
13 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/git-large-coco")
|
|
|
14 |
USE_GIT = True
|
15 |
+
except Exception:
|
16 |
+
from transformers import pipeline
|
17 |
captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
18 |
USE_GIT = False
|
19 |
|
20 |
def generate_caption(image_path):
|
|
|
21 |
try:
|
22 |
if USE_GIT:
|
23 |
image = Image.open(image_path)
|
24 |
inputs = processor(images=image, return_tensors="pt")
|
25 |
+
outputs = model.generate(**inputs, max_length=50)
|
26 |
return processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
27 |
else:
|
28 |
result = captioner(image_path)
|
29 |
return result[0]['generated_text']
|
30 |
except Exception as e:
|
31 |
+
return f"Error generating caption: {str(e)}"
|
|
|
32 |
|
33 |
+
@app.post("/imagecaption/")
|
34 |
+
async def caption_from_frontend(file: UploadFile = File(...)):
|
35 |
+
contents = await file.read()
|
36 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
|
37 |
+
tmp.write(contents)
|
38 |
+
image_path = tmp.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
caption = generate_caption(image_path)
|
41 |
+
return JSONResponse({"caption": caption})
|
42 |
|
43 |
@app.get("/")
|
44 |
+
def home():
|
45 |
+
return RedirectResponse(url="/")
|