Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,42 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import torch
|
| 4 |
-
from diffusers import StableDiffusionPipeline
|
| 5 |
-
from io import BytesIO
|
| 6 |
-
import requests
|
| 7 |
-
import tempfile
|
| 8 |
-
import base64
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
"stabilityai/stable-diffusion-2-1-base"
|
| 15 |
-
)
|
| 16 |
-
pipe_image = pipe_image.to("cpu") # CPU friendly
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def generate_image(prompt):
|
| 19 |
-
image =
|
| 20 |
return image
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
# Video Generation (Placeholder)
|
| 24 |
-
# ========================
|
| 25 |
-
# CPU তে ভিডিও Diffusion চালানো কঠিন, তাই placeholder
|
| 26 |
def generate_video(prompt):
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 32 |
-
temp_file = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
| 33 |
-
out = cv2.VideoWriter(temp_file.name, fourcc, 5, (width, height))
|
| 34 |
-
for i in range(10):
|
| 35 |
-
frame = np.zeros((height, width, 3), dtype=np.uint8)
|
| 36 |
-
frame[:, :, 0] = int(25*i) # Blue channel gradient
|
| 37 |
-
frame[:, :, 1] = int(255-25*i) # Green channel gradient
|
| 38 |
-
frame[:, :, 2] = 128 # Constant Red
|
| 39 |
-
out.write(frame)
|
| 40 |
-
out.release()
|
| 41 |
-
return temp_file.name
|
| 42 |
-
|
| 43 |
-
# ========================
|
| 44 |
-
# Voice / Text-to-Speech
|
| 45 |
-
# ========================
|
| 46 |
-
# CPU compatible HuggingFace TTS model
|
| 47 |
-
from TTS.api import TTS
|
| 48 |
-
|
| 49 |
-
tts_model = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False)
|
| 50 |
-
|
| 51 |
-
def generate_voice(text):
|
| 52 |
-
temp_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
|
| 53 |
-
tts_model.tts_to_file(text=text, file_path=temp_file.name)
|
| 54 |
-
return temp_file.name
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
# Gradio UI
|
| 58 |
-
# ========================
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
-
gr.Markdown("##
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
out_vid = gr.Video(label="Generated Video")
|
| 73 |
-
btn_vid = gr.Button("Generate Video")
|
| 74 |
-
btn_vid.click(generate_video, inputs=prompt_vid, outputs=out_vid)
|
| 75 |
-
|
| 76 |
-
# Voice Tab
|
| 77 |
-
with gr.Tab("Voice Generation"):
|
| 78 |
-
prompt_voice = gr.Textbox(label="Enter text for voice")
|
| 79 |
-
out_voice = gr.Audio(label="Generated Voice")
|
| 80 |
-
btn_voice = gr.Button("Generate Voice")
|
| 81 |
-
btn_voice.click(generate_voice, inputs=prompt_voice, outputs=out_voice)
|
| 82 |
|
| 83 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from diffusers import StableDiffusionPipeline, DiffusionPipeline
|
| 3 |
+
import torch, imageio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Load Image Generator
|
| 6 |
+
img_model = "runwayml/stable-diffusion-v1-5"
|
| 7 |
+
img_pipe = StableDiffusionPipeline.from_pretrained(img_model, torch_dtype=torch.float32)
|
| 8 |
+
img_pipe = img_pipe.to("cpu")
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Load Video Generator
|
| 11 |
+
vid_model = "damo-vilab/text-to-video-ms-1.7b"
|
| 12 |
+
vid_pipe = DiffusionPipeline.from_pretrained(vid_model, torch_dtype=torch.float32)
|
| 13 |
+
vid_pipe = vid_pipe.to("cpu")
|
| 14 |
+
|
| 15 |
+
# Image generation function
|
| 16 |
def generate_image(prompt):
|
| 17 |
+
image = img_pipe(prompt).images[0]
|
| 18 |
return image
|
| 19 |
|
| 20 |
+
# Video generation function
|
|
|
|
|
|
|
|
|
|
| 21 |
def generate_video(prompt):
|
| 22 |
+
video_frames = vid_pipe(prompt, num_frames=8).frames
|
| 23 |
+
output_path = "output.mp4"
|
| 24 |
+
imageio.mimsave(output_path, video_frames, fps=8)
|
| 25 |
+
return output_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Gradio Tabs UI
|
|
|
|
|
|
|
| 28 |
with gr.Blocks() as demo:
|
| 29 |
+
gr.Markdown("## 🎨 Free AI Image + Video Generator")
|
| 30 |
+
with gr.Tab("🖼️ Image Generator"):
|
| 31 |
+
text_in = gr.Textbox(label="ছবির জন্য প্রম্পট লিখুন")
|
| 32 |
+
img_out = gr.Image(type="pil")
|
| 33 |
+
btn1 = gr.Button("Generate Image")
|
| 34 |
+
btn1.click(generate_image, inputs=text_in, outputs=img_out)
|
| 35 |
+
|
| 36 |
+
with gr.Tab("🎬 Video Generator"):
|
| 37 |
+
text_in2 = gr.Textbox(label="ভিডিওর জন্য প্রম্পট লিখুন")
|
| 38 |
+
vid_out = gr.Video()
|
| 39 |
+
btn2 = gr.Button("Generate Video")
|
| 40 |
+
btn2.click(generate_video, inputs=text_in2, outputs=vid_out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
demo.launch()
|