|
|
|
""" |
|
Main entry point for Hugging Face Spaces deployment |
|
""" |
|
|
|
import os |
|
import logging |
|
import sys |
|
import traceback |
|
|
|
|
|
logging.basicConfig( |
|
level=logging.DEBUG, |
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
|
handlers=[logging.StreamHandler(sys.stdout)] |
|
) |
|
|
|
logger = logging.getLogger("faceforge") |
|
|
|
def create_app(): |
|
"""Creates and configures the integrated FastAPI application with both API and UI components.""" |
|
try: |
|
|
|
logger.info("Applying Gradio patch...") |
|
try: |
|
from patch_gradio_utils import apply_patch |
|
if apply_patch(): |
|
logger.info("Gradio patch applied successfully.") |
|
else: |
|
logger.warning("Failed to apply Gradio patch. The app may encounter errors.") |
|
except Exception as e: |
|
logger.warning(f"Error applying Gradio patch: {e}") |
|
logger.debug(traceback.format_exc()) |
|
|
|
|
|
logger.info("Setting up FastAPI application with API and UI for Hugging Face Spaces") |
|
from fastapi import FastAPI |
|
from fastapi.middleware.cors import CORSMiddleware |
|
import gradio as gr |
|
|
|
|
|
from faceforge_api.main import app as api_app |
|
from faceforge_ui.app import create_demo |
|
|
|
|
|
app = FastAPI(title="FaceForge") |
|
|
|
|
|
app.add_middleware( |
|
CORSMiddleware, |
|
allow_origins=["*"], |
|
allow_credentials=True, |
|
allow_methods=["*"], |
|
allow_headers=["*"], |
|
) |
|
|
|
|
|
logger.info("Mounting API at /api") |
|
app.mount("/api", api_app) |
|
|
|
|
|
|
|
if "BASE_URL" not in os.environ: |
|
os.environ["BASE_URL"] = "" |
|
logger.info("Setting BASE_URL to empty string for integrated app") |
|
|
|
|
|
logger.info("Creating Gradio UI") |
|
demo = create_demo() |
|
|
|
|
|
logger.info("Mounting Gradio UI") |
|
gr_app = gr.mount_gradio_app(app, demo, path="/") |
|
|
|
return app |
|
except Exception as e: |
|
logger.critical(f"Failed to create app: {e}") |
|
logger.debug(traceback.format_exc()) |
|
raise |
|
|
|
|
|
|
|
app = create_app() |
|
|
|
if __name__ == "__main__": |
|
|
|
import uvicorn |
|
port = int(os.environ.get("PORT", 7860)) |
|
logger.info(f"Starting integrated server on port {port}") |
|
uvicorn.run(app, host="0.0.0.0", port=port) |