kedimestan commited on
Commit
9445aeb
·
verified ·
1 Parent(s): c12e9d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -35
app.py CHANGED
@@ -1,60 +1,73 @@
1
  import gradio as gr
2
  import pandas as pd
 
3
  from shapely.geometry import Point, Polygon
4
 
5
  def load_csv(file):
6
  return pd.read_csv(file.name)
7
 
8
  def update_dropdowns(df):
9
- return gr.Dropdown(choices=df.columns.tolist()), gr.Dropdown(choices=df.columns.tolist())
10
 
11
- def create_scatter(df, x_col, y_col):
12
- if df is None or x_col is None or y_col is None:
13
  return None
14
- return gr.ScatterPlot(
15
- value=df[[x_col, y_col]],
16
- x=x_col,
17
- y=y_col,
18
- title=f"{x_col} vs {y_col}",
19
- interactive=True,
20
- tooltip=list(df.columns),
21
- color="blue"
22
- )
23
 
24
- def filter_points(selected_data, df, x_col, y_col):
25
- if not selected_data or not df.empty:
26
- return pd.DataFrame()
27
 
28
- # Seçilen polygon noktalarını al
29
- points = selected_data["points"]
30
- if len(points) < 3:
 
 
31
  return pd.DataFrame()
32
 
33
  # Polygon oluştur
34
- polygon = Polygon([(p["x"], p["y"]) for p in points])
 
 
 
 
 
 
35
 
36
- # İçerde kalan noktaları bul
37
- mask = df.apply(lambda row: polygon.contains(Point(row[x_col], row[y_col])), axis=1)
38
  return df[mask]
39
 
40
  with gr.Blocks() as demo:
41
- gr.Markdown("## Interaktif Veri Seçim Aracı")
42
 
43
- df_state = gr.State(pd.DataFrame())
44
 
45
  with gr.Row():
46
- csv_upload = gr.File(label="CSV Yükle", file_types=[".csv"])
47
- x_col = gr.Dropdown(label="X Sütunu")
48
- y_col = gr.Dropdown(label="Y Sütunu")
49
 
50
  plot = gr.ScatterPlot(
51
- label="Veri Dağılımı",
52
- show_export_button=True,
 
 
53
  interactive=True
54
  )
55
- results = gr.DataFrame(label="Seçilen Veriler")
56
 
57
- # CSV yükleme işlemi
 
 
 
 
 
 
58
  csv_upload.upload(
59
  load_csv,
60
  inputs=csv_upload,
@@ -65,20 +78,19 @@ with gr.Blocks() as demo:
65
  outputs=[x_col, y_col]
66
  )
67
 
68
- # Sütun değişikliklerinde plot güncelleme
69
  x_col.change(
70
- create_scatter,
71
  inputs=[df_state, x_col, y_col],
72
  outputs=plot
73
  )
74
-
75
  y_col.change(
76
- create_scatter,
77
  inputs=[df_state, x_col, y_col],
78
  outputs=plot
79
  )
80
 
81
- # Polygon seçim etkinliği
82
  plot.select(
83
  filter_points,
84
  inputs=[df_state, x_col, y_col],
 
1
  import gradio as gr
2
  import pandas as pd
3
+ import numpy as np
4
  from shapely.geometry import Point, Polygon
5
 
6
  def load_csv(file):
7
  return pd.read_csv(file.name)
8
 
9
  def update_dropdowns(df):
10
+ return gr.Dropdown(choices=df.columns), gr.Dropdown(choices=df.columns)
11
 
12
+ def create_plot(df, x_col, y_col):
13
+ if not x_col or not y_col:
14
  return None
15
+ return {
16
+ "x": df[x_col].tolist(),
17
+ "y": df[y_col].tolist(),
18
+ "color": "blue",
19
+ "tooltip": df.columns.tolist()
20
+ }
 
 
 
21
 
22
+ def filter_points(event: gr.SelectData, df, x_col, y_col):
23
+ if not hasattr(filter_points, "points"):
24
+ filter_points.points = []
25
 
26
+ # Yeni noktayı ekle
27
+ filter_points.points.append((event.x, event.y))
28
+
29
+ # 3'ten az nokta varsa boş dön
30
+ if len(filter_points.points) < 3:
31
  return pd.DataFrame()
32
 
33
  # Polygon oluştur
34
+ polygon = Polygon(filter_points.points)
35
+
36
+ # İçerde kalanları bul
37
+ mask = df[[x_col, y_col]].apply(
38
+ lambda row: polygon.contains(Point(row[x_col], row[y_col])),
39
+ axis=1
40
+ )
41
 
42
+ # Sonuçları döndürmeden önce noktaları sıfırla
43
+ filter_points.points = []
44
  return df[mask]
45
 
46
  with gr.Blocks() as demo:
47
+ gr.Markdown("## 🎯 Tam Çalışan Veri Seçim Uygulaması")
48
 
49
+ df_state = gr.State()
50
 
51
  with gr.Row():
52
+ csv_upload = gr.File(label="1. CSV Yükle", file_types=[".csv"])
53
+ x_col = gr.Dropdown(label="2. X Sütunu Seç")
54
+ y_col = gr.Dropdown(label="3. Y Sütunu Seç")
55
 
56
  plot = gr.ScatterPlot(
57
+ label="4. Noktaları Seçmek İçin Polygon Çiz",
58
+ x_title="X Ekseni",
59
+ y_title="Y Ekseni",
60
+ show_label=True,
61
  interactive=True
62
  )
 
63
 
64
+ results = gr.DataFrame(
65
+ label="Seçilen Veriler",
66
+ headers=["Tüm Sütunlar Gözükecek"],
67
+ max_rows=20
68
+ )
69
+
70
+ # CSV yükleme
71
  csv_upload.upload(
72
  load_csv,
73
  inputs=csv_upload,
 
78
  outputs=[x_col, y_col]
79
  )
80
 
81
+ # Plot güncelleme
82
  x_col.change(
83
+ create_plot,
84
  inputs=[df_state, x_col, y_col],
85
  outputs=plot
86
  )
 
87
  y_col.change(
88
+ create_plot,
89
  inputs=[df_state, x_col, y_col],
90
  outputs=plot
91
  )
92
 
93
+ # Polygon seçimi
94
  plot.select(
95
  filter_points,
96
  inputs=[df_state, x_col, y_col],