Views
No views yet
1import torch
2from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
3from PIL import Image
4
5# 🎯 CHANGED: Use your HF model instead of local path
6model_path = "Tushar365/qwen2-vl-2b-sft-tushar365" # Your HF repo
7
8print(f"🚀 Loading model from Hugging Face: {model_path}")
9
10# Load your fine-tuned model from HF
11model = Qwen2VLForConditionalGeneration.from_pretrained(
12 model_path,
13 torch_dtype=torch.bfloat16,
14 device_map="auto",
15 trust_remote_code=True # Required for custom models
16)
17
18# Load processor and tokenizer (these stay the same)
19processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", trust_remote_code=True)
20tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-VL-2B-Instruct", trust_remote_code=True)
21
22print("✅ Model loaded successfully from Hugging Face!")
23
24def test_single_image(image_path, question):
25 """Test with a single image"""
26 # Load image
27 image = Image.open(image_path)
28
29 # Prepare the conversation
30 messages = [
31 {
32 "role": "user",
33 "content": [
34 {"type": "image", "image": image},
35 {"type": "text", "text": question}
36 ]
37 }
38 ]
39
40 # Apply chat template
41 text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
42
43 # Process inputs
44 inputs = processor(text=[text], images=[image], return_tensors="pt")
45 inputs = inputs.to(model.device)
46
47 # Generate response
48 with torch.no_grad():
49 generated_ids = model.generate(
50 **inputs,
51 max_new_tokens=512,
52 temperature=0.1,
53 do_sample=True,
54 pad_token_id=tokenizer.eos_token_id
55 )
56
57 # Decode response
58 generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
59
60 # Extract just the assistant's response
61 assistant_response = generated_text.split("assistant\n")[-1]
62 return assistant_response
63
64def test_disaster_assessment(pre_image_path, post_image_path):
65 """Test disaster assessment with before/after images"""
66 # Test pre-image first
67 pre_response = test_single_image(
68 pre_image_path,
69 "This is a pre-disaster satellite image. I will show you the post-disaster image next for comparison."
70 )
71 print("Pre-disaster response:", pre_response)
72 print("\n" + "="*50 + "\n")
73
74 # Test post-image with comparison task
75 post_response = test_single_image(
76 post_image_path,
77 "This is the post-disaster satellite image. Compare with the previous pre-disaster image and provide a comprehensive building damage assessment report."
78 )
79 print("Disaster assessment:", post_response)
80 return post_response
81
82# Test your model
83print("Testing fine-tuned Qwen2-VL model from Hugging Face...")
84
85# Test with your training images
86result = test_disaster_assessment(
87 "/pre_flood.png",
88 "/post_flood.png"
89)
901@misc{vonwerra2022trl,
2 title = {{TRL: Transformer Reinforcement Learning}},
3 author = {Leandro von Werra and Younes Belkada and Lewis Tunstall and Edward Beeching and Tristan Thrush and Nathan Lambert and Shengyi Huang and Kashif Rasul and Quentin Gallou{\'e}dec},
4 year = 2020,
5 journal = {GitHub repository},
6 publisher = {GitHub},
7 howpublished = {\url{https://github.com/huggingface/trl}}
8}