Spaces:
Sleeping
Sleeping
Prudvireddy
commited on
Update tools.py
Browse files
tools.py
CHANGED
@@ -13,7 +13,7 @@ from langchain_community.utilities import WikipediaAPIWrapper
|
|
13 |
# import bitsandbytes as bnb
|
14 |
# import torch.nn as nn
|
15 |
# import torch
|
16 |
-
import pyttsx3
|
17 |
# from agents import get_agents_and_tasks
|
18 |
# from langchain_google_genai import ChatGoogleGenerativeAI
|
19 |
|
@@ -61,31 +61,66 @@ import pyttsx3
|
|
61 |
# pipe.enable_model_cpu_offload()
|
62 |
|
63 |
|
64 |
-
def generate_speech(text, speech_dir='./outputs/audio', lang='en', speed=170, voice='default', num=0):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
"""
|
66 |
-
Generates speech for given script.
|
67 |
"""
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
-
#
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
else:
|
75 |
-
|
76 |
-
voice_id = None
|
77 |
-
for v in voices:
|
78 |
-
if voice in v.name:
|
79 |
-
voice_id = v.id
|
80 |
-
break
|
81 |
-
if not voice_id:
|
82 |
-
raise ValueError(f"Voice '{voice}' not found.")
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
|
90 |
# class VideoGeneration(BaseModel):
|
91 |
# images_dir : str = Field(description='Path to images directory, such as "outputs/images"')
|
|
|
13 |
# import bitsandbytes as bnb
|
14 |
# import torch.nn as nn
|
15 |
# import torch
|
16 |
+
# import pyttsx3
|
17 |
# from agents import get_agents_and_tasks
|
18 |
# from langchain_google_genai import ChatGoogleGenerativeAI
|
19 |
|
|
|
61 |
# pipe.enable_model_cpu_offload()
|
62 |
|
63 |
|
64 |
+
# def generate_speech(text, speech_dir='./outputs/audio', lang='en', speed=170, voice='default', num=0):
|
65 |
+
# """
|
66 |
+
# Generates speech for given script.
|
67 |
+
# """
|
68 |
+
# engine = pyttsx3.init()
|
69 |
+
|
70 |
+
# # Set language and voice
|
71 |
+
# voices = engine.getProperty('voices')
|
72 |
+
# if voice == 'default':
|
73 |
+
# voice_id = voices[1].id
|
74 |
+
# else:
|
75 |
+
# # Try to find the voice with the given name
|
76 |
+
# voice_id = None
|
77 |
+
# for v in voices:
|
78 |
+
# if voice in v.name:
|
79 |
+
# voice_id = v.id
|
80 |
+
# break
|
81 |
+
# if not voice_id:
|
82 |
+
# raise ValueError(f"Voice '{voice}' not found.")
|
83 |
+
|
84 |
+
# engine.setProperty('voice', voice_id)
|
85 |
+
# engine.setProperty('rate', speed)
|
86 |
+
# # os.remove(os.path.join(os.path.dirname(os.path.abspath(__file__)), speech_dir, f'speech_{num}.mp3')) if os.path.exists(os.path.join(speech_dir, f'speech_{num}.mp3')) else None
|
87 |
+
# engine.save_to_file(text, os.path.join(os.path.dirname(os.path.abspath(__file__)), speech_dir, f'speech_{num}.mp3'))
|
88 |
+
# engine.runAndWait()
|
89 |
+
|
90 |
+
def generate_speech(text, speech_dir='./outputs/speeches', lang='en', speed=1.0, num=0):
|
91 |
"""
|
92 |
+
Generates speech for the given script using gTTS and adjusts the speed.
|
93 |
"""
|
94 |
+
# Ensure the speech directory exists
|
95 |
+
if not os.path.exists(speech_dir):
|
96 |
+
os.makedirs(speech_dir)
|
97 |
+
|
98 |
+
# Generate speech
|
99 |
+
tts = gTTS(text=text, lang=lang)
|
100 |
|
101 |
+
# Save the speech to an MP3 file
|
102 |
+
speech_path = os.path.join(speech_dir, f'speech_{num}.mp3')
|
103 |
+
temp_path = os.path.join(speech_dir, f'temp_speech_{num}.mp3')
|
104 |
+
if os.path.exists(speech_path):
|
105 |
+
os.remove(speech_path) # Remove existing file if it exists
|
106 |
+
|
107 |
+
tts.save(temp_path)
|
108 |
+
|
109 |
+
# Adjust the speed of the speech
|
110 |
+
sound = AudioSegment.from_file(temp_path)
|
111 |
+
if speed != 1.0:
|
112 |
+
sound_with_altered_speed = sound._spawn(sound.raw_data, overrides={
|
113 |
+
"frame_rate": int(sound.frame_rate * speed)
|
114 |
+
}).set_frame_rate(sound.frame_rate)
|
115 |
+
sound_with_altered_speed.export(speech_path, format="mp3")
|
116 |
else:
|
117 |
+
sound.export(speech_path, format="mp3")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
+
os.remove(temp_path) # Remove the temporary file
|
120 |
+
# print(f"Speech saved to {speech_path}")
|
121 |
+
|
122 |
+
# Example usage
|
123 |
+
# generate_speech("Hello, this is a test speech.", speed=1.2, num=1)
|
124 |
|
125 |
# class VideoGeneration(BaseModel):
|
126 |
# images_dir : str = Field(description='Path to images directory, such as "outputs/images"')
|