Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,15 @@
|
|
1 |
import pandas as pd
|
2 |
import gradio as gr
|
3 |
-
import
|
4 |
-
from fastapi import FastAPI
|
5 |
from fastapi.responses import JSONResponse
|
6 |
from retriever import get_relevant_passages
|
7 |
from reranker import rerank
|
|
|
8 |
|
9 |
-
#
|
|
|
|
|
|
|
10 |
def clean_df(df):
|
11 |
df = df.copy()
|
12 |
second_col = df.iloc[:, 2].astype(str)
|
@@ -21,10 +24,8 @@ def clean_df(df):
|
|
21 |
df["test_type"] = df.iloc[:, 5].apply(lambda x: eval(x) if isinstance(x, str) else x)
|
22 |
df["description"] = df.iloc[:, 6]
|
23 |
df["duration"] = pd.to_numeric(df.iloc[:, 9].astype(str).str.extract(r'(\d+)')[0], errors='coerce')
|
24 |
-
|
25 |
return df[["url", "adaptive_support", "remote_support", "description", "duration", "test_type"]]
|
26 |
|
27 |
-
# Load data and clean
|
28 |
try:
|
29 |
df = pd.read_csv("assesments.csv", encoding='utf-8')
|
30 |
df_clean = clean_df(df)
|
@@ -32,7 +33,7 @@ except Exception as e:
|
|
32 |
print(f"Error loading data: {e}")
|
33 |
df_clean = pd.DataFrame(columns=["url", "adaptive_support", "remote_support", "description", "duration", "test_type"])
|
34 |
|
35 |
-
#
|
36 |
def validate_and_fix_urls(candidates):
|
37 |
for candidate in candidates:
|
38 |
if not isinstance(candidate, dict):
|
@@ -48,7 +49,7 @@ def validate_and_fix_urls(candidates):
|
|
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"}
|
@@ -73,24 +74,20 @@ def recommend(query):
|
|
73 |
print(traceback.format_exc())
|
74 |
return {"error": f"Error processing request: {str(e)}"}
|
75 |
|
76 |
-
#
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
iface = gr.Interface(
|
81 |
-
fn=gradio_interface,
|
82 |
inputs=gr.Textbox(label="Enter Job Description", lines=4),
|
83 |
outputs="json",
|
84 |
title="SHL Assessment Recommender",
|
85 |
description="Paste a job description to get the most relevant SHL assessments."
|
86 |
)
|
87 |
|
88 |
-
# FastAPI
|
89 |
@app.get("/health")
|
90 |
async def health():
|
91 |
return JSONResponse(content={"status": "healthy"}, status_code=200)
|
92 |
|
93 |
-
# FastAPI-like Recommendation Endpoint in Gradio
|
94 |
@app.post("/recommend")
|
95 |
async def recommend_api(request: Request):
|
96 |
try:
|
@@ -103,11 +100,10 @@ async def recommend_api(request: Request):
|
|
103 |
except Exception as e:
|
104 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
105 |
|
106 |
-
#
|
107 |
-
|
108 |
-
|
109 |
-
@app.on_event("startup")
|
110 |
-
async def startup():
|
111 |
-
# Launch Gradio app when FastAPI app starts
|
112 |
-
iface.launch(inline=True, server_name="0.0.0.0", server_port=7860)
|
113 |
|
|
|
|
|
|
|
|
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 |
+
import uvicorn
|
8 |
|
9 |
+
# === FastAPI App ===
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
# === Load and Clean CSV ===
|
13 |
def clean_df(df):
|
14 |
df = df.copy()
|
15 |
second_col = df.iloc[:, 2].astype(str)
|
|
|
24 |
df["test_type"] = df.iloc[:, 5].apply(lambda x: eval(x) if isinstance(x, str) else x)
|
25 |
df["description"] = df.iloc[:, 6]
|
26 |
df["duration"] = pd.to_numeric(df.iloc[:, 9].astype(str).str.extract(r'(\d+)')[0], errors='coerce')
|
|
|
27 |
return df[["url", "adaptive_support", "remote_support", "description", "duration", "test_type"]]
|
28 |
|
|
|
29 |
try:
|
30 |
df = pd.read_csv("assesments.csv", encoding='utf-8')
|
31 |
df_clean = clean_df(df)
|
|
|
33 |
print(f"Error loading data: {e}")
|
34 |
df_clean = pd.DataFrame(columns=["url", "adaptive_support", "remote_support", "description", "duration", "test_type"])
|
35 |
|
36 |
+
# === Utility ===
|
37 |
def validate_and_fix_urls(candidates):
|
38 |
for candidate in candidates:
|
39 |
if not isinstance(candidate, dict):
|
|
|
49 |
candidate['url'] = f"https://www.shl.com{url}" if url.startswith('/') else f"https://www.shl.com/{url}"
|
50 |
return candidates
|
51 |
|
52 |
+
# === Recommendation Logic ===
|
53 |
def recommend(query):
|
54 |
if not query.strip():
|
55 |
return {"error": "Please enter a job description"}
|
|
|
74 |
print(traceback.format_exc())
|
75 |
return {"error": f"Error processing request: {str(e)}"}
|
76 |
|
77 |
+
# === Gradio UI ===
|
78 |
+
gr_interface = gr.Interface(
|
79 |
+
fn=recommend,
|
|
|
|
|
|
|
80 |
inputs=gr.Textbox(label="Enter Job Description", lines=4),
|
81 |
outputs="json",
|
82 |
title="SHL Assessment Recommender",
|
83 |
description="Paste a job description to get the most relevant SHL assessments."
|
84 |
)
|
85 |
|
86 |
+
# === FastAPI Endpoints ===
|
87 |
@app.get("/health")
|
88 |
async def health():
|
89 |
return JSONResponse(content={"status": "healthy"}, status_code=200)
|
90 |
|
|
|
91 |
@app.post("/recommend")
|
92 |
async def recommend_api(request: Request):
|
93 |
try:
|
|
|
100 |
except Exception as e:
|
101 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
102 |
|
103 |
+
# === Mount Gradio to FastAPI ===
|
104 |
+
from gradio.routes import mount_gradio_app
|
105 |
+
app = mount_gradio_app(app, gr_interface, path="/")
|
|
|
|
|
|
|
|
|
106 |
|
107 |
+
# === Run App ===
|
108 |
+
if __name__ == "__main__":
|
109 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|