Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,30 @@
|
|
1 |
-
import os
|
2 |
-
|
3 |
-
# β
Set Hugging Face cache to a writable path
|
4 |
-
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface_cache"
|
5 |
-
|
6 |
from fastapi import FastAPI, Request
|
7 |
from fastapi.responses import JSONResponse, FileResponse
|
8 |
-
from fastapi.middleware.cors import CORSMiddleware
|
9 |
from transformers import pipeline
|
|
|
|
|
10 |
|
11 |
-
#
|
|
|
|
|
|
|
12 |
hf_token = os.getenv("HF_TOKEN")
|
13 |
-
|
|
|
14 |
|
15 |
-
# β
Load the
|
16 |
-
|
17 |
-
|
18 |
-
model=model_id,
|
19 |
-
device=0,
|
20 |
-
use_auth_token=hf_token
|
21 |
-
)
|
22 |
|
23 |
-
#
|
24 |
app = FastAPI()
|
25 |
|
26 |
-
# β
CORS (allow frontend to access API)
|
27 |
-
app.add_middleware(
|
28 |
-
CORSMiddleware,
|
29 |
-
allow_origins=["*"],
|
30 |
-
allow_methods=["*"],
|
31 |
-
allow_headers=["*"],
|
32 |
-
)
|
33 |
-
|
34 |
@app.get("/")
|
35 |
async def serve_index():
|
36 |
return FileResponse("index.html")
|
37 |
|
38 |
@app.get("/script.js")
|
39 |
-
async def
|
40 |
return FileResponse("script.js")
|
41 |
|
42 |
@app.post("/api")
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI, Request
|
2 |
from fastapi.responses import JSONResponse, FileResponse
|
|
|
3 |
from transformers import pipeline
|
4 |
+
from huggingface_hub import login
|
5 |
+
import os
|
6 |
|
7 |
+
# π οΈ Fix write permission issue
|
8 |
+
os.environ["HF_HOME"] = "/data" # β
Hugging Face model cache path
|
9 |
+
|
10 |
+
# π Login with token from Hugging Face Secrets (don't hardcode!)
|
11 |
hf_token = os.getenv("HF_TOKEN")
|
12 |
+
if hf_token:
|
13 |
+
login(token=hf_token)
|
14 |
|
15 |
+
# β
Load the model pipeline (CPU-friendly)
|
16 |
+
model_id = os.getenv("MODEL_ID", "mistralai/Mistral-7B-Instruct-v0.2")
|
17 |
+
pipe = pipeline("text-generation", model=model_id, trust_remote_code=True)
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# π FastAPI app
|
20 |
app = FastAPI()
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
@app.get("/")
|
23 |
async def serve_index():
|
24 |
return FileResponse("index.html")
|
25 |
|
26 |
@app.get("/script.js")
|
27 |
+
async def serve_script():
|
28 |
return FileResponse("script.js")
|
29 |
|
30 |
@app.post("/api")
|