Upload 2 files
Browse files- app.py +79 -0
- requirements.txt +9 -0
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
|
| 4 |
+
from diffusers.utils import export_to_video
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# --- CONFIGURATION ---
|
| 8 |
+
# On utilise le CPU par défaut pour que ça tourne partout,
|
| 9 |
+
# mais sur Hugging Face, il faudra activer le GPU (T4 small) pour que ce soit rapide.
|
| 10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
+
|
| 12 |
+
print(f"🚀 Démarrage de l'API CygnisAI sur {device}...")
|
| 13 |
+
|
| 14 |
+
# --- 1. GÉNÉRATION D'IMAGES (Stable Diffusion XL Lightning) ---
|
| 15 |
+
# Modèle ultra-rapide (2-4 étapes)
|
| 16 |
+
image_model_id = "ByteDance/SDXL-Lightning"
|
| 17 |
+
try:
|
| 18 |
+
pipe_image = DiffusionPipeline.from_pretrained(
|
| 19 |
+
image_model_id,
|
| 20 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 21 |
+
use_safetensors=True
|
| 22 |
+
)
|
| 23 |
+
pipe_image.to(device)
|
| 24 |
+
print("✅ Modèle Image chargé.")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"❌ Erreur chargement modèle Image: {e}")
|
| 27 |
+
pipe_image = None
|
| 28 |
+
|
| 29 |
+
def generate_image(prompt):
|
| 30 |
+
if pipe_image is None:
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
image = pipe_image(prompt, num_inference_steps=4, guidance_scale=0).images[0]
|
| 34 |
+
output_path = "output_image.png"
|
| 35 |
+
image.save(output_path)
|
| 36 |
+
return output_path
|
| 37 |
+
|
| 38 |
+
# --- 2. GÉNÉRATION DE VIDÉOS (ModelScope) ---
|
| 39 |
+
# Ce modèle est lourd. Sur un Space gratuit CPU, cela peut prendre du temps ou crasher.
|
| 40 |
+
# Il est recommandé d'utiliser un Space GPU.
|
| 41 |
+
video_model_id = "damo-vilab/text-to-video-ms-1.7b"
|
| 42 |
+
try:
|
| 43 |
+
pipe_video = DiffusionPipeline.from_pretrained(video_model_id, torch_dtype=torch.float16 if device == "cuda" else torch.float32, variant="fp16")
|
| 44 |
+
pipe_video.scheduler = DPMSolverMultistepScheduler.from_config(pipe_video.scheduler.config)
|
| 45 |
+
pipe_video.to(device)
|
| 46 |
+
# Optimisation mémoire
|
| 47 |
+
# pipe_video.enable_model_cpu_offload()
|
| 48 |
+
print("✅ Modèle Vidéo chargé.")
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"⚠️ Modèle Vidéo non chargé (peut nécessiter plus de RAM/GPU): {e}")
|
| 51 |
+
pipe_video = None
|
| 52 |
+
|
| 53 |
+
def generate_video(prompt):
|
| 54 |
+
if pipe_video is None:
|
| 55 |
+
return "Erreur: Modèle vidéo non disponible."
|
| 56 |
+
|
| 57 |
+
video_frames = pipe_video(prompt, num_inference_steps=25).frames
|
| 58 |
+
video_path = export_to_video(video_frames, "output_video.mp4")
|
| 59 |
+
return video_path
|
| 60 |
+
|
| 61 |
+
# --- INTERFACE GRADIO & API ---
|
| 62 |
+
with gr.Blocks(title="CygnisAI Generative API") as demo:
|
| 63 |
+
gr.Markdown("# 🌌 CygnisAI Generative Engine")
|
| 64 |
+
|
| 65 |
+
with gr.Tab("Image Generation"):
|
| 66 |
+
img_input = gr.Textbox(label="Prompt Image")
|
| 67 |
+
img_output = gr.Image(label="Résultat")
|
| 68 |
+
img_btn = gr.Button("Générer Image")
|
| 69 |
+
img_btn.click(generate_image, inputs=img_input, outputs=img_output)
|
| 70 |
+
|
| 71 |
+
with gr.Tab("Video Generation"):
|
| 72 |
+
vid_input = gr.Textbox(label="Prompt Vidéo")
|
| 73 |
+
vid_output = gr.Video(label="Résultat")
|
| 74 |
+
vid_btn = gr.Button("Générer Vidéo")
|
| 75 |
+
vid_btn.click(generate_video, inputs=vid_input, outputs=vid_output)
|
| 76 |
+
|
| 77 |
+
# Lancement
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
diffusers
|
| 4 |
+
transformers
|
| 5 |
+
accelerate
|
| 6 |
+
safetensors
|
| 7 |
+
opencv-python
|
| 8 |
+
imageio
|
| 9 |
+
imageio-ffmpeg
|