Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
10 |
|
11 |
-
def
|
12 |
-
if
|
13 |
return None
|
14 |
-
return
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
tooltip=list(df.columns),
|
21 |
-
color="blue"
|
22 |
-
)
|
23 |
|
24 |
-
def filter_points(
|
25 |
-
if not
|
26 |
-
|
27 |
|
28 |
-
#
|
29 |
-
points
|
30 |
-
|
|
|
|
|
31 |
return pd.DataFrame()
|
32 |
|
33 |
# Polygon oluştur
|
34 |
-
polygon = Polygon(
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
#
|
37 |
-
|
38 |
return df[mask]
|
39 |
|
40 |
with gr.Blocks() as demo:
|
41 |
-
gr.Markdown("##
|
42 |
|
43 |
-
df_state = gr.State(
|
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="
|
52 |
-
|
|
|
|
|
53 |
interactive=True
|
54 |
)
|
55 |
-
results = gr.DataFrame(label="Seçilen Veriler")
|
56 |
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
#
|
69 |
x_col.change(
|
70 |
-
|
71 |
inputs=[df_state, x_col, y_col],
|
72 |
outputs=plot
|
73 |
)
|
74 |
-
|
75 |
y_col.change(
|
76 |
-
|
77 |
inputs=[df_state, x_col, y_col],
|
78 |
outputs=plot
|
79 |
)
|
80 |
|
81 |
-
# Polygon
|
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],
|