Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,102 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastapi import FastAPI, BackgroundTasks, HTTPException
|
| 3 |
-
from pydantic import BaseModel
|
| 4 |
-
from typing import Optional
|
| 5 |
import uvicorn
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
try:
|
| 9 |
from generation import generate_image_from_prompt
|
| 10 |
-
except
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
timestamp: Optional[str] = None
|
| 23 |
-
|
| 24 |
-
class ImageGenerationRequest(BaseModel):
|
| 25 |
-
prompt_type: Optional[str] = None
|
| 26 |
-
custom_prompt: Optional[str] = None
|
| 27 |
-
model: str = "black-forest-labs/FLUX.1-dev"
|
| 28 |
-
|
| 29 |
-
# --- DEFINICI脫N DE PROMPTS DE SOF脥A (TU ESTRATEGIA) ---
|
| 30 |
-
PROMPT_MAP = {
|
| 31 |
-
"lifestyle": {
|
| 32 |
-
"prompt": "foto selfie profesional con iPhone de Sofia Rivera, hermosa mujer latina cubanoamericana de 25 a帽os, cabello largo oscuro y ondulado, sonrisa c谩lida, departamento de Miami con vista al mar, golden hour, est茅tica influencer Instagram",
|
| 33 |
-
"negative": "borroso, de baja calidad, distorsionado, deformado, feo, mala anatom铆a",
|
| 34 |
-
},
|
| 35 |
-
"fitness": {
|
| 36 |
-
"prompt": "selfie de cuerpo completo en el espejo de Sofia Rivera, influencer latina fitness, cuerpo atl茅tico tonificado, sujetador deportivo negro, mallas de cintura alta, gimnasio moderno con espejos, iluminaci贸n natural, est茅tica fitness Instagram",
|
| 37 |
-
"negative": "borroso, de baja calidad, distorsionado, malas proporciones",
|
| 38 |
-
},
|
| 39 |
-
"premium_boudoir": {
|
| 40 |
-
"prompt": "selfie en el dormitorio de Sofia Rivera, influencer latina de 25 a帽os, lencer铆a de encaje blanco, luz suave de la ma帽ana a trav茅s de cortinas transparentes, cama lujosa con s谩banas de seda, expresi贸n sensual y segura, estilo boudoir de buen gusto, fotograf铆a profesional",
|
| 41 |
-
"negative": "expl铆cito, borroso, de baja calidad, distorsionado",
|
| 42 |
-
},
|
| 43 |
-
"fashion": {
|
| 44 |
-
"prompt": "foto de estilo urbano de Sofia Rivera, influencer de moda latina, outfit moderno de Miami, gafas de sol de dise帽ador, pose natural y segura, fondo urbano, golden hour, est茅tica moda Instagram",
|
| 45 |
-
"negative": "borroso, de baja calidad, mala iluminaci贸n",
|
| 46 |
-
},
|
| 47 |
-
"beach": {
|
| 48 |
-
"prompt": "foto de estilo de vida de Sofia Rivera en la playa, influencer latina, Miami Beach al atardecer, atuendo casual de playa, expresi贸n natural feliz, vibraciones tropicales, contenido lifestyle Instagram",
|
| 49 |
-
"negative": "borroso, de baja calidad, distorsionado",
|
| 50 |
-
},
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
# --- ENDPOINTS DE API (PARA AUTOMATIZACI脫N FUTURA) ---
|
| 54 |
-
@app.get("/health")
|
| 55 |
-
async def health():
|
| 56 |
-
return {"status": "ok", "service": "sofia-ai-backend"}
|
| 57 |
-
|
| 58 |
-
@app.post("/webhook/message")
|
| 59 |
-
async def webhook_message(body: MessageRequest, background_tasks: BackgroundTasks):
|
| 60 |
-
background_tasks.add_task(lambda: print(f"[Message] {body.platform}: {body.message}"))
|
| 61 |
-
return {"status": "queued"}
|
| 62 |
-
|
| 63 |
-
# --- INTERFAZ VISUAL (PARA QUE GENERES T脷 MANUALMENTE) ---
|
| 64 |
-
def ui_generate(style_selection, custom_text):
|
| 65 |
-
# Seleccionar prompt
|
| 66 |
-
if style_selection == "Personalizado":
|
| 67 |
-
final_prompt = custom_text
|
| 68 |
-
negative_prompt = "borroso, mala calidad"
|
| 69 |
-
else:
|
| 70 |
-
data = PROMPT_MAP.get(style_selection)
|
| 71 |
-
final_prompt = data["prompt"]
|
| 72 |
-
negative_prompt = data["negative"]
|
| 73 |
-
|
| 74 |
-
# Llamar a la generaci贸n
|
| 75 |
-
image_path, status = generate_image_from_prompt(
|
| 76 |
-
prompt=final_prompt,
|
| 77 |
-
negative_prompt=negative_prompt,
|
| 78 |
model_name="black-forest-labs/FLUX.1-dev",
|
| 79 |
seed=None
|
| 80 |
)
|
| 81 |
-
|
| 82 |
-
return image_path, final_prompt
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
with gr.Blocks(
|
| 86 |
-
gr.Markdown("# 馃摳
|
|
|
|
| 87 |
with gr.Row():
|
| 88 |
with gr.Column():
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
with gr.Column():
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
# MONTAR GRADIO SOBRE FASTAPI
|
| 99 |
-
# Esto hace que la ruta principal "/" muestre la interfaz visual
|
| 100 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
import uvicorn
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
|
| 5 |
+
# Intentar importar la funci贸n de generaci贸n
|
| 6 |
try:
|
| 7 |
from generation import generate_image_from_prompt
|
| 8 |
+
except Exception as e:
|
| 9 |
+
def generate_image_from_prompt(*args, **kwargs):
|
| 10 |
+
return None, f"Error de importaci贸n: {str(e)}"
|
| 11 |
+
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
def predict(style, custom_p):
|
| 15 |
+
# Si el motor generation.py funciona, esto crear谩 la imagen
|
| 16 |
+
# Usamos los estilos que ya definimos antes
|
| 17 |
+
img, status = generate_image_from_prompt(
|
| 18 |
+
prompt=custom_p if style == "Personalizado" else style,
|
| 19 |
+
negative_prompt="low quality, blurry",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
model_name="black-forest-labs/FLUX.1-dev",
|
| 21 |
seed=None
|
| 22 |
)
|
| 23 |
+
return img, status
|
|
|
|
| 24 |
|
| 25 |
+
# INTERFAZ SENCILLA Y DIRECTA
|
| 26 |
+
with gr.Blocks() as demo:
|
| 27 |
+
gr.Markdown("# 馃摳 SOFIA AI STUDIO")
|
| 28 |
+
|
| 29 |
with gr.Row():
|
| 30 |
with gr.Column():
|
| 31 |
+
style_input = gr.Dropdown(
|
| 32 |
+
label="Selecciona Estilo",
|
| 33 |
+
choices=["lifestyle", "fitness", "fashion", "beach", "Personalizado"],
|
| 34 |
+
value="lifestyle"
|
| 35 |
+
)
|
| 36 |
+
prompt_input = gr.Textbox(label="Prompt (Solo si usas Personalizado)", placeholder="Describe la escena...")
|
| 37 |
+
generate_btn = gr.Button("馃殌 GENERAR IMAGEN SOF脥A", variant="primary")
|
| 38 |
+
|
| 39 |
with gr.Column():
|
| 40 |
+
output_image = gr.Image(label="Resultado")
|
| 41 |
+
output_text = gr.Textbox(label="Estado del Servidor")
|
| 42 |
+
|
| 43 |
+
generate_btn.click(
|
| 44 |
+
fn=predict,
|
| 45 |
+
inputs=[style_input, prompt_input],
|
| 46 |
+
outputs=[output_image, output_text]
|
| 47 |
+
)
|
| 48 |
|
|
|
|
|
|
|
| 49 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|