cagrigungor commited on
Commit
1fda270
·
verified ·
1 Parent(s): 1334ad0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -57
app.py CHANGED
@@ -1,7 +1,8 @@
1
  import gradio as gr
2
  from PIL import Image
3
  import torch
4
- import tempfile
 
5
 
6
  from transformers import (
7
  pipeline,
@@ -9,11 +10,6 @@ from transformers import (
9
  AutoImageProcessor
10
  )
11
 
12
- from staticmap import StaticMap, CircleMarker
13
-
14
- # -------------------------
15
- # Model yükle
16
- # -------------------------
17
  MODEL_ID = "cagrigungor/fire-prediction"
18
 
19
  model = AutoModelForImageClassification.from_pretrained(MODEL_ID)
@@ -29,69 +25,68 @@ pipe = pipeline(
29
  # -------------------------
30
  # Inference
31
  # -------------------------
32
- def predict(image: Image.Image):
33
  if image is None:
34
- return {}
35
 
36
  image = image.convert("RGB")
37
  results = pipe(image)
38
 
39
  return {r["label"]: float(r["score"]) for r in results}
40
 
41
- # -------------------------
42
- # OSM'den görüntü üret
43
- # -------------------------
44
- def osm_to_image(lat, lon, zoom):
45
- m = StaticMap(224, 224)
46
- marker = CircleMarker((lon, lat), "red", 6)
47
- m.add_marker(marker)
48
-
49
- image = m.render(zoom=zoom)
50
- return image
51
 
52
  # -------------------------
53
- # OSM + Prediction
54
  # -------------------------
55
- def predict_from_osm(lat, lon, zoom):
56
- img = osm_to_image(lat, lon, zoom)
57
- preds = predict(img)
58
- return img, preds
59
 
60
- # -------------------------
61
- # UI
62
- # -------------------------
63
- with gr.Blocks(title="🔥 Wildfire Detection + OSM") as app:
64
- gr.Markdown("## 🔥 Wildfire Detection (Upload + OpenStreetMap)")
65
-
66
- with gr.Tab("📁 Manuel Görüntü Yükle"):
67
- upload_img = gr.Image(type="pil", label="Görüntü Yükle")
68
- upload_btn = gr.Button("Tahmin Et")
69
- upload_out = gr.Label(num_top_classes=2)
70
-
71
- upload_btn.click(
72
- fn=predict,
73
- inputs=upload_img,
74
- outputs=upload_out
75
- )
76
-
77
- with gr.Tab("🗺 OpenStreetMap"):
78
- lat = gr.Number(label="Latitude", value=39.0)
79
- lon = gr.Number(label="Longitude", value=35.0)
80
- zoom = gr.Slider(5, 15, value=10, step=1, label="Zoom")
81
 
82
- osm_btn = gr.Button("Haritadan Tahmin Et")
83
-
84
- osm_img = gr.Image(label="OSM Görüntüsü")
85
- osm_out = gr.Label(num_top_classes=2)
86
-
87
- osm_btn.click(
88
- fn=predict_from_osm,
89
- inputs=[lat, lon, zoom],
90
- outputs=[osm_img, osm_out]
91
- )
92
 
93
  # -------------------------
94
- # Run
95
  # -------------------------
96
- if __name__ == "__main__":
97
- app.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from PIL import Image
3
  import torch
4
+ import base64
5
+ import io
6
 
7
  from transformers import (
8
  pipeline,
 
10
  AutoImageProcessor
11
  )
12
 
 
 
 
 
 
13
  MODEL_ID = "cagrigungor/fire-prediction"
14
 
15
  model = AutoModelForImageClassification.from_pretrained(MODEL_ID)
 
25
  # -------------------------
26
  # Inference
27
  # -------------------------
28
+ def predict_from_image(image):
29
  if image is None:
30
+ return None
31
 
32
  image = image.convert("RGB")
33
  results = pipe(image)
34
 
35
  return {r["label"]: float(r["score"]) for r in results}
36
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  # -------------------------
39
+ # Base64 (haritadan gelen)
40
  # -------------------------
41
+ def predict_from_base64(base64_str):
42
+ if base64_str is None:
43
+ return None
 
44
 
45
+ image_bytes = base64.b64decode(base64_str.split(",")[1])
46
+ image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
47
+ return predict_from_image(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  # -------------------------
51
+ # UI
52
  # -------------------------
53
+ with gr.Blocks(title="🔥 Wildfire Detection with Map") as app:
54
+ gr.Markdown("## 🔥 Wildfire Detection (OSM + Image Upload)")
55
+
56
+ with gr.Tabs():
57
+ # -------------------------
58
+ # TAB 1: Manual Upload
59
+ # -------------------------
60
+ with gr.Tab("📤 Resim Yükle"):
61
+ img_input = gr.Image(type="pil", label="Görüntü Yükle")
62
+ btn1 = gr.Button("Tahmin Et")
63
+ out1 = gr.Label(num_top_classes=2)
64
+
65
+ btn1.click(
66
+ fn=predict_from_image,
67
+ inputs=img_input,
68
+ outputs=out1
69
+ )
70
+
71
+ # -------------------------
72
+ # TAB 2: Map
73
+ # -------------------------
74
+ with gr.Tab("🗺️ Harita (OSM)"):
75
+ gr.HTML("""
76
+ <iframe
77
+ src="/map.html"
78
+ style="width:100%; height:600px; border:none;">
79
+ </iframe>
80
+ """)
81
+
82
+ base64_input = gr.Textbox(visible=False)
83
+ btn2 = gr.Button("Haritadan Tahmin Et")
84
+ out2 = gr.Label(num_top_classes=2)
85
+
86
+ btn2.click(
87
+ fn=predict_from_base64,
88
+ inputs=base64_input,
89
+ outputs=out2
90
+ )
91
+
92
+ app.launch()