Spaces:
Build error
Build error
modified: app.py
Browse filesnew file: "requirements.txt\303\247"
- app.py +109 -4
- requirements.txtç +13 -0
app.py
CHANGED
|
@@ -1,7 +1,112 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import logging
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
+
# Cargar variables de entorno
|
| 8 |
+
load_dotenv()
|
| 9 |
|
| 10 |
+
# Configuración inicial
|
| 11 |
+
logging.basicConfig(filename='art_transformer.log', level=logging.INFO)
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
# Importaciones internas
|
| 15 |
+
from image_processor.analyzer import ImageAnalyzer
|
| 16 |
+
from image_processor.framer import FrameGenerator
|
| 17 |
+
from image_processor.priority_queue import PriorityQueue
|
| 18 |
+
from image_processor.google_auth import GooglePhotosAuth
|
| 19 |
+
from image_processor.monitoring import ResourceMonitor
|
| 20 |
+
|
| 21 |
+
class ArtTransformer:
|
| 22 |
+
def __init__(self):
|
| 23 |
+
self.openai_key = os.getenv("OPENAI_API_KEY")
|
| 24 |
+
self.analyzer = ImageAnalyzer()
|
| 25 |
+
self.framer = FrameGenerator(self.openai_key)
|
| 26 |
+
self.auth = GooglePhotosAuth()
|
| 27 |
+
self.queue = PriorityQueue()
|
| 28 |
+
self.monitor = ResourceMonitor(interval=15)
|
| 29 |
+
self.current_job = None
|
| 30 |
+
|
| 31 |
+
def process_album(self, image_paths, priority=2, progress=gr.Progress()):
|
| 32 |
+
try:
|
| 33 |
+
self.monitor.start()
|
| 34 |
+
|
| 35 |
+
# Autenticación con Google Photos
|
| 36 |
+
credentials = self.auth.authenticate()
|
| 37 |
+
|
| 38 |
+
# Configurar cola de prioridad
|
| 39 |
+
for idx, path in enumerate(image_paths):
|
| 40 |
+
self.queue.add_task(priority + idx/1000, path)
|
| 41 |
+
|
| 42 |
+
results = []
|
| 43 |
+
total = len(image_paths)
|
| 44 |
+
|
| 45 |
+
while not self.queue.is_empty():
|
| 46 |
+
if self.current_job and self.current_job.is_alive():
|
| 47 |
+
yield gr.Gallery(visible=True), f"⏳ Procesando {self.current_job.name}..."
|
| 48 |
+
|
| 49 |
+
next_path = self.queue.get_next_task()
|
| 50 |
+
self.current_job = threading.Thread(
|
| 51 |
+
target=self._process_single_image,
|
| 52 |
+
args=(next_path, results, progress),
|
| 53 |
+
name=f"Job-{datetime.now().strftime('%H%M%S')}"
|
| 54 |
+
)
|
| 55 |
+
self.current_job.start()
|
| 56 |
+
self.current_job.join()
|
| 57 |
+
|
| 58 |
+
return self._build_output(results), "✅ Proceso completado!"
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
logger.error(f"Error general: {str(e)}")
|
| 62 |
+
return None, f"❌ Error crítico: {str(e)}"
|
| 63 |
+
|
| 64 |
+
finally:
|
| 65 |
+
self.monitor.stop()
|
| 66 |
+
|
| 67 |
+
def create_interface():
|
| 68 |
+
with gr.Blocks(title="Art Transformer", theme=gr.themes.Soft()) as demo:
|
| 69 |
+
gr.Markdown("# 🎨 Transformador Artístico Automático")
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
with gr.Column(scale=1):
|
| 73 |
+
api_key = gr.Textbox(
|
| 74 |
+
label="OpenAI API Key",
|
| 75 |
+
type="password",
|
| 76 |
+
placeholder="sk-...",
|
| 77 |
+
info="Necesario para generar marcos"
|
| 78 |
+
)
|
| 79 |
+
priority = gr.Dropdown(
|
| 80 |
+
label="Prioridad",
|
| 81 |
+
choices=[("Alta", 0), ("Media", 1), ("Baja", 2)],
|
| 82 |
+
value=1
|
| 83 |
+
)
|
| 84 |
+
btn_run = gr.Button("Iniciar Procesamiento", variant="primary")
|
| 85 |
+
|
| 86 |
+
with gr.Column(scale=3):
|
| 87 |
+
gallery = gr.Gallery(
|
| 88 |
+
label="Resultados",
|
| 89 |
+
columns=4,
|
| 90 |
+
height="auto",
|
| 91 |
+
object_fit="contain"
|
| 92 |
+
)
|
| 93 |
+
status = gr.Textbox(label="Estado", interactive=False)
|
| 94 |
+
|
| 95 |
+
inputs = gr.File(
|
| 96 |
+
file_count="multiple",
|
| 97 |
+
file_types=["image"],
|
| 98 |
+
label="Subir imágenes"
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
btn_run.click(
|
| 102 |
+
fn=ArtTransformer().process_album,
|
| 103 |
+
inputs=[inputs, priority],
|
| 104 |
+
outputs=[gallery, status]
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
return demo
|
| 108 |
+
|
| 109 |
+
if __name__ == "__main__":
|
| 110 |
+
demo = create_interface()
|
| 111 |
+
demo.queue(concurrency_count=2, max_size=10)
|
| 112 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txtç
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
--extra-index-url https://download.pytorch.org/whl/cu121
|
| 2 |
+
torch==2.1.2+cu121
|
| 3 |
+
transformers==4.36.2
|
| 4 |
+
gradio==4.13.0
|
| 5 |
+
openai==1.12.0
|
| 6 |
+
pillow==10.1.0
|
| 7 |
+
python-dotenv==1.0.0
|
| 8 |
+
google-auth-oauthlib==1.2.0
|
| 9 |
+
google-auth-httplib2==0.2.0
|
| 10 |
+
gpustat==1.1
|
| 11 |
+
pyyaml==6.0.1
|
| 12 |
+
tqdm==4.66.1
|
| 13 |
+
psutil==5.9.7
|