Ahmed007 commited on
Commit
7519f79
1 Parent(s): 0375216

Add application file

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +21 -0
  3. requirements.txt +9 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ RUN useradd -m -u 1000 user
7
+
8
+ WORKDIR /app
9
+
10
+ COPY --chown=user ./requirements.txt requirements.txt
11
+
12
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
13
+
14
+ COPY --chown=user . /app
15
+
16
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, Form, UploadFile
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ from PIL import Image
4
+ import io
5
+
6
+ app = FastAPI()
7
+
8
+ model_id = "vikhyatk/moondream2"
9
+ revision = "2024-05-20"
10
+ model = AutoModelForCausalLM.from_pretrained(
11
+ model_id, trust_remote_code=True, revision=revision
12
+ )
13
+ tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)
14
+
15
+ @app.post("/analyze-image/")
16
+ async def analyze_image(file: UploadFile = File(...), question: str = Form(...)):
17
+ contents = await file.read()
18
+ image = Image.open(io.BytesIO(contents))
19
+ enc_image = model.encode_image(image)
20
+ answer = model.answer_question(enc_image, question, tokenizer)
21
+ return {"answer": answer}
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ datasets
4
+ transformers
5
+ accelerate
6
+ evaluate
7
+ bitsandbytes
8
+ accelerate
9
+ einops