__main__.py
from discord.src.discord import discord
from discord.ext import commands
from config import settings
from discord.src.ASR import ASR
from discord.src.gpt import gpt
#bot = commands.Bot(command_prefix='>', intents=intents)
discord.connect_discord(bot)
discord.listen(bot, ctx)
ASR.recognize_speech(ctx)
gpt.resume_text(r.recognize_google(audio, language="ru-RU"))
bot.run('ce74027bf71962ba79d901b0062621de05c020e1b30aea28badeef6f71d153d7')
ASR.py
#!/usr/bin/env python3
from os import path
import speech_recognition as sr
import pyaudio
import wave
def recognize_speech(ctx):
p = pyaudio.PyAudio()
stream = p.open(format=FRT,channels=CHAN,rate=RT,input=True,frames_per_buffer=CHUNK) # открываем поток для записи
print("rec")
frames = [] # формируем выборку данных фреймов
for i in range(0, int(RT / CHUNK * REC_SEC)):
data = stream.read(CHUNK)
frames.append(data)
print("done")
stream.stop_stream() # останавливаем и закрываем поток
stream.close()
p.terminate()
w = wave.open(OUTPUT, 'wb')
w.setnchannels(CHAN)
w.setsampwidth(p.get_sample_size(FRT))
w.setframerate(RT)
w.writeframes(b''.join(frames))
w.close()
sample = speech_r.WavFile('output.wav')
r = sr.Recognizer()
with sample as audio:
content = r.record(audio)
r.adjust_for_ambient_noise(audio)
ctx.send(r.recognize_google(audio, language="ru-RU"))
# get audio from the microphone
try:
ctx.send("You said " + r.recognize_google(audio))
except sr.UnknownValueError:
ctx.send("Could not understand audio")
except sr.RequestError as e:
ctx.send("Could not request results; {0}".format(e))
return r.recognize_google(audio)
gpt.py
import openai
def resume_text(r.recognize_google(audio, language="ru-RU"))
content = 'resume text'
messages.append({"content": content})
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
chat_response = completion.choices[0].message.content
print(f'ChatGPT: {chat_response}')
messages.append({"content": chat_response})