paddle12 commited on
Commit
01bfd43
·
verified ·
1 Parent(s): a4a2ce3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -72
app.py CHANGED
@@ -1,73 +1,66 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
-
44
- """
45
-
46
- title = "Awesome ML App"
47
- desc = "This is an awesome ML App. I'm really excited to show you"
48
- long_desc = "Let me tell you ALL about this awesome ML App"
49
- iface = gr.Interface(theme=gr.themes.Glass(),
50
- examples=[example1, ref_example, example2], title=title,
51
- description=desc, article=long_desc)
52
-
53
- demo = gr.ChatInterface(
54
- respond,
55
- additional_inputs=[
56
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
57
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
58
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
59
- gr.Slider(
60
- minimum=0.1,
61
- maximum=1.0,
62
- value=0.95,
63
- step=0.05,
64
- label="Top-p (nucleus sampling)",
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()