ruslanmv commited on
Commit
1e346bd
·
verified ·
1 Parent(s): 828c2eb

Update tool2.py

Browse files
Files changed (1) hide show
  1. tool2.py +7 -53
tool2.py CHANGED
@@ -39,9 +39,13 @@ def display_question(index, audio_enabled, selected_questions):
39
  return "Question index out of range.", [], None
40
 
41
  def get_explanation_and_answer(index, selected_questions):
42
- """Retrieves the explanation and correct answer for a given question index."""
43
  if 0 <= index < len(selected_questions):
44
- return selected_questions[index]["explanation"], selected_questions[index]["answer"]
 
 
 
 
45
  return "No explanation available.", ""
46
 
47
  # Text-to-speech function with rate limiting, retry mechanism, and client rotation
@@ -98,54 +102,4 @@ try:
98
  clients.append(client_3)
99
  print("Loaded voice transformer TTS client (client_3) ✔")
100
  except ValueError as e:
101
- print(f"Error loading voice transformer TTS client (client_3): {e}")
102
- print("Voice Transformer Text-to-Speech (client_3) will be unavailable.")
103
- except Exception as e: # Catch other potential exceptions during client initialization
104
- print(f"An unexpected error occurred during voice transformer TTS client (client_3) initialization: {e}")
105
- print("Voice Transformer Text-to-Speech (client_3) will be unavailable.")
106
-
107
- if not clients:
108
- print("No Text-to-speech clients loaded due to errors. Audio functionality will be disabled.")
109
- else:
110
- print(f"Loaded {len(clients)} TTS clients.")
111
-
112
- # Text-to-speech function with rate limiting, retry mechanism, and client rotation
113
- def text_to_speech(text, retries=3, delay=5):
114
- global clients # Ensure we are using the global clients list
115
- if not clients: # If no clients are loaded, return None immediately
116
- print("Warning: No Text-to-speech clients available.")
117
- return None
118
-
119
- client_index = 0 # Start with the first available client
120
- for attempt in range(retries):
121
- try:
122
- client = clients[client_index % len(clients)] # Use modulo to cycle through available clients
123
- client_index += 1 # Increment client_index for the next attempt in case of rate limit
124
- print(f"Attempt {attempt + 1} using client: {client_index % len(clients) + 1}") # Client index for logging (1-based)
125
- if client == client_fast: # Check if using client_fast
126
- result = client.predict(
127
- language="English",
128
- repo_id="csukuangfj/vits-piper-en_US-hfc_female-medium|1 speaker",
129
- text=text,
130
- sid="0",
131
- speed=0.8,
132
- api_name="/process"
133
- )
134
- else: # Assuming client_2 or client_3 or any other client in the future
135
- result = client.predict(
136
- text=text,
137
- api_name="/predict"
138
- )
139
- return result
140
- except httpx.HTTPStatusError as e:
141
- if e.response.status_code == 429:
142
- print(f"Rate limit exceeded using client {client_index % len(clients) + 1}. Retrying in {delay} seconds...")
143
- time.sleep(delay)
144
- else:
145
- raise e
146
- except Exception as e: # Catch any other potential errors during prediction
147
- print(f"Error during text-to-speech prediction using client {client_index % len(clients) + 1}: {e}")
148
- # Consider rotating client on any error, not just rate limits, to try other clients if available
149
- client_index += 1 # Rotate to the next client for the next attempt
150
- print("Max retries exceeded for all TTS clients. Could not process the request.")
151
- return None
 
39
  return "Question index out of range.", [], None
40
 
41
  def get_explanation_and_answer(index, selected_questions):
42
+ """Retrieves the explanation and correct answer for a given question index, handling missing explanations."""
43
  if 0 <= index < len(selected_questions):
44
+ question_data = selected_questions[index]
45
+ # Use get() with a default value to handle missing 'explanation' key
46
+ explanation = question_data.get("explanation", "No explanation available.")
47
+ correct_answer = question_data["answer"] # Assume 'answer' key always exists
48
+ return explanation, correct_answer
49
  return "No explanation available.", ""
50
 
51
  # Text-to-speech function with rate limiting, retry mechanism, and client rotation
 
102
  clients.append(client_3)
103
  print("Loaded voice transformer TTS client (client_3) ✔")
104
  except ValueError as e:
105
+ print(f"Error loading voice transformer TTS client (