OppaAI commited on
Commit
98e03e1
·
verified ·
1 Parent(s): cbb6127

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -59
app.py CHANGED
@@ -1,73 +1,36 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
- from fastmcp import FastMCPClient
4
  import os
 
 
5
 
6
- # Set up inference client (LLM)
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
- # Set up FastMCP client (replace with your actual MCP server URL)
10
- mcp = FastMCPClient("Jobicy Remote Jobs Agent", url="https://oppaai-job-search-mcp-server.hf.space/gradio_api/mcp/sse")
11
 
12
- def respond(
13
- message,
14
- history: list[tuple[str, str]],
15
- system_message,
16
- max_tokens,
17
- temperature,
18
- top_p,
19
- ):
20
- messages = [{"role": "system", "content": system_message}]
21
 
22
- for val in history:
23
- if val[0]:
24
- messages.append({"role": "user", "content": val[0]})
25
- if val[1]:
26
- messages.append({"role": "assistant", "content": val[1]})
27
 
28
- messages.append({"role": "user", "content": message})
 
 
 
 
29
 
30
- # Tool invocation keyword check (e.g., "search jobs for Python in Canada")
31
- if "search jobs" in message.lower():
32
- # Simple NLP parse: extract keyword, country, industry
33
- # (You can improve this with spaCy or regex later)
34
- keyword = message.split("for")[-1].strip().split(" in ")[0].strip()
35
- country = message.split(" in ")[-1].strip() if " in " in message else ""
36
- res = mcp.call_tool("search_jobs", inputs={"keyword": keyword, "country": country, "limit": 5})
37
 
38
- if "error" in res:
39
- return f"❌ {res['error']}"
40
- if not res.get("jobs"):
41
- return "No jobs found with those filters."
42
 
43
- job_lines = [
44
- f"**{j['title']}** at {j['company']} ({j['location']})\n{j['salary']}\nPosted: {j['pubDate']}\n[Apply Here]({j['url']})"
45
- for j in res["jobs"]
46
- ]
47
- return "\n\n---\n\n".join(job_lines)
48
 
49
- # Fallback to LLM
50
- response = ""
51
- for message in client.chat_completion(
52
- messages,
53
- max_tokens=max_tokens,
54
- stream=True,
55
- temperature=temperature,
56
- top_p=top_p,
57
- ):
58
- token = message.choices[0].delta.content or ""
59
- response += token
60
- yield response
61
 
62
  demo = gr.ChatInterface(
63
- respond,
64
- additional_inputs=[
65
- gr.Textbox(value="You are a friendly assistant who can also help find remote jobs.", label="System message"),
66
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
67
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
68
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
69
- ],
70
- title="Job Search Assistant Chatbot"
71
  )
72
 
73
  if __name__ == "__main__":
 
 
 
 
1
  import os
2
+ import gradio as gr
3
+ from smolagents import MCPClient, InferenceClientModel, CodeAgent
4
 
5
+ # Your MCP server URL (replace with your actual HF space MCP endpoint)
6
+ MCP_URL = "https://oppaai-job-search-mcp-server.hf.space/gradio_api/mcp/sse"
 
 
 
7
 
8
+ # Initialize MCP client to connect to your MCP server
9
+ mcp_client = MCPClient({"url": MCP_URL, "transport": "streamable-http"})
 
 
 
 
 
 
 
10
 
11
+ # Fetch available tools from MCP server
12
+ tools = mcp_client.get_tools()
 
 
 
13
 
14
+ # Initialize your LLM model from Hugging Face (requires HF token for private/protected models)
15
+ llm_model = InferenceClientModel(
16
+ model_id="HuggingFaceH4/zephyr-7b-beta",
17
+ token=os.getenv("HUGGINGFACE_TOKEN"), # Set this environment variable if needed
18
+ )
19
 
20
+ # Create an agent that uses the tools and the LLM
21
+ agent = CodeAgent(tools=tools, model=llm_model, stream_outputs=True)
 
 
 
 
 
22
 
 
 
 
 
23
 
24
+ def respond(message, history):
25
+ # Agent processes the input message, uses tools if needed, else LLM
26
+ reply = agent.run(message)
27
+ return reply
 
28
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  demo = gr.ChatInterface(
31
+ fn=respond,
32
+ title="Job Search Assistant (Agent)",
33
+ description="Ask me about job listings or general questions I’ll use the job search tool when needed!",
 
 
 
 
 
34
  )
35
 
36
  if __name__ == "__main__":