Spaces:
Runtime error
Runtime error
| from vosk import Model, KaldiRecognizer # оффлайн-распознавание от Vosk | |
| from vosk_tts import Model, Synth | |
| import speech_recognition # распознавание пользовательской речи (Speech-To-Text) | |
| import wave # создание и чтение аудиофайлов формата wav | |
| import json # работа с json-файлами и json-строками | |
| import os # работа с файловой системой | |
| import requests | |
| from pydub import AudioSegment | |
| from pydub.playback import play | |
| import urllib.request | |
| PATH_TO_MODEL = "vosk-model-tts-ru-0.4-multi" | |
| PATH_TO_OUTPUT = "C:/Users/user/Desktop/deepfake_sirius/materials/audio" #TODO: IT | |
| k = "sk-YOVNQzHmpga9My3dwlSo9BQN907TuPZQXcHn50ztigTwm3I2" | |
| files = [ | |
| ("input_face", open("C:\\Users\\user\\Desktop\\deepfake_sirius\\materials\\scale_1200.jpg", "rb")), #TODO: IT | |
| ("input_audio", open("C:\\Users\\user\\Desktop\\deepfake_sirius\\materials\\audio\\output.wav", "rb")), #TODO: IT | |
| ] | |
| payload = {} | |
| class VoiceGenerator: | |
| def __init__(self): | |
| self.model = Model(model_path=PATH_TO_MODEL) | |
| def generate(self, text, file_name='output.wav'): | |
| synth = Synth(self.model) | |
| path = os.path.join(PATH_TO_OUTPUT, file_name) | |
| synth.synth(text, path) | |
| return path | |
| def record_and_recognize_audio(file_path): | |
| with speech_recognition.AudioFile(file_path) as source: | |
| audio = recognizer.record(source) | |
| try: | |
| recognized_data = recognizer.recognize_google(audio, language="ru").lower() | |
| except speech_recognition.UnknownValueError: | |
| pass | |
| except speech_recognition.RequestError: | |
| pass | |
| return recognized_data | |
| def ask(request): | |
| instruction = """ | |
| Ответь на запрос так, как ответил бы на него Павел Воля. Используй данные из биографии Павла Воли, если это потребуется. Отвечай на запрос в его стиле. Ответ должен содержать не болеее 10 предложений. | |
| """ | |
| result = requests.post( | |
| url='https://llm.api.cloud.yandex.net/llm/v1alpha/instruct', | |
| headers={ | |
| "Authorization": "Api-Key AQVNyVqBi-XoJ1cAo7VIxq6ztgXm3owqowtso5Qb", | |
| }, | |
| json={ | |
| "model": "general", | |
| "instruction_text": instruction, | |
| "request_text": request, | |
| "generation_options": { | |
| "max_tokens": 1500, | |
| "temperature": 0.5 | |
| } | |
| } | |
| ) | |
| data = json.loads(result.text) | |
| return(data['result']['alternatives'][0]['text']) | |
| if __name__ == "__main__": | |
| # инициализация инструментов распознавания и ввода речи | |
| recognizer = speech_recognition.Recognizer() | |
| vg = VoiceGenerator() | |
| while True: | |
| # старт записи речи с последующим выводом распознанной речи | |
| # и удалением записанного в микрофон аудио | |
| voice_input = record_and_recognize_audio() | |
| os.remove("microphone-results.wav") | |
| print(voice_input) | |
| path_to_file = vg.generate(ask(voice_input)) | |
| print(path_to_file) | |
| response = requests.post( | |
| "https://api.gooey.ai/v2/Lipsync/form/", | |
| headers={ | |
| "Authorization": "Bearer " + k, | |
| }, | |
| files=files, | |
| data={"json": json.dumps(payload)}, | |
| ) | |
| assert response.ok, response.content | |
| #song = AudioSegment.from_wav(path_to_file) | |
| result = response.json() | |
| print(response.status_code, result["output"]["output_video"]) | |
| #play(song) | |
| urllib.request.urlretrieve(result["output"]["output_video"], "C:\\Users\\user\\Desktop\\deepfake_sirius\\materials\\video.mp4") | |
| os.startfile("C:\\Users\\user\\Desktop\\deepfake_sirius\\materials\\video.mp4") | |
| break; |