Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import plotly.express as px
|
| 4 |
-
import io
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# File upload handler
|
| 14 |
-
def handle_file(file):
|
| 15 |
-
ext = file.name.split('.')[-1]
|
| 16 |
-
if ext == "csv":
|
| 17 |
-
df = pd.read_csv(file.name)
|
| 18 |
-
elif ext in ["xlsx", "xls"]:
|
| 19 |
-
df = pd.read_excel(file.name)
|
| 20 |
-
else:
|
| 21 |
-
return "Unsupported file format", None
|
| 22 |
-
|
| 23 |
-
# Save dataframe globally
|
| 24 |
-
df_cache["df"] = df
|
| 25 |
-
markdown_table = df_to_markdown(df)
|
| 26 |
-
return markdown_table, f"File loaded: {file.name}"
|
| 27 |
-
|
| 28 |
-
# Chatbot response (simple echo for now)
|
| 29 |
-
def chat_response(message, history):
|
| 30 |
-
return history + [[message, "Terima kasih! Fitur analisis lanjut sedang dikembangkan."]]
|
| 31 |
-
|
| 32 |
-
# Plot page: visualize table
|
| 33 |
-
def show_plot():
|
| 34 |
-
df = df_cache.get("df")
|
| 35 |
-
if df is None:
|
| 36 |
-
return "Please upload a file on Page 1 first."
|
| 37 |
-
|
| 38 |
-
fig = px.bar(df, x=df.columns[0], y=df.columns[1], title="Visualisasi Kolom 1 vs Kolom 2")
|
| 39 |
return fig
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
plot_output = gr.Plot()
|
| 61 |
-
btn = gr.Button("Tampilkan Visualisasi")
|
| 62 |
-
btn.click(show_plot, outputs=plot_output)
|
| 63 |
-
|
| 64 |
-
# Launch with tabs
|
| 65 |
-
app = gr.TabbedInterface([page1, page2], ["Data & Tanya", "Visualisasi"])
|
| 66 |
-
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import plotly.express as px
|
|
|
|
| 4 |
|
| 5 |
+
def load_table(file):
|
| 6 |
+
df = pd.read_excel(file.name) if file.name.endswith(".xlsx") else pd.read_csv(file.name)
|
| 7 |
+
table_md = df.to_markdown(index=False)
|
| 8 |
+
return table_md, df
|
| 9 |
|
| 10 |
+
def visualize(df):
|
| 11 |
+
if df is None or df.empty:
|
| 12 |
+
return "No data to plot"
|
| 13 |
+
fig = px.bar(df, x=df.columns[0], y=df.columns[1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
return fig
|
| 15 |
|
| 16 |
+
with gr.Blocks() as demo:
|
| 17 |
+
gr.Markdown("## Selamat Datang di Tertata")
|
| 18 |
+
|
| 19 |
+
with gr.Tab("1. Upload & Chat"):
|
| 20 |
+
with gr.Row():
|
| 21 |
+
with gr.Column(scale=1, min_width=300):
|
| 22 |
+
file_input = gr.File(label="Upload file CSV/XLSX")
|
| 23 |
+
chatbox = gr.Textbox(label="Masukkan pertanyaan Anda", placeholder="Tanyakan sesuatu...", lines=1)
|
| 24 |
+
with gr.Column(scale=2, min_width=500, scrollable=True):
|
| 25 |
+
table_output = gr.Textbox(label="Tabel (Markdown)", lines=20, interactive=False, show_copy_button=True)
|
| 26 |
+
df_state = gr.State()
|
| 27 |
+
file_input.change(fn=load_table, inputs=file_input, outputs=[table_output, df_state])
|
| 28 |
+
|
| 29 |
+
with gr.Tab("2. Visualisasi"):
|
| 30 |
+
vis_output = gr.Plot()
|
| 31 |
+
vis_btn = gr.Button("Tampilkan Visualisasi")
|
| 32 |
+
vis_btn.click(fn=visualize, inputs=df_state, outputs=vis_output)
|
| 33 |
+
|
| 34 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|