paddle12 commited on
Commit
655da70
·
verified ·
1 Parent(s): 26610f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -59
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
- # 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...", lines=1)
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()
 
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()