|
|
| from transformers import pipeline |
| from gtts import gTTS |
| from pydub import AudioSegment |
| import os |
|
|
| def emotional_tts(text, emotion="neutral", output_path="emotional_speech.mp3"): |
| """ |
| Generates speech from text with a simulated emotion. |
| """ |
| tts = gTTS(text, lang='en', slow=False) |
| tts.save("temp.mp3") |
|
|
| audio = AudioSegment.from_mp3("temp.mp3") |
| if emotion == "POSITIVE": |
| audio = audio.speedup(playback_speed=1.2) |
| audio = audio._spawn(audio.raw_data, overrides={"frame_rate": int(audio.frame_rate * 1.1)}).set_frame_rate(audio.frame_rate) |
| elif emotion == "NEGATIVE": |
| audio = audio.speedup(playback_speed=0.9) |
| audio = audio._spawn(audio.raw_data, overrides={"frame_rate": int(audio.frame_rate * 0.9)}).set_frame_rate(audio.frame_rate) |
| |
| audio.export(output_path, format="mp3") |
| os.remove("temp.mp3") |
|
|
| def agentic_ai(text): |
| """ |
| An agent that analyzes the sentiment of a text and reads it aloud with the corresponding emotion. |
| """ |
| |
| sentiment_pipeline = pipeline("sentiment-analysis") |
| sentiment = sentiment_pipeline(text)[0]['label'] |
|
|
| |
| emotional_tts(text, emotion=sentiment, output_path="agent_speech.mp3") |
| print(f"Agent read the text with a {sentiment} emotion and saved it to agent_speech.mp3") |
|
|
| if __name__ == '__main__': |
| text_to_process = "I am so excited about the future of AI!" |
| agentic_ai(text_to_process) |
|
|