YourUsername commited on
Commit
49fabfb
·
1 Parent(s): c3bea8b

added serive for mysql

Browse files
Modules/MySQL.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import sys
5
+ from typing import Annotated, Literal
6
+
7
+ import gradio as gr
8
+ from ._docstrings import autodoc
9
+
10
+ try:
11
+ import mysql.connector
12
+ from mysql.connector import Error
13
+ except ImportError:
14
+ mysql = None
15
+ Error = Exception
16
+
17
+ # Lấy thông tin kết nối từ biến môi trường
18
+ DEFAULT_HOST = os.getenv("MYSQL_HOST", "localhost")
19
+ DEFAULT_USER = os.getenv("MYSQL_USER", "root")
20
+ DEFAULT_PASSWORD = os.getenv("MYSQL_PASSWORD", "")
21
+ DEFAULT_DATABASE = os.getenv("MYSQL_DATABASE", "")
22
+ DEFAULT_PORT = int(os.getenv("MYSQL_PORT", "3306"))
23
+
24
+ # Single source of truth for the LLM-facing tool description
25
+ TOOL_SUMMARY = (
26
+ "Execute MySQL queries against a database; returns query results or error messages. "
27
+ "Uses environment variables for connection parameters if not provided."
28
+ )
29
+
30
+
31
+ @autodoc(
32
+ summary=TOOL_SUMMARY,
33
+ )
34
+ def MySQL(
35
+ query: Annotated[str, "SQL query to execute"],
36
+ host: Annotated[str, "MySQL server host address"] = DEFAULT_HOST,
37
+ username: Annotated[str, "MySQL username"] = DEFAULT_USER,
38
+ password: Annotated[str, "MySQL password"] = DEFAULT_PASSWORD,
39
+ database: Annotated[str, "Database name to connect to"] = DEFAULT_DATABASE,
40
+ port: Annotated[int, "MySQL server port"] = DEFAULT_PORT,
41
+ query_type: Annotated[Literal["SELECT", "INSERT", "UPDATE", "DELETE", "DDL"], "Type of SQL query to execute"] = "SELECT"
42
+ ) -> str:
43
+ if mysql is None:
44
+ return "Error: mysql-connector-python package is not installed. Run: pip install mysql-connector-python"
45
+
46
+ if not query or not query.strip():
47
+ return "Error: Query is required."
48
+
49
+ if not host or not username:
50
+ return "Error: Host and username are required."
51
+
52
+ connection = None
53
+ try:
54
+ # Establish connection to MySQL database
55
+ connection = mysql.connector.connect(
56
+ host=host,
57
+ port=port,
58
+ database=database,
59
+ user=username,
60
+ password=password
61
+ )
62
+
63
+ if connection.is_connected():
64
+ cursor = connection.cursor(dictionary=True)
65
+ cursor.execute(query)
66
+
67
+ if query_type.upper() in ["SELECT", "SHOW", "DESCRIBE", "EXPLAIN"]:
68
+ # Fetch results for SELECT queries
69
+ records = cursor.fetchall()
70
+ if records:
71
+ result = "Query Results:\n"
72
+ for i, row in enumerate(records):
73
+ result += f"Row {i+1}: {dict(row)}\n"
74
+ result += f"\nTotal records: {len(records)}"
75
+ else:
76
+ result = "Query executed successfully. No records found."
77
+ else:
78
+ # For INSERT, UPDATE, DELETE, DDL queries
79
+ connection.commit()
80
+ affected_rows = cursor.rowcount
81
+ result = f"Query executed successfully. Affected rows: {affected_rows}"
82
+
83
+ cursor.close()
84
+ return result
85
+ except Error as e:
86
+ error_msg = f"MySQL Error: {str(e)}"
87
+ return error_msg
88
+ except Exception as e:
89
+ error_msg = f"Error: {str(e)}"
90
+ return error_msg
91
+ finally:
92
+ if connection and connection.is_connected():
93
+ connection.close()
94
+
95
+
96
+ def build_interface() -> gr.Interface:
97
+ return gr.Interface(
98
+ fn=MySQL,
99
+ inputs=[
100
+ gr.Code(label="SQL Query", language="sql"),
101
+ gr.Textbox(label="Host", value=DEFAULT_HOST),
102
+ gr.Textbox(label="Username", value=DEFAULT_USER),
103
+ gr.Textbox(label="Password", type="password", value=DEFAULT_PASSWORD),
104
+ gr.Textbox(label="Database", value=DEFAULT_DATABASE),
105
+ gr.Number(label="Port", value=DEFAULT_PORT),
106
+ gr.Radio(
107
+ label="Query Type",
108
+ choices=["SELECT", "INSERT", "UPDATE", "DELETE", "DDL"],
109
+ value="SELECT"
110
+ )
111
+ ],
112
+ outputs=gr.Textbox(label="Result", lines=10, max_lines=20),
113
+ title="MySQL Database Connector",
114
+ description="<div style=\"text-align:center\">Execute MySQL queries against a database. Uses environment variables for defaults.</div>",
115
+ api_description=TOOL_SUMMARY,
116
+ flagging_mode="never",
117
+ )
118
+
119
+
120
+ __all__ = ["MySQL", "build_interface"]
Modules/__pycache__/Agent_Terminal.cpython-311.pyc ADDED
Binary file (10.3 kB). View file
 
