AnshulS commited on
Commit
fdb3da7
·
verified ·
1 Parent(s): b8bc311

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -30
app.py CHANGED
@@ -8,10 +8,10 @@ from gradio.routes import mount_gradio_app
8
  from retriever import get_relevant_passages
9
  from reranker import rerank
10
 
11
- # FastAPI app instance
12
  app = FastAPI()
13
 
14
- # --- CSV Loading and Cleaning ---
15
  def clean_df(df):
16
  df = df.copy()
17
  second_col = df.iloc[:, 2].astype(str)
@@ -25,11 +25,7 @@ def clean_df(df):
25
  df["adaptive_support"] = df.iloc[:, 4].map(lambda x: "Yes" if x == "T" else "No")
26
  df["test_type"] = df.iloc[:, 5].apply(lambda x: eval(x) if isinstance(x, str) else x)
27
  df["description"] = df.iloc[:, 6]
28
-
29
- df["duration"] = pd.to_numeric(
30
- df.iloc[:, 9].astype(str).str.extract(r'(\d+)')[0],
31
- errors='coerce'
32
- )
33
 
34
  return df[["url", "adaptive_support", "remote_support", "description", "duration", "test_type"]]
35
 
@@ -40,7 +36,7 @@ except Exception as e:
40
  print(f"Error loading data: {e}")
41
  df_clean = pd.DataFrame(columns=["url", "adaptive_support", "remote_support", "description", "duration", "test_type"])
42
 
43
- # --- Utility ---
44
  def validate_and_fix_urls(candidates):
45
  for candidate in candidates:
46
  if not isinstance(candidate, dict):
@@ -56,7 +52,7 @@ def validate_and_fix_urls(candidates):
56
  candidate['url'] = f"https://www.shl.com{url}" if url.startswith('/') else f"https://www.shl.com/{url}"
57
  return candidates
58
 
59
- # --- Core Recommend Logic ---
60
  def recommend(query):
61
  if not query.strip():
62
  return {"error": "Please enter a job description"}
@@ -90,33 +86,39 @@ iface = gr.Interface(
90
  description="Paste a job description to get the most relevant SHL assessments."
91
  )
92
 
93
- mount_gradio_app(app, iface, path="/")
94
- # --- Mount Gradio at "/" ---
95
- #app = mount_gradio_app(app, iface, path="/")
96
 
97
- # --- /health Endpoint ---
98
- @app.get("/health", response_class=JSONResponse)
99
  async def health_check():
100
  return JSONResponse(
101
- status_code=200,
102
  content={"status": "healthy"},
103
- media_type="application/json"
 
104
  )
105
 
106
- # --- /recommend Endpoint ---
107
- @app.post("/recommend", response_class=JSONResponse)
108
  async def recommend_api(request: Request):
109
- body = await request.json()
110
- query = body.get("query", "").strip()
111
- if not query:
 
 
 
 
 
 
 
112
  return JSONResponse(
113
- status_code=400,
114
- content={"error": "Missing 'query' in request body"},
115
- media_type="application/json"
 
 
 
 
 
 
116
  )
117
- result = recommend(query)
118
- return JSONResponse(
119
- status_code=200,
120
- content=result,
121
- media_type="application/json"
122
- )
 
8
  from retriever import get_relevant_passages
9
  from reranker import rerank
10
 
11
+ # --- Initialize FastAPI ---
12
  app = FastAPI()
13
 
14
+ # --- Load and clean CSV ---
15
  def clean_df(df):
16
  df = df.copy()
17
  second_col = df.iloc[:, 2].astype(str)
 
25
  df["adaptive_support"] = df.iloc[:, 4].map(lambda x: "Yes" if x == "T" else "No")
26
  df["test_type"] = df.iloc[:, 5].apply(lambda x: eval(x) if isinstance(x, str) else x)
27
  df["description"] = df.iloc[:, 6]
28
+ df["duration"] = pd.to_numeric(df.iloc[:, 9].astype(str).str.extract(r'(\d+)')[0], errors='coerce')
 
 
 
 
29
 
30
  return df[["url", "adaptive_support", "remote_support", "description", "duration", "test_type"]]
31
 
 
36
  print(f"Error loading data: {e}")
37
  df_clean = pd.DataFrame(columns=["url", "adaptive_support", "remote_support", "description", "duration", "test_type"])
38
 
39
+ # --- Fix URLs ---
40
  def validate_and_fix_urls(candidates):
41
  for candidate in candidates:
42
  if not isinstance(candidate, dict):
 
52
  candidate['url'] = f"https://www.shl.com{url}" if url.startswith('/') else f"https://www.shl.com/{url}"
53
  return candidates
54
 
55
+ # --- Recommendation Logic ---
56
  def recommend(query):
57
  if not query.strip():
58
  return {"error": "Please enter a job description"}
 
86
  description="Paste a job description to get the most relevant SHL assessments."
87
  )
88
 
89
+ # Mount Gradio UI without overwriting `app`
90
+ mount_gradio_app(app, iface, path="/")
 
91
 
92
+ # --- Health Endpoint ---
93
+ @app.get("/health")
94
  async def health_check():
95
  return JSONResponse(
 
96
  content={"status": "healthy"},
97
+ media_type="application/json",
98
+ status_code=200
99
  )
100
 
101
+ # --- API Recommendation Endpoint ---
102
+ @app.post("/recommend")
103
  async def recommend_api(request: Request):
104
+ try:
105
+ body = await request.json()
106
+ query = body.get("query", "").strip()
107
+ if not query:
108
+ return JSONResponse(
109
+ content={"error": "Missing 'query' in request body"},
110
+ media_type="application/json",
111
+ status_code=400
112
+ )
113
+ result = recommend(query)
114
  return JSONResponse(
115
+ content=result,
116
+ media_type="application/json",
117
+ status_code=200
118
+ )
119
+ except Exception as e:
120
+ return JSONResponse(
121
+ content={"error": str(e)},
122
+ media_type="application/json",
123
+ status_code=500
124
  )