Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,11 +2,21 @@ import pandas as pd
|
|
2 |
import gradio as gr
|
3 |
from fastapi import FastAPI, Request
|
4 |
from fastapi.responses import JSONResponse, RedirectResponse
|
|
|
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):
|
@@ -28,6 +38,7 @@ def clean_df(df):
|
|
28 |
try:
|
29 |
df = pd.read_csv("assesments.csv", encoding='utf-8')
|
30 |
df_clean = clean_df(df)
|
|
|
31 |
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"])
|
@@ -50,7 +61,7 @@ def validate_and_fix_urls(candidates):
|
|
50 |
|
51 |
# === Recommendation Logic ===
|
52 |
def recommend(query):
|
53 |
-
if not query.strip():
|
54 |
return {"error": "Please enter a job description"}
|
55 |
try:
|
56 |
top_k_df = get_relevant_passages(query, df_clean, top_k=20)
|
@@ -73,15 +84,6 @@ def recommend(query):
|
|
73 |
print(traceback.format_exc())
|
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():
|
@@ -103,6 +105,24 @@ async def recommend_api(request: Request):
|
|
103 |
async def root():
|
104 |
return RedirectResponse(url="/gradio")
|
105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
# === Mount Gradio App ===
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import gradio as gr
|
3 |
from fastapi import FastAPI, Request
|
4 |
from fastapi.responses import JSONResponse, RedirectResponse
|
5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
6 |
from retriever import get_relevant_passages
|
7 |
from reranker import rerank
|
8 |
|
9 |
+
# === Create FastAPI App ===
|
10 |
+
app = FastAPI(title="SHL Assessment Recommender API")
|
11 |
+
|
12 |
+
# Add CORS middleware to allow cross-origin requests
|
13 |
+
app.add_middleware(
|
14 |
+
CORSMiddleware,
|
15 |
+
allow_origins=["*"],
|
16 |
+
allow_credentials=True,
|
17 |
+
allow_methods=["*"],
|
18 |
+
allow_headers=["*"],
|
19 |
+
)
|
20 |
|
21 |
# === Load and Clean CSV ===
|
22 |
def clean_df(df):
|
|
|
38 |
try:
|
39 |
df = pd.read_csv("assesments.csv", encoding='utf-8')
|
40 |
df_clean = clean_df(df)
|
41 |
+
print(f"Successfully loaded {len(df_clean)} assessments")
|
42 |
except Exception as e:
|
43 |
print(f"Error loading data: {e}")
|
44 |
df_clean = pd.DataFrame(columns=["url", "adaptive_support", "remote_support", "description", "duration", "test_type"])
|
|
|
61 |
|
62 |
# === Recommendation Logic ===
|
63 |
def recommend(query):
|
64 |
+
if not query or not query.strip():
|
65 |
return {"error": "Please enter a job description"}
|
66 |
try:
|
67 |
top_k_df = get_relevant_passages(query, df_clean, top_k=20)
|
|
|
84 |
print(traceback.format_exc())
|
85 |
return {"error": f"Error processing request: {str(e)}"}
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
# === FastAPI Endpoints ===
|
88 |
@app.get("/health")
|
89 |
async def health():
|
|
|
105 |
async def root():
|
106 |
return RedirectResponse(url="/gradio")
|
107 |
|
108 |
+
# === Create Gradio Interface ===
|
109 |
+
def create_gr_interface():
|
110 |
+
return gr.Interface(
|
111 |
+
fn=recommend,
|
112 |
+
inputs=gr.Textbox(label="Enter Job Description", lines=4, placeholder="Paste a job description here..."),
|
113 |
+
outputs=gr.JSON(),
|
114 |
+
title="SHL Assessment Recommender",
|
115 |
+
description="Paste a job description to get the most relevant SHL assessments.",
|
116 |
+
theme=gr.themes.Soft(),
|
117 |
+
allow_flagging="never"
|
118 |
+
)
|
119 |
+
|
120 |
# === Mount Gradio App ===
|
121 |
+
# This is the new recommended way to integrate Gradio with FastAPI
|
122 |
+
gr_app = create_gr_interface()
|
123 |
+
app = gr.mount_gradio_app(app, gr_app, path="/gradio")
|
124 |
+
|
125 |
+
# Entry point for running the application directly
|
126 |
+
if __name__ == "__main__":
|
127 |
+
import uvicorn
|
128 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|