Jacqkues commited on
Commit
31d6137
·
verified ·
1 Parent(s): bf765df

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -131
app.py DELETED
@@ -1,131 +0,0 @@
1
- import gradio as gr
2
- from database import Database
3
- from filesource import FileSource
4
- from agent import run_agent
5
- from services.utils import get_db_scheme_from_uri
6
- source = None
7
-
8
-
9
- def connect_to_file(file):
10
- global source
11
- try:
12
- source = FileSource(file.name)
13
- status = source.connect()
14
- schema = source._pretify_schema()
15
- status = "Connection successful!"
16
- except Exception as e:
17
- schema = ""
18
- status = f"Error: {str(e)}"
19
- return schema, status
20
-
21
-
22
- def connect_to_database(db_url):
23
- global source
24
- try:
25
- dialect = get_db_scheme_from_uri(db_url)
26
- source = Database(db_url,dialect)
27
- status = source.connect()
28
- schema = source._pretify_schema()
29
- status = "Connection successful!"
30
- except Exception as e:
31
- schema = ""
32
- status = f"Error: {str(e)}"
33
- return schema, status
34
-
35
- # Function to add user message to chat history
36
- def user(user_message, chat_history):
37
- chat_history.append({"role": "user", "content": user_message})
38
- return "", chat_history
39
-
40
- # Function to generate a bot response
41
- def bot(chat_history):
42
-
43
- if source is None:
44
- chat_history.append({"role":"assistant","content":"please connect to a database before asking question"})
45
- yield chat_history
46
- else:
47
- answer = run_agent(source,chat_history[-1]['content'])
48
- chat_history.append({"role":"assistant","content":""})
49
-
50
- for chunk in answer:
51
- chat_history[-1]['content'] += chunk
52
- yield chat_history
53
-
54
- # Create the Gradio interface
55
- with gr.Blocks(theme=gr.themes.Default(), css="""
56
- .gr-button { margin: 5px; border-radius:16px; }
57
- .gr-textbox, .gr-text-area, .gr-dropdown, .gr-json { border-radius: 8px; }
58
- .gr-row { gap: 10px; }
59
- .gr-tab { border-radius: 8px; }
60
- .status-text { font-size: 0.9em; color: #555; }
61
- .gr-json { max-height: 300px; overflow-y: auto; } /* Added scrolling for JSON */
62
- """) as demo:
63
- gr.Markdown(
64
- f"""
65
- # 🤖 MCP DB Answer
66
- Your mcp server that allow you to talk to any database
67
-
68
-
69
- Powered by Ibis it support : PostgreSQL , SQLite , MySQL , MSSQL , ClickHouse , BigQuery and many other
70
-
71
-
72
- Also support .CSV and .parquet files
73
-
74
- """,
75
- elem_classes=["header"]
76
- )
77
-
78
- with gr.Column(scale=3):
79
- with gr.Tabs():
80
- with gr.TabItem("💬 Chat"):
81
- with gr.Group():
82
- main_chat_disp = gr.Chatbot(
83
- label=None, height=600,
84
- avatar_images=(None, "https://huggingface.co/spaces/Space-Share/bucket/resolve/main/images/pfp.webp"),
85
- show_copy_button=True, render_markdown=True, sanitize_html=True, type='messages'
86
- )
87
- with gr.Row(variant="compact"):
88
- user_msg_tb = gr.Textbox(
89
- show_label=False, placeholder="Talk with your data...",
90
- scale=7, lines=1, max_lines=3
91
- )
92
- send_btn = gr.Button("Send", variant="primary", scale=1, min_width=100)
93
- with gr.TabItem("Config"):
94
- with gr.Row():
95
- # Left column for database configuration.
96
- with gr.Column(scale=1):
97
- gr.Markdown("## Database Configuration")
98
- # Textbox for entering the database URL.
99
- db_url_tb = gr.Textbox(
100
- show_label=True, label="Database URL", placeholder="Enter the URL to connect to the database..."
101
- )
102
- # Button to connect to the database.
103
- connect_btn = gr.Button("Connect", variant="primary")
104
-
105
- file_uploader = gr.File(
106
- label="Upload File", file_types=[".csv", ".parquet", ".xls", ".xlsx"]
107
- )
108
- # Button to connect to the database.
109
- load_btn = gr.Button("Load", variant="primary")
110
-
111
- # Right column for displaying the database schema and status message.
112
- with gr.Column(scale=3):
113
- gr.Markdown("## Database Schema")
114
- # Textarea to display the database schema.
115
- schema_ta = gr.TextArea(
116
- show_label=False, placeholder="Database schema will be displayed here...",
117
- lines=20, max_lines=50, interactive=False
118
- )
119
- # Textbox to display the status message.
120
- status_tb = gr.Textbox(
121
- show_label=False, placeholder="Status message will be displayed here...",
122
- lines=1, max_lines=1, interactive=False, elem_classes=["status-text"]
123
- )
124
- connect_btn.click(fn=connect_to_database, inputs=db_url_tb, outputs=[schema_ta, status_tb])
125
- load_btn.click(fn=connect_to_file, inputs=file_uploader, outputs=[schema_ta, status_tb])
126
- send_btn.click(fn=user, inputs=[user_msg_tb, main_chat_disp], outputs=[user_msg_tb, main_chat_disp], queue=False).then(
127
- fn=bot, inputs=main_chat_disp, outputs=main_chat_disp
128
- )
129
-
130
- if __name__ == "__main__":
131
- demo.launch(mcp_server=True)