Robin Chiu commited on
Commit
321debd
·
1 Parent(s): 9012bf8

add database dir.

Browse files
app.py CHANGED
@@ -3,7 +3,7 @@ import pandas as pd
3
  import sys
4
  import os
5
 
6
- from utils.tools import get_kb, get_schema, get_tables, get_meaning
7
 
8
  @gr.mcp.tool()
9
  def get_all_databases() -> list:
@@ -141,7 +141,41 @@ def meaning_query(db_name, table_name):
141
  if len(result) == 0:
142
  return pd.DataFrame({"message": ["沒有找到相關欄位意義"]})
143
 
144
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
  # 建立 Gradio 界面
147
  with gr.Blocks(title="資料庫查詢工具") as demo:
@@ -188,6 +222,15 @@ with gr.Blocks(title="資料庫查詢工具") as demo:
188
  # 當資料庫選擇變更時,更新資料表下拉選單
189
  # meaning_db.change(update_tables, inputs=meaning_db, outputs=meaning_table)
190
  meaning_search.click(meaning_query, inputs=[meaning_db, meaning_table], outputs=meaning_result)
 
 
 
 
 
 
 
 
 
191
  # 啟動 Gradio 應用程式
192
  if __name__ == "__main__":
193
  demo.launch(mcp_server=True, server_name="0.0.0.0",allowed_paths=["/"], share=True)
 
3
  import sys
4
  import os
5
 
6
+ from utils.tools import get_kb, get_schema, get_tables, get_meaning, execute_sqlite_query
7
 
8
  @gr.mcp.tool()
9
  def get_all_databases() -> list:
 
141
  if len(result) == 0:
142
  return pd.DataFrame({"message": ["沒有找到相關欄位意義"]})
143
 
144
+ return pd.DataFrame(result)
145
+
146
+ def execute_sqlite(db_name: str, query: str):
147
+ """
148
+ Execute an SQL query on a specified SQLite database and return results.
149
+
150
+ This function allows direct SQL query execution on the selected database,
151
+ returning either the query results or error messages in a standardized format.
152
+
153
+ Args:
154
+ db_name (str): The name of the database to execute the query against. Must not be empty.
155
+ query (str): The SQL query to execute. Must not be empty.
156
+
157
+ Returns:
158
+ pandas.DataFrame: If query executes successfully, returns DataFrame with query results.
159
+ If query fails or parameters are missing, returns error message in DataFrame.
160
+
161
+ Example:
162
+ >>> result = execute_sqlite("sales_db", "SELECT * FROM customers LIMIT 10")
163
+ >>> print(result)
164
+ # Returns DataFrame with first 10 rows from customers table
165
+ """
166
+ if not db_name or not query:
167
+ return pd.DataFrame({"message": ["請選擇資料庫和SQL"]})
168
+
169
+ result = execute_sqlite_query(db_name, query)
170
+
171
+ if len(result) == 0:
172
+ return pd.DataFrame({"message": ["沒有找到相關資料"]})
173
+
174
+ if result['success']:
175
+ return pd.DataFrame(result['data'], columns=result['columns'])
176
+ else:
177
+ return pd.DataFrame({"message": [result['error']]})
178
+
179
 
180
  # 建立 Gradio 界面
181
  with gr.Blocks(title="資料庫查詢工具") as demo:
 
222
  # 當資料庫選擇變更時,更新資料表下拉選單
223
  # meaning_db.change(update_tables, inputs=meaning_db, outputs=meaning_table)
224
  meaning_search.click(meaning_query, inputs=[meaning_db, meaning_table], outputs=meaning_result)
225
+
226
+ with gr.Tab("sqlite SQL 執行"):
227
+ with gr.Row():
228
+ sql_db = gr.Dropdown(choices=all_dbs, label="選擇資料庫", value=all_dbs[0] if all_dbs else None)
229
+ sql_query = gr.Textbox(label="sqlite SQL")
230
+ sql_exec = gr.Button("執行 SQL")
231
+ sql_result = gr.DataFrame(label="查詢結果")
232
+ sql_exec.click(execute_sqlite, inputs=[sql_db, sql_query], outputs=sql_result)
233
+
234
  # 啟動 Gradio 應用程式
235
  if __name__ == "__main__":
236
  demo.launch(mcp_server=True, server_name="0.0.0.0",allowed_paths=["/"], share=True)
database/alien_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e160643c9abb43ba69917532f2654637e677460df25678f9daecdefb416046ed
3
+ size 2371584
database/archeology_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b525a54561e866f12cb373e5d3588ffcfdda13f59966cd9e3e28bb2633b21fdd
3
+ size 2547712
database/credit_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c1ba8d05ed529c159792070b45734c90698200ee28e11d07586a2f17eddfd87b
3
+ size 1306624
database/cross_db_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d7e40379640260a2a16fb014b9af240a2d187de9223470f05ac92c390ec5e657
3
+ size 1261568
database/crypto_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:49103c15d930b290298ac52e2f965877f3caacde6c5269c6c798e596b4c7a5e1
3
+ size 2969600
database/cybermarket_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:23153eba8d258ef4846257326c4481850d7632bc2efdbe2b2236efa00d316922
3
+ size 4231168
database/disaster_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a6ded0fe31d3baef51295aee4a94ad5f21af77a2d6e226b64ad217e466617e09
3
+ size 2097152
database/fake_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9a2c6888064e47d82fcf7d47fd0d4bafa5f3ac20569f87b1c98db2e5d2ff9f96
3
+ size 2666496
database/gaming_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:830ff704c00442b83d243f1e96dc79441436b20b7afbd20b15bccad5052f54ec
3
+ size 1388544
database/insider_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5a340024cb8022f819874086bef2a409074e7f15018d3a8dc9c6d0387d796bae
3
+ size 1486848
database/mental_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:64c03a0f0857cd017eed8193df3069e2d0f42e5eeac10fcb03c7918ecec6470f
3
+ size 1470464
database/museum_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4bc022713669f1cab2736e453a012f53eea396e9b94457e744c858cc034d1fb3
3
+ size 2019328
database/news_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3d7163ee93932af7e33ecf46e4ef2c98e9bd8132c3f3ed4c434670feeb441142
3
+ size 1716224
database/polar_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6d351808b75c7adfb9d073741345ab2368eeb0accc53369977b531764f54fe8d
3
+ size 2740224
database/robot_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:aef0e7b7134f891da0ae5621638abb12229accd916fb0c7fb2691a66223c05c4
3
+ size 2179072
database/solar_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d143744388100c33eaf7330af84dc71321fc7c317332961e4129196fd66cbc27
3
+ size 2215936
database/vaccine_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2aaf3f42a086d2a70365390bde5db91682fcc842c27febad665f3fe14f0bd402
3
+ size 1290240
database/virtual_template.sqlite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d963a35900ac10f5b4a12cdf672ed999742fcf2b04b704e52411c6a1af2acd77
3
+ size 2551808
pyproject.toml CHANGED
@@ -1,12 +1,9 @@
1
  [project]
