saifa8237 commited on
Commit
0d09902
·
verified ·
1 Parent(s): f141714

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -41
app.py CHANGED
@@ -9,65 +9,59 @@ from tempfile import NamedTemporaryFile
9
  from PIL import Image
10
  from io import BytesIO
11
 
12
- # Constants
13
- UNSPLASH_IMAGES = [
14
- "https://images.unsplash.com/photo-1603398938378-7c6a282eb53b",
15
- "https://images.unsplash.com/photo-1588776814546-ec7e23689fcc",
16
- "https://images.unsplash.com/photo-1599566150163-29194dcaad36",
17
- "https://images.unsplash.com/photo-1621072157481-1a4c4e4ff57e"
18
- ]
19
 
20
- # Lightweight text generator
21
  generator = pipeline("text-generation", model="tiiuae/falcon-rw-1b")
22
 
23
- # Remedy script generator
24
  def generate_script(topic):
25
- try:
26
- prompt = f"Suggest a simple 60-second home remedy for {topic}."
27
- response = generator(prompt, max_new_tokens=150)
28
- return response[0]["generated_text"].strip()
29
- except:
30
- return f"A simple home remedy for {topic} is to rest, stay hydrated, and eat a balanced diet."
31
 
32
- # Voice generation
33
  def generate_voice(text, lang="en"):
34
  tts = gTTS(text=text, lang=lang)
35
  temp_audio = NamedTemporaryFile(delete=False, suffix=".mp3")
36
  tts.save(temp_audio.name)
37
  return temp_audio.name
38
 
39
- # Image downloader (resized to save RAM)
40
- def download_images():
41
- paths = []
42
- for i, url in enumerate(UNSPLASH_IMAGES[:3]):
43
- response = requests.get(url)
44
- img = Image.open(BytesIO(response.content))
45
- img = img.resize((854, 480)) # Resize to 480p width
46
- path = f"image_{i+1}.jpg"
47
- img.save(path)
48
- paths.append(path)
49
- return paths
50
 
51
- # Video creation (subtitles skipped to avoid RAM issues)
52
- def create_video(topic, script, audio_path, image_files):
 
 
 
 
 
 
53
  audio_clip = AudioFileClip(audio_path)
54
  duration = audio_clip.duration
55
- per_image_duration = duration / len(image_files)
56
-
57
- image_clips = [ImageClip(img).set_duration(per_image_duration).resize(height=480) for img in image_files]
58
- slideshow = concatenate_videoclips(image_clips, method="compose")
59
 
60
- video = slideshow.set_audio(audio_clip)
 
 
 
61
  output_path = f"/tmp/{topic.replace(' ', '_')}_video.mp4"
62
- video.write_videofile(output_path, fps=24, codec="libx264", audio_codec="aac")
63
  return output_path
64
 
65
- # Gradio pipeline
66
  def generate_remedy(topic, language):
67
  script = generate_script(topic)
68
- audio = generate_voice(script, lang="ur" if language == "Urdu" else "en")
69
- images = download_images()
70
- video = create_video(topic, script, audio, images)
 
71
  return video, script
72
 
73
  # Gradio interface
@@ -81,8 +75,8 @@ iface = gr.Interface(
81
  gr.Video(label="Generated Remedy Video"),
82
  gr.Textbox(label="Remedy Script")
83
  ],
84
- title="RemedyReels: AI-Powered Home Remedy Video Generator",
85
- description="Type a medical topic and get a narrated video remedy with Unsplash images."
86
  )
87
 
88
  iface.launch()
 
9
  from PIL import Image
10
  from io import BytesIO
11
 
12
+ # Single Unsplash image for speed
13
+ UNSPLASH_IMAGE = "https://images.unsplash.com/photo-1603398938378-7c6a282eb53b"
 
 
 
 
 
14
 
15
+ # Lightweight text generation model
16
  generator = pipeline("text-generation", model="tiiuae/falcon-rw-1b")
17
 
18
+ # Generate remedy script
19
  def generate_script(topic):
20
+ prompt = f"Suggest a short 30-second natural home remedy for {topic}."
21
+ response = generator(prompt, max_new_tokens=60)
22
+ return response[0]["generated_text"].strip()
 
 
 
23
 
24
+ # Generate voice using gTTS
25
  def generate_voice(text, lang="en"):
26
  tts = gTTS(text=text, lang=lang)
27
  temp_audio = NamedTemporaryFile(delete=False, suffix=".mp3")
28
  tts.save(temp_audio.name)
29
  return temp_audio.name
30
 
31
+ # Download one image
32
+ def download_image():
33
+ response = requests.get(UNSPLASH_IMAGE)
34
+ img = Image.open(BytesIO(response.content))
35
+ path = "image.jpg"
36
+ img.save(path)
37
+ return path
 
 
 
 
38
 
39
+ # Generate subtitle
40
+ def create_subtitle(text, duration, size):
41
+ wrapped = textwrap.fill(text, width=50)
42
+ txt_clip = TextClip(wrapped, fontsize=24, color='white', bg_color='black', size=(size[0], 80))
43
+ return txt_clip.set_duration(duration).set_position(("center", size[1] - 100))
44
+
45
+ # Generate video
46
+ def create_video(topic, script, audio_path, image_file):
47
  audio_clip = AudioFileClip(audio_path)
48
  duration = audio_clip.duration
 
 
 
 
49
 
50
+ img_clip = ImageClip(image_file).set_duration(duration).resize(height=480)
51
+ subtitle = create_subtitle(script, duration, img_clip.size)
52
+
53
+ video = CompositeVideoClip([img_clip.set_audio(audio_clip), subtitle])
54
  output_path = f"/tmp/{topic.replace(' ', '_')}_video.mp4"
55
+ video.write_videofile(output_path, fps=15, codec="libx264", audio_codec="aac")
56
  return output_path
57
 
58
+ # Main function for Gradio
59
  def generate_remedy(topic, language):
60
  script = generate_script(topic)
61
+ lang_code = "ur" if language == "Urdu" else "en"
62
+ audio = generate_voice(script, lang=lang_code)
63
+ image = download_image()
64
+ video = create_video(topic, script, audio, image)
65
  return video, script
66
 
67
  # Gradio interface
 
75
  gr.Video(label="Generated Remedy Video"),
76
  gr.Textbox(label="Remedy Script")
77
  ],
78
+ title="RemedyReels (Fast Mode)",
79
+ description="Quick AI home remedy video generator with voice-over, subtitles, and Unsplash image."
80
  )
81
 
82
  iface.launch()