import gradio as gr from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor from qwen_vl_utils import process_vision_info # Load the model and processor model = Qwen2VLForConditionalGeneration.from_pretrained( "Qwen/Qwen2-VL-72B-Instruct", torch_dtype="auto", device_map="auto" ) processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct") # Define a function to process input and generate a response def generate_response(image, text): # Prepare the input messages = [ { "role": "user", "content": [ {"type": "image", "image": image}, {"type": "text", "text": text}, ], } ] # Process the input data text_data = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) image_inputs, video_inputs = process_vision_info(messages) inputs = processor( text=[text_data], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt", ) # 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 the Gradio interface interface = gr.Interface( fn=generate_response, inputs=[gr.Image(type="pil", label="Input Image"), gr.Textbox(label="Input Text")], outputs="text", title="Qwen2-VL-72B-Instruct", description="Generate AI responses based on image and text input using Qwen2-VL-72B-Instruct.", ) # Launch the app interface.launch()