AnshulS commited on
Commit
8a0479e
·
verified ·
1 Parent(s): 2b80c17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -38
app.py CHANGED
@@ -1,15 +1,16 @@
1
  import pandas as pd
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=["*"],
@@ -80,7 +81,6 @@ def recommend(query):
80
  result['recommended_assessments'] = validate_and_fix_urls(result['recommended_assessments'])
81
  return result
82
  except Exception as e:
83
- import traceback
84
  print(traceback.format_exc())
85
  return {"error": f"Error processing request: {str(e)}"}
86
 
@@ -101,40 +101,23 @@ async def recommend_api(request: Request):
101
  except Exception as e:
102
  return JSONResponse(content={"error": str(e)}, status_code=500)
103
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
- # === Create Gradio Interface as a Separate App ===
106
- # For newer Gradio versions (5.x), create a standalone Gradio app
107
- with gr.Blocks(title="SHL Assessment Recommender") as demo:
108
- gr.Markdown("# SHL Assessment Recommender")
109
- gr.Markdown("Paste a job description to get the most relevant SHL assessments.")
110
-
111
- with gr.Row():
112
- job_description = gr.Textbox(
113
- label="Enter Job Description",
114
- lines=4,
115
- placeholder="Paste a job description here..."
116
- )
117
-
118
- with gr.Row():
119
- submit_btn = gr.Button("Get Recommendations", variant="primary")
120
-
121
- with gr.Row():
122
- output = gr.JSON(label="Recommended Assessments")
123
-
124
- submit_btn.click(fn=recommend, inputs=job_description, outputs=output)
125
-
126
- # Add path to access Gradio directly at root
127
- @app.get("/")
128
- async def root():
129
- return RedirectResponse(url="/")
130
-
131
- # For Hugging Face Spaces deployment with Gradio SDK
132
- app = gr.mount_gradio_app(app, demo)
133
 
134
- # If running standalone (not through Hugging Face), use this instead
135
  if __name__ == "__main__":
136
  import uvicorn
137
- # Uncomment the line below if running locally not on HF
138
- # uvicorn.run(app, host="0.0.0.0", port=7860)
139
- # Use this for Hugging Face deployment
140
- demo.launch()
 
1
  import pandas as pd
2
  import gradio as gr
3
  from fastapi import FastAPI, Request
4
+ from fastapi.responses import JSONResponse
5
  from fastapi.middleware.cors import CORSMiddleware
6
+ import traceback
7
  from retriever import get_relevant_passages
8
  from reranker import rerank
9
 
10
+ # Create FastAPI app
11
+ app = FastAPI()
12
 
13
+ # Add CORS middleware
14
  app.add_middleware(
15
  CORSMiddleware,
16
  allow_origins=["*"],
 
81
  result['recommended_assessments'] = validate_and_fix_urls(result['recommended_assessments'])
82
  return result
83
  except Exception as e:
 
84
  print(traceback.format_exc())
85
  return {"error": f"Error processing request: {str(e)}"}
86
 
 
101
  except Exception as e:
102
  return JSONResponse(content={"error": str(e)}, status_code=500)
103
 
104
+ # Create a Gradio interface
105
+ demo = gr.Interface(
106
+ fn=recommend,
107
+ inputs=gr.Textbox(
108
+ label="Enter Job Description",
109
+ lines=4,
110
+ placeholder="Paste a job description here..."
111
+ ),
112
+ outputs=gr.JSON(label="Recommended Assessments"),
113
+ title="SHL Assessment Recommender",
114
+ description="Paste a job description to get the most relevant SHL assessments.",
115
+ )
116
 
117
+ # This is the pattern for Gradio 5.x
118
+ app = gr.mount_gradio_app(app, demo, path="/")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
+ # Entry point
121
  if __name__ == "__main__":
122
  import uvicorn
123
+ uvicorn.run(app, host="0.0.0.0", port=7860)