sid221 commited on
Commit
5c14626
·
1 Parent(s): 411a9f8

Add backend files for Caption Magic

Browse files
Files changed (5) hide show
  1. app.py +72 -0
  2. caption_generator.py +0 -0
  3. config.py +0 -0
  4. hashtag_engine.py +0 -0
  5. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
+ # ----------------------------
10
+ # Create FastAPI App
11
+ # ----------------------------
12
+
13
+ app = FastAPI()
14
+
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=["*"],
22
+ )
23
+
24
+ # ----------------------------
25
+ # Load Model Once At Startup
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
34
+
35
+
36
+ # ----------------------------
37
+ # Root Test Route
38
+ # ----------------------------
39
+
40
+ @app.get("/")
41
+ def root():
42
+ return {"message": "AI Caption Generator Backend Running 🚀"}
43
+
44
+
45
+ # ----------------------------
46
+ # Generate Caption Endpoint
47
+ # ----------------------------
48
+
49
+ @app.post("/generate")
50
+ async def generate_caption(
51
+ file: UploadFile = File(...),
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
+ }
caption_generator.py ADDED
File without changes
config.py ADDED
File without changes
hashtag_engine.py ADDED
File without changes
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ transformers
4
+ torch
5
+ torchvision
6
+ pillow