Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,91 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
# --- CONFIGURATION
|
| 8 |
-
|
| 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
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def generate_video(prompt):
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# --- INTERFACE GRADIO ---
|
| 39 |
-
with gr.Blocks(title="CygnisAI API (
|
| 40 |
-
gr.Markdown("# 🌌 CygnisAI API (
|
| 41 |
-
gr.Markdown("
|
| 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
|
| 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.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import random
|
| 4 |
+
import time
|
|
|
|
| 5 |
|
| 6 |
+
# --- CONFIGURATION ---
|
| 7 |
+
print("🚀 Démarrage de l'API CygnisAI (Proxy Pollinations)...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def generate_image(prompt):
|
| 10 |
+
if not prompt:
|
| 11 |
+
return None
|
| 12 |
+
|
| 13 |
+
# Construction de l'URL Pollinations
|
| 14 |
+
# On utilise le modèle FLUX par défaut pour la qualité
|
| 15 |
+
seed = random.randint(0, 100000)
|
| 16 |
+
clean_prompt = requests.utils.quote(prompt)
|
| 17 |
+
image_url = f"https://pollinations.ai/p/{cleanPrompt}?width=1024&height=1024&seed={seed}&model=flux&nologo=true"
|
| 18 |
+
|
| 19 |
+
print(f"📸 Génération Image: {prompt} -> {image_url}")
|
| 20 |
+
|
| 21 |
+
# On télécharge l'image pour l'afficher dans Gradio
|
| 22 |
+
try:
|
| 23 |
+
response = requests.get(image_url)
|
| 24 |
+
if response.status_code == 200:
|
| 25 |
+
output_path = "output_image.jpg"
|
| 26 |
+
with open(output_path, "wb") as f:
|
| 27 |
+
f.write(response.content)
|
| 28 |
+
return output_path
|
| 29 |
+
else:
|
| 30 |
+
return None
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"Erreur téléchargement image: {e}")
|
| 33 |
+
return None
|
| 34 |
|
| 35 |
def generate_video(prompt):
|
| 36 |
+
if not prompt:
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
+
# Pollinations ne fait pas encore de vidéo "text-to-video" stable via URL simple comme les images.
|
| 40 |
+
# Mais on peut utiliser leur endpoint expérimental ou un autre service gratuit si dispo.
|
| 41 |
+
# Pour l'instant, on va utiliser l'API Pixabay comme fallback fiable,
|
| 42 |
+
# ou simuler une vidéo via Pollinations si possible (souvent des GIFs).
|
| 43 |
+
|
| 44 |
+
# Tentative avec l'endpoint vidéo expérimental de Pollinations (souvent instable)
|
| 45 |
+
# url = f"https://pollinations.ai/p/{cleanPrompt}?model=turbo&animate=true"
|
| 46 |
+
|
| 47 |
+
# Pour garantir un résultat, on va utiliser Pixabay via l'API (comme dans l'app principale)
|
| 48 |
+
# Mais ici on est en Python, donc on peut le faire proprement.
|
| 49 |
+
|
| 50 |
+
API_KEY = "53929922-0888380397f008597974652ae" # Clé Pixabay publique
|
| 51 |
+
url = f"https://pixabay.com/api/videos/?key={API_KEY}&q={requests.utils.quote(prompt)}&pretty=true"
|
| 52 |
+
|
| 53 |
+
print(f"🎥 Recherche Vidéo: {prompt}")
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
response = requests.get(url)
|
| 57 |
+
data = response.json()
|
| 58 |
+
if data['hits']:
|
| 59 |
+
video_url = data['hits'][0]['videos']['medium']['url']
|
| 60 |
+
# Télécharger la vidéo
|
| 61 |
+
video_path = "output_video.mp4"
|
| 62 |
+
v_resp = requests.get(video_url)
|
| 63 |
+
with open(video_path, "wb") as f:
|
| 64 |
+
f.write(v_resp.content)
|
| 65 |
+
return video_path
|
| 66 |
+
else:
|
| 67 |
+
return "Aucune vidéo trouvée."
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return f"Erreur: {e}"
|
| 70 |
|
| 71 |
# --- INTERFACE GRADIO ---
|
| 72 |
+
with gr.Blocks(title="CygnisAI API (Pollinations Proxy)", theme=gr.themes.Soft()) as demo:
|
| 73 |
+
gr.Markdown("# 🌌 CygnisAI API (Powered by Pollinations & Pixabay)")
|
| 74 |
+
gr.Markdown("Génération ultra-rapide sans GPU requis.")
|
| 75 |
|
| 76 |
with gr.Tab("Image Generation"):
|
| 77 |
with gr.Row():
|
| 78 |
+
img_input = gr.Textbox(label="Prompt", placeholder="Un chat cybernétique...")
|
| 79 |
+
img_btn = gr.Button("Générer Image", variant="primary")
|
| 80 |
img_output = gr.Image(label="Résultat")
|
| 81 |
img_btn.click(generate_image, inputs=img_input, outputs=img_output)
|
| 82 |
|
| 83 |
with gr.Tab("Video Generation"):
|
| 84 |
+
with gr.Row():
|
| 85 |
+
vid_input = gr.Textbox(label="Prompt", placeholder="Nature, City, Space...")
|
| 86 |
+
vid_btn = gr.Button("Rechercher Vidéo", variant="primary")
|
| 87 |
+
vid_output = gr.Video(label="Résultat")
|
| 88 |
+
vid_btn.click(generate_video, inputs=vid_input, outputs=vid_output)
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
demo.launch()
|