-
-
Save frankgx97/a9516c9f612631a0469a7e61ec2b0812 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| import hashlib | |
| import os | |
| import unicodedata | |
| import re | |
| def clean_text(text: str) -> str: | |
| # Remove accents | |
| text = ''.join(c for c in unicodedata.normalize('NFKD', text) if not unicodedata.combining(c)) | |
| # Remove special characters except letters, numbers, and spaces | |
| text = re.sub(r'[^a-zA-Z0-9\s]', '', text) | |
| # Replace spaces with underscores | |
| text = text.replace(" ", "_") | |
| return text | |
| def clean_input(text: str) -> str: | |
| text = text.replace(" qn. ", " quelq'un ") | |
| text = text.replace(" qch. ", " quelque chose ") | |
| text = text.replace("/", ",") | |
| return text.strip() | |
| def text_to_audio(api_key, text, language="fr", voice="FrenchFemale", output_format="mp3"): | |
| """ | |
| Convert French text to audio using Narakeet API. | |
| Args: | |
| api_key (str): Narakeet API key. | |
| text (str): Text input to convert to audio. | |
| language (str): Language of the text. Default is French ("fr"). | |
| voice (str): Voice to use for narration. Default is "FrenchFemale". | |
| output_format (str): Output file format ("mp3" or "m4a"). | |
| Returns: | |
| str: Path to the generated audio file. | |
| """ | |
| # Base URL for Narakeet API | |
| url = "https://api.narakeet.com/text-to-speech/mp3?voice=marion" | |
| # Generate a filename based on the text content (hash for uniqueness) | |
| max_length = 50 # Limit for file name excerpt length | |
| file_name_excerpt = text[:max_length].strip().replace(" ", "_") | |
| hashed_excerpt = hashlib.md5(text.encode('utf-8')).hexdigest()[:8] | |
| file_name = f"{file_name_excerpt}_{hashed_excerpt}" | |
| file_name = clean_text(file_name) + "." + output_format | |
| # Create the payload for the API | |
| payload = { | |
| "voice": voice, | |
| "text": text, | |
| "language": language, | |
| "outputFormat": output_format | |
| } | |
| data=text | |
| # Headers with API key | |
| headers = { | |
| "x-api-key": api_key, | |
| "Content-Type": "text/plain", | |
| "accept": "application/octet-stream" | |
| } | |
| # Send request to Narakeet API | |
| response = requests.post(url, data=data, headers=headers) | |
| # Check if request was successful | |
| if response.status_code == 200: | |
| # Save the audio file | |
| output_path = os.path.join(os.getcwd(), file_name) | |
| with open(output_path, "wb") as audio_file: | |
| audio_file.write(response.content) | |
| return output_path | |
| else: | |
| print(f"Error: {response.status_code} - {response.text}") | |
| return None | |
| # Infinite loop for taking input and generating audio | |
| if __name__ == "__main__": | |
| # Replace 'your_api_key' with your actual Narakeet API key | |
| narakeet_api_key = "your-narakeet-api-key" | |
| while True: | |
| # Take text input | |
| french_text = input("Enter French text (or type 'exit' to quit): ") | |
| french_text = clean_input(french_text) | |
| # Exit condition | |
| if french_text.lower() == "exit": | |
| print("Exiting the program.") | |
| break | |
| # Call the function to create audio | |
| audio_file = text_to_audio(narakeet_api_key, french_text, output_format="mp3") | |
| # Output result | |
| if audio_file: | |
| print(f"Audio generated successfully: {audio_file}") | |
| else: | |
| print("Failed to generate audio. Please try again.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment