ruslanmv commited on
Commit
78b8b85
·
verified ·
1 Parent(s): 253e25a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -5
app.py CHANGED
@@ -15,6 +15,9 @@ from datetime import datetime # Import datetime for timestamp
15
  # Load environment variables
16
  load_dotenv()
17
 
 
 
 
18
  # Function to read questions from JSON
19
  def read_questions_from_json(file_path):
20
  if not os.path.exists(file_path):
@@ -41,8 +44,12 @@ def save_interview_history(history, filename="interview_history.json"):
41
  # Function to convert text to speech (OpenAI's TTS usage, adjust if needed)
42
  def convert_text_to_speech(text):
43
  start_time = time.time()
 
 
 
 
44
  try:
45
- client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
46
  response = client.audio.speech.create(model="tts-1", voice="alloy", input=text)
47
 
48
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
@@ -60,8 +67,12 @@ def convert_text_to_speech(text):
60
  # Function to transcribe audio (OpenAI Whisper usage, adjust if needed)
61
  def transcribe_audio(audio_file_path):
62
  start_time = time.time()
 
 
 
 
63
  try:
64
- client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
65
  with open(audio_file_path, "rb") as audio_file:
66
  transcription = client.audio.transcriptions.create(model="whisper-1", file=audio_file)
67
  print(f"DEBUG - Audio transcription time: {time.time() - start_time:.2f} seconds")
@@ -322,7 +333,7 @@ def main():
322
  print(f"Error reading questions: {e}")
323
  return
324
 
325
- global initial_api_key_status_message # Declare as global to modify
326
  initial_api_key_status_message = check_api_key() # Check API key and update status
327
 
328
  interview_func, initial_message, final_message = conduct_interview(questions) # Initialize even if API key is missing
@@ -545,7 +556,7 @@ def main():
545
  Must return a list of {'role':'assistant','content':'...'} messages
546
  plus empty text for user_input and path for audio_output.
547
  """
548
- global interview_func, questions, initial_api_key_status_message # Access global variables
549
 
550
  current_api_key_status = check_api_key() # Check API key status right before starting interview
551
  if not current_api_key_status.startswith("✅"): # If API key is not valid
@@ -600,7 +611,7 @@ def main():
600
  Re-initialize the interview function entirely
601
  to start from scratch, clearing the Chatbot.
602
  """
603
- global interview_func, initial_message, final_message, questions # Access global variables
604
  interview_func, initial_msg, final_msg = conduct_interview(questions) # Re-init with current questions
605
  return [], "", None
606
 
 
15
  # Load environment variables
16
  load_dotenv()
17
 
18
+ # Initialize API key status message globally
19
+ initial_api_key_status_message = "Checking API Key..."
20
+
21
  # Function to read questions from JSON
22
  def read_questions_from_json(file_path):
23
  if not os.path.exists(file_path):
 
44
  # Function to convert text to speech (OpenAI's TTS usage, adjust if needed)
45
  def convert_text_to_speech(text):
46
  start_time = time.time()
47
+ api_key = os.getenv("OPENAI_API_KEY")
48
+ if not api_key:
49
+ print("API key is missing, cannot perform text-to-speech.")
50
+ return None
51
  try:
52
+ client = OpenAI(api_key=api_key)
53
  response = client.audio.speech.create(model="tts-1", voice="alloy", input=text)
54
 
55
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
 
67
  # Function to transcribe audio (OpenAI Whisper usage, adjust if needed)
68
  def transcribe_audio(audio_file_path):
69
  start_time = time.time()
70
+ api_key = os.getenv("OPENAI_API_KEY")
71
+ if not api_key:
72
+ print("API key is missing, cannot perform audio transcription.")
73
+ return None
74
  try:
75
+ client = OpenAI(api_key=api_key)
76
  with open(audio_file_path, "rb") as audio_file:
77
  transcription = client.audio.transcriptions.create(model="whisper-1", file=audio_file)
78
  print(f"DEBUG - Audio transcription time: {time.time() - start_time:.2f} seconds")
 
333
  print(f"Error reading questions: {e}")
334
  return
335
 
336
+ global initial_api_key_status_message # Access and set the global variable
337
  initial_api_key_status_message = check_api_key() # Check API key and update status
338
 
339
  interview_func, initial_message, final_message = conduct_interview(questions) # Initialize even if API key is missing
 
556
  Must return a list of {'role':'assistant','content':'...'} messages
557
  plus empty text for user_input and path for audio_output.
558
  """
559
+ global interview_func, questions, initial_api_key_status_message # Access global variables, use global not nonlocal here
560
 
561
  current_api_key_status = check_api_key() # Check API key status right before starting interview
562
  if not current_api_key_status.startswith("✅"): # If API key is not valid
 
611
  Re-initialize the interview function entirely
612
  to start from scratch, clearing the Chatbot.
613
  """
614
+ global interview_func, initial_message, final_message, questions # Access global variables, use global not nonlocal here
615
  interview_func, initial_msg, final_msg = conduct_interview(questions) # Re-init with current questions
616
  return [], "", None
617