AnshulS commited on
Commit
e84dd7a
·
verified ·
1 Parent(s): 6880adc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -2
app.py CHANGED
@@ -1,16 +1,23 @@
1
  import pandas as pd
2
  import gradio as gr
 
 
3
  from retriever import get_relevant_passages
4
  from reranker import rerank
5
 
 
 
 
6
  # === Load and Clean CSV ===
7
  def clean_df(df):
8
  df = df.copy()
9
  second_col = df.iloc[:, 2].astype(str)
 
10
  if second_col.str.contains('http').any() or second_col.str.contains('www').any():
11
  df["url"] = second_col
12
  else:
13
  df["url"] = "https://www.shl.com" + second_col.str.replace(r'^(?!/)', '/', regex=True)
 
14
  df["remote_support"] = df.iloc[:, 3].map(lambda x: "Yes" if x == "T" else "No")
15
  df["adaptive_support"] = df.iloc[:, 4].map(lambda x: "Yes" if x == "T" else "No")
16
  df["test_type"] = df.iloc[:, 5].apply(lambda x: eval(x) if isinstance(x, str) else x)
@@ -25,6 +32,7 @@ except Exception as e:
25
  print(f"Error loading data: {e}")
26
  df_clean = pd.DataFrame(columns=["url", "adaptive_support", "remote_support", "description", "duration", "test_type"])
27
 
 
28
  def validate_and_fix_urls(candidates):
29
  for candidate in candidates:
30
  if not isinstance(candidate, dict):
@@ -40,6 +48,7 @@ def validate_and_fix_urls(candidates):
40
  candidate['url'] = f"https://www.shl.com{url}" if url.startswith('/') else f"https://www.shl.com/{url}"
41
  return candidates
42
 
 
43
  def recommend(query):
44
  if not query.strip():
45
  return {"error": "Please enter a job description"}
@@ -65,13 +74,31 @@ def recommend(query):
65
  return {"error": f"Error processing request: {str(e)}"}
66
 
67
  # === Gradio UI ===
68
- gr.Interface(
69
  fn=recommend,
70
  inputs=gr.Textbox(label="Enter Job Description", lines=4),
71
  outputs="json",
72
  title="SHL Assessment Recommender",
73
  description="Paste a job description to get the most relevant SHL assessments."
74
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
 
76
  from gradio.routes import mount_gradio_app
77
  app = mount_gradio_app(app, gr_interface, path="/")
 
1
  import pandas as pd
2
  import gradio as gr
3
+ from fastapi import FastAPI, Request
4
+ from fastapi.responses import JSONResponse
5
  from retriever import get_relevant_passages
6
  from reranker import rerank
7
 
8
+ # === FastAPI App ===
9
+ app = FastAPI()
10
+
11
  # === Load and Clean CSV ===
12
  def clean_df(df):
13
  df = df.copy()
14
  second_col = df.iloc[:, 2].astype(str)
15
+
16
  if second_col.str.contains('http').any() or second_col.str.contains('www').any():
17
  df["url"] = second_col
18
  else:
19
  df["url"] = "https://www.shl.com" + second_col.str.replace(r'^(?!/)', '/', regex=True)
20
+
21
  df["remote_support"] = df.iloc[:, 3].map(lambda x: "Yes" if x == "T" else "No")
22
  df["adaptive_support"] = df.iloc[:, 4].map(lambda x: "Yes" if x == "T" else "No")
23
  df["test_type"] = df.iloc[:, 5].apply(lambda x: eval(x) if isinstance(x, str) else x)
 
32
  print(f"Error loading data: {e}")
33
  df_clean = pd.DataFrame(columns=["url", "adaptive_support", "remote_support", "description", "duration", "test_type"])
34
 
35
+ # === Utility ===
36
  def validate_and_fix_urls(candidates):
37
  for candidate in candidates:
38
  if not isinstance(candidate, dict):
 
48
  candidate['url'] = f"https://www.shl.com{url}" if url.startswith('/') else f"https://www.shl.com/{url}"
49
  return candidates
50
 
51
+ # === Recommendation Logic ===
52
  def recommend(query):
53
  if not query.strip():
54
  return {"error": "Please enter a job description"}
 
74
  return {"error": f"Error processing request: {str(e)}"}
75
 
76
  # === Gradio UI ===
77
+ gr_interface = gr.Interface(
78
  fn=recommend,
79
  inputs=gr.Textbox(label="Enter Job Description", lines=4),
80
  outputs="json",
81
  title="SHL Assessment Recommender",
82
  description="Paste a job description to get the most relevant SHL assessments."
83
+ )
84
+
85
+ # === FastAPI Endpoints ===
86
+ @app.get("/health")
87
+ async def health():
88
+ return JSONResponse(content={"status": "healthy"}, status_code=200)
89
+
90
+ @app.post("/recommend")
91
+ async def recommend_api(request: Request):
92
+ try:
93
+ data = await request.json()
94
+ query = data.get("query", "").strip()
95
+ if not query:
96
+ return JSONResponse(content={"error": "Missing query"}, status_code=400)
97
+ result = recommend(query)
98
+ return JSONResponse(content=result, status_code=200)
99
+ except Exception as e:
100
+ return JSONResponse(content={"error": str(e)}, status_code=500)
101
 
102
+ # === Mount Gradio to FastAPI ===
103
  from gradio.routes import mount_gradio_app
104
  app = mount_gradio_app(app, gr_interface, path="/")