Spaces:
Running
Running
File size: 5,599 Bytes
6e912a0 37958d7 05a809a 37958d7 e142d65 6e912a0 f98580a ab52f60 6e912a0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import gradio as gr
from gradio_client import Client
import os
import json
# Function to load question sets from a directory
def load_question_sets_vce(directory='questions'):
question_sets = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(".json"):
question_sets.append(os.path.join( file)[:-5]) # remove the .json extension
return question_sets
exams = load_question_sets_vce('questions/')
print("question_sets:", exams)
def select_exam_vce(exam_name):
file_path = os.path.join(os.getcwd(), 'questions', f'{exam_name}.json')
try:
with open(file_path, 'r') as f:
questions = json.load(f)
print(f"Loaded {len(questions)} questions")
return questions # Ensure the questions are returned here
except FileNotFoundError:
print(f"File {file_path} not found.")
return [] # Return an empty list to indicate no questions were found
# Text-to-speech function with rate limiting, retry mechanism, and client rotation
import time
import httpx
# Text-to-speech clients
client_1 = Client("ruslanmv/text-to-speech-fast")
client_2 = Client("ruslanmv/Text-To-Speech") # Set timeout to 5 seconds
client_3 = Client("ruslanmv/Text-to-Voice-Transformers")
clients = [client_1, client_3,client_2]
# Text-to-speech function with rate limiting, retry mechanism, and client rotation
def text_to_speech(text, retries=3, delay=5):
client_index = 0 # Start with the first client
for attempt in range(retries):
try:
client = clients[client_index]
print(f"Attempt {attempt + 1}")
if client_index == 0:
result = client.predict(
language="English",
repo_id="csukuangfj/vits-piper-en_US-hfc_female-medium|1 speaker",
text=text,
sid="0",
speed=0.8,
api_name="/process"
)
else:
result = client.predict(
text=text,
api_name="/predict"
)
return result
except httpx.HTTPStatusError as e:
if e.response.status_code == 429:
print(f"Rate limit exceeded. Retrying in {delay} seconds...")
client_index = (client_index + 1) % len(clients) # Rotate to the next client
time.sleep(delay)
else:
raise e
print("Max retries exceeded. Could not process the request.")
return None
# Function to start exam
def start_exam(exam_choice, audio_enabled):
global selected_questions
selected_questions = select_exam_vce(exam_choice)
question, options, audio_path = display_question(0, audio_enabled)
return (
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
gr.update(visible=False), # Hide the audio_checkbox
gr.update(visible=True), question, gr.update(choices=options, visible=True), gr.update(visible=True),
gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), 0, "", audio_path
)
# Function to display a question
def display_question(index, audio_enabled):
if index < 0 or index >= len(selected_questions):
return "No more questions.", [], None
question_text_ = selected_questions[index]['question']
question_text = f"**Question {index + 1}:** {question_text_}" # Numbering added
choices_options = selected_questions[index]['options']
audio_path = text_to_speech(question_text_ + " " + " ".join(choices_options)) if audio_enabled else None
return question_text, choices_options, audio_path
# Function to check the answer
def check_answer(index, answer):
correct_answer = selected_questions[index]['correct']
if answer == correct_answer:
return f"Correct! The answer is: {correct_answer}"
else:
return f"Incorrect. The correct answer is: {correct_answer}"
# Function to update the question
def update_question(index, audio_enabled):
question, options, audio_path = display_question(index, audio_enabled)
return question, gr.update(choices=options), index, audio_path
# Function to handle the answer submission
def handle_answer(index, answer, audio_enabled):
result = check_answer(index, answer)
audio_path = text_to_speech(result) if audio_enabled else None
return result, audio_path
# Function to handle the next question
def handle_next(index, audio_enabled):
new_index = min(index + 1, len(selected_questions) - 1)
question, options, new_index, audio_path = update_question(new_index, audio_enabled)
return question, options, new_index, "", audio_path
# Function to handle the previous question
def handle_previous(index, audio_enabled):
new_index = max(index - 1, 0)
question, options, new_index, audio_path = update_question(new_index, audio_enabled)
return question, options, new_index, "", audio_path
# Function to return to the home page
def return_home():
return (
gr.update(visible=True),
gr.update(visible=True),
gr.update(visible=True),
gr.update(visible=True),
gr.update(visible=True), # Show the audio_checkbox
#gr.update(visible=True), # Show question slider
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False),
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), "", ""
) |