File size: 3,418 Bytes
146e1eb
 
 
 
 
22f3ff8
 
bb49a7b
 
 
 
22f3ff8
 
c9e461d
22f3ff8
 
bb49a7b
22f3ff8
bb49a7b
22f3ff8
146e1eb
bb49a7b
22f3ff8
146e1eb
bb49a7b
 
146e1eb
22f3ff8
 
 
 
 
146e1eb
bb49a7b
146e1eb
bb49a7b
146e1eb
bb49a7b
22f3ff8
bb49a7b
22f3ff8
 
bb49a7b
22f3ff8
 
 
 
 
bb49a7b
22f3ff8
 
 
 
bb49a7b
22f3ff8
 
 
 
 
 
 
 
 
 
 
 
 
 
bb49a7b
22f3ff8
 
bb49a7b
22f3ff8
 
 
 
 
 
 
bb49a7b
22f3ff8
 
 
 
 
 
 
 
 
 
 
 
 
bb49a7b
146e1eb
bb49a7b
146e1eb
bb49a7b
146e1eb
22f3ff8
146e1eb
bb49a7b
22f3ff8
 
bb49a7b
 
 
 
22f3ff8
146e1eb
bb49a7b
22f3ff8
c9e461d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import torch
from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
from qwen_vl_utils import process_vision_info
import gradio as gr
from PIL import Image
from huggingface_hub import login
import os
import warnings

# 抑制警告
warnings.filterwarnings("ignore", category=RuntimeWarning)

# ========== 使用你的 secret 名称 fmv 登录 ==========
token = os.getenv("fmv")
if token:
    login(token=token)
    print("Successfully logged in with token!")
else:
    print("Warning: Token not found")
# ==========================================

# Hugging Face model repository path
model_path = "hiko1999/Qwen2-Wildfire-2B"

# Load model and processor
print(f"Loading model: {model_path}")
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = Qwen2VLForConditionalGeneration.from_pretrained(
    model_path, 
    torch_dtype=torch.bfloat16,
    device_map="cpu"
)
processor = AutoProcessor.from_pretrained(model_path)
print("Model loaded successfully!")

# Define prediction function
def predict(image):
    """Process image and generate description"""
    if image is None:
        return "Error: No image uploaded"
    
    try:
        # Build message with English prompt
        messages = [
            {
                "role": "user",
                "content": [
                    {"type": "image", "image": image},
                    {"type": "text", "text": "Describe this wildfire scene in English. Include details about the fire intensity, affected area, and visible environmental conditions."}
                ]
            }
        ]
        
        # Process input
        text = processor.apply_chat_template(
            messages, 
            tokenize=False, 
            add_generation_prompt=True
        )
        image_inputs, video_inputs = process_vision_info(messages)
        inputs = processor(
            text=[text], 
            images=image_inputs, 
            videos=video_inputs, 
            padding=True, 
            return_tensors="pt"
        )
        
        # Ensure running on CPU
        inputs = inputs.to("cpu")
        
        # Generate output
        generated_ids = model.generate(
            **inputs, 
            max_new_tokens=256,
            do_sample=True,
            temperature=0.7
        )
        
        # Decode output
        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]
        
    except Exception as e:
        return f"Prediction failed: {str(e)}"

# Gradio interface function
def gradio_interface(image):
    """Main function for Gradio interface"""
    result = predict(image)
    return result

# Create Gradio interface (all in English)
interface = gr.Interface(
    fn=gradio_interface,
    inputs=gr.Image(type="pil", label="Upload Wildfire Image"),
    outputs=gr.Textbox(label="AI Analysis Result", lines=10),
    title="🔥 Wildfire Scene Analysis System",
    description="Upload a wildfire-related image and AI will automatically analyze and describe the fire situation in English."
)

# Launch interface
if __name__ == "__main__":
    interface.launch(share=False)