Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,43 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
from fastmcp import FastMCP
|
|
|
|
| 4 |
|
| 5 |
# Initialize FastMCP agent
|
| 6 |
mcp = FastMCP("RapidAPI Job Search Agent")
|
| 7 |
|
| 8 |
-
RAPIDAPI_URL = "https://indeed-indeed.p.rapidapi.com/apisearch?v=2&format=json
|
| 9 |
RAPIDAPI_HOST = "indeed-indeed.p.rapidapi.com"
|
| 10 |
|
| 11 |
# Load RapidAPI key from HF secret (environment variable)
|
| 12 |
RAPIDAPI_KEY = os.getenv("RAPIDAPI_KEY") # Make sure this matches your secret name exactly
|
| 13 |
|
| 14 |
@mcp.tool(name="search_jobs")
|
| 15 |
-
def search_jobs_tool(query: str, location: str,
|
| 16 |
"""
|
| 17 |
Search jobs from a RapidAPI job search API based on query, location, and optional filters.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
params = {
|
| 20 |
-
"
|
| 21 |
-
"
|
| 22 |
-
"limit":
|
| 23 |
-
#
|
| 24 |
-
#
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
-
# Clean params
|
| 27 |
params = {k: v for k, v in params.items() if v}
|
| 28 |
|
| 29 |
headers = {
|
|
@@ -39,18 +53,27 @@ def search_jobs_tool(query: str, location: str, date_created: str = None, salary
|
|
| 39 |
except requests.RequestException as e:
|
| 40 |
return {"error": str(e)}
|
| 41 |
|
| 42 |
-
def search_jobs_ui(query, location,
|
| 43 |
"""
|
| 44 |
-
UI handler for searching jobs using FastMCP + RapidAPI job API
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
"""
|
| 46 |
try:
|
| 47 |
-
result = search_jobs_tool(query, location,
|
| 48 |
|
| 49 |
if "error" in result:
|
| 50 |
return f"❌ Error: {result['error']}"
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
jobs = result.get("jobs", []) # often APIs return jobs under 'jobs' or 'data'
|
| 54 |
|
| 55 |
if not jobs:
|
| 56 |
return "No jobs found for your search."
|
|
@@ -73,13 +96,13 @@ app = gr.Interface(
|
|
| 73 |
inputs=[
|
| 74 |
gr.Textbox(label="Job Title / Keyword"),
|
| 75 |
gr.Textbox(label="Location"),
|
| 76 |
-
gr.
|
| 77 |
gr.Textbox(label="Salary (optional)"),
|
| 78 |
gr.Textbox(label="Job Type (optional: fulltime, parttime, contract)")
|
| 79 |
],
|
| 80 |
outputs="markdown",
|
| 81 |
title="Job Search with RapidAPI + FastMCP",
|
| 82 |
-
description="Search jobs using a RapidAPI job search API
|
| 83 |
)
|
| 84 |
|
| 85 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
from fastmcp import FastMCP
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
# Initialize FastMCP agent
|
| 8 |
mcp = FastMCP("RapidAPI Job Search Agent")
|
| 9 |
|
| 10 |
+
RAPIDAPI_URL = "https://indeed-indeed.p.rapidapi.com/apisearch?v=2&format=json"
|
| 11 |
RAPIDAPI_HOST = "indeed-indeed.p.rapidapi.com"
|
| 12 |
|
| 13 |
# Load RapidAPI key from HF secret (environment variable)
|
| 14 |
RAPIDAPI_KEY = os.getenv("RAPIDAPI_KEY") # Make sure this matches your secret name exactly
|
| 15 |
|
| 16 |
@mcp.tool(name="search_jobs")
|
| 17 |
+
def search_jobs_tool(query: str, location: str, limit: int = 10, salary: str = None, job_type: str = None):
|
| 18 |
"""
|
| 19 |
Search jobs from a RapidAPI job search API based on query, location, and optional filters.
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
query (str): Job title or keywords to search for.
|
| 23 |
+
location (str): Location (city, region) to search jobs in.
|
| 24 |
+
limit (int): Number of job results to return (default is 10).
|
| 25 |
+
salary (str, optional): Minimum salary filter.
|
| 26 |
+
job_type (str, optional): Job type filter (e.g., fulltime, parttime).
|
| 27 |
+
|
| 28 |
+
Returns:
|
| 29 |
+
dict: JSON response from the API containing job listings or error details.
|
| 30 |
"""
|
| 31 |
params = {
|
| 32 |
+
"q": query,
|
| 33 |
+
"l": location,
|
| 34 |
+
"limit": limit,
|
| 35 |
+
"sort": "date", # Automatically sort by most recent date
|
| 36 |
+
# Add optional filters only if provided
|
| 37 |
+
**({"salary": salary} if salary else {}),
|
| 38 |
+
**({"job_type": job_type} if job_type else {}),
|
| 39 |
}
|
| 40 |
+
# Clean params to remove None values
|
| 41 |
params = {k: v for k, v in params.items() if v}
|
| 42 |
|
| 43 |
headers = {
|
|
|
|
| 53 |
except requests.RequestException as e:
|
| 54 |
return {"error": str(e)}
|
| 55 |
|
| 56 |
+
def search_jobs_ui(query, location, limit=10, salary=None, job_type=None):
|
| 57 |
"""
|
| 58 |
+
Gradio UI handler for searching jobs using FastMCP + RapidAPI job API.
|
| 59 |
+
|
| 60 |
+
Args:
|
| 61 |
+
query (str): Job title or keyword.
|
| 62 |
+
location (str): Job location.
|
| 63 |
+
limit (int, optional): Number of jobs to retrieve (default 10).
|
| 64 |
+
salary (str, optional): Salary filter.
|
| 65 |
+
job_type (str, optional): Job type filter.
|
| 66 |
+
|
| 67 |
+
Returns:
|
| 68 |
+
str: Markdown-formatted list of jobs or error message.
|
| 69 |
"""
|
| 70 |
try:
|
| 71 |
+
result = search_jobs_tool(query, location, limit, salary, job_type)
|
| 72 |
|
| 73 |
if "error" in result:
|
| 74 |
return f"❌ Error: {result['error']}"
|
| 75 |
|
| 76 |
+
jobs = result.get("jobs", []) # adapt based on actual API response
|
|
|
|
| 77 |
|
| 78 |
if not jobs:
|
| 79 |
return "No jobs found for your search."
|
|
|
|
| 96 |
inputs=[
|
| 97 |
gr.Textbox(label="Job Title / Keyword"),
|
| 98 |
gr.Textbox(label="Location"),
|
| 99 |
+
gr.Number(value=10, label="Number of Results (limit)", precision=0),
|
| 100 |
gr.Textbox(label="Salary (optional)"),
|
| 101 |
gr.Textbox(label="Job Type (optional: fulltime, parttime, contract)")
|
| 102 |
],
|
| 103 |
outputs="markdown",
|
| 104 |
title="Job Search with RapidAPI + FastMCP",
|
| 105 |
+
description="Search jobs using a RapidAPI job search API. Results are sorted by most recent date automatically."
|
| 106 |
)
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|