Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,99 +1,91 @@
|
|
1 |
import os
|
2 |
import requests
|
3 |
-
import
|
|
|
|
|
|
|
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 |
-
#
|
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 |
-
#
|
|
|
|
|
|
|
22 |
def generate_script(topic):
|
23 |
-
|
24 |
-
|
25 |
-
return
|
26 |
|
27 |
-
#
|
28 |
-
def
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
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 |
-
#
|
44 |
def download_images():
|
45 |
-
|
46 |
-
|
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 |
-
|
53 |
-
return
|
54 |
|
55 |
-
#
|
56 |
-
def create_subtitle(text, duration,
|
57 |
wrapped = textwrap.fill(text, width=60)
|
58 |
-
txt_clip = TextClip(wrapped, fontsize=
|
59 |
-
return txt_clip.set_duration(duration).set_position(("center",
|
60 |
|
61 |
-
#
|
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 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
final = final.set_audio(final_audio)
|
84 |
|
85 |
-
|
86 |
-
|
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 |
-
|
94 |
images = download_images()
|
95 |
-
create_video(topic, script,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
-
|
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()
|
|
|
|