lukiod commited on
Commit
5aa812a
·
verified ·
1 Parent(s): d3d1961

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from fastapi import FastAPI, File, UploadFile
3
+ from pydantic import BaseModel
4
+ from typing import List
5
+ import torch
6
+ from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
7
+ from qwen_vl_utils import process_vision_info
8
+ from byaldi import RAGMultiModalModel
9
+ from PIL import Image
10
+ import io
11
+
12
+ # Initialize FastAPI app
13
+ app = FastAPI()
14
+
15
+ # Define model and processor paths
16
+ RAG_MODEL = "vidore/colpali"
17
+ QWN_MODEL = "Qwen/Qwen2-VL-7B-Instruct"
18
+ QWN_PROCESSOR = "Qwen/Qwen2-VL-2B-Instruct"
19
+
20
+ # Load models and processors
21
+ RAG = RAGMultiModalModel.from_pretrained(RAG_MODEL)
22
+
23
+ model = Qwen2VLForConditionalGeneration.from_pretrained(
24
+ QWN_MODEL,
25
+ torch_dtype=torch.bfloat16,
26
+ attn_implementation="flash_attention_2",
27
+ device_map="auto",
28
+ trust_remote_code=True
29
+ ).cuda().eval()
30
+
31
+ processor = AutoProcessor.from_pretrained(QWN_PROCESSOR, trust_remote_code=True)
32
+
33
+ # Define request model
34
+ class DocumentRequest(BaseModel):
35
+ text_query: str
36
+
37
+ # Define processing function
38
+ def document_rag(text_query, image):
39
+ messages = [
40
+ {
41
+ "role": "user",
42
+ "content": [
43
+ {
44
+ "type": "image",
45
+ "image": image,
46
+ },
47
+ {"type": "text", "text": text_query},
48
+ ],
49
+ }
50
+ ]
51
+ text = processor.apply_chat_template(
52
+ messages, tokenize=False, add_generation_prompt=True
53
+ )
54
+ image_inputs, video_inputs = process_vision_info(messages)
55
+ inputs = processor(
56
+ text=[text],
57
+ images=image_inputs,
58
+ videos=video_inputs,
59
+ padding=True,
60
+ return_tensors="pt",
61
+ )
62
+ inputs = inputs.to("cuda")
63
+ generated_ids = model.generate(**inputs, max_new_tokens=50)
64
+ generated_ids_trimmed = [
65
+ out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
66
+ ]
67
+ output_text = processor.batch_decode(
68
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
69
+ )
70
+ return output_text[0]
71
+
72
+ # Define API endpoints
73
+ @app.post("/process_document")
74
+ async def process_document(request: DocumentRequest, file: UploadFile = File(...)):
75
+ # Read and process the uploaded file
76
+ contents = await file.read()
77
+ image = Image.open(io.BytesIO(contents))
78
+
79
+ # Process the document
80
+ result = document_rag(request.text_query, image)
81
+
82
+ return {"result": result}
83
+
84
+ if __name__ == "__main__":
85
+ import uvicorn
86
+ uvicorn.run(app, host="0.0.0.0", port=8000)