mindspark121 commited on
Commit
96f1d8c
Β·
verified Β·
1 Parent(s): 45eb46b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -7
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
- os.environ["HF_HOME"] = "/tmp/huggingface"
13
- os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
14
- os.environ["SENTENCE_TRANSFORMERS_HOME"] = "/tmp/huggingface"
 
 
 
15
  # βœ… Initialize FastAPI
16
  app = FastAPI()
17
 
18
- # βœ… Set OpenRouter API Key
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
- embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2", cache_folder="/app/cache")
25
- summarization_model = AutoModelForSeq2SeqLM.from_pretrained("google/long-t5-tglobal-base", cache_dir="/app/cache")
26
- summarization_tokenizer = AutoTokenizer.from_pretrained("google/long-t5-tglobal-base", cache_dir="/app/cache")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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: