aliSaac510 commited on
Commit
a6779c6
·
verified ·
1 Parent(s): eb04b2e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +16 -29
main.py CHANGED
@@ -1,5 +1,4 @@
1
  from fastapi import FastAPI, HTTPException
2
- from sentence_transformers import SentenceTransformer
3
  from transformers import AutoImageProcessor, AutoModel
4
  import torch
5
  from PIL import Image
@@ -7,31 +6,25 @@ import requests
7
  from io import BytesIO
8
  import uvicorn
9
 
10
- app = FastAPI(title="Movie Linker AI API")
11
 
12
- # Load Models
13
- print("Loading Models... please wait.")
14
 
15
- # 1. Image Model: DINOv2 (using transformers directly for stability)
16
  img_model_id = 'facebook/dinov2-base'
17
  img_processor = AutoImageProcessor.from_pretrained(img_model_id)
18
  img_model = AutoModel.from_pretrained(img_model_id)
19
- img_model.eval() # Set to evaluation mode
20
 
21
- # 2. Text Model: Qwen (Choice: 1.5B or 0.6B)
22
- text_model_name = 'Alibaba-NLP/gte-Qwen2-1.5b-instruct'
23
- text_model = SentenceTransformer(text_model_name, trust_remote_code=True)
24
-
25
- print("All models loaded successfully.")
26
 
27
  @app.get("/")
28
  def home():
29
  return {
30
  "status": "online",
31
- "models": {
32
- "image": img_model_id,
33
- "text": text_model_name
34
- }
35
  }
36
 
37
  @app.post("/embed/image")
@@ -40,26 +33,20 @@ async def embed_image(image_url: str):
40
  response = requests.get(image_url, timeout=10)
41
  img = Image.open(BytesIO(response.content)).convert("RGB")
42
 
43
- # Process image for DINOv2
44
  inputs = img_processor(images=img, return_tensors="pt")
45
 
46
  with torch.no_grad():
47
  outputs = img_model(**inputs)
48
- # DINOv2 uses the CLS token (first token) for the global representation
49
- # This is available in last_hidden_state[:, 0, :]
50
  embedding = outputs.last_hidden_state[:, 0, :].squeeze().tolist()
51
 
52
- return {"success": True, "dimension": len(embedding), "embedding": embedding}
53
- except Exception as e:
54
- raise HTTPException(status_code=400, detail=str(e))
55
-
56
- @app.post("/embed/text")
57
- async def embed_text(text: str):
58
- try:
59
- # Instruction-tuned models like Qwen work best with prompts
60
- processed_text = f"query: {text}"
61
- embedding = text_model.encode(processed_text).tolist()
62
- return {"success": True, "dimension": len(embedding), "embedding": embedding}
63
  except Exception as e:
64
  raise HTTPException(status_code=400, detail=str(e))
65
 
 
1
  from fastapi import FastAPI, HTTPException
 
2
  from transformers import AutoImageProcessor, AutoModel
3
  import torch
4
  from PIL import Image
 
6
  from io import BytesIO
7
  import uvicorn
8
 
9
+ app = FastAPI(title="Movie Linker - Image Embedding API (DINOv2)")
10
 
11
+ # Load Model
12
+ print("Loading DINOv2 Model... please wait.")
13
 
14
+ # Image Model: DINOv2
15
  img_model_id = 'facebook/dinov2-base'
16
  img_processor = AutoImageProcessor.from_pretrained(img_model_id)
17
  img_model = AutoModel.from_pretrained(img_model_id)
18
+ img_model.eval()
19
 
20
+ print("DINOv2 loaded successfully.")
 
 
 
 
21
 
22
  @app.get("/")
23
  def home():
24
  return {
25
  "status": "online",
26
+ "model": img_model_id,
27
+ "endpoint": "/embed/image"
 
 
28
  }
29
 
30
  @app.post("/embed/image")
 
33
  response = requests.get(image_url, timeout=10)
34
  img = Image.open(BytesIO(response.content)).convert("RGB")
35
 
36
+ # Process image
37
  inputs = img_processor(images=img, return_tensors="pt")
38
 
39
  with torch.no_grad():
40
  outputs = img_model(**inputs)
41
+ # Use CLS token for global representation (768 dimensions)
 
42
  embedding = outputs.last_hidden_state[:, 0, :].squeeze().tolist()
43
 
44
+ return {
45
+ "success": True,
46
+ "model": img_model_id,
47
+ "dimension": len(embedding),
48
+ "embedding": embedding
49
+ }
 
 
 
 
 
50
  except Exception as e:
51
  raise HTTPException(status_code=400, detail=str(e))
52