sid221 commited on
Commit
35063fc
·
1 Parent(s): 5c14626

Fix model loading (remove model_loader)

Browse files
Files changed (1) hide show
  1. app.py +24 -8
app.py CHANGED
@@ -2,8 +2,9 @@ from fastapi import FastAPI, UploadFile, File, Form
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from PIL import Image
4
  import io
 
 
5
 
6
- from model_loader import load_model
7
  from caption_generator import generate_captions
8
 
9
  # ----------------------------
@@ -15,7 +16,7 @@ app = FastAPI()
15
  # Enable CORS (important for frontend connection)
16
  app.add_middleware(
17
  CORSMiddleware,
18
- allow_origins=["*"], # For development only
19
  allow_credentials=True,
20
  allow_methods=["*"],
21
  allow_headers=["*"],
@@ -26,8 +27,17 @@ app.add_middleware(
26
  # ----------------------------
27
 
28
  try:
29
- processor, model = load_model()
30
- print("✅ Model loaded successfully.")
 
 
 
 
 
 
 
 
 
31
  except Exception as e:
32
  print("❌ Failed to load model:", e)
33
  processor, model = None, None
@@ -52,21 +62,27 @@ async def generate_caption(
52
  tone: str = Form("casual")
53
  ):
54
  if processor is None or model is None:
55
- return {"error": "Model not loaded properly."}
56
 
57
  try:
58
  contents = await file.read()
59
  image = Image.open(io.BytesIO(contents)).convert("RGB")
60
 
61
- captions = generate_captions(image, processor, model, tone)
 
 
 
 
 
62
 
63
  return {
64
  "status": "success",
65
- "captions": captions
 
66
  }
67
 
68
  except Exception as e:
69
  return {
70
  "status": "error",
71
  "message": str(e)
72
- }
 
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from PIL import Image
4
  import io
5
+ import torch
6
+ from transformers import BlipProcessor, BlipForConditionalGeneration
7
 
 
8
  from caption_generator import generate_captions
9
 
10
  # ----------------------------
 
16
  # Enable CORS (important for frontend connection)
17
  app.add_middleware(
18
  CORSMiddleware,
19
+ allow_origins=["*"], # OK for hackathon
20
  allow_credentials=True,
21
  allow_methods=["*"],
22
  allow_headers=["*"],
 
27
  # ----------------------------
28
 
29
  try:
30
+ device = "cuda" if torch.cuda.is_available() else "cpu"
31
+
32
+ processor = BlipProcessor.from_pretrained(
33
+ "Salesforce/blip-image-captioning-base"
34
+ )
35
+ model = BlipForConditionalGeneration.from_pretrained(
36
+ "Salesforce/blip-image-captioning-base"
37
+ ).to(device)
38
+
39
+ print("✅ BLIP model loaded successfully.")
40
+
41
  except Exception as e:
42
  print("❌ Failed to load model:", e)
43
  processor, model = None, None
 
62
  tone: str = Form("casual")
63
  ):
64
  if processor is None or model is None:
65
+ return {"status": "error", "message": "Model not loaded properly ❌"}
66
 
67
  try:
68
  contents = await file.read()
69
  image = Image.open(io.BytesIO(contents)).convert("RGB")
70
 
71
+ captions = generate_captions(
72
+ image=image,
73
+ processor=processor,
74
+ model=model,
75
+ tone=tone
76
+ )
77
 
78
  return {
79
  "status": "success",
80
+ "captions": captions,
81
+ "emoji": "✨🔥"
82
  }
83
 
84
  except Exception as e:
85
  return {
86
  "status": "error",
87
  "message": str(e)
88
+ }