Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| from datasets import load_dataset | |
| import random | |
| import hashlib | |
| import datetime | |
| # Initialize Hugging Face API | |
| hf_token = os.environ['HF_TOKEN'] | |
| dataset_repo_id = "kmanninen/heyakida" | |
| audio_dataset = load_dataset(dataset_repo_id, token=hf_token) | |
| class GlobalState: | |
| def new_label(self): | |
| texts = [ | |
| "Hey Akida", | |
| "Okay Akida", | |
| "Hey Anita", | |
| "Hey Chiquita" | |
| ] | |
| label = random.choice(texts) | |
| return label | |
| def update_label(self): | |
| self.label = self.new_label() | |
| def __init__(self): | |
| self.label = self.new_label() | |
| global_state = GlobalState() | |
| def generate_unique_code(): | |
| current_time = datetime.datetime.now().isoformat() | |
| hash_object = hashlib.sha256(current_time.encode()) | |
| hex_dig = hash_object.hexdigest() | |
| unique_code = hex_dig[:8] | |
| return unique_code | |
| def save_audio_dataset(audio, label, dataset, dataset_repo_id): | |
| """Save recorded audio and return status""" | |
| if audio is None: | |
| return "No audio recorded" | |
| # audio is a tuple of (sample_rate, audio_data) or a file path | |
| if isinstance(audio, tuple): | |
| # If it's a tuple, the audio was recorded in memory | |
| audio_path = audio[0] # Gradio saves it temporarily and returns the path | |
| else: | |
| # If it's a string, it's already a file path | |
| audio_path = audio | |
| print(audio_path) | |
| if not audio_path: | |
| return "No audio file path received" | |
| try: | |
| # Create dataset object. | |
| new_data = { | |
| "audio": audio_path, | |
| "label": "Hey Akida" | |
| } | |
| # Concatenate the new dataset with the existing one | |
| dataset = dataset['train'].add_item(new_data) | |
| dataset.push_to_hub(repo_id=dataset_repo_id, token=hf_token) | |
| unique_code = generate_unique_code() | |
| return unique_code | |
| except Exception as e: | |
| print(f"Failed to push {e}") | |
| raise e | |
| def save_audio(audio): | |
| try: | |
| unique_code = save_audio_dataset(audio, global_state.label, audio_dataset, dataset_repo_id) | |
| global_state.update_label() | |
| confirmation_text = f"Your audio recording was successfully submitted, thank you! Here is the confirmation code: **{unique_code}**" | |
| return confirmation_text | |
| except Exception as e: | |
| print (f"Error saving audio: {str(e)}") | |
| raise e | |
| # Define the Gradio interface | |
| with gr.Blocks() as block: | |
| prompt_box = gr.Markdown(value=f"## Please record a short audio snippet of the following phrase using your microphone:") | |
| label_box = gr.Markdown(value="\"Hey Akida\"") | |
| audio_input = gr.Audio( | |
| sources=['microphone'], | |
| type="filepath", | |
| min_length=1, | |
| max_length=3 | |
| ) | |
| submit_button = gr.Button("Submit") | |
| submit_button.click( | |
| fn=save_audio, | |
| inputs=[audio_input], | |
| outputs=[label_box] | |
| ) | |
| # Launch the app | |
| block.launch() |