OppaAI commited on
Commit
467f662
·
verified ·
1 Parent(s): bf33900

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -48
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  import requests
3
- from fastmcp import FastMCP, Client
4
 
5
  # Initialize FastMCP agent
6
  mcp = FastMCP("Indeed Job Search Agent")
@@ -9,16 +9,6 @@ mcp = FastMCP("Indeed Job Search Agent")
9
  def search_jobs_tool(query: str, location: str, date_created: str = None, salary: str = None, job_type: str = None):
10
  """
11
  Search jobs from Indeed API based on query, location and optional filters.
12
-
13
- Args:
14
- query (str): Job title or keyword to search.
15
- location (str): City or region to search in.
16
- date_created (str, optional): Filter by creation date (e.g. "2024-06-01").
17
- salary (str, optional): Minimum salary filter.
18
- job_type (str, optional): Type of job (e.g. "fulltime", "parttime").
19
-
20
- Returns:
21
- dict: Job search results or error message.
22
  """
23
  url = "https://employers.indeed.com/api/v2/jobs"
24
  params = {
@@ -46,52 +36,36 @@ def search_jobs_tool(query: str, location: str, date_created: str = None, salary
46
  except requests.RequestException as e:
47
  return {"error": str(e)}
48
 
49
- async def search_jobs_ui(query, location, date_created=None, salary=None, job_type=None):
50
  """
51
- UI handler for searching jobs using FastMCP + Indeed API.
52
-
53
- Args:
54
- query (str): Job title or keyword to search.
55
- location (str): Job location.
56
- date_created (str, optional): Date filter (YYYY-MM-DD).
57
- salary (str, optional): Salary filter.
58
- job_type (str, optional): Job type filter.
59
-
60
- Returns:
61
- str: Markdown-formatted job list or error message.
62
  """
63
- async with Client(mcp) as client:
64
- try:
65
- result = await client.call_tool("search_jobs", {
66
- "query": query,
67
- "location": location,
68
- "date_created": date_created,
69
- "salary": salary,
70
- "job_type": job_type
71
- })
72
 
73
- if "error" in result:
74
- return f"❌ Error: {result['error']}"
75
 
76
- jobs = result.get("results", [])
77
- if not jobs:
78
- return "No jobs found for your search."
79
 
80
- output = ""
81
- for job in jobs:
82
- title = job.get("title", "No Title")
83
- company = job.get("company", "Unknown Company")
84
- job_location = job.get("location", "Unknown Location")
85
- job_url = job.get("url", "#")
86
- output += f"**{title}** at *{company}*\n📍 {job_location}\n[Apply Here]({job_url})\n\n"
87
 
88
- return output
89
- except Exception as e:
90
- return f"❌ An error occurred: {str(e)}"
91
 
92
  # Gradio Interface
93
  app = gr.Interface(
94
- fn=gr.async_function(search_jobs_ui), # Important to wrap async functions
95
  inputs=[
96
  gr.Textbox(label="Job Title / Keyword"),
97
  gr.Textbox(label="Location"),
 
1
  import gradio as gr
2
  import requests
3
+ from fastmcp import FastMCP
4
 
5
  # Initialize FastMCP agent
6
  mcp = FastMCP("Indeed Job Search Agent")
 
9
  def search_jobs_tool(query: str, location: str, date_created: str = None, salary: str = None, job_type: str = None):
10
  """
11
  Search jobs from Indeed API based on query, location and optional filters.
 
 
 
 
 
 
 
 
 
 
12
  """
13
  url = "https://employers.indeed.com/api/v2/jobs"
14
  params = {
 
36
  except requests.RequestException as e:
37
  return {"error": str(e)}
38
 
39
+ def search_jobs_ui(query, location, date_created=None, salary=None, job_type=None):
40
  """
41
+ UI handler for searching jobs using FastMCP + Indeed API (sync).
 
 
 
 
 
 
 
 
 
 
42
  """
43
+ try:
44
+ # 直接調用工具函數,唔用 async client
45
+ result = search_jobs_tool(query, location, date_created, salary, job_type)
 
 
 
 
 
 
46
 
47
+ if "error" in result:
48
+ return f"❌ Error: {result['error']}"
49
 
50
+ jobs = result.get("results", [])
51
+ if not jobs:
52
+ return "No jobs found for your search."
53
 
54
+ output = ""
55
+ for job in jobs:
56
+ title = job.get("title", "No Title")
57
+ company = job.get("company", "Unknown Company")
58
+ job_location = job.get("location", "Unknown Location")
59
+ job_url = job.get("url", "#")
60
+ output += f"**{title}** at *{company}*\n📍 {job_location}\n[Apply Here]({job_url})\n\n"
61
 
62
+ return output
63
+ except Exception as e:
64
+ return f"❌ An error occurred: {str(e)}"
65
 
66
  # Gradio Interface
67
  app = gr.Interface(
68
+ fn=search_jobs_ui,
69
  inputs=[
70
  gr.Textbox(label="Job Title / Keyword"),
71
  gr.Textbox(label="Location"),