Sp2503 commited on
Commit
d3dad7b
Β·
verified Β·
1 Parent(s): c0e90e0

Rename main.py to app.py

Browse files
Files changed (1) hide show
  1. main.py β†’ app.py +5 -10
main.py β†’ app.py RENAMED
@@ -6,15 +6,14 @@ from pydantic import BaseModel
6
  from sentence_transformers import SentenceTransformer, util
7
  from huggingface_hub import snapshot_download
8
 
9
- # --- Cache Configuration ---
10
  os.environ["HF_HOME"] = "/app/hf_cache"
11
  os.environ["TRANSFORMERS_CACHE"] = "/app/hf_cache"
12
  os.environ["TORCH_DISABLE_CUDA"] = "1"
13
 
14
- # --- Hugging Face Repo ---
15
  HF_REPO = "Sp2503/Muril-Model"
16
 
17
- # --- Download model & embeddings from Hugging Face Hub ---
18
  print("πŸ“¦ Downloading model & embeddings from Hugging Face Hub...")
19
  model_dir = snapshot_download(repo_id=HF_REPO, repo_type="model")
20
  print(f"βœ… Model snapshot available at: {model_dir}")
@@ -23,14 +22,14 @@ MODEL_PATH = model_dir
23
  CSV_PATH = os.path.join(model_dir, "muril_multilingual_dataset.csv")
24
  EMBED_PATH = os.path.join(model_dir, "answer_embeddings.pt")
25
 
26
- # --- Load resources ---
27
  print("βš™οΈ Loading model and embeddings...")
28
  model = SentenceTransformer(MODEL_PATH)
29
  df = pd.read_csv(CSV_PATH).dropna(subset=['question', 'answer'])
30
  answer_embeddings = torch.load(EMBED_PATH, map_location="cpu")
31
  print("βœ… Model and embeddings loaded successfully.")
32
 
33
- # --- FastAPI Setup ---
34
  app = FastAPI(title="MuRIL Multilingual QA API")
35
 
36
  class QueryRequest(BaseModel):
@@ -42,7 +41,7 @@ class QAResponse(BaseModel):
42
 
43
  @app.get("/")
44
  def root():
45
- return {"status": "βœ… API ready", "model_loaded": True}
46
 
47
  @app.post("/get-answer", response_model=QAResponse)
48
  def get_answer_endpoint(request: QueryRequest):
@@ -64,7 +63,3 @@ def get_answer_endpoint(request: QueryRequest):
64
  best_idx = torch.argmax(cosine_scores).item()
65
  answer = filtered_df.iloc[best_idx]['answer']
66
  return {"answer": answer}
67
-
68
- if __name__ == "__main__":
69
- import uvicorn
70
- uvicorn.run("main:app", host="0.0.0.0", port=8080)
 
6
  from sentence_transformers import SentenceTransformer, util
7
  from huggingface_hub import snapshot_download
8
 
9
+ # --- Cache Config ---
10
  os.environ["HF_HOME"] = "/app/hf_cache"
11
  os.environ["TRANSFORMERS_CACHE"] = "/app/hf_cache"
12
  os.environ["TORCH_DISABLE_CUDA"] = "1"
13
 
14
+ # --- Download Model & Embeddings from Hub ---
15
  HF_REPO = "Sp2503/Muril-Model"
16
 
 
17
  print("πŸ“¦ Downloading model & embeddings from Hugging Face Hub...")
18
  model_dir = snapshot_download(repo_id=HF_REPO, repo_type="model")
19
  print(f"βœ… Model snapshot available at: {model_dir}")
 
22
  CSV_PATH = os.path.join(model_dir, "muril_multilingual_dataset.csv")
23
  EMBED_PATH = os.path.join(model_dir, "answer_embeddings.pt")
24
 
25
+ # --- Load Model ---
26
  print("βš™οΈ Loading model and embeddings...")
27
  model = SentenceTransformer(MODEL_PATH)
28
  df = pd.read_csv(CSV_PATH).dropna(subset=['question', 'answer'])
29
  answer_embeddings = torch.load(EMBED_PATH, map_location="cpu")
30
  print("βœ… Model and embeddings loaded successfully.")
31
 
32
+ # --- FastAPI App ---
33
  app = FastAPI(title="MuRIL Multilingual QA API")
34
 
35
  class QueryRequest(BaseModel):
 
41
 
42
  @app.get("/")
43
  def root():
44
+ return {"status": "βœ… API is running", "model_loaded": True}
45
 
46
  @app.post("/get-answer", response_model=QAResponse)
47
  def get_answer_endpoint(request: QueryRequest):
 
63
  best_idx = torch.argmax(cosine_scores).item()
64
  answer = filtered_df.iloc[best_idx]['answer']
65
  return {"answer": answer}