Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from fastapi import FastAPI, HTTPException
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from huggingface_hub import hf_hub_download
|
|
@@ -24,19 +25,22 @@ def get_model():
|
|
| 24 |
return _model
|
| 25 |
|
| 26 |
print("📥 Downloading model...")
|
| 27 |
-
|
| 28 |
repo_id=REPO_ID,
|
| 29 |
filename=FILENAME,
|
| 30 |
cache_dir=CACHE_DIR,
|
| 31 |
local_dir_use_symlinks=False,
|
| 32 |
)
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
print("🔄 Loading model into memory...")
|
| 36 |
_model = AutoModelForCausalLM.from_pretrained(
|
| 37 |
-
|
|
|
|
| 38 |
model_type=MODEL_TYPE,
|
| 39 |
-
gpu_layers=0 #
|
|
|
|
| 40 |
)
|
| 41 |
print("✅ Model loaded")
|
| 42 |
return _model
|
|
@@ -57,6 +61,17 @@ def generate_text(req: PromptRequest):
|
|
| 57 |
max_new_tokens=req.max_new_tokens,
|
| 58 |
temperature=req.temperature
|
| 59 |
)
|
| 60 |
-
return {"response": output}
|
| 61 |
except Exception as e:
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import traceback
|
| 3 |
from fastapi import FastAPI, HTTPException
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 25 |
return _model
|
| 26 |
|
| 27 |
print("📥 Downloading model...")
|
| 28 |
+
local_file = hf_hub_download(
|
| 29 |
repo_id=REPO_ID,
|
| 30 |
filename=FILENAME,
|
| 31 |
cache_dir=CACHE_DIR,
|
| 32 |
local_dir_use_symlinks=False,
|
| 33 |
)
|
| 34 |
+
local_dir = os.path.dirname(local_file)
|
| 35 |
+
print("✅ Model downloaded at", local_file)
|
| 36 |
|
| 37 |
print("🔄 Loading model into memory...")
|
| 38 |
_model = AutoModelForCausalLM.from_pretrained(
|
| 39 |
+
local_dir,
|
| 40 |
+
model_file=FILENAME,
|
| 41 |
model_type=MODEL_TYPE,
|
| 42 |
+
gpu_layers=0, # CPU only
|
| 43 |
+
threads=os.cpu_count() or 2
|
| 44 |
)
|
| 45 |
print("✅ Model loaded")
|
| 46 |
return _model
|
|
|
|
| 61 |
max_new_tokens=req.max_new_tokens,
|
| 62 |
temperature=req.temperature
|
| 63 |
)
|
| 64 |
+
return {"ok": True, "response": output}
|
| 65 |
except Exception as e:
|
| 66 |
+
tb = traceback.format_exc()
|
| 67 |
+
print("❌ ERROR in /generate:\n", tb)
|
| 68 |
+
raise HTTPException(status_code=500, detail={"error": str(e), "trace": tb})
|
| 69 |
+
|
| 70 |
+
# --- Health Check ---
|
| 71 |
+
@app.get("/health")
|
| 72 |
+
def health():
|
| 73 |
+
try:
|
| 74 |
+
_ = get_model()
|
| 75 |
+
return {"ok": True}
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return {"ok": False, "error": str(e)}
|