-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
146 lines (129 loc) · 5.52 KB
/
main.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
import os
import edge_tts as tts
from edge_tts import VoicesManager
import asyncio, concurrent.futures
import gradio as gr
from rvc_infer import rvc_convert
import config
import hashlib
from datetime import datetime
from langchain.chat_models.gigachat import GigaChat
from langchain.schema import HumanMessage, SystemMessage
def date_to_short_hash():
current_date = datetime.now()
date_str = current_date.strftime("%Y-%m-%d %H:%M:%S")
sha256_hash = hashlib.sha256(date_str.encode()).hexdigest()
short_hash = sha256_hash[:8]
return short_hash
model = "DenVot.pth"
can_speak = True
voice_instances = []
chat = GigaChat(credentials=config.API_AUTH, verify_ssl_certs=False, model="GigaChat-Pro")
async def load_voices():
voicesobj = await VoicesManager.create()
global voice_instances
voice_instances = [data["ShortName"] for data in voicesobj.voices]
async def speech(mess, pitch, voice):
communicate = tts.Communicate(mess, voice)
i = 0
file_name = "test"
await communicate.save("input\\" + file_name)
output_path = rvc_convert(model_path=os.getcwd() + "\\models\\" + model,
input_path=os.getcwd() + "\\input\\" + file_name,
f0_up_key=pitch)
os.rename("output\\out.wav", "output\\" + date_to_short_hash() + ".wav")
os.remove("input\\" + file_name)
print("DenVot: " + file_name)
global can_speak
can_speak = True
messages = list()
messages.append(SystemMessage(content="Ты милый мальчик по имени Денвот, ты любишь отвечать на вопросы! Не перепутай свою роль!"))
def GigaMessage(request):
global messages
messages.append(HumanMessage(content=request))
response = chat(messages)
messages.pop(1)
return response
def get_last_file(directory):
files = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]
if not files:
return None
last_file = max(files, key=lambda f: os.path.getctime(os.path.join(directory, f)))
return os.path.join(directory, last_file)
pool = concurrent.futures.ThreadPoolExecutor()
pool.submit(asyncio.run, load_voices()).result()
def makeSpeech(request, pitch, voice):
global can_speak
if can_speak is False: return
can_speak = False
result = pool.submit(asyncio.run, speech(request, pitch, voice)).result()
return get_last_file("output")
def makeSpeechViaAnswer(question, pitch, voice):
global can_speak
if can_speak is False: return
can_speak = False
requested = GigaMessage(question).content
print(requested)
result = pool.submit(asyncio.run, speech(requested, pitch, voice)).result()
return get_last_file("output")
with gr.Blocks() as grad:
with gr.Tab("Озвучка текста"):
with gr.Row():
with gr.Column():
gr.Markdown("""
# Введите текст для озвучки
DenVot с радостью озвучит его!
""")
print(voice_instances)
combobox = gr.Dropdown(voice_instances, label="Голос", info="Список всех доступных голосов", value="ru-RU-DmitryNeural")
request = gr.TextArea(placeholder="Напиши текст для озвучки денвотика!!")
btn = gr.Button()
btn.label = "Запуск"
btn.value = "Запуск"
gr.Markdown("""
Выберите питч для денвотика!
""")
slider = gr.Slider()
with gr.Column():
gr.Markdown("""
# Тут результат
DenVot же такой классный!!
""")
out1 = gr.Audio()
clear = gr.ClearButton(out1)
clear.label = "Очистить"
clear.value = "Очистить"
with gr.Tab("Вопросы"):
with gr.Row():
with gr.Column():
gr.Markdown("""
### Или же задайте вопрос денвотику
DenVot с радостью ответит на него!
""")
question = gr.TextArea(placeholder="Задай свой вопрос денвотику!!")
quiz = gr.Button()
quiz.label = "Задать вопрос"
quiz.value = "Задать вопрос"
gr.Markdown("""
Выберите питч для денвотика!
""")
slider2 = gr.Slider()
with gr.Column():
gr.Markdown("""
# Тут результат
DenVot же такой классный!!
""")
out2 = gr.Audio()
clear2 = gr.ClearButton(out2)
clear2.label = "Очистить"
clear2.value = "Очистить"
request.label = "Текст"
slider.maximum = 24
slider.minimum = -24
slider.value = 6
slider.label = "Питч"
question.label = "Вопрос"
btn.click(makeSpeech, inputs=[request, slider, combobox], outputs=out1)
quiz.click(makeSpeechViaAnswer, inputs=[question, slider, combobox], outputs=out2)
grad.title = "Голосовой DenVot"
grad.launch()