OppaAI commited on
Commit
0aafde1
·
verified ·
1 Parent(s): fddd1c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -22
app.py CHANGED
@@ -1,7 +1,8 @@
1
  import asyncio
2
- import re
3
  import os
 
4
  import gradio as gr
 
5
  from pydantic_ai import Agent
6
  from pydantic_ai.mcp import MCPServerHTTP
7
  from pydantic_ai.models.gemini import GeminiModel
@@ -11,42 +12,60 @@ from pydantic_ai.providers.google_gla import GoogleGLAProvider
11
  SSE_URL = "https://oppaai-job-search-mcp-server.hf.space/gradio_api/mcp/sse"
12
  server = MCPServerHTTP(url=SSE_URL)
13
 
14
- GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") # Make sure your GEMINI_API_KEY is set in environment variables
 
15
 
16
- # Use OpenRouter hosted LLM Qwen3-30B-A3B model
17
  model = GeminiModel(
18
- 'gemini-2.0-flash',
19
- provider=GoogleGLAProvider(api_key=GEMINI_API_KEY)
 
20
  )
21
 
22
- # Create Agent with MCP Server
23
  agent = Agent(
24
  model=model,
25
  mcp_servers=[server],
26
  instructions="""
27
- Your name is Jobcy. You are an AI assistant designed to help users to find remote jobs by searching through job listings from various sources, including the Jobicy API and other platforms.
28
- You will list the job listings in a structured format, including the job title, company, location, and the google search link.
29
  """
30
  )
31
 
 
32
  async def chat_with_agent(user_input, history):
33
  history = history or []
34
- result = await agent.run(user_input)
35
- history.append(("User", user_input))
36
- history.append(("Jobcy", result))
 
 
 
 
 
37
  return history, history
38
 
39
- async def main():
40
- async with agent.run_mcp_servers():
41
- with gr.Blocks() as demo:
42
- chatbot = gr.Chatbot()
43
- user_input = gr.Textbox(placeholder="Ask Jobcy about remote jobs or anything else...", label="Your Message")
44
- state = gr.State([])
 
 
 
 
45
 
46
- user_input.submit(chat_with_agent, inputs=[user_input, state], outputs=[chatbot, state])
47
 
48
- demo.title = "Jobcy Remote Job Search Assistant"
49
- demo.launch(share=True)
 
 
 
 
 
 
50
 
51
- if __name__ == "__main__":
52
- asyncio.run(main())
 
1
  import asyncio
 
2
  import os
3
+ import re
4
  import gradio as gr
5
+
6
  from pydantic_ai import Agent
7
  from pydantic_ai.mcp import MCPServerHTTP
8
  from pydantic_ai.models.gemini import GeminiModel
 
12
  SSE_URL = "https://oppaai-job-search-mcp-server.hf.space/gradio_api/mcp/sse"
13
  server = MCPServerHTTP(url=SSE_URL)
14
 
15
+ # Load Gemini API Key from environment
16
+ GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
17
 
18
+ # Define model
19
  model = GeminiModel(
20
+ "gemini-2.0-flash",
21
+ provider=GoogleGLAProvider(api_key=GEMINI_API_KEY),
22
+ thinking_budget=0 # Optional: disables internal thinking token usage
23
  )
24
 
25
+ # Define agent
26
  agent = Agent(
27
  model=model,
28
  mcp_servers=[server],
29
  instructions="""
30
+ Your name is Jobcy. You are an AI assistant designed to help users find remote jobs by searching through job listings from various sources, including the Jobicy API and other platforms.
31
+ Return job listings in a structured format: job title, company, location, and a Google search link.
32
  """
33
  )
34
 
35
+ # Async chat function with timeout protection
36
  async def chat_with_agent(user_input, history):
37
  history = history or []
38
+ history.append(("You", user_input))
39
+ try:
40
+ result = await asyncio.wait_for(agent.run(user_input), timeout=30)
41
+ history.append(("Jobcy", result))
42
+ except asyncio.TimeoutError:
43
+ history.append(("Jobcy", "⏳ Sorry, I took too long to respond. Try again in a moment."))
44
+ except Exception as e:
45
+ history.append(("Jobcy", f"⚠️ Error: {type(e).__name__}: {e}"))
46
  return history, history
47
 
48
+ # Main app UI
49
+ with gr.Blocks() as demo:
50
+ gr.Markdown("## 💼 Jobcy – Your Remote Job Assistant")
51
+ chatbot = gr.Chatbot()
52
+ user_input = gr.Textbox(
53
+ placeholder="Ask Jobcy about remote jobs...",
54
+ label="Your Message",
55
+ lines=2
56
+ )
57
+ state = gr.State([])
58
 
59
+ user_input.submit(chat_with_agent, inputs=[user_input, state], outputs=[chatbot, state])
60
 
61
+ gr.Examples(
62
+ examples=[
63
+ "Find remote software engineer jobs in Canada",
64
+ "Show me part-time data analyst jobs I can do from home",
65
+ "List any remote marketing jobs with no degree required",
66
+ ],
67
+ inputs=user_input
68
+ )
69
 
70
+ demo.queue(concurrency_count=2)
71
+ demo.launch(share=True)