2
- name = "newsagent"
3
  version = "0.1.0"
4
  description = "Add your description here"
5
  readme = "README.md"
6
  requires-python = ">=3.13"
7
  dependencies = [
8
- "bs4>=0.0.2",
9
  "gradio[mcp]>=5.33.1",
10
- "litellm>=1.74.1",
11
- "smolagents[litellm,mcp]>=1.20.0",
12
  ]
 
1
  [project]
2
+ name = "LiveSQLBench-Lite-MCP"
3
  version = "0.1.0"
4
  description = "Add your description here"
5
  readme = "README.md"
6
  requires-python = ">=3.13"
7
  dependencies = [
 
8
  "gradio[mcp]>=5.33.1",
 
 
9
  ]
requirements.txt CHANGED
@@ -1,3 +1 @@
1
  gradio[mcp]
2
- bs4
3
- smolagents[litellm,mcp]
 
1
  gradio[mcp]
 
 
utils/tools.py CHANGED
@@ -1,4 +1,10 @@
1
  import pandas as pd
 
 
 
 
 
 
2
 
3
 
4
  kb_df = pd.read_csv("./data/kb.csv")
@@ -27,7 +33,72 @@ def get_meaning(db_name, table_name):
27
  result = result[['column_name', 'meaning']]
28
  return result
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  get_kb('solar', 'PP')
31
  get_schema('solar', 'alerts')
32
  get_tables('solar')
33
  get_meaning('solar', 'alerts')
 
 
 
1
  import pandas as pd
2
+ import sqlite3
3
+ import os.path
4
+ import gradio as gr
5
+ import os
6
+ import shutil
7
+ import uuid
8
 
9
 
10
  kb_df = pd.read_csv("./data/kb.csv")
 
33
  result = result[['column_name', 'meaning']]
34
  return result
35
 
36
+ def execute_sqlite_query(db_name, query):
37
+ """
38
+ 執行 SQLite 查詢並返回結果
39
+
40
+ 參數:
41
+ db_name (str): 數據庫名稱
42
+ query (str): SQL 查詢字符串
43
+
44
+ 返回:
45
+ dict: 包含查詢結果或錯誤信息的字典
46
+ 成功時: {'success': True, 'columns': 列名列表, 'data': 查詢結果}
47
+ 失敗時: {'success': False, 'error': 錯誤信息}
48
+ """
49
+
50
+ db_path = f"./database/{db_name}_template.sqlite"
51
+
52
+ # 檢查數據庫文件是否存在
53
+ if not os.path.exists(db_path):
54
+ return {'success': False, 'error': f"數據庫文件不存在: {db_path}"}
55
+
56
+ uid = uuid.uuid4()
57
+ tmp_db_file = f'/tmp/{uid}_{os.path.basename(db_path)}'
58
+ shutil.copy(db_path, tmp_db_file)
59
+
60
+ try:
61
+ # 連接到 SQLite 數據庫
62
+ conn = sqlite3.connect(tmp_db_file)
63
+ cursor = conn.cursor()
64
+
65
+ # 執行查詢
66
+ cursor.execute(query)
67
+
68
+ # 獲取結果
69
+ rows = cursor.fetchall()
70
+
71
+ # 獲取列名
72
+ columns = [description[0] for description in cursor.description] if cursor.description else []
73
+
74
+ # 關閉連接
75
+ conn.close()
76
+
77
+ os.remove(tmp_db_file)
78
+ return {
79
+ 'success': True,
80
+ 'columns': columns,
81
+ 'data': rows
82
+ }
83
+
84
+ except sqlite3.Error as e:
85
+ # 捕獲 SQLite 錯誤
86
+ os.remove(tmp_db_file)
87
+ return {
88
+ 'success': False,
89
+ 'error': str(e)
90
+ }
91
+ except Exception as e:
92
+ # 捕獲其他錯誤
93
+ os.remove(tmp_db_file)
94
+ return {
95
+ 'success': False,
96
+ 'error': f"執行查詢時發生錯誤: {str(e)}"
97
+ }
98
+
99
  get_kb('solar', 'PP')
100
  get_schema('solar', 'alerts')
101
  get_tables('solar')
102
  get_meaning('solar', 'alerts')
103
+ result = execute_sqlite_query('solar', 'SELECT * from test')
104
+ print(result)
uv.lock CHANGED
The diff for this file is too large to render. See raw diff