Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,66 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
if __name__ == "__main__":
|
| 73 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
# Global cache for dataframe
|
| 7 |
+
df_cache = {}
|
| 8 |
+
|
| 9 |
+
# Convert DataFrame to markdown
|
| 10 |
+
def df_to_markdown(df):
|
| 11 |
+
return df.to_markdown(index=False)
|
| 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 |
+
# Interface: Page 1 - Upload + Table + Chat
|
| 42 |
+
with gr.Blocks() as page1:
|
| 43 |
+
gr.Markdown("# Selamat Datang di Tertata 🎉")
|
| 44 |
+
|
| 45 |
+
file_input = gr.File(label="Upload CSV atau Excel", file_types=[".csv", ".xlsx", ".xls"])
|
| 46 |
+
markdown_output = gr.Markdown()
|
| 47 |
+
file_status = gr.Textbox(label="Status", interactive=False)
|
| 48 |
+
|
| 49 |
+
file_input.change(fn=handle_file, inputs=file_input, outputs=[markdown_output, file_status])
|
| 50 |
+
|
| 51 |
+
gr.Markdown("## Tanya Jawab 📩")
|
| 52 |
+
chatbot = gr.Chatbot()
|
| 53 |
+
msg = gr.Textbox(placeholder="Tanyakan sesuatu tentang tabel...")
|
| 54 |
+
msg.submit(chat_response, inputs=[msg, chatbot], outputs=chatbot)
|
| 55 |
+
|
| 56 |
+
# Interface: Page 2 - Visualization
|
| 57 |
+
with gr.Blocks() as page2:
|
| 58 |
+
gr.Markdown("# 📊 Visualisasi Data")
|
| 59 |
+
gr.Markdown("Menampilkan grafik dari dua kolom pertama.")
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|