Modules/__pycache__/Code_Interpreter.cpython-311.pyc ADDED
Binary file (3.2 kB). View file
 
Modules/__pycache__/Deep_Research.cpython-311.pyc ADDED
Binary file (43.2 kB). View file
 
Modules/__pycache__/File_System.cpython-311.pyc ADDED
Binary file (39.8 kB). View file
 
Modules/__pycache__/Generate_Image.cpython-311.pyc ADDED
Binary file (8.08 kB). View file
 
Modules/__pycache__/Generate_Speech.cpython-311.pyc ADDED
Binary file (37.5 kB). View file
 
Modules/__pycache__/Generate_Video.cpython-311.pyc ADDED
Binary file (10.8 kB). View file
 
Modules/__pycache__/Memory_Manager.cpython-311.pyc ADDED
Binary file (18.6 kB). View file
 
Modules/__pycache__/MySQL.cpython-311.pyc ADDED
Binary file (6.32 kB). View file
 
Modules/__pycache__/Obsidian_Vault.cpython-311.pyc ADDED
Binary file (30 kB). View file
 
Modules/__pycache__/Shell_Command.cpython-311.pyc ADDED
Binary file (7.08 kB). View file
 
Modules/__pycache__/Web_Fetch.cpython-311.pyc ADDED
Binary file (16.6 kB). View file
 
Modules/__pycache__/Web_Search.cpython-311.pyc ADDED
Binary file (23.8 kB). View file
 
Modules/__pycache__/__init__.cpython-311.pyc ADDED
Binary file (242 Bytes). View file
 
Modules/__pycache__/_docstrings.cpython-311.pyc ADDED
Binary file (6.14 kB). View file
 
Transcendent-SQL-4B/readme.md ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Convert mysql sang sqlite
2
+
3
+ Giả sử tôi đặt tên file .sql sử dụng trong mysql là schema.sql
4
+
5
+ ```python
6
+
7
+ import sqlite3
8
+ import re
9
+ import os
10
+
11
+ # Cấu hình tên file
12
+ INPUT_SQL_FILE = 'schema.sql' # Tên file SQL gốc của bạn
13
+ OUTPUT_DB_FILE = 'database.db' # Tên file SQLite muốn tạo
14
+
15
+ def convert_mysql_to_sqlite(sql_content):
16
+ # 1. Xóa các dòng comment SQL (bắt đầu bằng -- hoặc /*)
17
+ sql_content = re.sub(r'/\*.*?\*/', '', sql_content, flags=re.DOTALL)
18
+
19
+ # 2. Xóa cú pháp COMMENT '...' của MySQL
20
+ # Regex này tìm từ khóa COMMENT theo sau là chuỗi trong dấu nháy đơn
21
+ sql_content = re.sub(r"COMMENT\s+'.*?'", "", sql_content)
22
+
23
+ # 3. Xử lý AUTO_INCREMENT
24
+ # SQLite tự động tăng nếu là INTEGER PRIMARY KEY, nên chỉ cần xóa từ khóa này
25
+ sql_content = re.sub(r"AUTO_INCREMENT", "", sql_content, flags=re.IGNORECASE)
26
+
27
+ # 4. Thay thế kiểu dữ liệu BIT(1) thành INTEGER (SQLite không có BIT)
28
+ sql_content = re.sub(r"BIT$1$", "INTEGER", sql_content, flags=re.IGNORECASE)
29
+
30
+ # 5. Thay thế giá trị bit b'0', b'1' thành 0, 1
31
+ sql_content = re.sub(r"b'0'", "0", sql_content)
32
+ sql_content = re.sub(r"b'1'", "1", sql_content)
33
+
34
+ # 6. Xóa các thiết lập cuối bảng của MySQL (ENGINE=InnoDB...)
35
+ sql_content = re.sub(r"\) ENGINE=.*?;", ");", sql_content, flags=re.DOTALL)
36
+
37
+ # 7. Thay thế \' trong chuỗi thành '' (cách escape của SQLite)
38
+ sql_content = sql_content.replace("\\'", "''")
39
+
40
+ return sql_content
41
+
42
+ # Xóa file cũ nếu có
43
+ if os.path.exists(OUTPUT_DB_FILE):
44
+ os.remove(OUTPUT_DB_FILE)
45
+
46
+ # Kết nối SQLite
47
+ conn = sqlite3.connect(OUTPUT_DB_FILE)
48
+ cursor = conn.cursor()
49
+
50
+ print("Đang đọc và xử lý file SQL...")
51
+
52
+ try:
53
+ with open(INPUT_SQL_FILE, 'r', encoding='utf-8') as f:
54
+ raw_sql = f.read()
55
+
56
+ clean_sql = convert_mysql_to_sqlite(raw_sql)
57
+
58
+ # Tách các câu lệnh bằng dấu chấm phẩy và thực thi từng câu
59
+ statements = clean_sql.split(';')
60
+
61
+ count = 0
62
+ for statement in statements:
63
+ if statement.strip():
64
+ try:
65
+ cursor.execute(statement)
66
+ count += 1
67
+ except sqlite3.Error as e:
68
+ print(f"Lỗi tại câu lệnh: {statement[:50]}...")
69
+ print(f"Chi tiết: {e}")
70
+
71
+ conn.commit()
72
+ print(f"Xong! Đã tạo file '{OUTPUT_DB_FILE}' với {count} câu lệnh thành công.")
73
+
74
+ except FileNotFoundError:
75
+ print(f"Lỗi: Không tìm thấy file '{INPUT_SQL_FILE}'")
76
+ except Exception as e:
77
+ print(f"Lỗi không mong muốn: {e}")
78
+ finally:
79
+ conn.close()
80
+ ```
81
+
app.py CHANGED
@@ -94,6 +94,7 @@ from Modules.Deep_Research import build_interface as build_research_interface
94
  from Modules.File_System import build_interface as build_fs_interface
