Spaces:
No application file
No application file
| """ | |
| Data Scientist.: Dr.Eddy Giusepe Chirinos Isidro | |
| Nota: Não consegui resolver um problema neste script ... estou pesquisando sobre isso. | |
| """ | |
| import pyaudio | |
| import wave | |
| import keyboard | |
| import time | |
| # Substitua sua chave de API OpenAI: | |
| import openai | |
| import os | |
| from dotenv import load_dotenv, find_dotenv | |
| _ = load_dotenv(find_dotenv()) # read local .env file | |
| openai.api_key = os.environ['OPENAI_API_KEY'] | |
| def record_audio(filename, chunk=1024, channels=1, rate=44100, format=pyaudio.paInt16): | |
| p = pyaudio.PyAudio() | |
| stream = p.open(format=format, | |
| channels=channels, | |
| rate=rate, | |
| input=True, | |
| frames_per_buffer=chunk) | |
| frames = [] | |
| recording = False | |
| print("🤗 Pressione 'r' para 🎙️ iniciar/pausar 🎙️ a gravação e pressione 'esc' para interromper a gravação e salvar o arquivo. 🤗") | |
| while not keyboard.is_pressed('esc'): | |
| if keyboard.is_pressed('r'): | |
| recording = not recording | |
| print("Recording..." if recording else "Recording paused.") | |
| time.sleep(0.2) | |
| if recording: | |
| data = stream.read(chunk) | |
| frames.append(data) | |
| print("Gravação interrompida e salva...") | |
| stream.stop_stream() | |
| stream.close() | |
| p.terminate() | |
| with wave.open(filename, 'wb') as wf: | |
| wf.setnchannels(channels) | |
| wf.setsampwidth(p.get_sample_size(format)) | |
| wf.setframerate(rate) | |
| wf.writeframes(b''.join(frames)) | |
| print("File saved as", filename) | |
| # openai whisper call | |
| audio_file= open("output.wav", "rb") | |
| transcript = openai.Audio.transcribe("whisper-1", audio_file, language="pt") | |
| print(transcript["text"]) | |
| if __name__ == '__main__': | |
| record_audio('output.wav') | |