saifa8237 commited on
Commit
343646a
Β·
verified Β·
1 Parent(s): 9d2a056

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -64
app.py CHANGED
@@ -1,99 +1,91 @@
1
  import os
2
  import requests
3
- import pyttsx3
 
 
 
4
  from transformers import pipeline
 
5
  from PIL import Image
6
  from io import BytesIO
7
- from moviepy.editor import *
8
- import textwrap
9
 
10
- # ========== CONFIG ==========
11
- HF_API_KEY = os.getenv("Hugging_API_KEY")
12
- TTS_MODEL = "facebook/fastspeech2-en-ljspeech" # Use Hugging Face TTS model
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
 
21
- # --- Generate remedy text from Hugging Face ---
 
 
 
22
  def generate_script(topic):
23
- print("πŸ”  Generating remedy script from Hugging Face...")
24
- generator = pipeline("text-generation", model="gpt2") # Use GPT-2 for script generation
25
- return generator(f"Give a simple 60-second home remedy for {topic} in natural language.", max_length=150)[0]['generated_text'].strip()
26
 
27
- # --- Generate voiceover using Hugging Face TTS or offline ---
28
- def generate_voiceover(text, filename="voice.mp3"):
29
- print("πŸ”Š Generating voiceover...")
30
-
31
- # If you want online TTS (Hugging Face)
32
- tts = pipeline("text-to-speech", model=TTS_MODEL, tokenizer=TTS_MODEL)
33
- tts(text, path=filename)
34
-
35
- # If using offline TTS (uncomment if preferred)
36
- # engine = pyttsx3.init()
37
- # engine.setProperty('rate', 150)
38
- # engine.save_to_file(text, filename)
39
- # engine.runAndWait()
40
-
41
- return filename
42
 
43
- # --- Download images (3–5) ---
44
  def download_images():
45
- print("πŸ–ΌοΈ Downloading images...")
46
- image_paths = []
47
- for i, url in enumerate(UNSPLASH_IMAGES[:4]):
48
  response = requests.get(url)
49
  img = Image.open(BytesIO(response.content))
50
  path = f"image_{i+1}.jpg"
51
  img.save(path)
52
- image_paths.append(path)
53
- return image_paths
54
 
55
- # --- Create subtitle text clip ---
56
- def create_subtitle(text, duration, video_size):
57
  wrapped = textwrap.fill(text, width=60)
58
- txt_clip = TextClip(wrapped, fontsize=30, color='white', bg_color='black', size=(video_size[0], 100))
59
- return txt_clip.set_duration(duration).set_position(("center", video_size[1] - 120))
60
 
61
- # --- Create slideshow video with audio and subtitles ---
62
  def create_video(topic, script, audio_path, image_files):
63
- print("🎞️ Creating final video...")
64
  audio_clip = AudioFileClip(audio_path)
65
  duration = audio_clip.duration
66
  per_image_duration = duration / len(image_files)
67
 
68
- image_clips = []
69
- for path in image_files:
70
- img_clip = ImageClip(path).set_duration(per_image_duration).resize(height=720)
71
- image_clips.append(img_clip)
72
-
73
  slideshow = concatenate_videoclips(image_clips, method="compose")
74
- video_size = slideshow.size
75
- subtitle = create_subtitle(script, duration, video_size)
76
- final = CompositeVideoClip([slideshow.set_audio(audio_clip), subtitle])
77
 
78
- # Optional background music if 'music.mp3' exists
79
- if os.path.exists("music.mp3"):
80
- print("🎡 Adding background music...")
81
- music = AudioFileClip("music.mp3").volumex(0.2).set_duration(duration)
82
- final_audio = CompositeAudioClip([audio_clip.volumex(1.0), music])
83
- final = final.set_audio(final_audio)
84
 
85
- filename = f"{topic.replace(' ', '_')}_remedy_video.mp4"
86
- final.write_videofile(filename, fps=24)
87
- print(f"βœ… Video saved as: {filename}")
88
-
89
- # --- Main function ---
90
- def generate_remedy_video():
91
- topic = input("Enter a home remedy topic (e.g., hair fall): ")
92
  script = generate_script(topic)
