dmedhi commited on
Commit
0f7e5ca
1 Parent(s): 75f34fd

gguf to transformers model

Browse files

- Issue with GGUF, changed to transformers

Files changed (1) hide show
  1. app.py +22 -31
app.py CHANGED
@@ -16,23 +16,18 @@ class RequestData(BaseModel):
16
 
17
 
18
  def load_model():
19
- chat_handler = MoondreamChatHandler.from_pretrained(
20
- repo_id="vikhyatk/moondream2",
21
- filename="*mmproj*",
 
22
  )
23
-
24
- llm = Llama.from_pretrained(
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
- data_uri = f"""data:image/png;base64,{image}"""
50
- messages = [
51
- {"role": "system", "content": "You are an assistant who perfectly describes images."},
52
- {
53
- "role": "user",
54
- "content": [
55
- {"type": "image_url", "image_url": {"url": data_uri }},
56
- {"type" : "text", "text": f"""{prompt}"""}
57
- ]
58
- }
59
- ]
60
-
61
- response = llm.create_chat_completion(
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