ikraamkb commited on
Commit
03b45a4
·
verified ·
1 Parent(s): 4c56bf5

Update Summarization/appImage.py

Browse files
Files changed (1) hide show
  1. Summarization/appImage.py +0 -44
Summarization/appImage.py CHANGED
@@ -1,48 +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
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="/")"""
46
  from fastapi import UploadFile
47
  from transformers import AutoProcessor, AutoModelForCausalLM, pipeline
48
  from PIL import Image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
 
 
 
2
  from fastapi import UploadFile
3
  from transformers import AutoProcessor, AutoModelForCausalLM, pipeline
4
  from PIL import Image