marveljo commited on
Commit
8f0412a
·
verified ·
1 Parent(s): bb737c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -16
app.py CHANGED
@@ -1,28 +1,66 @@
1
- from fastapi import FastAPI, UploadFile
2
- from transformers import AutoProcessor, AutoModelForImageTextToText
 
3
  import torch
4
  from PIL import Image
5
  import io
6
 
7
- app = FastAPI()
 
 
8
 
9
  model_id = "HPAI-BSC/Aloe-Vision-7B-AR"
 
10
  processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
11
- model = AutoModelForImageTextToText.from_pretrained(
12
  model_id,
13
- dtype=torch.bfloat16,
14
  device_map="auto",
15
- trust_remote_code=True
16
  )
17
 
 
 
 
18
  @app.post("/analyze")
19
- async def analyze(file: UploadFile):
20
- image = Image.open(io.BytesIO(await file.read())).convert("RGB")
21
- messages = [{"role": "user", "content": [{"type": "image", "image": image}, {"type": "text", "text": "Describe this image"}]}]
22
- text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
23
- image_inputs = processor.process_vision_info(messages)
24
- inputs = processor(text=[text], **image_inputs, return_tensors="pt").to(model.device)
25
-
26
- generated = model.generate(**inputs, max_new_tokens=256)
27
- output_text = processor.batch_decode(generated, skip_special_tokens=True)[0]
28
- return {"result": output_text}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, Form
2
+ from fastapi.responses import JSONResponse
3
+ from transformers import AutoProcessor, AutoModelForVision2Seq
4
  import torch
5
  from PIL import Image
6
  import io
7
 
8
+ app = FastAPI(title="Aloe Vision Backend")
9
+
10
+ print("🚀 Loading model, please wait...")
11
 
12
  model_id = "HPAI-BSC/Aloe-Vision-7B-AR"
13
+
14
  processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
15
+ model = AutoModelForVision2Seq.from_pretrained(
16
  model_id,
17
+ torch_dtype=torch.bfloat16,
18
  device_map="auto",
19
+ trust_remote_code=True,
20
  )
21
 
22
+ print("✅ Model loaded!")
23
+
24
+
25
  @app.post("/analyze")
26
+ async def analyze(file: UploadFile, prompt: str = Form("Describe the image")):
27
+ """Receive an image + prompt, return model output."""
28
+ try:
29
+ image_bytes = await file.read()
30
+ image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
31
+
32
+ messages = [
33
+ {
34
+ "role": "user",
35
+ "content": [
36
+ {"type": "image", "image": image},
37
+ {"type": "text", "text": prompt}
38
+ ]
39
+ }
40
+ ]
41
+
42
+ text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
43
+ image_inputs = processor.process_vision_info(messages)
44
+ inputs = processor(
45
+ text=[text],
46
+ **image_inputs,
47
+ return_tensors="pt"
48
+ ).to(model.device)
49
+
50
+ generated = model.generate(
51
+ **inputs,
52
+ max_new_tokens=256,
53
+ do_sample=False,
54
+ eos_token_id=processor.tokenizer.eos_token_id,
55
+ )
56
+
57
+ output_text = processor.batch_decode(generated, skip_special_tokens=True)[0]
58
+ return {"result": output_text}
59
+
60
+ except Exception as e:
61
+ return JSONResponse(content={"error": str(e)}, status_code=500)
62
+
63
+
64
+ @app.get("/")
65
+ def root():
66
+ return {"status": "ok", "message": "Aloe Vision Backend running!"}