first commit
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import requests
|
||||
import json
|
||||
import time
|
||||
|
||||
|
||||
def reboot_send(mes_reboot):
|
||||
data = {
|
||||
"msgtype": "markdown",
|
||||
"markdown": {
|
||||
"content": mes_reboot,
|
||||
}
|
||||
}
|
||||
r = requests.post(url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c6f7d16f-5e5a-4e7e-b23c-a8516bfa47a6',
|
||||
json=data)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
mes1 = "# 标题\n" \
|
||||
"[这是一个链接](http://work.weixin.qq.com/api/doc)\n" \
|
||||
"@郭安"
|
||||
reboot_send(mes1)
|
||||
Binary file not shown.
@@ -0,0 +1,21 @@
|
||||
from gradio_client import Client
|
||||
import wave
|
||||
import os
|
||||
client = Client("http://127.0.0.1:7860/")
|
||||
result = client.predict(
|
||||
"试一下效果怎么样?你好这是一个测试音频,实现了api音频实时输出",
|
||||
"example",
|
||||
"简体中文",
|
||||
0.8,
|
||||
fn_index=0
|
||||
)
|
||||
print(result)
|
||||
# 获取当前目录
|
||||
current_dir = os.getcwd()
|
||||
# 构建目标文件路径
|
||||
target_file_path = os.path.join(current_dir, 'audio.wav')
|
||||
print(target_file_path)
|
||||
# 复制文件到当前目录
|
||||
with open(result[1], 'rb') as source_file:
|
||||
with open(target_file_path, 'wb') as target_file:
|
||||
target_file.write(source_file.read())
|
||||
@@ -0,0 +1,136 @@
|
||||
import _thread as thread
|
||||
import base64
|
||||
import datetime
|
||||
import hashlib
|
||||
import hmac
|
||||
import json
|
||||
from urllib.parse import urlparse
|
||||
import ssl
|
||||
from datetime import datetime
|
||||
from time import mktime
|
||||
from urllib.parse import urlencode
|
||||
from wsgiref.handlers import format_date_time
|
||||
|
||||
import websocket # 使用websocket_client
|
||||
answer = ""
|
||||
|
||||
class Ws_Param(object):
|
||||
# 初始化
|
||||
def __init__(self, APPID, APIKey, APISecret, Spark_url):
|
||||
self.APPID = APPID
|
||||
self.APIKey = APIKey
|
||||
self.APISecret = APISecret
|
||||
self.host = urlparse(Spark_url).netloc
|
||||
self.path = urlparse(Spark_url).path
|
||||
self.Spark_url = Spark_url
|
||||
|
||||
# 生成url
|
||||
def create_url(self):
|
||||
# 生成RFC1123格式的时间戳
|
||||
now = datetime.now()
|
||||
date = format_date_time(mktime(now.timetuple()))
|
||||
|
||||
# 拼接字符串
|
||||
signature_origin = "host: " + self.host + "\n"
|
||||
signature_origin += "date: " + date + "\n"
|
||||
signature_origin += "GET " + self.path + " HTTP/1.1"
|
||||
|
||||
# 进行hmac-sha256进行加密
|
||||
signature_sha = hmac.new(self.APISecret.encode('utf-8'), signature_origin.encode('utf-8'),
|
||||
digestmod=hashlib.sha256).digest()
|
||||
|
||||
signature_sha_base64 = base64.b64encode(signature_sha).decode(encoding='utf-8')
|
||||
|
||||
authorization_origin = f'api_key="{self.APIKey}", algorithm="hmac-sha256", headers="host date request-line", signature="{signature_sha_base64}"'
|
||||
|
||||
authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')
|
||||
|
||||
# 将请求的鉴权参数组合为字典
|
||||
v = {
|
||||
"authorization": authorization,
|
||||
"date": date,
|
||||
"host": self.host
|
||||
}
|
||||
# 拼接鉴权参数,生成url
|
||||
url = self.Spark_url + '?' + urlencode(v)
|
||||
# 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致
|
||||
return url
|
||||
|
||||
|
||||
# 收到websocket错误的处理
|
||||
def on_error(ws, error):
|
||||
print("### error:", error)
|
||||
|
||||
|
||||
# 收到websocket关闭的处理
|
||||
def on_close(ws,one,two):
|
||||
print(" ")
|
||||
|
||||
|
||||
# 收到websocket连接建立的处理
|
||||
def on_open(ws):
|
||||
thread.start_new_thread(run, (ws,))
|
||||
|
||||
|
||||
def run(ws, *args):
|
||||
data = json.dumps(gen_params(appid=ws.appid, domain= ws.domain,question=ws.question))
|
||||
ws.send(data)
|
||||
|
||||
|
||||
# 收到websocket消息的处理
|
||||
def on_message(ws, message):
|
||||
# print(message)
|
||||
data = json.loads(message)
|
||||
code = data['header']['code']
|
||||
if code != 0:
|
||||
print(f'请求错误: {code}, {data}')
|
||||
ws.close()
|
||||
else:
|
||||
choices = data["payload"]["choices"]
|
||||
status = choices["status"]
|
||||
content = choices["text"][0]["content"]
|
||||
print(content,end ="")
|
||||
global answer
|
||||
answer += content
|
||||
# print(1)
|
||||
if status == 2:
|
||||
ws.close()
|
||||
|
||||
|
||||
def gen_params(appid, domain,question):
|
||||
"""
|
||||
通过appid和用户的提问来生成请参数
|
||||
"""
|
||||
data = {
|
||||
"header": {
|
||||
"app_id": appid,
|
||||
"uid": "1234"
|
||||
},
|
||||
"parameter": {
|
||||
"chat": {
|
||||
"domain": domain,
|
||||
"temperature": 0.5,
|
||||
"max_tokens": 2048
|
||||
}
|
||||
},
|
||||
"payload": {
|
||||
"message": {
|
||||
"text": question
|
||||
}
|
||||
}
|
||||
}
|
||||
return data
|
||||
|
||||
|
||||
def main(appid, api_key, api_secret, Spark_url,domain, question):
|
||||
# print("星火:")
|
||||
wsParam = Ws_Param(appid, api_key, api_secret, Spark_url)
|
||||
websocket.enableTrace(False)
|
||||
wsUrl = wsParam.create_url()
|
||||
ws = websocket.WebSocketApp(wsUrl, on_message=on_message, on_error=on_error, on_close=on_close, on_open=on_open)
|
||||
ws.appid = appid
|
||||
ws.question = question
|
||||
ws.domain = domain
|
||||
ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
|
||||
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,51 @@
|
||||
import SparkApi
|
||||
#以下密钥信息从控制台获取
|
||||
appid = "7ba99478" #填写控制台中获取的 APPID 信息
|
||||
api_secret = "YzdkOTIwNmRlMjE4YzE4MzliZDE3NzMy" #填写控制台中获取的 APISecret 信息
|
||||
api_key ="81486a3645701f6c4ab2e5a9c36a222e" #填写控制台中获取的 APIKey 信息
|
||||
|
||||
#用于配置大模型版本,默认“general/generalv2”
|
||||
domain = "generalv3" # v3版本
|
||||
#云端环境的服务地址
|
||||
Spark_url = "ws://spark-api.xf-yun.com/v3.1/chat" # v3环境的地址
|
||||
|
||||
|
||||
text =[]
|
||||
|
||||
# length = 0
|
||||
|
||||
def getText(role,content):
|
||||
jsoncon = {}
|
||||
jsoncon["role"] = role
|
||||
jsoncon["content"] = content
|
||||
text.append(jsoncon)
|
||||
return text
|
||||
|
||||
def getlength(text):
|
||||
length = 0
|
||||
for content in text:
|
||||
temp = content["content"]
|
||||
leng = len(temp)
|
||||
length += leng
|
||||
return length
|
||||
|
||||
def checklen(text):
|
||||
while (getlength(text) > 8000):
|
||||
del text[0]
|
||||
return text
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
text.clear
|
||||
while(1):
|
||||
Input = input("\n" +"我:")
|
||||
question = checklen(getText("user",Input))
|
||||
SparkApi.answer =""
|
||||
print("星火:",end = "")
|
||||
SparkApi.main(appid,api_key,api_secret,Spark_url,domain,question)
|
||||
getText("assistant",SparkApi.answer)
|
||||
# print(str(text))
|
||||
last_content = text[-1]['content']
|
||||
print(last_content)
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
# -*- coding=utf-8
|
||||
from qcloud_cos import CosConfig
|
||||
from qcloud_cos import CosS3Client
|
||||
import sys
|
||||
import os
|
||||
import logging
|
||||
import json
|
||||
|
||||
|
||||
# 正常情况日志级别使用 INFO,需要定位时可以修改为 DEBUG,此时 SDK 会打印和服务端的通信信息
|
||||
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
|
||||
|
||||
|
||||
# 1. 设置用户属性, 包括 secret_id, secret_key, region 等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成
|
||||
secret_id = 'AKIDuKcoz6AC70zxmiihou03W5nSgKuPG0l3' # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
|
||||
secret_key = 'JWcedHBINXuMkNv78HTuQOI8OI6eSQb0' # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
|
||||
region = 'ap-chengdu' # 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
|
||||
# COS 支持的所有 region 列表参见https://cloud.tencent.com/document/product/436/6224
|
||||
token = None # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048
|
||||
scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填
|
||||
|
||||
|
||||
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
|
||||
client = CosS3Client(config)
|
||||
|
||||
|
||||
response = client.list_objects(
|
||||
Bucket='guoan-1304933652',
|
||||
Prefix='hello'
|
||||
)
|
||||
print(response['Contents'][0]['Key'])
|
||||
@@ -0,0 +1,41 @@
|
||||
# -*- coding=utf-8
|
||||
from qcloud_cos import CosConfig
|
||||
from qcloud_cos import CosS3Client
|
||||
import sys
|
||||
import os
|
||||
import logging
|
||||
import json
|
||||
|
||||
def get_file_list(username):
|
||||
file_list = []
|
||||
# 正常情况日志级别使用 INFO,需要定位时可以修改为 DEBUG,此时 SDK 会打印和服务端的通信信息
|
||||
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
|
||||
|
||||
|
||||
# 1. 设置用户属性, 包括 secret_id, secret_key, region 等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成
|
||||
secret_id = 'AKIDuKcoz6AC70zxmiihou03W5nSgKuPG0l3' # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
|
||||
secret_key = 'JWcedHBINXuMkNv78HTuQOI8OI6eSQb0' # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
|
||||
region = 'ap-chengdu' # 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
|
||||
# COS 支持的所有 region 列表参见https://cloud.tencent.com/document/product/436/6224
|
||||
token = None # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048
|
||||
scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填
|
||||
|
||||
|
||||
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
|
||||
client = CosS3Client(config)
|
||||
|
||||
|
||||
response = client.list_objects(
|
||||
Bucket='guoan-1304933652',
|
||||
Prefix=f'{username}'
|
||||
)
|
||||
if len(response['Contents']) == 0:
|
||||
print(response)
|
||||
return file_list
|
||||
else:
|
||||
for i in range(len(response['Contents'])):
|
||||
file_list.append(response['Contents'][i]['Key'])
|
||||
print(response)
|
||||
return file_list
|
||||
if __name__ == '__main__':
|
||||
get_file_list("Tester2")
|
||||
@@ -0,0 +1,66 @@
|
||||
# -*- coding=utf-8
|
||||
from qcloud_cos import CosConfig
|
||||
from qcloud_cos import CosS3Client
|
||||
import sys
|
||||
import os
|
||||
import logging
|
||||
|
||||
# 正常情况日志级别使用 INFO,需要定位时可以修改为 DEBUG,此时 SDK 会打印和服务端的通信信息
|
||||
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
|
||||
|
||||
# 1. 设置用户属性, 包括 secret_id, secret_key, region等。Appid 已在 CosConfig 中移除,请在参数 Bucket 中带上 Appid。Bucket 由 BucketName-Appid 组成
|
||||
secret_id = 'AKIDuKcoz6AC70zxmiihou03W5nSgKuPG0l3' # 用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
|
||||
secret_key = 'JWcedHBINXuMkNv78HTuQOI8OI6eSQb0' # 用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
|
||||
region = 'ap-chengdu' # 替换为用户的 region,已创建桶归属的 region 可以在控制台查看,https://console.cloud.tencent.com/cos5/bucket
|
||||
# COS 支持的所有 region 列表参见 https://cloud.tencent.com/document/product/436/6224
|
||||
token = None # 如果使用永久密钥不需要填入 token,如果使用临时密钥需要填入,临时密钥生成和使用指引参见 https://cloud.tencent.com/document/product/436/14048
|
||||
scheme = 'https' # 指定使用 http/https 协议来访问 COS,默认为 https,可不填
|
||||
|
||||
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
|
||||
client = CosS3Client(config)
|
||||
|
||||
#### 文件流简单上传(不支持超过5G的文件,推荐使用下方高级上传接口)
|
||||
# 强烈建议您以二进制模式(binary mode)打开文件,否则可能会导致错误
|
||||
with open('select_list.py', 'rb') as fp:
|
||||
response = client.put_object(
|
||||
Bucket='guoan-1304933652',
|
||||
Body=fp,
|
||||
Key='/Tester2/select_list.py',
|
||||
StorageClass='STANDARD',
|
||||
EnableMD5=False
|
||||
)
|
||||
print(response['ETag'])
|
||||
|
||||
#### 字节流简单上传
|
||||
# response = client.put_object(
|
||||
# Bucket='guoan-1304933652',
|
||||
# Body=b'bytes',
|
||||
# Key='select_list.py',
|
||||
# EnableMD5=False
|
||||
# )
|
||||
# print(response['ETag'])
|
||||
|
||||
|
||||
#### chunk 简单上传
|
||||
# import requests
|
||||
# stream = requests.get('https://cloud.tencent.com/document/product/436/7778')
|
||||
#
|
||||
# # 网络流将以 Transfer-Encoding:chunked 的方式传输到 COS
|
||||
# response = client.put_object(
|
||||
# Bucket='examplebucket-1250000000',
|
||||
# Body=stream,
|
||||
# Key='picture.jpg'
|
||||
# )
|
||||
# print(response['ETag'])
|
||||
|
||||
#### 高级上传接口(推荐)
|
||||
# 根据文件大小自动选择简单上传或分块上传,分块上传具备断点续传功能。
|
||||
# response = client.upload_file(
|
||||
# Bucket='examplebucket-1250000000',
|
||||
# LocalFilePath='local.txt',
|
||||
# Key='picture.jpg',
|
||||
# PartSize=1,
|
||||
# MAXThread=10,
|
||||
# EnableMD5=False
|
||||
# )
|
||||
# print(response['ETag'])
|
||||
@@ -0,0 +1,3 @@
|
||||
[quen]
|
||||
name =
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
[hardware]
|
||||
gpu = GPU1,GPU2,GPU3,GPU4
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import configparser
|
||||
import time
|
||||
|
||||
|
||||
def config_read():
|
||||
# 读取
|
||||
config = configparser.ConfigParser()
|
||||
config.read('config.ini')
|
||||
|
||||
mesg = config['video_path']['rtmp']
|
||||
data = mesg.split(',')
|
||||
def test1_1():
|
||||
# 读取
|
||||
config = configparser.ConfigParser()
|
||||
config.read('config.ini')
|
||||
|
||||
num1 = config['que']['num']
|
||||
name = config['que']['name']
|
||||
# test_n = int(num)
|
||||
print(type(int(num1)))
|
||||
num2 = int(num1)
|
||||
if num2 == 0:
|
||||
print("ok")
|
||||
config.set('que', 'num1', "1")
|
||||
config.write(open("config.ini", "w"))
|
||||
time.sleep(10)
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
config.read('config.ini')
|
||||
num = config['que']['num']
|
||||
name = config['que']['name']
|
||||
print(num)
|
||||
data = name.split(',')
|
||||
print(len(data))
|
||||
|
||||
if __name__ == "__main__":
|
||||
# config_read()
|
||||
while 1:
|
||||
test1_1()
|
||||
time.sleep(1000)
|
||||
@@ -0,0 +1,71 @@
|
||||
import configparser
|
||||
import time
|
||||
|
||||
# 创建配置对象
|
||||
config = configparser.ConfigParser()
|
||||
|
||||
# 读取配置文件
|
||||
config.read('config.ini')
|
||||
|
||||
def read_config():
|
||||
name = config['quen']['name']
|
||||
print(name)
|
||||
if len(name) == 0:
|
||||
print("list is empty!")
|
||||
return
|
||||
data = name.split(',')
|
||||
# print(data[0])
|
||||
return data
|
||||
|
||||
def write_config(add_name):
|
||||
name = config['quen']['name']
|
||||
if len(name) == 0:
|
||||
print("ok")
|
||||
data = []
|
||||
data.append(add_name)
|
||||
new_name = ''.join(data)
|
||||
config.set('quen', 'name', new_name)
|
||||
# 写入配置文件
|
||||
with open('config.ini', 'w') as configfile:
|
||||
config.write(configfile)
|
||||
return
|
||||
|
||||
data = name.split(',')
|
||||
if add_name in data:
|
||||
data.remove(add_name)
|
||||
data.append(add_name)
|
||||
new_name = ','.join(data)
|
||||
config.set('quen', 'name', new_name)
|
||||
# 写入配置文件
|
||||
with open('config.ini', 'w') as configfile:
|
||||
config.write(configfile)
|
||||
def del_name():
|
||||
name = config['quen']['name']
|
||||
if len(name) == 0:
|
||||
print("list is empty!")
|
||||
return
|
||||
data = name.split(',')
|
||||
print(len(data))
|
||||
data.pop(0)
|
||||
new_name = ','.join(data)
|
||||
config.set('quen', 'name', new_name)
|
||||
# 写入配置文件
|
||||
with open('config.ini', 'w') as configfile:
|
||||
config.write(configfile)
|
||||
|
||||
|
||||
def read_gpu_config():
|
||||
gpu = config['quen']['gpu']
|
||||
# if len(name) == 0:
|
||||
# print("list is empty!")
|
||||
# return
|
||||
# data = name.split(',')
|
||||
# print(data[0])
|
||||
return gpu
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
while 1:
|
||||
write_config("guoan")
|
||||
time.sleep(1000)
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
import configparser
|
||||
|
||||
# 创建配置对象
|
||||
config = configparser.ConfigParser()
|
||||
|
||||
# 读取配置文件
|
||||
config.read('config.ini')
|
||||
|
||||
|
||||
def read_config():
|
||||
name = config['quen']['name']
|
||||
print(name)
|
||||
if len(name) == 0:
|
||||
print("list is empty!")
|
||||
return
|
||||
data = name.split(',')
|
||||
print(data)
|
||||
|
||||
return data
|
||||
|
||||
def write_config(add_name):
|
||||
name = config['quen']['name']
|
||||
if len(name) == 0:
|
||||
print("ok")
|
||||
data = []
|
||||
data.append(add_name)
|
||||
new_name = ''.join(data)
|
||||
config.set('quen', 'name', new_name)
|
||||
# 写入配置文件
|
||||
with open('config.ini', 'w') as configfile:
|
||||
config.write(configfile)
|
||||
configfile.close()
|
||||
return
|
||||
|
||||
data = name.split(',')
|
||||
if add_name in data:
|
||||
data.remove(add_name)
|
||||
data.append(add_name)
|
||||
new_name = ','.join(data)
|
||||
config.set('quen', 'name', new_name)
|
||||
# 写入配置文件
|
||||
with open('config.ini', 'w') as configfile:
|
||||
config.write(configfile)
|
||||
def del_name():
|
||||
name = config['quen']['name']
|
||||
if len(name) == 0:
|
||||
print("list is empty!")
|
||||
return
|
||||
data = name.split(',')
|
||||
print(len(data))
|
||||
data.pop(0)
|
||||
new_name = ','.join(data)
|
||||
config.set('quen', 'name', new_name)
|
||||
# 写入配置文件
|
||||
with open('config.ini', 'w') as configfile:
|
||||
config.write(configfile)
|
||||
|
||||
|
||||
def read_gpu_config():
|
||||
gpu = config['quen']['gpu']
|
||||
# if len(name) == 0:
|
||||
# print("list is empty!")
|
||||
# return
|
||||
# data = name.split(',')
|
||||
# print(data[0])
|
||||
return gpu
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
del_name()
|
||||
read_config()
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import configparser
|
||||
import time
|
||||
|
||||
|
||||
def config_read():
|
||||
# 读取
|
||||
config = configparser.ConfigParser()
|
||||
config.read('config.ini')
|
||||
|
||||
mesg = config['video_path']['rtmp']
|
||||
data = mesg.split(',')
|
||||
def test1_1():
|
||||
# 读取
|
||||
config = configparser.ConfigParser()
|
||||
config.read('config.ini')
|
||||
|
||||
num1 = config['que']['num']
|
||||
name = config['que']['name']
|
||||
# test_n = int(num)
|
||||
print(type(int(num1)))
|
||||
num2 = int(num1)
|
||||
if num2 == 0:
|
||||
print("ok")
|
||||
config.set('que', 'num1', "1")
|
||||
config.write(open("config.ini", "w"))
|
||||
time.sleep(10)
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
config.read('config.ini')
|
||||
num = config['que']['num']
|
||||
name = config['que']['name']
|
||||
print(num)
|
||||
data = name.split(',')
|
||||
print(len(data))
|
||||
|
||||
if __name__ == "__main__":
|
||||
# config_read()
|
||||
config_read()
|
||||
@@ -0,0 +1,62 @@
|
||||
import configparser
|
||||
# 创建配置对象
|
||||
config = configparser.ConfigParser()
|
||||
# 读取配置文件
|
||||
config.read('config_gpu.ini')
|
||||
|
||||
def read_config_gpu():
|
||||
name = config['hardware']['gpu']
|
||||
if len(name) == 0:
|
||||
print("list is empty!")
|
||||
data = []
|
||||
return data
|
||||
data = name.split(',')
|
||||
return data
|
||||
|
||||
def write_config_gpu(add_name):
|
||||
name = config['hardware']['gpu']
|
||||
if len(name) == 0:
|
||||
print("ok")
|
||||
data = []
|
||||
data.append(add_name)
|
||||
new_name = ''.join(data)
|
||||
config.set('hardware', 'gpu', new_name)
|
||||
# 写入配置文件
|
||||
with open('config_gpu.ini', 'w') as configfile:
|
||||
config.write(configfile)
|
||||
return
|
||||
|
||||
data = name.split(',')
|
||||
if add_name in data:
|
||||
data.remove(add_name)
|
||||
data.append(add_name)
|
||||
new_name = ','.join(data)
|
||||
config.set('hardware', 'gpu', new_name)
|
||||
# 写入配置文件
|
||||
with open('config_gpu.ini', 'w') as configfile:
|
||||
config.write(configfile)
|
||||
def del_name_gpu():
|
||||
name = config['hardware']['gpu']
|
||||
if len(name) == 0:
|
||||
print("list is empty!")
|
||||
return
|
||||
data = name.split(',')
|
||||
print(len(data))
|
||||
data.pop(0)
|
||||
new_name = ','.join(data)
|
||||
config.set('hardware', 'gpu', new_name)
|
||||
# 写入配置文件
|
||||
with open('config_gpu.ini', 'w') as configfile:
|
||||
config.write(configfile)
|
||||
|
||||
if __name__ == '__main__':
|
||||
gpus = read_config_gpu()
|
||||
# 检查每个 GPU 是否都在列表中
|
||||
for gpu_id in range(1, 5): # 假设我们只有到 GPU4
|
||||
gpu_str = f"GPU{gpu_id}"
|
||||
if gpu_str not in gpus:
|
||||
print(f"{gpu_str} is go")
|
||||
write_config_gpu(f"GPU{gpu_id}")
|
||||
break
|
||||
else:
|
||||
print(f"{gpu_str} is available")
|
||||
Reference in New Issue
Block a user