gguf to transformers model
Browse files- Issue with GGUF, changed to transformers
app.py
CHANGED
@@ -16,23 +16,18 @@ class RequestData(BaseModel):
|
|
16 |
|
17 |
|
18 |
def load_model():
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
22 |
)
|
23 |
-
|
24 |
-
|
25 |
-
repo_id="vikhyatk/moondream2",
|
26 |
-
filename="*text-model*",
|
27 |
-
chat_handler=chat_handler,
|
28 |
-
n_ctx=2048, # n_ctx should be increased to accommodate the image embedding
|
29 |
-
)
|
30 |
-
return llm
|
31 |
|
32 |
|
33 |
|
34 |
llm = load_model()
|
35 |
-
|
36 |
|
37 |
@app.get("/")
|
38 |
def greet_json():
|
@@ -43,27 +38,23 @@ def greet_json():
|
|
43 |
def query(data: RequestData):
|
44 |
prompt = data.prompt
|
45 |
image = data.image
|
46 |
-
|
|
|
47 |
try:
|
48 |
# decode base64 to image
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
messages = messages
|
63 |
-
)
|
64 |
-
output = response['choices'][0]['message']['content']
|
65 |
-
|
66 |
-
return {"response": str(output)}
|
67 |
except Exception as e:
|
68 |
raise HTTPException(status_code=500, detail=str(e))
|
69 |
|
|
|
16 |
|
17 |
|
18 |
def load_model():
|
19 |
+
model_id = "models"
|
20 |
+
revision = "2024-08-26"
|
21 |
+
model = AutoModelForCausalLM.from_pretrained(
|
22 |
+
model_id, trust_remote_code=True, revision=revision
|
23 |
)
|
24 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
|
25 |
+
return model, tokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
|
28 |
|
29 |
llm = load_model()
|
30 |
+
print("INFO: Model & Tokenizer loaded!")
|
31 |
|
32 |
@app.get("/")
|
33 |
def greet_json():
|
|
|
38 |
def query(data: RequestData):
|
39 |
prompt = data.prompt
|
40 |
image = data.image
|
41 |
+
print(f"INFO: prompt - {prompt}")
|
42 |
+
|
43 |
try:
|
44 |
# decode base64 to image
|
45 |
+
image = base64.b64decode(image)
|
46 |
+
|
47 |
+
with NamedTemporaryFile(delete=True, suffix=".png") as temp_image:
|
48 |
+
temp_image.write(image)
|
49 |
+
temp_image.flush()
|
50 |
+
|
51 |
+
image = Image.open(temp_image.name)
|
52 |
+
|
53 |
+
image = Image.open("<IMAGE_PATH>")
|
54 |
+
enc_image = MODEL.encode_image(image)
|
55 |
+
response = MODEL.answer_question(enc_image, str(prompt), TOKENIZER)
|
56 |
+
|
57 |
+
return {"response": str(response)}
|
|
|
|
|
|
|
|
|
|
|
58 |
except Exception as e:
|
59 |
raise HTTPException(status_code=500, detail=str(e))
|
60 |
|