bep40 commited on
Commit
32f53d0
·
verified ·
1 Parent(s): b5e487f

Fix API routing: static files AFTER API routes + CORS

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -1,23 +1,26 @@
1
  import os
2
- import base64
3
- import io
4
- import json
5
- from typing import Optional
6
  from fastapi import FastAPI, HTTPException, Request, Response
7
  from fastapi.staticfiles import StaticFiles
8
  from fastapi.middleware.cors import CORSMiddleware
9
  from pydantic import BaseModel
10
- import httpx
11
- import asyncio
12
 
13
  HF_TOKEN = os.environ.get("HF_TOKEN", "")
14
  if not HF_TOKEN:
15
  print("WARNING: HF_TOKEN not set! AI features will fail.")
16
 
 
17
  app = FastAPI(title="Comic AI Generator", version="2.0")
18
 
19
- # Mount static files (the frontend)
20
- app.mount("/", StaticFiles(directory="static", html=True), name="static")
 
 
 
 
 
 
21
 
22
  # ================= MODELS =================
23
  class TextGenRequest(BaseModel):
@@ -97,7 +100,7 @@ async def hf_binary_request(url: str, payload: dict):
97
  raise HTTPException(status_code=500, detail=str(e))
98
  await asyncio.sleep(5)
99
 
100
- # ================= ENDPOINTS =================
101
  @app.post("/api/text")
102
  async def generate_text(req: TextGenRequest):
103
  url = f"https://api-inference.huggingface.co/models/{req.model}"
@@ -210,3 +213,6 @@ async def list_models():
210
  "tts_model": "microsoft/speecht5_tts",
211
  "token_configured": bool(HF_TOKEN)
212
  }
 
 
 
 
1
  import os
2
+ import asyncio
3
+ import httpx
 
 
4
  from fastapi import FastAPI, HTTPException, Request, Response
5
  from fastapi.staticfiles import StaticFiles
6
  from fastapi.middleware.cors import CORSMiddleware
7
  from pydantic import BaseModel
 
 
8
 
9
  HF_TOKEN = os.environ.get("HF_TOKEN", "")
10
  if not HF_TOKEN:
11
  print("WARNING: HF_TOKEN not set! AI features will fail.")
12
 
13
+ # Create FastAPI app
14
  app = FastAPI(title="Comic AI Generator", version="2.0")
15
 
16
+ # Add CORS middleware
17
+ app.add_middleware(
18
+ CORSMiddleware,
19
+ allow_origins=["*"],
20
+ allow_credentials=True,
21
+ allow_methods=["*"],
22
+ allow_headers=["*"],
23
+ )
24
 
25
  # ================= MODELS =================
26
  class TextGenRequest(BaseModel):
 
100
  raise HTTPException(status_code=500, detail=str(e))
101
  await asyncio.sleep(5)
102
 
103
+ # ================= API ROUTES (MUST BE BEFORE STATIC MOUNT) =================
104
  @app.post("/api/text")
105
  async def generate_text(req: TextGenRequest):
106
  url = f"https://api-inference.huggingface.co/models/{req.model}"
 
213
  "tts_model": "microsoft/speecht5_tts",
214
  "token_configured": bool(HF_TOKEN)
215
  }
216
+
217
+ # ================= STATIC FILES (MUST BE LAST - catch-all) =================
218
+ app.mount("/", StaticFiles(directory="static", html=True), name="static")