Update app.py
Browse files
app.py
CHANGED
|
@@ -24,6 +24,9 @@ N_GPU_LAYERS = int(os.environ.get("N_GPU_LAYERS", "0"))
|
|
| 24 |
MAX_INPUT_LENGTH = 1500
|
| 25 |
END_TOKEN = "<|endoftext|>"
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
# ==================== LIFECYCLE MANAGEMENT ====================
|
| 28 |
@asynccontextmanager
|
| 29 |
async def lifespan(app: FastAPI):
|
|
@@ -57,9 +60,7 @@ app = FastAPI(
|
|
| 57 |
app.add_middleware(
|
| 58 |
CORSMiddleware,
|
| 59 |
allow_origins=[
|
| 60 |
-
"
|
| 61 |
-
"http://localhost:3000", # For local development
|
| 62 |
-
"http://localhost:5173", # Vite dev server
|
| 63 |
],
|
| 64 |
allow_credentials=True,
|
| 65 |
allow_methods=["*"],
|
|
@@ -74,9 +75,6 @@ class TransformRequest(BaseModel):
|
|
| 74 |
class HumanizeRequest(BaseModel):
|
| 75 |
text: str = Field(..., min_length=1, max_length=MAX_INPUT_LENGTH)
|
| 76 |
|
| 77 |
-
# ==================== GLOBAL MODEL ====================
|
| 78 |
-
llm = None
|
| 79 |
-
|
| 80 |
# ==================== STYLE PROMPTS ====================
|
| 81 |
STYLE_PROMPTS = {
|
| 82 |
"professional": """You are a professional writing assistant. Rewrite the text below in formal, corporate business language.
|
|
@@ -142,6 +140,8 @@ STYLE_TEMPERATURES = {
|
|
| 142 |
# ==================== HELPER FUNCTIONS ====================
|
| 143 |
def load_model():
|
| 144 |
"""Load the GGUF model"""
|
|
|
|
|
|
|
| 145 |
print(f"🔄 Loading model from: {MODEL_PATH}")
|
| 146 |
|
| 147 |
try:
|
|
@@ -150,14 +150,20 @@ def load_model():
|
|
| 150 |
print("📥 Downloading model from Hugging Face Hub...")
|
| 151 |
try:
|
| 152 |
from huggingface_hub import hf_hub_download
|
| 153 |
-
|
|
|
|
| 154 |
repo_id=MODEL_REPO,
|
| 155 |
filename=MODEL_FILE,
|
| 156 |
local_dir=".",
|
| 157 |
token=os.environ.get("HF_TOKEN", None)
|
| 158 |
)
|
|
|
|
| 159 |
except ImportError:
|
| 160 |
print("⚠️ huggingface-hub not installed, using local model path")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
model = Llama(
|
| 163 |
model_path=MODEL_PATH,
|
|
@@ -174,6 +180,8 @@ def load_model():
|
|
| 174 |
|
| 175 |
except Exception as e:
|
| 176 |
print(f"❌ Failed to load model: {e}")
|
|
|
|
|
|
|
| 177 |
return None
|
| 178 |
|
| 179 |
def clean_output(text: str) -> str:
|
|
@@ -217,9 +225,10 @@ async def transform_with_model(text: str, style: str) -> str:
|
|
| 217 |
global llm
|
| 218 |
|
| 219 |
if llm is None:
|
|
|
|
| 220 |
llm = load_model()
|
| 221 |
if llm is None:
|
| 222 |
-
raise HTTPException(status_code=503, detail="Model not available")
|
| 223 |
|
| 224 |
try:
|
| 225 |
# Build prompt
|
|
@@ -259,6 +268,8 @@ async def transform_with_model(text: str, style: str) -> str:
|
|
| 259 |
|
| 260 |
except Exception as e:
|
| 261 |
print(f"❌ Model error: {e}")
|
|
|
|
|
|
|
| 262 |
raise HTTPException(status_code=500, detail=f"Model error: {str(e)}")
|
| 263 |
|
| 264 |
# ==================== API ENDPOINTS ====================
|
|
@@ -277,10 +288,11 @@ async def root():
|
|
| 277 |
async def health_check():
|
| 278 |
"""Detailed health check"""
|
| 279 |
return {
|
| 280 |
-
"status": "healthy" if llm else "
|
| 281 |
"model_loaded": llm is not None,
|
| 282 |
"threads": N_THREADS,
|
| 283 |
-
"context_size": N_CTX
|
|
|
|
| 284 |
}
|
| 285 |
|
| 286 |
@app.post("/api/humanize")
|
|
@@ -345,6 +357,8 @@ async def transform_text(request: TransformRequest):
|
|
| 345 |
raise
|
| 346 |
except Exception as e:
|
| 347 |
print(f"❌ Transformation error: {e}")
|
|
|
|
|
|
|
| 348 |
raise HTTPException(status_code=500, detail=f"Transformation failed: {str(e)}")
|
| 349 |
|
| 350 |
@app.get("/api/styles")
|
|
@@ -367,23 +381,11 @@ async def get_styles():
|
|
| 367 |
"default_style": "casual"
|
| 368 |
}
|
| 369 |
|
| 370 |
-
# Error handler
|
| 371 |
-
@app.exception_handler(Exception)
|
| 372 |
-
async def general_exception_handler(request, exc):
|
| 373 |
-
return JSONResponse(
|
| 374 |
-
status_code=500,
|
| 375 |
-
content={
|
| 376 |
-
"error": "Internal server error",
|
| 377 |
-
"message": str(exc),
|
| 378 |
-
"path": request.url.path
|
| 379 |
-
}
|
| 380 |
-
)
|
| 381 |
-
|
| 382 |
# ==================== MAIN ====================
|
| 383 |
if __name__ == "__main__":
|
| 384 |
import uvicorn
|
| 385 |
|
| 386 |
-
port = int(os.environ.get("PORT",
|
| 387 |
|
| 388 |
uvicorn.run(
|
| 389 |
"app:app",
|
|
|
|
| 24 |
MAX_INPUT_LENGTH = 1500
|
| 25 |
END_TOKEN = "<|endoftext|>"
|
| 26 |
|
| 27 |
+
# ==================== GLOBAL MODEL ====================
|
| 28 |
+
llm = None
|
| 29 |
+
|
| 30 |
# ==================== LIFECYCLE MANAGEMENT ====================
|
| 31 |
@asynccontextmanager
|
| 32 |
async def lifespan(app: FastAPI):
|
|
|
|
| 60 |
app.add_middleware(
|
| 61 |
CORSMiddleware,
|
| 62 |
allow_origins=[
|
| 63 |
+
"*", # Allow all origins temporarily, update with your Vercel URL
|
|
|
|
|
|
|
| 64 |
],
|
| 65 |
allow_credentials=True,
|
| 66 |
allow_methods=["*"],
|
|
|
|
| 75 |
class HumanizeRequest(BaseModel):
|
| 76 |
text: str = Field(..., min_length=1, max_length=MAX_INPUT_LENGTH)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
| 78 |
# ==================== STYLE PROMPTS ====================
|
| 79 |
STYLE_PROMPTS = {
|
| 80 |
"professional": """You are a professional writing assistant. Rewrite the text below in formal, corporate business language.
|
|
|
|
| 140 |
# ==================== HELPER FUNCTIONS ====================
|
| 141 |
def load_model():
|
| 142 |
"""Load the GGUF model"""
|
| 143 |
+
global MODEL_PATH # Access the global MODEL_PATH
|
| 144 |
+
|
| 145 |
print(f"🔄 Loading model from: {MODEL_PATH}")
|
| 146 |
|
| 147 |
try:
|
|
|
|
| 150 |
print("📥 Downloading model from Hugging Face Hub...")
|
| 151 |
try:
|
| 152 |
from huggingface_hub import hf_hub_download
|
| 153 |
+
# Use a different variable name to avoid conflict
|
| 154 |
+
downloaded_path = hf_hub_download(
|
| 155 |
repo_id=MODEL_REPO,
|
| 156 |
filename=MODEL_FILE,
|
| 157 |
local_dir=".",
|
| 158 |
token=os.environ.get("HF_TOKEN", None)
|
| 159 |
)
|
| 160 |
+
print(f"✅ Model downloaded to: {downloaded_path}")
|
| 161 |
except ImportError:
|
| 162 |
print("⚠️ huggingface-hub not installed, using local model path")
|
| 163 |
+
# If we can't download, use fallback path
|
| 164 |
+
MODEL_PATH = os.path.join("/code", MODEL_FILE)
|
| 165 |
+
|
| 166 |
+
print(f"📁 Model path: {MODEL_PATH}")
|
| 167 |
|
| 168 |
model = Llama(
|
| 169 |
model_path=MODEL_PATH,
|
|
|
|
| 180 |
|
| 181 |
except Exception as e:
|
| 182 |
print(f"❌ Failed to load model: {e}")
|
| 183 |
+
import traceback
|
| 184 |
+
traceback.print_exc()
|
| 185 |
return None
|
| 186 |
|
| 187 |
def clean_output(text: str) -> str:
|
|
|
|
| 225 |
global llm
|
| 226 |
|
| 227 |
if llm is None:
|
| 228 |
+
# Try to load model if not loaded
|
| 229 |
llm = load_model()
|
| 230 |
if llm is None:
|
| 231 |
+
raise HTTPException(status_code=503, detail="Model not available. Please check if model file exists.")
|
| 232 |
|
| 233 |
try:
|
| 234 |
# Build prompt
|
|
|
|
| 268 |
|
| 269 |
except Exception as e:
|
| 270 |
print(f"❌ Model error: {e}")
|
| 271 |
+
import traceback
|
| 272 |
+
traceback.print_exc()
|
| 273 |
raise HTTPException(status_code=500, detail=f"Model error: {str(e)}")
|
| 274 |
|
| 275 |
# ==================== API ENDPOINTS ====================
|
|
|
|
| 288 |
async def health_check():
|
| 289 |
"""Detailed health check"""
|
| 290 |
return {
|
| 291 |
+
"status": "healthy" if llm else "model_loading_failed",
|
| 292 |
"model_loaded": llm is not None,
|
| 293 |
"threads": N_THREADS,
|
| 294 |
+
"context_size": N_CTX,
|
| 295 |
+
"model_path": MODEL_PATH
|
| 296 |
}
|
| 297 |
|
| 298 |
@app.post("/api/humanize")
|
|
|
|
| 357 |
raise
|
| 358 |
except Exception as e:
|
| 359 |
print(f"❌ Transformation error: {e}")
|
| 360 |
+
import traceback
|
| 361 |
+
traceback.print_exc()
|
| 362 |
raise HTTPException(status_code=500, detail=f"Transformation failed: {str(e)}")
|
| 363 |
|
| 364 |
@app.get("/api/styles")
|
|
|
|
| 381 |
"default_style": "casual"
|
| 382 |
}
|
| 383 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
# ==================== MAIN ====================
|
| 385 |
if __name__ == "__main__":
|
| 386 |
import uvicorn
|
| 387 |
|
| 388 |
+
port = int(os.environ.get("PORT", 7860))
|
| 389 |
|
| 390 |
uvicorn.run(
|
| 391 |
"app:app",
|