File size: 1,437 Bytes
ab3ecd9
 
 
25a0dc3
ab3ecd9
72c3059
6bcad45
ab3ecd9
 
 
 
 
 
 
 
 
25a0dc3
0f7e5ca
 
 
 
25a0dc3
0f7e5ca
 
25a0dc3
 
 
bddfec4
0f7e5ca
25a0dc3
ab3ecd9
 
72c3059
ab3ecd9
 
 
 
 
 
0f7e5ca
 
ab3ecd9
 
0f7e5ca
 
 
 
 
 
 
 
 
 
 
ab3ecd9
 
1a43104
9b775e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import base64

from fastapi import FastAPI, HTTPException
from PIL import Image
from pydantic import BaseModel
from tempfile import NamedTemporaryFile
from transformers import AutoModelForCausalLM, AutoTokenizer

app = FastAPI()

# define request body
class RequestData(BaseModel):
    prompt: str
    image: str


def load_model():
    model_id = "models"
    revision = "2024-08-26"
    model = AutoModelForCausalLM.from_pretrained(
        model_id, trust_remote_code=True, revision=revision
    )
    tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
    return model, tokenizer



MODEL, TOKENIZER = load_model()
print("INFO: Model & Tokenizer loaded!")

@app.get("/")
def greet_json():
    return {"message": "Server is UP!"}


@app.post("/query")
def query(data: RequestData):
    prompt = data.prompt
    image = data.image
    print(f"INFO: prompt - {prompt}")
    
    try:
        # decode base64 to image
        image = base64.b64decode(image)

        with NamedTemporaryFile(delete=True, suffix=".png") as temp_image:
            temp_image.write(image)
            temp_image.flush()

            image = Image.open(temp_image.name)
            enc_image = MODEL.encode_image(image)
            response = MODEL.answer_question(enc_image, str(prompt), TOKENIZER)

            return {"response": str(response)}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))