shaheerawan3 commited on
Commit
ace9a0a
·
verified ·
1 Parent(s): ee4b9c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -35
app.py CHANGED
@@ -62,47 +62,55 @@ class FastVideoGenerator:
62
  @staticmethod
63
  def clean_text(text):
64
  """Clean text to handle encoding issues"""
65
- # Normalize unicode characters
66
- text = unicodedata.normalize('NFKD', text)
67
- # Replace special characters with standard ASCII
 
68
  text = text.encode('ascii', 'ignore').decode('ascii')
69
- # Replace common special characters
70
- replacements = {
71
- '–': '-', # en dash
72
- '—': '-', # em dash
73
- ''': "'", # curly quote
74
- ''': "'", # curly quote
75
- '"': '"', # curly quote
76
- '"': '"', # curly quote
77
- '…': '...' # ellipsis
78
- }
79
- for old, new in replacements.items():
80
- text = text.replace(old, new)
81
- return text
 
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
- style_prompts = {
88
- 'Professional': "Write a clear, professional video script about:",
89
- 'Creative': "Write an engaging, creative video script about:",
90
- 'Educational': "Write an informative educational video script about:"
91
- }
92
-
93
- prompt = FastVideoGenerator.clean_text(prompt)
94
-
95
- with pipeline('text-generation', model='distilgpt2') as generator:
 
 
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
- script = output[0]['generated_text']
104
- script = script.replace(style_prompts[style], '').strip()
105
- return FastVideoGenerator.clean_text(script)
 
 
 
 
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
- # If TTS fails, try with further cleaning
153
- cleaned_chunk = re.sub(r'[^a-zA-Z0-9\s.,!?-]', '', chunk)
154
- tts = gTTS(text=cleaned_chunk, lang='en', slow=False)
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):