jeffrey1963 commited on
Commit
2714ed5
·
verified ·
1 Parent(s): 08cfc67

Update NLP_dispatcher.py

Browse files
Files changed (1) hide show
  1. NLP_dispatcher.py +60 -24
NLP_dispatcher.py CHANGED
@@ -1,6 +1,8 @@
 
 
1
  import gradio as gr
2
 
3
- # Define routing keywords for Extension Agents and Ag Lenders
4
  extension_keywords = {
5
  "Jerry": ["depreciation", "tractor", "equipment", "machinery"],
6
  "Amanda": ["enterprise", "budget", "revenue", "expenses"],
@@ -14,45 +16,79 @@ lender_keywords = {
14
  "Warren": ["approval", "final plan", "executive", "capstone"]
15
  }
16
 
17
- JERRY_URL = os.environ.get("JERRY_URL", "https://huggingface.co/spaces/jeffrey1963/Jerry_Depreciation")
 
 
 
 
18
 
 
 
 
19
 
20
- # Match farm plan to best agent or lender
21
- def route_to_agent(farm_plan):
22
- farm_plan_lower = farm_plan.lower()
23
-
24
  for agent, keywords in extension_keywords.items():
25
- for word in keywords:
26
- if word in farm_plan_lower:
27
- return f"📞 Connecting you to Extension Agent {agent}. They can help you with that topic and guide you through Extension resources."
 
 
28
 
 
29
  for lender, keywords in lender_keywords.items():
30
- for word in keywords:
31
- if word in farm_plan_lower:
32
- return f"🏦 Forwarding you to Loan Officer {lender} for project review and financial analysis."
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- return "🤖 Sorry, I couldn't match your plan to a specific agent or lender. Try using keywords like 'depreciation', 'income', or 'loan'."
 
 
 
 
 
 
 
35
 
36
- def render_result(route, student_id):
37
- if route == "extension_jerry":
38
- link = f"{JERRY_URL}?student_id={student_id or ''}"
39
- return (f"☎️ Connecting you to **Extension Agent Jerry** for depreciation help.\n\n"
40
- f"➡️ **Open Jerry**: {link}")
41
- # ... other routes ...
42
 
 
 
 
 
43
 
44
  with gr.Blocks() as app:
45
  gr.Markdown("""
46
- # 🧭 AgLendGPT Directory: Extension Agents & Lenders
47
  Submit your farm plan below. We’ll connect you to the right Extension agent for support, or to an Ag Credit officer for projects.
48
  """)
49
 
50
- farm_plan_input = gr.Textbox(label="Describe your farm plan", placeholder="e.g., I want to expand with new equipment and need help calculating depreciation.", lines=4)
51
- submit_btn = gr.Button("📡 Connect Me")
52
- response = gr.Textbox(label="Routing Result")
 
 
 
 
 
 
 
53
 
54
- submit_btn.click(fn=route_to_agent, inputs=farm_plan_input, outputs=response)
 
55
 
 
56
 
57
  if __name__ == "__main__":
58
  app.launch()
 
1
+ import os
2
+ import urllib.parse
3
  import gradio as gr
4
 
5
+ # --- Routing dictionaries (kept exactly as you had) ---
6
  extension_keywords = {
7
  "Jerry": ["depreciation", "tractor", "equipment", "machinery"],
8
  "Amanda": ["enterprise", "budget", "revenue", "expenses"],
 
16
  "Warren": ["approval", "final plan", "executive", "capstone"]
17
  }
18
 
19
+ # Space URL for Jerry (you can override in Space Secrets/Env)
20
+ JERRY_URL = os.environ.get(
21
+ "JERRY_URL",
22
+ "https://huggingface.co/spaces/jeffrey1963/Jerry_Depreciation"
23
+ )
24
 
25
+ # --- Core routing: return a route key + human message ---
26
+ def route_to_agent(farm_plan: str):
27
+ plan = (farm_plan or "").lower()
28
 
29
+ # Extension agents
 
 
 
30
  for agent, keywords in extension_keywords.items():
31
+ if any(k in plan for k in keywords):
32
+ route_key = f"extension_{agent.lower()}"
33
+ msg = (f"📞 Connecting you to Extension Agent {agent}. "
34
+ f"They can help you with that topic and guide you through Extension resources.")
35
+ return route_key, msg
36
 
37
+ # Lenders
38
  for lender, keywords in lender_keywords.items():
39
+ if any(k in plan for k in keywords):
40
+ route_key = f"lender_{lender.lower()}"
41
+ msg = (f"🏦 Forwarding you to Loan Officer {lender} "
42
+ f"for project review and financial analysis.")
43
+ return route_key, msg
44
+
45
+ # Fallback
46
+ return "unknown", ("🤖 Sorry, I couldn't match your plan to a specific agent or lender. "
47
+ "Try using keywords like 'depreciation', 'income', or 'loan'.")
48
+
49
+ # --- Render the final message, optionally adding deep links that carry Student ID ---
50
+ def render_result(route_key: str, base_msg: str, student_id: str):
51
+ sid = (student_id or "").strip()
52
+ sid_q = urllib.parse.quote_plus(sid) if sid else ""
53
 
54
+ # Only special-case Jerry for now; you can add others similarly
55
+ if route_key == "extension_jerry":
56
+ # Pass the student_id through the URL so Jerry can pre-load purchases/ledger
57
+ link = f"{JERRY_URL}?student_id={sid_q}" if sid_q else JERRY_URL
58
+ extra = f"\n\n➡️ **Open Jerry**: {link}"
59
+ if not sid:
60
+ extra += "\n\n⚠️ Tip: add your **Student ID** above so Jerry can load/save your depreciation schedule."
61
+ return base_msg + extra
62
 
63
+ # Other routes: just return the base message
64
+ return base_msg
 
 
 
 
65
 
66
+ # --- Gradio app ---
67
+ def dispatch(farm_plan: str, student_id: str):
68
+ route_key, msg = route_to_agent(farm_plan)
69
+ return render_result(route_key, msg, student_id)
70
 
71
  with gr.Blocks() as app:
72
  gr.Markdown("""
73
+ # 🧭 AgLendGPT Directory: Extension Agents & Lenders
74
  Submit your farm plan below. We’ll connect you to the right Extension agent for support, or to an Ag Credit officer for projects.
75
  """)
76
 
77
+ with gr.Row():
78
+ farm_plan_input = gr.Textbox(
79
+ label="Describe your farm plan",
80
+ placeholder="e.g., I want to expand with new equipment and need help calculating depreciation.",
81
+ lines=4
82
+ )
83
+ student_id = gr.Textbox(
84
+ label="Student ID (for saving work across Spaces)",
85
+ placeholder="e.g., netid or student number"
86
+ )
87
 
88
+ submit_btn = gr.Button("📡 Connect Me")
89
+ response = gr.Markdown() # Markdown so we can show clickable link
90
 
91
+ submit_btn.click(fn=dispatch, inputs=[farm_plan_input, student_id], outputs=response)
92
 
93
  if __name__ == "__main__":
94
  app.launch()