AdityaAjithKumar commited on
Commit
3b6109d
·
1 Parent(s): c0b019c

Add Qwen3.5-0.8B OCR API

Browse files
Files changed (3) hide show
  1. Dockerfile +6 -0
  2. app.py +36 -0
  3. requirements.txt +5 -0
Dockerfile ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ FROM python:3.12-slim
2
+ WORKDIR /app
3
+ COPY . .
4
+ RUN pip install --no-cache-dir fastapi uvicorn torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu transformers pillow
5
+ EXPOSE 7860
6
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File, Form
2
+ from pydantic import BaseModel
3
+ from transformers import AutoModelForCausalLM, AutoProcessor
4
+ import torch
5
+ from PIL import Image
6
+ import io
7
+
8
+ app = FastAPI(title="Qwen3.5-0.8B OCR API")
9
+
10
+ model_id = "Qwen/Qwen3.5-0.8B"
11
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="cpu")
12
+ processor = AutoProcessor.from_pretrained(model_id)
13
+
14
+
15
+ @app.post("/ocr")
16
+ async def ocr(image: UploadFile = File(...), prompt: str = Form("Extract all text from this image accurately")):
17
+ img = Image.open(io.BytesIO(await image.read()))
18
+
19
+ messages = [{"role": "user", "content": [
20
+ {"type": "image", "image": img},
21
+ {"type": "text", "text": prompt}
22
+ ]}]
23
+
24
+ text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
25
+ inputs = processor(text, images=img, return_tensors="pt").to("cpu")
26
+
27
+ outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.7)
28
+ response = processor.decode(outputs[0], skip_special_tokens=True)
29
+
30
+ return {"extracted_text": response}
31
+
32
+
33
+ # Simple test endpoint
34
+ @app.get("/")
35
+ async def root():
36
+ return {"message": "Qwen3.5-0.8B OCR API is running! Use POST /ocr with image file."}
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ torch --index-url https://download.pytorch.org/whl/cpu
4
+ transformers
5
+ pillow