-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.py
93 lines (83 loc) · 2.98 KB
/
chat.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
import sounddevice as sd
import soundfile as sf
import numpy as np
import openai
import os
import requests
import re
from colorama import Fore, Style, init
import datetime
import base64
from pydub import AudioSegment
from pydub.playback import play
init()
def open_file(filepath):
with open(filepath, 'r', encoding='utf-8') as infile:
return infile.read()
api_key = open_file('openaiapikey2.txt')
elapikey = open_file('elabapikey.txt')
conversation1 = []
chatbot1 = open_file('chatbot1.txt')
def chatgpt(api_key, conversation, chatbot, user_input, temperature=0.9, frequency_penalty=0.2, presence_penalty=0):
openai.api_key = api_key
conversation.append({"role": "user","content": user_input})
messages_input = conversation.copy()
prompt = [{"role": "system", "content": chatbot}]
messages_input.insert(0, prompt[0])
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
temperature=temperature,
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty,
messages=messages_input)
chat_response = completion['choices'][0]['message']['content']
conversation.append({"role": "assistant", "content": chat_response})
return chat_response
def text_to_speech(text, voice_id, api_key):
url = f'https://api.elevenlabs.io/v1/text-to-speech/{voice_id}'
headers = {
'Accept': 'audio/mpeg',
'xi-api-key': api_key,
'Content-Type': 'application/json'
}
data = {
'text': text,
'model_id': 'eleven_monolingual_v1',
'voice_settings': {
'stability': 0.6,
'similarity_boost': 0.85
}
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
with open('output.mp3', 'wb') as f:
f.write(response.content)
audio = AudioSegment.from_mp3('output.mp3')
play(audio)
else:
print('Error:', response.text)
def print_colored(agent, text):
agent_colors = {
"Julie:": Fore.YELLOW,
}
color = agent_colors.get(agent, "")
print(color + f"{agent}: {text}" + Style.RESET_ALL, end="")
voice_id1 = 'Your Voice ID'
def record_and_transcribe(duration=8, fs=44100):
print('Recording...')
myrecording = sd.rec(int(duration * fs), samplerate=fs, channels=2)
sd.wait()
print('Recording complete.')
filename = 'myrecording.wav'
sf.write(filename, myrecording, fs)
with open(filename, "rb") as file:
openai.api_key = api_key
result = openai.Audio.transcribe("whisper-1", file)
transcription = result['text']
return transcription
while True:
user_message = record_and_transcribe()
response = chatgpt(api_key, conversation1, chatbot1, user_message)
print_colored("Julie:", f"{response}\n\n")
user_message_without_generate_image = re.sub(r'(Response:|Narration:|Image: generate_image:.*|)', '', response).strip()
text_to_speech(user_message_without_generate_image, voice_id1, elapikey)