95
  from Modules.Obsidian_Vault import build_interface as build_obsidian_interface
96
  from Modules.Shell_Command import build_interface as build_shell_interface
 
97
 
98
  # Optional environment flags used to conditionally show API schemas (unchanged behavior)
99
  HF_IMAGE_TOKEN = bool(os.getenv("HF_READ_TOKEN"))
@@ -118,6 +119,7 @@ deep_research_interface = build_research_interface()
118
  fs_interface = build_fs_interface()
119
  shell_interface = build_shell_interface()
120
  obsidian_interface = build_obsidian_interface()
 
121
 
122
  _interfaces = [
123
  agent_terminal_interface,
@@ -132,6 +134,7 @@ _interfaces = [
132
  image_generation_interface,
133
  video_generation_interface,
134
  deep_research_interface,
 
135
  ]
136
  _tab_names = [
137
  "Agent Terminal",
@@ -146,6 +149,7 @@ _tab_names = [
146
  "Generate Image",
147
  "Generate Video",
148
  "Deep Research",
 
149
  ]
150
 
151
  with gr.Blocks(title="Nymbo/Tools MCP") as demo:
 
94
  from Modules.File_System import build_interface as build_fs_interface
95
  from Modules.Obsidian_Vault import build_interface as build_obsidian_interface
96
  from Modules.Shell_Command import build_interface as build_shell_interface
97
+ from Modules.MySQL import build_interface as build_mysql_interface
98
 
99
  # Optional environment flags used to conditionally show API schemas (unchanged behavior)
100
  HF_IMAGE_TOKEN = bool(os.getenv("HF_READ_TOKEN"))
 
119
  fs_interface = build_fs_interface()
120
  shell_interface = build_shell_interface()
121
  obsidian_interface = build_obsidian_interface()
122
+ mysql_interface = build_mysql_interface()
123
 
124
  _interfaces = [
125
  agent_terminal_interface,
 
134
  image_generation_interface,
135
  video_generation_interface,
136
  deep_research_interface,
137
+ mysql_interface,
138
  ]
139
  _tab_names = [
140
  "Agent Terminal",
 
149
  "Generate Image",
150
  "Generate Video",
151
  "Deep Research",
152
+ "MySQL Database",
153
  ]
154
 
155
  with gr.Blocks(title="Nymbo/Tools MCP") as demo:
finetuned/train.ipynb ADDED
File without changes
requirements.txt CHANGED
@@ -11,4 +11,5 @@ Pillow
11
  huggingface_hub>=0.30.0
12
  markdownify
13
  scipy
14
- onnxruntime
 
 
11
  huggingface_hub>=0.30.0
12
  markdownify
13
  scipy
14
+ onnxruntime
15
+ mysql-connector-python