Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor | |
| from qwen_vl_utils import process_vision_info | |
| import torch | |
| # Specify the local cache path for models | |
| local_path = "Qwen/Qwen2.5-VL-7B-Instruct" | |
| # Load model and processor | |
| model = Qwen2_5_VLForConditionalGeneration.from_pretrained( | |
| local_path, torch_dtype="auto", device_map="auto" | |
| ) | |
| processor = AutoProcessor.from_pretrained(local_path) | |
| # Function to process image and text and generate the output | |
| # Specify a duration to avoid timeout | |
| def generate_output(image, text, button_click): | |
| # Prepare input data | |
| messages = [ | |
| { | |
| "role": "user", | |
| "content": [ | |
| {"type": "image", "image": image}, | |
| {"type": "text", "text": text}, | |
| ], | |
| } | |
| ] | |
| # Prepare inputs for the model | |
| text_input = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) | |
| image_inputs, video_inputs = process_vision_info(messages) | |
| inputs = processor( | |
| text=[text_input], | |
| images=image_inputs, | |
| videos=video_inputs, | |
| padding=True, | |
| return_tensors="pt", | |
| ) | |
| inputs = inputs.to("cuda") | |
| # Generate the output | |
| generated_ids = model.generate(**inputs, max_new_tokens=128) | |
| generated_ids_trimmed = [ | |
| out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids) | |
| ] | |
| output_text = processor.batch_decode( | |
| generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False | |
| ) | |
| return output_text[0] | |
| # Create Gradio interface | |
| iface = gr.Interface( | |
| fn=generate_output, | |
| inputs=[ | |
| gr.Image(type="pil", label="Upload Image"), | |
| gr.Textbox(lines=2, placeholder="Enter a question related to the image", label="Input Text"), | |
| ], | |
| outputs=gr.Textbox(label="Model Output"), | |
| ) | |
| # Launch the Gradio interface | |
| iface.launch() | |