-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS2S.py
82 lines (59 loc) · 2.3 KB
/
S2S.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
from langchain_openai import ChatOpenAI
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
translation_template = """
Translate the following sentence into {language}, return ONLY the translation, nothing else.
Sentence: {sentence}
"""
llm = ChatOpenAI(temperature=0.0, model="gpt-4-turbo", openai_api_key="YOUR_API_KEY")
def translate(sentence, language="French"):
data_input = {"language": language, "sentence": sentence}
translation = translation_chain.invoke(data_input)
return translation
from elevenlabs.client import ElevenLabs
from elevenlabs import play, stream
client = ElevenLabs()
def gen_dub(text):
print("Generating audio...")
audio = client.generate(
text=text,
voice="", # Insert voice model here!
model="eleven_multilingual_v2"
)
play(audio)
import assemblyai as aai
def on_open(session_opened: aai.RealtimeSessionOpened):
"This function is called when the connection has been established."
print("Session ID:", session_opened.session_id)
def on_data(transcript: aai.RealtimeTranscript):
"This function is called when a new transcript has been received."
if not transcript.text:
return
if isinstance(transcript, aai.RealtimeFinalTranscript):
print(transcript.text, end="\r\n")
print("Translating...")
translation = translate(str(transcript.text))
print(f"Translation: {translation}")
gen_dub(translation)
else:
print(transcript.text, end="\r")
def on_error(error: aai.RealtimeError):
"This function is called when the connection has been closed."
print("An error occured:", error)
def on_close():
"This function is called when the connection has been closed."
print("Closing Session")
transcriber = aai.RealtimeTranscriber(
on_data=on_data,
on_error=on_error,
sample_rate=44_100,
on_open=on_open, # optional
on_close=on_close, # optional
)
# Start the connection, likely have to restart kernal (runs better as full code in something like VSCode)
transcriber.connect()
microphone_stream = aai.extras.MicrophoneStream()
transcriber.stream(microphone_stream)
#To close use
#transcriber.close()