Bntuhan commited on
Commit
af33715
·
verified ·
1 Parent(s): 7c8c6b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -27
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import gradio as gr
2
  from PIL import Image
3
  import torch
 
 
4
 
5
  from transformers import (
6
  pipeline,
@@ -8,9 +10,6 @@ from transformers import (
8
  AutoImageProcessor
9
  )
10
 
11
- # -------------------------
12
- # Model & Processor yükle
13
- # -------------------------
14
  MODEL_ID = "cagrigungor/fire-prediction"
15
 
16
  model = AutoModelForImageClassification.from_pretrained(MODEL_ID)
@@ -19,43 +18,75 @@ processor = AutoImageProcessor.from_pretrained(MODEL_ID)
19
  pipe = pipeline(
20
  task="image-classification",
21
  model=model,
22
- image_processor=processor
 
23
  )
24
 
25
  # -------------------------
26
- # Inference fonksiyonu
27
  # -------------------------
28
- def predict(image: Image.Image):
29
  if image is None:
30
- return "Lütfen bir görüntü yükleyin."
31
 
32
  image = image.convert("RGB")
33
-
34
  results = pipe(image)
35
 
36
- # Sonuçları okunabilir hale getir
37
- output = []
38
- for r in results:
39
- label = r["label"]
40
- score = round(r["score"] * 100, 2)
41
- output.append(f"{label}: %{score}")
42
 
43
- return "\n".join(output)
44
 
45
  # -------------------------
46
- # Gradio UI
47
  # -------------------------
48
- app = gr.Interface(
49
- fn=predict,
50
- inputs=gr.Image(type="pil", label="Yangın Görüntüsü Yükle"),
51
- outputs=gr.Textbox(label="Tahmin Sonucu"),
52
- title="🔥 Wildfire Detection",
53
- description="ViT tabanlı yangın tespit modeli. Görüntü yükleyerek test edebilirsiniz.",
54
- allow_flagging="never"
55
- )
56
 
57
  # -------------------------
58
- # Run
59
  # -------------------------
60
- if __name__ == "__main__":
61
- 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)
 
18
  pipe = pipeline(
19
  task="image-classification",
20
  model=model,
21
+ image_processor=processor,
22
+ device=-1
23
  )
24
 
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()