File size: 2,751 Bytes
3b63c0b
025171b
343646a
 
 
 
f151169
343646a
025171b
 
85cf9cd
0d09902
 
025171b
0d09902
f141714
343646a
0d09902
025171b
0d09902
 
 
e6856ce
0d09902
343646a
 
 
 
 
e6856ce
0d09902
 
 
 
 
 
 
e6856ce
0d09902
 
 
 
 
 
 
 
e6856ce
 
 
0d09902
 
 
 
343646a
0d09902
343646a
f151169
0d09902
343646a
3b63c0b
0d09902
 
 
 
343646a
 
 
 
 
 
 
 
 
 
 
 
 
0d09902
 
343646a
e6856ce
343646a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import requests
import textwrap
from moviepy.editor import *
from gtts import gTTS
import gradio as gr
from transformers import pipeline
from tempfile import NamedTemporaryFile
from PIL import Image
from io import BytesIO

# Single Unsplash image for speed
UNSPLASH_IMAGE = "https://images.unsplash.com/photo-1603398938378-7c6a282eb53b"

# Lightweight text generation model
generator = pipeline("text-generation", model="tiiuae/falcon-rw-1b")

# Generate remedy script
def generate_script(topic):
    prompt = f"Suggest a short 30-second natural home remedy for {topic}."
    response = generator(prompt, max_new_tokens=60)
    return response[0]["generated_text"].strip()

# Generate voice using gTTS
def generate_voice(text, lang="en"):
    tts = gTTS(text=text, lang=lang)
    temp_audio = NamedTemporaryFile(delete=False, suffix=".mp3")
    tts.save(temp_audio.name)
    return temp_audio.name

# Download one image
def download_image():
    response = requests.get(UNSPLASH_IMAGE)
    img = Image.open(BytesIO(response.content))
    path = "image.jpg"
    img.save(path)
    return path

# Generate subtitle
def create_subtitle(text, duration, size):
    wrapped = textwrap.fill(text, width=50)
    txt_clip = TextClip(wrapped, fontsize=24, color='white', bg_color='black', size=(size[0], 80))
    return txt_clip.set_duration(duration).set_position(("center", size[1] - 100))

# Generate video
def create_video(topic, script, audio_path, image_file):
    audio_clip = AudioFileClip(audio_path)
    duration = audio_clip.duration

    img_clip = ImageClip(image_file).set_duration(duration).resize(height=480)
    subtitle = create_subtitle(script, duration, img_clip.size)
    
    video = CompositeVideoClip([img_clip.set_audio(audio_clip), subtitle])
    output_path = f"/tmp/{topic.replace(' ', '_')}_video.mp4"
    video.write_videofile(output_path, fps=15, codec="libx264", audio_codec="aac")
    return output_path

# Main function for Gradio
def generate_remedy(topic, language):
    script = generate_script(topic)
    lang_code = "ur" if language == "Urdu" else "en"
    audio = generate_voice(script, lang=lang_code)
    image = download_image()
    video = create_video(topic, script, audio, image)
    return video, script

# Gradio interface
iface = gr.Interface(
    fn=generate_remedy,
    inputs=[
        gr.Textbox(label="Enter medical condition (e.g., cough, headache)"),
        gr.Radio(["English", "Urdu"], label="Narration Language")
    ],
    outputs=[
        gr.Video(label="Generated Remedy Video"),
        gr.Textbox(label="Remedy Script")
    ],
    title="RemedyReels (Fast Mode)",
    description="Quick AI home remedy video generator with voice-over, subtitles, and Unsplash image."
)

iface.launch()