93
- audio_file = generate_voiceover(script)
94
  images = download_images()
95
- create_video(topic, script, audio_file, images)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
- # --- Entry point ---
98
- if __name__ == "__main__":
99
- generate_remedy_video()
 
1
  import os
2
  import requests
3
+ import textwrap
4
+ from moviepy.editor import *
5
+ from gtts import gTTS
6
+ import gradio as gr
7
  from transformers import pipeline
8
+ 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
+ # Hugging Face model
21
+ generator = pipeline("text-generation", model="tiiuae/falcon-7b-instruct")
22
+
23
+ # Remedy script generator
24
  def generate_script(topic):
25
+ prompt = f"Suggest a simple 60-second home remedy for {topic}."
26
+ response = generator(prompt, max_new_tokens=150)
27
+ return response[0]["generated_text"].strip()
28
 
29
+ # Voice generation
30
+ def generate_voice(text, lang="en"):
31
+ tts = gTTS(text=text, lang=lang)
32
+ temp_audio = NamedTemporaryFile(delete=False, suffix=".mp3")
33
+ tts.save(temp_audio.name)
34
+ return temp_audio.name
 
 
 
 
 
 
 
 
 
35
 
36
+ # Image downloader
37
  def download_images():
38
+ paths = []
39
+ for i, url in enumerate(UNSPLASH_IMAGES[:3]):
 
40
  response = requests.get(url)
41
  img = Image.open(BytesIO(response.content))
42
  path = f"image_{i+1}.jpg"
43
  img.save(path)
44
+ paths.append(path)
45
+ return paths
46
 
47
+ # Subtitle generator
48
+ def create_subtitle(text, duration, size):
49
  wrapped = textwrap.fill(text, width=60)
50
+ txt_clip = TextClip(wrapped, fontsize=28, color='white', bg_color='black', size=(size[0], 100))
51
+ return txt_clip.set_duration(duration).set_position(("center", size[1] - 100))
52
 
53
+ # Video creation
54
  def create_video(topic, script, audio_path, image_files):
 
55
  audio_clip = AudioFileClip(audio_path)
56
  duration = audio_clip.duration
57
  per_image_duration = duration / len(image_files)
58
 
59
+ image_clips = [ImageClip(img).set_duration(per_image_duration).resize(height=720) for img in image_files]
 
 
 
 
60
  slideshow = concatenate_videoclips(image_clips, method="compose")
 
 
 
61
 
62
+ subtitle = create_subtitle(script, duration, slideshow.size)
63
+ video = CompositeVideoClip([slideshow.set_audio(audio_clip), subtitle])
64
+ output_path = f"/tmp/{topic.replace(' ', '_')}_video.mp4"
65
+ video.write_videofile(output_path, fps=24)
66
+ return output_path
 
67
 
68
+ # Gradio pipeline
69
+ def generate_remedy(topic, language):
 
 
 
 
 
70
  script = generate_script(topic)
71
+ audio = generate_voice(script, lang="ur" if language == "Urdu" else "en")
72
  images = download_images()
73
+ video = create_video(topic, script, audio, images)
74
+ return video, script
75
+
76
+ # Gradio interface
77
+ iface = gr.Interface(
78
+ fn=generate_remedy,
79
+ inputs=[
80
+ gr.Textbox(label="Enter medical condition (e.g., cough, headache)"),
81
+ gr.Radio(["English", "Urdu"], label="Narration Language")
82
+ ],
83
+ outputs=[
84
+ gr.Video(label="Generated Remedy Video"),
85
+ gr.Textbox(label="Remedy Script")
86
+ ],
87
+ title="RemedyReels: AI-Powered Home Remedy Video Generator",
88
+ description="Type a medical topic and get a narrated video remedy with subtitles and Unsplash images."
89
+ )
90
 
91
+ iface.launch()