forked from veldanava/AiWaifu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.py
50 lines (39 loc) · 1.15 KB
/
ai.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
import openai
from gtts import gTTS
import os
import io
from pydub import AudioSegment
from pydub.playback import play
# This project was inspired by Adi Panda's YouTube channel content with the same topic namely AI Waifu, but this project is simpler
# ur openai key
openai.api_key = "paste in here"
print("Hello oniisan....")
# def function input
def chat():
# Get user input
input_text = input("question: ")
# send query to ChatGPT
response = openai.Completion.create(
engine="text-davinci-003",
prompt=input_text,
temperature=0.5,
max_tokens=300,
n=1,
stop=None,
timeout=20,
)
# get ChatGPT response
output_text = response.choices[0].text.strip()
# convert to speech using gTTS
tts = gTTS(text=output_text, lang='ja')
# play the speech using PyDub
audio_bytes = io.BytesIO()
tts.write_to_fp(audio_bytes)
audio_bytes.seek(0)
audio_data = AudioSegment.from_file(audio_bytes, format="mp3")
play(audio_data)
# if audio not installed
# print(output_text)
# run with loop
while True:
chat()