Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +21 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
from qwen_vl_utils import process_vision_info
|
| 6 |
+
|
| 7 |
+
# Load model and processor
|
| 8 |
+
model_id = "DiagramAgent/Diagram_to_Code_Agent"
|
| 9 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
|
| 10 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 11 |
+
|
| 12 |
+
def generate_code(image):
|
| 13 |
+
messages = [{"role": "user", "content": [{"type": "image", "image": image}, {"type": "text", "text": "diagram"}]}]
|
| 14 |
+
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 15 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
| 16 |
+
inputs = processor(text=[text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt").to(model.device)
|
| 17 |
+
generated_ids = model.generate(**inputs, max_new_tokens=8192)
|
| 18 |
+
output_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
| 19 |
+
return output_text[0]
|
| 20 |
+
|
| 21 |
+
gr.Interface(fn=generate_code, inputs=gr.Image(type="pil"), outputs="text", title="Diagram to Code Agent").launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|
| 4 |
+
Pillow
|
| 5 |
+
tqdm
|
| 6 |
+
git+https://github.com/QwenLM/Qwen-VL.git
|