Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,54 @@
|
|
| 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 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
pipe_image
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
return
|
| 37 |
-
|
| 38 |
-
# ---
|
| 39 |
-
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 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()
|
|
|
|
| 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 CPU ---
|
| 8 |
+
device = "cpu"
|
| 9 |
+
print(f"🚀 Démarrage de l'API CygnisAI sur {device} (Mode Économique)...")
|
| 10 |
+
|
| 11 |
+
# --- 1. GÉNÉRATION D'IMAGES (Modèle Tiny) ---
|
| 12 |
+
# On utilise un modèle très léger pour le CPU
|
| 13 |
+
image_model_id = "segmind/tiny-sd"
|
| 14 |
+
try:
|
| 15 |
+
pipe_image = DiffusionPipeline.from_pretrained(image_model_id, torch_dtype=torch.float32)
|
| 16 |
+
# Pas de .to(device) car on est déjà sur CPU par défaut
|
| 17 |
+
print("✅ Modèle Image chargé.")
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"❌ Erreur chargement modèle Image: {e}")
|
| 20 |
+
pipe_image = None
|
| 21 |
+
|
| 22 |
+
def generate_image(prompt):
|
| 23 |
+
if pipe_image is None: return None
|
| 24 |
+
# 15 étapes seulement pour aller vite
|
| 25 |
+
image = pipe_image(prompt, num_inference_steps=15).images[0]
|
| 26 |
+
output_path = "output_image.png"
|
| 27 |
+
image.save(output_path)
|
| 28 |
+
return output_path
|
| 29 |
+
|
| 30 |
+
# --- 2. GÉNÉRATION DE VIDÉOS (Désactivée ou Mock) ---
|
| 31 |
+
# Sur CPU Basic, la vidéo est impossible (timeout garanti).
|
| 32 |
+
# On va simuler ou utiliser un modèle ultra-light si dispo, mais pour l'instant
|
| 33 |
+
# on désactive pour éviter de frustrer l'utilisateur.
|
| 34 |
+
|
| 35 |
+
def generate_video(prompt):
|
| 36 |
+
return "Erreur: La génération de vidéo nécessite un GPU. Veuillez utiliser l'API Pixabay intégrée à l'application."
|
| 37 |
+
|
| 38 |
+
# --- INTERFACE GRADIO ---
|
| 39 |
+
with gr.Blocks(title="CygnisAI API (CPU Mode)", theme=gr.themes.Soft()) as demo:
|
| 40 |
+
gr.Markdown("# 🌌 CygnisAI API (CPU Mode)")
|
| 41 |
+
gr.Markdown("⚠️ Mode CPU activé. La génération vidéo est désactivée. Les images sont en basse résolution pour la rapidité.")
|
| 42 |
+
|
| 43 |
+
with gr.Tab("Image Generation"):
|
| 44 |
+
with gr.Row():
|
| 45 |
+
img_input = gr.Textbox(label="Prompt", placeholder="Un chat...")
|
| 46 |
+
img_btn = gr.Button("Générer (Lent)", variant="primary")
|
| 47 |
+
img_output = gr.Image(label="Résultat")
|
| 48 |
+
img_btn.click(generate_image, inputs=img_input, outputs=img_output)
|
| 49 |
+
|
| 50 |
+
with gr.Tab("Video Generation"):
|
| 51 |
+
gr.Markdown("❌ Indisponible sur ce serveur.")
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|