0. Configs
1. MySQL Connection 2. FastGPT database uploader
This commit is contained in:
commit
4d84586a9d
22
.gitignore
vendored
Normal file
22
.gitignore
vendored
Normal file
@ -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
|
20
config.py
Normal file
20
config.py
Normal file
@ -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()
|
||||
|
42
fastgpt_uploader.py
Normal file
42
fastgpt_uploader.py
Normal file
@ -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
|
7
main.py
Normal file
7
main.py
Normal file
@ -0,0 +1,7 @@
|
||||
import config
|
||||
import mysql_connector
|
||||
import fastgpt_uploader
|
||||
|
||||
config.end_mysql()
|
||||
|
||||
mysql_connector.end_mysql()
|
25
mysql_connector.py
Normal file
25
mysql_connector.py
Normal file
@ -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.")
|
Loading…
Reference in New Issue
Block a user