-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.py
More file actions
102 lines (83 loc) · 2.9 KB
/
sample.py
File metadata and controls
102 lines (83 loc) · 2.9 KB
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
# To execute this sample, please install ffmpeg.
import sys
import time
import math
import datetime
import GttsAIStreamer as gasr
# print sentence by a character incrementally.
def print_incremental(st, interval_sec):
for i in range(len(st)):
if not running:
break
print(f"{st[i]}", end='')
sys.stdout.flush()
interruptible_sleep(interval_sec)
# Customized sleep for making available of running flag interruption.
def interruptible_sleep(time_sec):
counter = math.floor(time_sec / 0.10)
frac = time_sec - (counter * 0.10)
for i in range(counter):
if not running:
break
time.sleep(0.10)
if not running:
return
time.sleep(frac)
# callback for getting answer of ChatGPT
# The voice generated by GttsGenerator is given.
def answer_with_voice_cb(user_message, completion, voice):
print(f"\n[{user_message.extern.author.name} {user_message.extern.datetime}] {user_message.message}\n")
time_str = datetime.datetime.now().strftime ('%H:%M:%S')
message = completion.choices[0]["message"]["content"]
# Play the voice by VoicePlayer of GttsAIStreamer
# If you use Python launcher named py, please create proper player as follows.
# player = gasr.VoicePlayer(voice, python_command="py")
player = gasr.VoicePlayer(voice)
player.start()
interruptible_sleep(3)
print(f"[ChatGPT {time_str}] ", end='')
print_incremental(message, 0.05)
print("\n")
# Wait finishing Playng the voice.
player.join()
running = False
# YouTube Video ID and ChatGPT API Key is given from command line in this example.
if len(sys.argv) <= 2:
exit(0)
# Set params of getting messages from stream source.
stream_params=gasr.streamParams(video_id=sys.argv[1])
# Set params of Chat AI.
ai_params=gasr.aiParams(
api_key = sys.argv[2],
system_role = "You are a cheerful assistant who speek English and can get conversation exciting with user.",
)
# Create GttsVoiceGenerator
# You can choose other language by its 'lang=' argument like 'ja'.
# If you want to change language, Please change the assignment for system role of ai params also.
# The instance isn't necessary for English generator.
streamer_params=gasr.streamerParams(
voice_generator=gasr.GttsGenerator(lang='en'),
answer_with_voice_cb=answer_with_voice_cb
)
# Create GttsAIStreamer instance.
# 'voice_generator=' is omittable for English generator.
ai_streamer =gasr.GttsAIStreamer(
gasr.params(
stream_params=stream_params,
ai_params=ai_params,
streamer_params=streamer_params
)
)
running = True
# Wake up internal thread to get chat messages from stream and play gTTS voices of reading ChatGPT answers aloud.
ai_streamer.start()
# Wait any key inputted from keyboad.
input()
# Turn off runnging flag in order to finish playing gTTS voices of the sample.
running=False
# Finish generating gTTS voices.
# Internal thread will stop soon.
ai_streamer.disconnect()
# terminating internal thread.
ai_streamer.join()
del ai_streamer