Coots commited on
Commit
e7328db
Β·
verified Β·
1 Parent(s): 15ad6c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -25
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
- # βœ… Read token from secret env (set in Space)
 
 
 
12
  hf_token = os.getenv("HF_TOKEN")
13
- model_id = os.getenv("MODEL_ID", "mistralai/Mistral-7B-Instruct-v0.2")
 
14
 
15
- # βœ… Load the gated Mistral model
16
- pipe = pipeline(
17
- "text-generation",
18
- model=model_id,
19
- device=0,
20
- use_auth_token=hf_token
21
- )
22
 
23
- # βœ… Setup FastAPI app
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 serve_js():
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")