Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -62,47 +62,55 @@ class FastVideoGenerator:
|
|
| 62 |
@staticmethod
|
| 63 |
def clean_text(text):
|
| 64 |
"""Clean text to handle encoding issues"""
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
text = text.encode('ascii', 'ignore').decode('ascii')
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
@staticmethod
|
| 84 |
@st.cache_data
|
| 85 |
def generate_script_cached(prompt, style, length, temperature=0.7):
|
| 86 |
"""Cached script generation with proper text cleaning"""
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
| 96 |
output = generator(
|
| 97 |
f"{style_prompts[style]} {prompt}. Make it {length} seconds long.",
|
| 98 |
max_length=min(length * 3, 1000),
|
| 99 |
num_return_sequences=1,
|
| 100 |
temperature=temperature
|
| 101 |
)
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
def create_frame_fast(self, text, theme, frame_number, total_frames, size=(1280, 720)):
|
| 108 |
"""Create frame with cleaned text"""
|
|
@@ -145,16 +153,15 @@ class FastVideoGenerator:
|
|
| 145 |
for i, chunk in enumerate(chunks):
|
| 146 |
chunk_path = self.temp_dir / f"audio_chunk_{i}.mp3"
|
| 147 |
try:
|
|
|
|
|
|
|
| 148 |
tts = gTTS(text=chunk, lang='en', slow=False)
|
| 149 |
tts.save(str(chunk_path))
|
| 150 |
audio_paths.append(chunk_path)
|
| 151 |
except Exception as e:
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
tts.save(str(chunk_path))
|
| 156 |
-
audio_paths.append(chunk_path)
|
| 157 |
-
|
| 158 |
return audio_paths
|
| 159 |
|
| 160 |
def create_optimized_video(self, script, theme, duration=30):
|
|
|
|
| 62 |
@staticmethod
|
| 63 |
def clean_text(text):
|
| 64 |
"""Clean text to handle encoding issues"""
|
| 65 |
+
if not isinstance(text, str):
|
| 66 |
+
text = str(text)
|
| 67 |
+
|
| 68 |
+
# Remove non-ASCII characters
|
| 69 |
text = text.encode('ascii', 'ignore').decode('ascii')
|
| 70 |
+
|
| 71 |
+
# Replace common problematic characters
|
| 72 |
+
text = text.replace('–', '-') # en dash
|
| 73 |
+
text = text.replace('—', '-') # em dash
|
| 74 |
+
text = text.replace('"', '"') # smart quotes
|
| 75 |
+
text = text.replace('"', '"') # smart quotes
|
| 76 |
+
text = text.replace(''', "'") # smart apostrophe
|
| 77 |
+
text = text.replace(''', "'") # smart apostrophe
|
| 78 |
+
text = text.replace('…', '...') # ellipsis
|
| 79 |
+
|
| 80 |
+
# Remove any remaining non-standard characters
|
| 81 |
+
text = re.sub(r'[^\x00-\x7F]+', '', text)
|
| 82 |
+
|
| 83 |
+
return text.strip()
|
| 84 |
|
| 85 |
@staticmethod
|
| 86 |
@st.cache_data
|
| 87 |
def generate_script_cached(prompt, style, length, temperature=0.7):
|
| 88 |
"""Cached script generation with proper text cleaning"""
|
| 89 |
+
try:
|
| 90 |
+
style_prompts = {
|
| 91 |
+
'Professional': "Write a clear, professional video script about:",
|
| 92 |
+
'Creative': "Write an engaging, creative video script about:",
|
| 93 |
+
'Educational': "Write an informative educational video script about:"
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
# Clean prompt before processing
|
| 97 |
+
prompt = FastVideoGenerator.clean_text(prompt)
|
| 98 |
+
|
| 99 |
+
generator = pipeline('text-generation', model='distilgpt2')
|
| 100 |
output = generator(
|
| 101 |
f"{style_prompts[style]} {prompt}. Make it {length} seconds long.",
|
| 102 |
max_length=min(length * 3, 1000),
|
| 103 |
num_return_sequences=1,
|
| 104 |
temperature=temperature
|
| 105 |
)
|
| 106 |
+
|
| 107 |
+
script = output[0]['generated_text']
|
| 108 |
+
script = script.replace(style_prompts[style], '').strip()
|
| 109 |
+
return FastVideoGenerator.clean_text(script)
|
| 110 |
+
|
| 111 |
+
except Exception as e:
|
| 112 |
+
st.error(f"Script generation failed: {str(e)}")
|
| 113 |
+
raise
|
| 114 |
|
| 115 |
def create_frame_fast(self, text, theme, frame_number, total_frames, size=(1280, 720)):
|
| 116 |
"""Create frame with cleaned text"""
|
|
|
|
| 153 |
for i, chunk in enumerate(chunks):
|
| 154 |
chunk_path = self.temp_dir / f"audio_chunk_{i}.mp3"
|
| 155 |
try:
|
| 156 |
+
# Additional cleaning for gTTS
|
| 157 |
+
chunk = re.sub(r'[^\x00-\x7F]+', '', chunk)
|
| 158 |
tts = gTTS(text=chunk, lang='en', slow=False)
|
| 159 |
tts.save(str(chunk_path))
|
| 160 |
audio_paths.append(chunk_path)
|
| 161 |
except Exception as e:
|
| 162 |
+
st.error(f"Audio generation failed for chunk {i}: {str(e)}")
|
| 163 |
+
raise
|
| 164 |
+
|
|
|
|
|
|
|
|
|
|
| 165 |
return audio_paths
|
| 166 |
|
| 167 |
def create_optimized_video(self, script, theme, duration=30):
|