Upload folder using huggingface_hub
Browse files- README.md +3 -9
- app.py +82 -0
- packages.txt +1 -0
- requirements.txt +9 -0
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title: Qwen
|
| 3 |
-
emoji: 🏃
|
| 4 |
-
colorFrom: yellow
|
| 5 |
-
colorTo: gray
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 4.44.0
|
| 8 |
app_file: app.py
|
| 9 |
-
|
|
|
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Qwen-OCR
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
app_file: app.py
|
| 4 |
+
sdk: gradio
|
| 5 |
+
sdk_version: 4.41.0
|
| 6 |
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
|
| 5 |
+
from qwen_vl_utils import process_vision_info
|
| 6 |
+
from byaldi import RAGMultiModalModel
|
| 7 |
+
import re
|
| 8 |
+
|
| 9 |
+
min_pixels = 256 * 28 * 28
|
| 10 |
+
max_pixels = 1280 * 28 * 28
|
| 11 |
+
|
| 12 |
+
def model_inference(images, text):
|
| 13 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 14 |
+
"Qwen/Qwen2-VL-2B-Instruct",
|
| 15 |
+
trust_remote_code=True,
|
| 16 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 17 |
+
)
|
| 18 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", min_pixels=min_pixels,max_pixels=max_pixels)
|
| 19 |
+
|
| 20 |
+
images = [{"type": "image", "image": Image.open(image[0])} for image in images]
|
| 21 |
+
images.append({"type": "text", "text": text})
|
| 22 |
+
|
| 23 |
+
messages = [{"role": "user", "content": images}]
|
| 24 |
+
|
| 25 |
+
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 26 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
| 27 |
+
inputs = processor(
|
| 28 |
+
text=[text],
|
| 29 |
+
images=image_inputs,
|
| 30 |
+
videos=video_inputs,
|
| 31 |
+
padding=True,
|
| 32 |
+
return_tensors="pt",
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 36 |
+
inputs = inputs.to(device)
|
| 37 |
+
model = model.to(device)
|
| 38 |
+
|
| 39 |
+
generated_ids = model.generate(**inputs, max_new_tokens=512)
|
| 40 |
+
generated_ids_trimmed = [
|
| 41 |
+
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
output_text = processor.batch_decode(
|
| 45 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
del model
|
| 49 |
+
del processor
|
| 50 |
+
return output_text[0]
|
| 51 |
+
|
| 52 |
+
def search_and_highlight(text, keywords):
|
| 53 |
+
if not keywords:
|
| 54 |
+
return text
|
| 55 |
+
|
| 56 |
+
keywords = [kw.strip().lower() for kw in keywords.split(',')]
|
| 57 |
+
highlighted_text = text
|
| 58 |
+
|
| 59 |
+
for keyword in keywords:
|
| 60 |
+
pattern = re.compile(re.escape(keyword), re.IGNORECASE)
|
| 61 |
+
highlighted_text = pattern.sub(f'**{keyword}**', highlighted_text)
|
| 62 |
+
|
| 63 |
+
return highlighted_text
|
| 64 |
+
|
| 65 |
+
def process_and_search(images, keywords):
|
| 66 |
+
extracted_text = model_inference(images, keywords)
|
| 67 |
+
|
| 68 |
+
highlighted_text = search_and_highlight(extracted_text, keywords)
|
| 69 |
+
|
| 70 |
+
return highlighted_text
|
| 71 |
+
|
| 72 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 73 |
+
keywords = gr.Textbox(placeholder="Enter keywords to search (comma-separated)", label="Search Keywords")
|
| 74 |
+
output_gallery = gr.Gallery(label="Image", height=600, show_label=True)
|
| 75 |
+
|
| 76 |
+
answer_button = gr.Button("Answer and Search", variant="primary")
|
| 77 |
+
output = gr.Markdown(label="Output with Highlighted Search Results")
|
| 78 |
+
|
| 79 |
+
answer_button.click(process_and_search, inputs=[output_gallery, keywords], outputs=output)
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
demo.queue(max_size=10).launch(share=True)
|
packages.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
poppler-utils
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
colpali-engine==0.2.0
|
| 2 |
+
pdf2image
|
| 3 |
+
GPUtil
|
| 4 |
+
accelerate==0.30.1
|
| 5 |
+
mteb>=1.12.22
|
| 6 |
+
git+https://github.com/huggingface/transformers
|
| 7 |
+
qwen-vl-utils
|
| 8 |
+
torchvision
|
| 9 |
+
fastapi<0.113.0
|