Se agrega el proyecto al repositorio
This commit is contained in:
0
api_openai/__init__.py
Normal file
0
api_openai/__init__.py
Normal file
BIN
api_openai/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
api_openai/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
BIN
api_openai/__pycache__/whisper.cpython-312.pyc
Normal file
BIN
api_openai/__pycache__/whisper.cpython-312.pyc
Normal file
Binary file not shown.
45
api_openai/whisper.py
Normal file
45
api_openai/whisper.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
from openai import OpenAI
|
||||
|
||||
|
||||
def whisper_api(file_path: str) -> str:
|
||||
# cargar la variable del entorno desde el archivo .env
|
||||
load_dotenv()
|
||||
|
||||
# Usar la variable de entorno API_KEY
|
||||
api_key: str = os.getenv("OPENAI_API_KEY")
|
||||
|
||||
# Cargar el modelo whisper
|
||||
client = OpenAI(api_key=api_key)
|
||||
|
||||
audio_file = open(file_path, "rb")
|
||||
transcription = client.audio.transcriptions.create(
|
||||
model="whisper-1",
|
||||
file=audio_file,
|
||||
response_format="text"
|
||||
)
|
||||
|
||||
return transcription
|
||||
|
||||
|
||||
def tts_api(text: str):
|
||||
# cargar la variable del entorno desde el archivo .env
|
||||
load_dotenv()
|
||||
|
||||
# Usar la variable de entorno API_KEY
|
||||
api_key: str = os.getenv("OPENAI_API_KEY")
|
||||
|
||||
# Cargar el modelo whisper
|
||||
client = OpenAI(api_key=api_key)
|
||||
|
||||
speech_file_path = os.path.join('audios', 'voice.ogg')
|
||||
response = client.audio.speech.create(
|
||||
model="tts-1",
|
||||
voice="alloy",
|
||||
input=text
|
||||
)
|
||||
|
||||
response.stream_to_file(speech_file_path)
|
||||
|
||||
return speech_file_path
|
||||
Reference in New Issue
Block a user