saima730 commited on
Commit
e0e70f0
1 Parent(s): ddc0afe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -19
app.py CHANGED
@@ -4,6 +4,7 @@ from moviepy.editor import TextClip, AudioFileClip
4
  from transformers import RagTokenizer, RagRetriever, RagSequenceForGeneration
5
  import torch
6
  import tempfile
 
7
 
8
  # Initialize RAG model components
9
  tokenizer = RagTokenizer.from_pretrained("facebook/rag-sequence-nq")
@@ -13,31 +14,53 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
13
  model = model.to(device)
14
 
15
  def generate_response(input_text):
16
- input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to(device)
17
- generated = model.generate(input_ids)
18
- response = tokenizer.batch_decode(generated, skip_special_tokens=True)[0]
19
- return response
 
 
 
 
20
 
21
  def text_to_speech(text):
22
- tts = gTTS(text)
23
- with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as temp_audio_file:
24
- tts.save(temp_audio_file.name)
25
- return temp_audio_file.name
 
 
 
 
26
 
27
  def text_to_video(text, audio_filename):
28
- text_clip = TextClip(text, fontsize=50, color='white', bg_color='black', size=(640, 480))
29
- text_clip = text_clip.set_duration(10)
30
- audio_clip = AudioFileClip(audio_filename)
31
- video_clip = text_clip.set_audio(audio_clip)
32
- with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video_file:
33
- video_clip.write_videofile(temp_video_file.name, codec='libx264')
34
- return temp_video_file.name
 
 
 
 
35
 
36
  def process_text(input_text):
37
- response = generate_response(input_text)
38
- audio_file = text_to_speech(response)
39
- video_file = text_to_video(response, audio_file)
40
- return response, audio_file, video_file
 
 
 
 
 
 
 
 
 
 
41
 
42
  iface = gr.Interface(
43
  fn=process_text,
 
4
  from transformers import RagTokenizer, RagRetriever, RagSequenceForGeneration
5
  import torch
6
  import tempfile
7
+ import os
8
 
9
  # Initialize RAG model components
10
  tokenizer = RagTokenizer.from_pretrained("facebook/rag-sequence-nq")
 
14
  model = model.to(device)
15
 
16
  def generate_response(input_text):
17
+ try:
18
+ input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to(device)
19
+ generated = model.generate(input_ids)
20
+ response = tokenizer.batch_decode(generated, skip_special_tokens=True)[0]
21
+ return response
22
+ except Exception as e:
23
+ print(f"Error in generate_response: {e}")
24
+ return "Error generating response"
25
 
26
  def text_to_speech(text):
27
+ try:
28
+ tts = gTTS(text)
29
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as temp_audio_file:
30
+ tts.save(temp_audio_file.name)
31
+ return temp_audio_file.name
32
+ except Exception as e:
33
+ print(f"Error in text_to_speech: {e}")
34
+ return None
35
 
36
  def text_to_video(text, audio_filename):
37
+ try:
38
+ text_clip = TextClip(text, fontsize=50, color='white', bg_color='black', size=(640, 480))
39
+ text_clip = text_clip.set_duration(10)
40
+ audio_clip = AudioFileClip(audio_filename)
41
+ video_clip = text_clip.set_audio(audio_clip)
42
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_video_file:
43
+ video_clip.write_videofile(temp_video_file.name, codec='libx264')
44
+ return temp_video_file.name
45
+ except Exception as e:
46
+ print(f"Error in text_to_video: {e}")
47
+ return None
48
 
49
  def process_text(input_text):
50
+ try:
51
+ response = generate_response(input_text)
52
+ audio_file = text_to_speech(response)
53
+ if audio_file:
54
+ video_file = text_to_video(response, audio_file)
55
+ if video_file:
56
+ return response, audio_file, video_file
57
+ else:
58
+ return response, audio_file, "Error generating video"
59
+ else:
60
+ return response, "Error generating audio", None
61
+ except Exception as e:
62
+ print(f"Error in process_text: {e}")
63
+ return "Error processing text", None, None
64
 
65
  iface = gr.Interface(
66
  fn=process_text,