Spaces:
Runtime error
Runtime error
Create tool.py
Browse files
tool.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from gradio_client import Client
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
|
5 |
+
# Function to load question sets from a directory
|
6 |
+
def load_question_sets_vce(directory='questions'):
|
7 |
+
question_sets = []
|
8 |
+
for root, dirs, files in os.walk(directory):
|
9 |
+
for file in files:
|
10 |
+
if file.endswith(".json"):
|
11 |
+
question_sets.append(os.path.join( file)[:-5]) # remove the .json extension
|
12 |
+
return question_sets
|
13 |
+
|
14 |
+
exams = load_question_sets_vce('questions/')
|
15 |
+
print("question_sets:", exams)
|
16 |
+
|
17 |
+
def select_exam_vce(exam_name):
|
18 |
+
file_path = os.path.join(os.getcwd(), 'questions', f'{exam_name}.json')
|
19 |
+
try:
|
20 |
+
with open(file_path, 'r') as f:
|
21 |
+
questions = json.load(f)
|
22 |
+
print(f"Loaded {len(questions)} questions")
|
23 |
+
return questions # Ensure the questions are returned here
|
24 |
+
except FileNotFoundError:
|
25 |
+
print(f"File {file_path} not found.")
|
26 |
+
return [] # Return an empty list to indicate no questions were found
|
27 |
+
|
28 |
+
|
29 |
+
# Text-to-speech function with rate limiting, retry mechanism, and client rotation
|
30 |
+
import time
|
31 |
+
import httpx
|
32 |
+
# Text-to-speech clients
|
33 |
+
client_1 = Client("ruslanmv/text-to-speech-fast")
|
34 |
+
client_2 = Client("ruslanmv/Text-To-Speech")
|
35 |
+
client_3 = Client("ruslanmv/Text-to-Voice-Transformers")
|
36 |
+
clients = [client_1, client_2, client_3]
|
37 |
+
# Text-to-speech function with rate limiting, retry mechanism, and client rotation
|
38 |
+
def text_to_speech(text, retries=3, delay=5):
|
39 |
+
client_index = 0 # Start with the first client
|
40 |
+
for attempt in range(retries):
|
41 |
+
try:
|
42 |
+
client = clients[client_index]
|
43 |
+
print(f"Attempt {attempt + 1}")
|
44 |
+
if client_index == 0:
|
45 |
+
result = client.predict(
|
46 |
+
language="English",
|
47 |
+
repo_id="csukuangfj/vits-piper-en_US-hfc_female-medium|1 speaker",
|
48 |
+
text=text,
|
49 |
+
sid="0",
|
50 |
+
speed=0.8,
|
51 |
+
api_name="/process"
|
52 |
+
)
|
53 |
+
else:
|
54 |
+
result = client.predict(
|
55 |
+
text=text,
|
56 |
+
api_name="/predict"
|
57 |
+
)
|
58 |
+
return result
|
59 |
+
except httpx.HTTPStatusError as e:
|
60 |
+
if e.response.status_code == 429:
|
61 |
+
print(f"Rate limit exceeded. Retrying in {delay} seconds...")
|
62 |
+
client_index = (client_index + 1) % len(clients) # Rotate to the next client
|
63 |
+
time.sleep(delay)
|
64 |
+
else:
|
65 |
+
raise e
|
66 |
+
|
67 |
+
print("Max retries exceeded. Could not process the request.")
|
68 |
+
return None
|