Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,85 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
4 |
-
from sklearn.datasets import make_blobs
|
5 |
-
|
6 |
-
def create_dataset():
|
7 |
-
# Örnek veri oluştur
|
8 |
-
data, _ = make_blobs(n_samples=300, centers=3, n_features=2, random_state=42)
|
9 |
-
df = pd.DataFrame(data, columns=['X', 'Y'])
|
10 |
-
df['Category'] = ['A', 'B', 'C'] * 100
|
11 |
-
return df
|
12 |
|
13 |
def update_columns(csv_file):
|
14 |
if csv_file is None:
|
15 |
-
return [gr.Dropdown(choices=[])
|
16 |
df = pd.read_csv(csv_file.name)
|
17 |
numeric_cols = df.select_dtypes(include=['number']).columns.tolist()
|
18 |
return gr.Dropdown(choices=numeric_cols), gr.Dropdown(choices=numeric_cols)
|
19 |
|
20 |
-
def
|
21 |
if csv_file is None or x_col is None or y_col is None:
|
22 |
-
|
23 |
-
x_col, y_col = 'X', 'Y'
|
24 |
-
else:
|
25 |
-
df = pd.read_csv(csv_file.name)
|
26 |
|
27 |
-
|
28 |
-
df,
|
29 |
-
x=x_col,
|
30 |
-
y=y_col,
|
31 |
-
color='Category' if 'Category' in df.columns else None,
|
32 |
-
title="Interactive Scatter Plot with Polygon Selection",
|
33 |
-
hover_data=df.columns.tolist()
|
34 |
-
)
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
)
|
41 |
-
|
42 |
-
config = {
|
43 |
-
'modeBarButtonsToAdd': [
|
44 |
-
'drawclosedpath',
|
45 |
-
'eraseshape'
|
46 |
-
]
|
47 |
-
}
|
48 |
-
return fig, df
|
49 |
|
50 |
-
def get_selected_points(
|
51 |
-
if
|
52 |
return pd.DataFrame()
|
53 |
|
54 |
-
|
55 |
-
for
|
56 |
-
|
57 |
-
x_path = shape['path'].split('L')
|
58 |
-
polygon_points = [tuple(map(float, point.split(','))) for point in x_path]
|
59 |
-
|
60 |
-
# Polygon içinde kalan noktaları bul
|
61 |
-
for idx, row in df.iterrows():
|
62 |
-
if is_point_in_polygon((row['X'], row['Y']), polygon_points):
|
63 |
-
selected_points.append(idx)
|
64 |
-
|
65 |
-
return df.iloc[selected_points] if selected_points else pd.DataFrame()
|
66 |
-
|
67 |
-
def is_point_in_polygon(point, polygon):
|
68 |
-
# Ray casting algoritması ile nokta-polygon çakışma kontrolü
|
69 |
-
x, y = point
|
70 |
-
inside = False
|
71 |
-
n = len(polygon)
|
72 |
-
|
73 |
-
for i in range(n):
|
74 |
-
j = (i + 1) % n
|
75 |
-
xi, yi = polygon[i]
|
76 |
-
xj, yj = polygon[j]
|
77 |
-
|
78 |
-
intersect = ((yi > y) != (yj > y)) and (x < (xj - xi) * (y - yi) / (yj - yi) + xi)
|
79 |
-
if intersect:
|
80 |
-
inside = not inside
|
81 |
-
|
82 |
-
return inside
|
83 |
|
84 |
with gr.Blocks() as app:
|
85 |
gr.Markdown("## 🔷 Polygon ile Veri Seçimi")
|
@@ -90,7 +47,7 @@ with gr.Blocks() as app:
|
|
90 |
y_dropdown = gr.Dropdown(label="Y Ekseni")
|
91 |
|
92 |
with gr.Row():
|
93 |
-
|
94 |
selected_table = gr.DataFrame(label="Seçilen Veriler")
|
95 |
|
96 |
df_state = gr.State()
|
@@ -102,18 +59,18 @@ with gr.Blocks() as app:
|
|
102 |
)
|
103 |
|
104 |
x_dropdown.change(
|
105 |
-
fn=
|
106 |
inputs=[csv_upload, x_dropdown, y_dropdown],
|
107 |
-
outputs=[
|
108 |
)
|
109 |
|
110 |
y_dropdown.change(
|
111 |
-
fn=
|
112 |
inputs=[csv_upload, x_dropdown, y_dropdown],
|
113 |
-
outputs=[
|
114 |
)
|
115 |
|
116 |
-
|
117 |
fn=get_selected_points,
|
118 |
inputs=df_state,
|
119 |
outputs=selected_table
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def update_columns(csv_file):
|
6 |
if csv_file is None:
|
7 |
+
return gr.Dropdown(choices=[]), gr.Dropdown(choices=[])
|
8 |
df = pd.read_csv(csv_file.name)
|
9 |
numeric_cols = df.select_dtypes(include=['number']).columns.tolist()
|
10 |
return gr.Dropdown(choices=numeric_cols), gr.Dropdown(choices=numeric_cols)
|
11 |
|
12 |
+
def create_scatter_plot(csv_file, x_col, y_col):
|
13 |
if csv_file is None or x_col is None or y_col is None:
|
14 |
+
return None, None
|
|
|
|
|
|
|
15 |
|
16 |
+
df = pd.read_csv(csv_file.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
# Gradio ScatterPlot için uygun formatta veri hazırla
|
19 |
+
return (
|
20 |
+
gr.ScatterPlot(
|
21 |
+
value=df,
|
22 |
+
x=x_col,
|
23 |
+
y=y_col,
|
24 |
+
tooltip=df.columns.tolist(),
|
25 |
+
title="Interactive Scatter Plot",
|
26 |
+
color_legend_title="Values",
|
27 |
+
interactive=True,
|
28 |
+
brush_radius=5
|
29 |
+
),
|
30 |
+
df
|
31 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
def get_selected_points(evt: gr.SelectData, df):
|
34 |
+
if evt is None or df is None:
|
35 |
return pd.DataFrame()
|
36 |
|
37 |
+
# Seçilen indeksleri kullanarak verileri filtrele
|
38 |
+
selected_indices = [i["index"] for i in evt.selected]
|
39 |
+
return df.iloc[selected_indices]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
with gr.Blocks() as app:
|
42 |
gr.Markdown("## 🔷 Polygon ile Veri Seçimi")
|
|
|
47 |
y_dropdown = gr.Dropdown(label="Y Ekseni")
|
48 |
|
49 |
with gr.Row():
|
50 |
+
scatter_plot = gr.ScatterPlot(interactive=True, brush_radius=5)
|
51 |
selected_table = gr.DataFrame(label="Seçilen Veriler")
|
52 |
|
53 |
df_state = gr.State()
|
|
|
59 |
)
|
60 |
|
61 |
x_dropdown.change(
|
62 |
+
fn=create_scatter_plot,
|
63 |
inputs=[csv_upload, x_dropdown, y_dropdown],
|
64 |
+
outputs=[scatter_plot, df_state]
|
65 |
)
|
66 |
|
67 |
y_dropdown.change(
|
68 |
+
fn=create_scatter_plot,
|
69 |
inputs=[csv_upload, x_dropdown, y_dropdown],
|
70 |
+
outputs=[scatter_plot, df_state]
|
71 |
)
|
72 |
|
73 |
+
scatter_plot.select(
|
74 |
fn=get_selected_points,
|
75 |
inputs=df_state,
|
76 |
outputs=selected_table
|