forked from Ikaros-521/AI-Vtuber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
149 lines (124 loc) · 5.52 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from gradio_client import Client
import json, logging
import traceback
# 配置日志输出格式
logging.basicConfig(
level=logging.DEBUG, # 设置日志级别,可以根据需求调整
format="%(asctime)s [%(levelname)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
# 请求gradio的api
def gradio_api(data):
def get_value_by_index(response, index):
try:
# 确保响应是元组或列表,并且索引在范围内
if isinstance(response, (tuple, list)) and index < len(response):
return response[index]
else:
return None
except IndexError:
logging.error(traceback.format_exc())
# 索引超出范围
return None
def get_file_path(data):
try:
url = data.pop('url') # 获取并移除URL
fn_index = data.pop('fn_index') # 获取并移除函数索引
data_analysis = data.pop('data_analysis')
client = Client(url)
# data是一个字典,包含了所有需要的参数
data_values = list(data.values())
result = client.predict(fn_index=fn_index, *data_values)
logging.info(result)
if isinstance(result, (tuple, list)):
# 获取索引为1的元素
file_path = get_value_by_index(result, int(data_analysis))
if file_path:
logging.info(f"文件路径:{file_path}")
return file_path
else:
logging.error("Invalid index or response format.")
return None
except Exception as e:
logging.error(traceback.format_exc())
# 索引超出范围
return None
data_str = data["request_parameters"]
formatted_data_str = data_str.format(content=data["content"])
data_json = json.loads(formatted_data_str)
return get_file_path(data_json)
# result = client.predict(
# "你好", # str in '输入文本内容' Textbox component
# "派蒙_ZH", # str (Option from: [('派蒙_ZH', '派蒙_ZH'), ('纳西妲_ZH', '纳西妲_ZH')]) in 'Speaker' Dropdown component
# 0.5, # int | float (numeric value between 0 and 1) in 'SDP Ratio' Slider component
# 0.6, # int | float (numeric value between 0.1 and 2) in 'Noise' Slider component
# 0.9, # int | float (numeric value between 0.1 and 2) in 'Noise_W' Slider component
# 1, # int | float (numeric value between 0.1 and 2) in 'Length' Slider component
# "ZH", # str (Option from: [('ZH', 'ZH'), ('JP', 'JP'), ('EN', 'EN'), ('mix', 'mix'), ('auto', 'auto')]) in 'Language' Dropdown component
# None, # str (filepath on your computer (or URL) of file) in 'Audio prompt' Audio component
# "Happy", # str in 'Text prompt' Textbox component
# "Text prompt", # str in 'Prompt Mode' Radio component
# "", # str in '辅助文本' Textbox component
# 0.7, # int | float (numeric value between 0 and 1) in 'Weight' Slider component
# fn_index=fn_index
# )
# Define the JSON template string
data_str = '{{"url": "https://v2.genshinvoice.top/", "fn_index": 0, "data_analysis": 1, "text_input": "{content}", "speaker_option": "派蒙_ZH", "sdp_ratio": 0.5, "noise": 0.6, "noise_w": 0.9, "length": 1, "language": "ZH", "audio_prompt_url": null, "text_prompt": "Happy", "prompt_mode": "Text prompt", "auxiliary_text": "", "weight": 0.7}}'
# Insert dynamic content using format
formatted_data_str = data_str.format(content="你好")
data = {
"url": "https://v2.genshinvoice.top/",
"fn_index": 0,
"data_analysis": 1,
"text_input": "你好",
"speaker_option": "派蒙_ZH",
"sdp_ratio": 0.5,
"noise": 0.6,
"noise_w": 0.9,
"length": 1,
"language": "ZH",
"audio_prompt_url": None,
"text_prompt": "Happy",
"prompt_mode": "Text prompt",
"auxiliary_text": "",
"weight": 0.7
}
# print(json.dumps(data))
# Parse the JSON string
data = json.loads(formatted_data_str)
print(data)
logging.info(get_file_path(data))
'''
data = {
"url": "https://xzjosh-nana7mi-bert-vits2.hf.space/--replicas/m9qdw/",
"fn_index": 0,
"data_analysis": 1,
"text_input": "你好",
"speaker_option": "Nana7mi",
"sdp_ratio": 0.5,
"noise": 0.6,
"noise_w": 0.9,
"length": 1
}
print(json.dumps(data))
data_str = "{{"url": "https://xzjosh-nana7mi-bert-vits2.hf.space/--replicas/m9qdw/", "fn_index": 0, "data_analysis": 1, "text_input": "{content}", "speaker_option": "Nana7mi", "sdp_ratio": 0.5, "noise": 0.6, "noise_w": 0.9, "length": 1}}"
logging.info(get_file_path(data))
data = {
"url": "https://frankzxshen-vits-fast-fineturning-models-ba.hf.space/",
"fn_index": 2,
"data_analysis": 1,
"text": "你好",
"character": "果穗",
"language": "日本語",
"noise": 0.6,
"noise_w": 0.6,
"speed": 1,
"symbol_input": False
}
logging.info(get_file_path(data))
'''
data = {
'content': '你好',
'request_parameters': '{{"url": "https://v2.genshinvoice.top/", "fn_index": 0, "data_analysis": 1, "text_input": "{content}", "speaker_option": "派蒙_ZH", "sdp_ratio": 0.5, "noise": 0.6, "noise_w": 0.9, "length": 1, "language": "ZH", "audio_prompt_url": null, "text_prompt": "Happy", "prompt_mode": "Text prompt", "auxiliary_text": "", "weight": 0.7}}'
}
gradio_api(data)