commit 4d84586a9d509159232872833dd4f4fd07836a85 Author: mxr612 Date: Mon Aug 19 22:13:16 2024 +0800 0. Configs 1. MySQL Connection 2. FastGPT database uploader diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e393ac5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +# Ignore Python byte code files +__pycache__/ +*.pyc +*.pyo +*.pyd + +/config.json + +# Ignore the virtual environment directory +venv/ + +# Ignore the build directory +build/ + +# Ignore the dist directory +dist/ + +# Ignore the .env file for environment variables +.env + +# Ignore any .DS_Store files created by macOS +.DS_Store diff --git a/config.py b/config.py new file mode 100644 index 0000000..6cb640c --- /dev/null +++ b/config.py @@ -0,0 +1,20 @@ + +# Load Cogfigs + +import os +import json + +# Get the absolute path of the main file +main_file_path = os.path.abspath(__file__) + +# Get the directory in which the main file resides +MAIN_DIRECTORY = os.path.dirname(main_file_path) + +def load_config(): + # Open and load the JSON file + with open(MAIN_DIRECTORY+"/config.json", 'r') as file: + data = json.load(file) + return data + +__CONFIG__ = load_config() + diff --git a/fastgpt_uploader.py b/fastgpt_uploader.py new file mode 100644 index 0000000..75a3732 --- /dev/null +++ b/fastgpt_uploader.py @@ -0,0 +1,42 @@ +import json +import requests +from markdownify import markdownify as md + +import config + +key = config.__CONFIG__["fastgpt_api"] +url = config.__CONFIG__["fastgpt_host"] +setId = config.__CONFIG__["fastgpt_setId"] + +# Config FastGPT + +from datetime import datetime + +def __new_set(): + headers = {"Authorization": f"Bearer {key}", "Content-Type": "application/json"} + payload = { + "datasetId":setId, + "name":str(datetime.now()), + "type":"virtual" + } + result = requests.post( + url + "/core/dataset/collection/create", headers=headers, data=json.dumps(payload) + ).json() + return result["data"] + +colId = __new_set() + +def submit_data(q,a): + try: + headers = {"Authorization": f"Bearer {key}", "Content-Type": "application/json"} + payload = { + "collectionId": colId, + "trainingMode": "chunk", + "data": [{"q":q,"a":a}] + } + response = requests.post( + url + "/core/dataset/data/pushData", headers=headers, data=json.dumps(payload) + ) + return True if response.json()["code"] == 200 else False + except: + return False diff --git a/main.py b/main.py new file mode 100644 index 0000000..0ac1f68 --- /dev/null +++ b/main.py @@ -0,0 +1,7 @@ +import config +import mysql_connector +import fastgpt_uploader + +config.end_mysql() + +mysql_connector.end_mysql() \ No newline at end of file diff --git a/mysql_connector.py b/mysql_connector.py new file mode 100644 index 0000000..63bd0d0 --- /dev/null +++ b/mysql_connector.py @@ -0,0 +1,25 @@ +# ConfigMysql + +from config import __CONFIG__ + +import mysql.connector + +mysql_config = { + 'user': __CONFIG__['mysql_user'], + 'password': __CONFIG__['mysql_password'], + 'host': __CONFIG__['mysql_host'], # Typically 'localhost' if the database is on the same machine + 'database': 'fastdoi', + 'raise_on_warnings': True +} + +# Establish a connection to the MySQL database +cnx = mysql.connector.connect(**mysql_config) +cursor = cnx.cursor() + +def end_mysql(): + try: + # Close the cursor and connection + cursor.close() + cnx.close() + except: + print("No Mysql Opened.")