GhAyoub commited on
Commit
9d39de6
·
1 Parent(s): e95448d

[OCR API] Added initial API code.

Browse files
Files changed (2) hide show
  1. Dockerfile +29 -9
  2. app.py +44 -3
Dockerfile CHANGED
@@ -1,16 +1,36 @@
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
- USER user
8
- ENV PATH="/home/user/.local/bin:$PATH"
9
-
10
  WORKDIR /app
11
 
12
- 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
- CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
 
 
 
 
 
 
 
 
 
1
+ # Use Python 3.12 as the base image
2
+ FROM python:3.12
3
 
4
+ # Install system dependencies
5
+ RUN apt-get update && apt-get install -y \
6
+ ffmpeg \
7
+ git \
8
+ && rm -rf /var/lib/apt/lists/*
9
 
10
+ # Create a non-root user
11
  RUN useradd -m -u 1000 user
 
 
 
12
  WORKDIR /app
13
 
14
+ # Install Python dependencies directly
15
+ RUN pip install --no-cache-dir --upgrade pip && \
16
+ pip install --no-cache-dir \
17
+ torch \
18
+ torchvision \
19
+ git+https://github.com/huggingface/transformers \
20
+ accelerate \
21
+ qwen-vl-utils[decord]==0.0.8 \
22
+ fastapi \
23
+ uvicorn[standard]
24
 
25
+ # Copy application files
26
  COPY --chown=user . /app
27
+
28
+ # Switch to the non-root user
29
+ USER user
30
+
31
+ # Set environment variables
32
+ ENV HOME=/home/user \
33
+ PATH=/home/user/.local/bin:$PATH
34
+
35
+ # Command to run the application
36
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
app.py CHANGED
@@ -1,7 +1,48 @@
1
- from fastapi import FastAPI
 
 
 
2
 
3
  app = FastAPI()
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  @app.get("/")
6
- def greet_json():
7
- return {"Hello": "World!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Query
2
+ from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
3
+ from qwen_vl_utils import process_vision_info
4
+ import torch
5
 
6
  app = FastAPI()
7
 
8
+ checkpoint = "Qwen/Qwen2.5-VL-3B-Instruct"
9
+ min_pixels = 256*28*28
10
+ max_pixels = 1280*28*28
11
+ processor = AutoProcessor.from_pretrained(
12
+ checkpoint,
13
+ min_pixels=min_pixels,
14
+ max_pixels=max_pixels
15
+ )
16
+ model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
17
+ checkpoint,
18
+ torch_dtype=torch.bfloat16,
19
+ device_map="auto",
20
+ # attn_implementation="flash_attention_2",
21
+ )
22
+
23
  @app.get("/")
24
+ def read_root():
25
+ return {"message": "API is live. Use the /predict endpoint."}
26
+
27
+ @app.get("/predict")
28
+ def predict(image_url: str = Query(...), prompt: str = Query(...)):
29
+ messages = [
30
+ {"role": "system", "content": "You are a helpful assistant with vision abilities."},
31
+ {"role": "user", "content": [{"type": "image", "image": image_url}, {"type": "text", "text": prompt}]},
32
+ ]
33
+ text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
34
+ image_inputs, video_inputs = process_vision_info(messages)
35
+ inputs = processor(
36
+ text=[text],
37
+ images=image_inputs,
38
+ videos=video_inputs,
39
+ padding=True,
40
+ return_tensors="pt",
41
+ ).to(model.device)
42
+ with torch.no_grad():
43
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
44
+ generated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]
45
+ output_texts = processor.batch_decode(
46
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
47
+ )
48
+ return {"response": output_texts[0]}