Spaces:
Runtime error
Runtime error
import gradio as gr | |
import pandas as pd | |
import plotly.express as px | |
from sklearn.datasets import make_blobs | |
def create_dataset(): | |
# Örnek veri oluştur | |
data, _ = make_blobs(n_samples=300, centers=3, n_features=2, random_state=42) | |
df = pd.DataFrame(data, columns=['X', 'Y']) | |
df['Category'] = ['A', 'B', 'C'] * 100 | |
return df | |
def update_columns(csv_file): | |
if csv_file is None: | |
return [gr.Dropdown(choices=[])] * 2 | |
df = pd.read_csv(csv_file.name) | |
numeric_cols = df.select_dtypes(include=['number']).columns.tolist() | |
return gr.Dropdown(choices=numeric_cols), gr.Dropdown(choices=numeric_cols) | |
def create_plot(csv_file, x_col, y_col): | |
if csv_file is None or x_col is None or y_col is None: | |
df = create_dataset() | |
x_col, y_col = 'X', 'Y' | |
else: | |
df = pd.read_csv(csv_file.name) | |
fig = px.scatter( | |
df, | |
x=x_col, | |
y=y_col, | |
color='Category' if 'Category' in df.columns else None, | |
title="Interactive Scatter Plot with Polygon Selection", | |
hover_data=df.columns.tolist() | |
) | |
fig.update_layout( | |
dragmode='drawclosedpath', # Polygon çizim modu | |
newshape=dict(line_color='cyan'), # Çizim rengi | |
margin=dict(l=20, r=20, b=20, t=40) | |
) | |
config = { | |
'modeBarButtonsToAdd': [ | |
'drawclosedpath', | |
'eraseshape' | |
] | |
} | |
return fig, df | |
def get_selected_points(plot_data, df): | |
if plot_data is None or 'range' in plot_data: | |
return pd.DataFrame() | |
selected_points = [] | |
for shape in plot_data.get('shapes', []): | |
if shape['type'] == 'path': | |
x_path = shape['path'].split('L') | |
polygon_points = [tuple(map(float, point.split(','))) for point in x_path] | |
# Polygon içinde kalan noktaları bul | |
for idx, row in df.iterrows(): | |
if is_point_in_polygon((row['X'], row['Y']), polygon_points): | |
selected_points.append(idx) | |
return df.iloc[selected_points] if selected_points else pd.DataFrame() | |
def is_point_in_polygon(point, polygon): | |
# Ray casting algoritması ile nokta-polygon çakışma kontrolü | |
x, y = point | |
inside = False | |
n = len(polygon) | |
for i in range(n): | |
j = (i + 1) % n | |
xi, yi = polygon[i] | |
xj, yj = polygon[j] | |
intersect = ((yi > y) != (yj > y)) and (x < (xj - xi) * (y - yi) / (yj - yi) + xi) | |
if intersect: | |
inside = not inside | |
return inside | |
with gr.Blocks() as app: | |
gr.Markdown("## 🔷 Polygon ile Veri Seçimi") | |
with gr.Row(): | |
csv_upload = gr.UploadButton(label="CSV Yükle", file_types=[".csv"]) | |
x_dropdown = gr.Dropdown(label="X Ekseni") | |
y_dropdown = gr.Dropdown(label="Y Ekseni") | |
with gr.Row(): | |
plot = gr.Plot(label="Scatter Plot") | |
selected_table = gr.DataFrame(label="Seçilen Veriler") | |
df_state = gr.State() | |
csv_upload.upload( | |
fn=update_columns, | |
inputs=csv_upload, | |
outputs=[x_dropdown, y_dropdown] | |
) | |
x_dropdown.change( | |
fn=create_plot, | |
inputs=[csv_upload, x_dropdown, y_dropdown], | |
outputs=[plot, df_state] | |
) | |
y_dropdown.change( | |
fn=create_plot, | |
inputs=[csv_upload, x_dropdown, y_dropdown], | |
outputs=[plot, df_state] | |
) | |
plot.select( | |
fn=get_selected_points, | |
inputs=df_state, | |
outputs=selected_table | |
) | |
if __name__ == "__main__": | |
app.launch() |