Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,21 +9,47 @@ from fastapi import FastAPI, HTTPException
|
|
9 |
from pydantic import BaseModel
|
10 |
from sentence_transformers import SentenceTransformer
|
11 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
12 |
-
|
13 |
-
|
14 |
-
os.environ["
|
|
|
|
|
|
|
15 |
# β
Initialize FastAPI
|
16 |
app = FastAPI()
|
17 |
|
18 |
-
# β
|
19 |
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
|
20 |
if not OPENROUTER_API_KEY:
|
21 |
raise ValueError("β OPENROUTER_API_KEY is missing. Set it as an environment variable.")
|
22 |
|
23 |
OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1/chat/completions"
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
# β
Load Datasets
|
29 |
try:
|
|
|
9 |
from pydantic import BaseModel
|
10 |
from sentence_transformers import SentenceTransformer
|
11 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
12 |
+
|
13 |
+
# β
Set a writable cache directory inside the container
|
14 |
+
os.environ["HF_HOME"] = "/app/cache"
|
15 |
+
os.environ["TRANSFORMERS_CACHE"] = "/app/cache"
|
16 |
+
os.environ["SENTENCE_TRANSFORMERS_HOME"] = "/app/cache"
|
17 |
+
|
18 |
# β
Initialize FastAPI
|
19 |
app = FastAPI()
|
20 |
|
21 |
+
# β
Securely Fetch API Key
|
22 |
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
|
23 |
if not OPENROUTER_API_KEY:
|
24 |
raise ValueError("β OPENROUTER_API_KEY is missing. Set it as an environment variable.")
|
25 |
|
26 |
OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1/chat/completions"
|
27 |
+
|
28 |
+
# β
Load AI Models with explicit caching & remote code trust
|
29 |
+
try:
|
30 |
+
embedding_model = SentenceTransformer(
|
31 |
+
"sentence-transformers/all-MiniLM-L6-v2",
|
32 |
+
cache_folder="/app/cache",
|
33 |
+
trust_remote_code=True # β
Fix potential caching issues
|
34 |
+
)
|
35 |
+
summarization_model = AutoModelForSeq2SeqLM.from_pretrained(
|
36 |
+
"google/long-t5-tglobal-base",
|
37 |
+
cache_dir="/app/cache",
|
38 |
+
trust_remote_code=True # β
Trust remote code
|
39 |
+
)
|
40 |
+
summarization_tokenizer = AutoTokenizer.from_pretrained(
|
41 |
+
"google/long-t5-tglobal-base",
|
42 |
+
cache_dir="/app/cache",
|
43 |
+
trust_remote_code=True
|
44 |
+
)
|
45 |
+
print("β
Models Loaded Successfully!")
|
46 |
+
except Exception as e:
|
47 |
+
print(f"β Model loading error: {e}")
|
48 |
+
|
49 |
+
# β
API Health Check
|
50 |
+
@app.get("/")
|
51 |
+
def health_check():
|
52 |
+
return {"status": "FastAPI is running!"}
|
53 |
|
54 |
# β
Load Datasets
|
55 |
try:
|