dmedhi commited on
Commit
25a0dc3
1 Parent(s): ab3ecd9

add transformers moondream

Browse files
Files changed (3) hide show
  1. Dockerfile +0 -1
  2. app.py +20 -9
  3. requirements.txt +2 -0
Dockerfile CHANGED
@@ -13,6 +13,5 @@ COPY --chown=user ./requirements.txt requirements.txt
13
  RUN pip install --no-cache-dir --upgrade -r requirements.txt
14
 
15
  COPY --chown=user . /app
16
- RUN chmod +x /app/moondream
17
 
18
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
 
13
  RUN pip install --no-cache-dir --upgrade -r requirements.txt
14
 
15
  COPY --chown=user . /app
 
16
 
17
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
app.py CHANGED
@@ -3,7 +3,9 @@ import subprocess
3
  from tempfile import NamedTemporaryFile
4
 
5
  from fastapi import FastAPI, HTTPException
 
6
  from pydantic import BaseModel
 
7
 
8
  app = FastAPI()
9
 
@@ -14,6 +16,20 @@ class RequestData(BaseModel):
14
  image: str
15
 
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  @app.get("/")
18
  def greet_json():
19
  return {"Hello": "World!"}
@@ -32,15 +48,10 @@ def query(data: RequestData):
32
  temp_image.write(image)
33
  temp_image.flush()
34
 
35
- result = subprocess.run(
36
- ["./moondream", "--prompt", prompt, "--image", temp_image.name],
37
- capture_output=True,
38
- text=True,
39
- )
40
-
41
- if result.returncode != 0:
42
- raise Exception(result.stderr)
43
 
44
- return {"response": result.stdout}
45
  except Exception as e:
46
  raise HTTPException(status_code=500, detail=str(e))
 
3
  from tempfile import NamedTemporaryFile
4
 
5
  from fastapi import FastAPI, HTTPException
6
+ from PIL import Image
7
  from pydantic import BaseModel
8
+ from transformers import AutoModelForCausalLM, AutoTokenizer
9
 
10
  app = FastAPI()
11
 
 
16
  image: str
17
 
18
 
19
+ def load_model():
20
+ model_id = "vikhyatk/moondream2"
21
+ revision = "2024-08-26"
22
+ model = AutoModelForCausalLM.from_pretrained(
23
+ model_id, trust_remote_code=True, revision=revision
24
+ )
25
+ tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
26
+ return model, tokenizer
27
+
28
+
29
+ MODEL, TOKENIZER = load_model()
30
+ print("INFO: Model loaded successfully!")
31
+
32
+
33
  @app.get("/")
34
  def greet_json():
35
  return {"Hello": "World!"}
 
48
  temp_image.write(image)
49
  temp_image.flush()
50
 
51
+ image = Image.open(temp_image.name)
52
+ enc_image = MODEL.encode_image(image)
53
+ response = MODEL.answer_question(enc_image, str(prompt), TOKENIZER)
 
 
 
 
 
54
 
55
+ return {"response": str(response)}
56
  except Exception as e:
57
  raise HTTPException(status_code=500, detail=str(e))
requirements.txt CHANGED
@@ -1,3 +1,5 @@
1
  fastapi
2
  uvicorn[standard]
3
  pydantic
 
 
 
1
  fastapi
2
  uvicorn[standard]
3
  pydantic
4
+ transformers
5
+ torch