Spaces:
Sleeping
Sleeping
Leon Seidel
commited on
Commit
·
89a6335
1
Parent(s):
8a84485
Add app
Browse files- app.py +51 -0
- prompt.py +16 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
|
2 |
+
from qwen_vl_utils import process_vision_info
|
3 |
+
from prompt import smoke_detection_prompt
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
model_name = "leon-se/ForestFireVLM-3B"
|
7 |
+
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
8 |
+
model_name, torch_dtype="auto", device_map="auto"
|
9 |
+
)
|
10 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
11 |
+
|
12 |
+
def generate(image):
|
13 |
+
messages = [
|
14 |
+
{
|
15 |
+
"role": "user",
|
16 |
+
"content": [
|
17 |
+
{
|
18 |
+
"type": "image",
|
19 |
+
"image": image,
|
20 |
+
},
|
21 |
+
{"type": "text", "text": smoke_detection_prompt},
|
22 |
+
],
|
23 |
+
}
|
24 |
+
]
|
25 |
+
|
26 |
+
# Preparation for inference
|
27 |
+
text = processor.apply_chat_template(
|
28 |
+
messages, tokenize=False, add_generation_prompt=True
|
29 |
+
)
|
30 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
31 |
+
inputs = processor(
|
32 |
+
text=[text],
|
33 |
+
images=image_inputs,
|
34 |
+
videos=video_inputs,
|
35 |
+
padding=True,
|
36 |
+
return_tensors="pt",
|
37 |
+
)
|
38 |
+
inputs = inputs.to("cuda")
|
39 |
+
|
40 |
+
# Inference: Generation of the output
|
41 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
42 |
+
generated_ids_trimmed = [
|
43 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
44 |
+
]
|
45 |
+
output_text = processor.batch_decode(
|
46 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
47 |
+
)
|
48 |
+
return output_text
|
49 |
+
|
50 |
+
demo = gr.Interface(fn=generate, inputs="image", outputs="label")
|
51 |
+
demo.launch()
|
prompt.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
smoke_detection_prompt = """
|
2 |
+
Analyze the aerial image from the UAV's camera and detect potential forest fires. Answer the following questions based on the image analysis
|
3 |
+
in a JSON schema. If a question cannot be determined from the image answer with 'Cannot be determined'.
|
4 |
+
|
5 |
+
forest_fire_smoke_visible: Is smoke from a forest fire visible in the image? Answer with one of the following options: ['Yes', 'No']
|
6 |
+
forest_fire_flames_visible: Are flames from a forest fire visible in the image? Answer with one of the following options: ['Yes', 'No']
|
7 |
+
confirm_uncontrolled_forest_fire: Can you confirm that this is an uncontrolled forest fire? Answer with one of the following options: ['Yes', 'Closer investigation required', 'No forest fire visible']
|
8 |
+
fire_state: What is the current state of the forest fire? Answer with one of the following options: ['Ignition Phase', 'Growth Phase', 'Fully Developed Phase', 'Decay Phase', 'Cannot be determined', 'No forest fire visible']
|
9 |
+
fire_type: What type of fire is it? Answer with one of the following options: ['Ground Fire', 'Surface Fire', 'Crown Fire', 'Cannot be determined', 'No forest fire visible']
|
10 |
+
fire_intensity: What is the intensity of the fire? Answer with one of the following options: ['Low', 'Moderate', 'High', 'Cannot be determined', 'No forest fire visible']
|
11 |
+
fire_size: What is the size of the fire? Answer with one of the following options: ['Small', 'Medium', 'Large', 'Cannot be determined', 'No forest fire visible']
|
12 |
+
fire_hotspots: Does the forest fire have multiple hotspots? Answer with one of the following options: ['Multiple hotspots', 'One hotspot', 'Cannot be determined', 'No forest fire visible']
|
13 |
+
infrastructure_nearby: Is there infrastructure visible near the forest fire? Answer with one of the following options: ['Yes', 'No', 'Cannot be determined', 'No forest fire visible']
|
14 |
+
people_nearby: Are there people visible near the forest fire? Answer with one of the following options: ['Yes', 'No', 'Cannot be determined', 'No forest fire visible']
|
15 |
+
tree_vitality: Describe the vitality of the trees around the fire. Answer with one of the following options: ['Vital', 'Moderate Vitality', 'Declining', 'Dead', 'Cannot be determined', 'No forest fire visible']
|
16 |
+
"""
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
qwen_vl_utils
|
2 |
+
transformers
|
3 |
+
gradio
|