Spaces:
Build error
Build error
import logging | |
import os | |
from .utils import timeit | |
import openai | |
logging.basicConfig(level=logging.INFO) | |
log = logging.getLogger(__name__) | |
def set_openai_key(openai_api_key_textbox = None): | |
log.info(f"Setting OpenAI key.") | |
if openai_api_key_textbox is not None: | |
os.environ["OPENAI_API_KEY"] = openai_api_key_textbox | |
try: | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
except KeyError as e: | |
log.warning("OPENAI_API_KEY not found in environment variables.") | |
pass | |
set_openai_key() | |
def speech_to_text(audio_path): | |
log.info("Transcribing audio...") | |
transcript = openai.Audio.transcribe("whisper-1", open(audio_path, "rb")) | |
text = transcript["text"] | |
log.info(f"Transcript: \n\t{text}") | |
return text | |
def top_response(prompt, system=None, model="gpt-3.5-turbo", max_tokens=20, temperature=0.8): | |
_prompt = [ | |
{ | |
"role": "user", | |
"content": prompt, | |
}, | |
] | |
if system: | |
_prompt = [ | |
{ | |
"role": "system", | |
"content": system, | |
}, | |
] + _prompt | |
log.info(f"API call to {model} with prompt: \n\n\t{_prompt}\n\n") | |
_response = openai.ChatCompletion.create( | |
model=model, | |
messages=_prompt, | |
temperature=temperature, | |
n=1, | |
max_tokens=max_tokens, | |
) | |
log.info(f"API reponse: \n\t{_response}") | |
response: str = _response['choices'][0]['message']['content'] | |
return response | |