Farm_Bot / NLP_dispatcher.py
jeffrey1963's picture
Update NLP_dispatcher.py
2714ed5 verified
import os
import urllib.parse
import gradio as gr
# --- Routing dictionaries (kept exactly as you had) ---
extension_keywords = {
"Jerry": ["depreciation", "tractor", "equipment", "machinery"],
"Amanda": ["enterprise", "budget", "revenue", "expenses"],
"James": ["partial budget", "compare", "replace", "cost saving"],
"Edward": ["balance sheet", "assets", "liabilities", "net worth"],
"Alessandra": ["income statement", "profit", "loss", "cash flow"]
}
lender_keywords = {
"Carlos": ["loan", "financing", "refinance", "repayment"],
"Warren": ["approval", "final plan", "executive", "capstone"]
}
# Space URL for Jerry (you can override in Space Secrets/Env)
JERRY_URL = os.environ.get(
"JERRY_URL",
"https://huggingface.co/spaces/jeffrey1963/Jerry_Depreciation"
)
# --- Core routing: return a route key + human message ---
def route_to_agent(farm_plan: str):
plan = (farm_plan or "").lower()
# Extension agents
for agent, keywords in extension_keywords.items():
if any(k in plan for k in keywords):
route_key = f"extension_{agent.lower()}"
msg = (f"📞 Connecting you to Extension Agent {agent}. "
f"They can help you with that topic and guide you through Extension resources.")
return route_key, msg
# Lenders
for lender, keywords in lender_keywords.items():
if any(k in plan for k in keywords):
route_key = f"lender_{lender.lower()}"
msg = (f"🏦 Forwarding you to Loan Officer {lender} "
f"for project review and financial analysis.")
return route_key, msg
# Fallback
return "unknown", ("🤖 Sorry, I couldn't match your plan to a specific agent or lender. "
"Try using keywords like 'depreciation', 'income', or 'loan'.")
# --- Render the final message, optionally adding deep links that carry Student ID ---
def render_result(route_key: str, base_msg: str, student_id: str):
sid = (student_id or "").strip()
sid_q = urllib.parse.quote_plus(sid) if sid else ""
# Only special-case Jerry for now; you can add others similarly
if route_key == "extension_jerry":
# Pass the student_id through the URL so Jerry can pre-load purchases/ledger
link = f"{JERRY_URL}?student_id={sid_q}" if sid_q else JERRY_URL
extra = f"\n\n➡️ **Open Jerry**: {link}"
if not sid:
extra += "\n\n⚠️ Tip: add your **Student ID** above so Jerry can load/save your depreciation schedule."
return base_msg + extra
# Other routes: just return the base message
return base_msg
# --- Gradio app ---
def dispatch(farm_plan: str, student_id: str):
route_key, msg = route_to_agent(farm_plan)
return render_result(route_key, msg, student_id)
with gr.Blocks() as app:
gr.Markdown("""
# 🧭 AgLendGPT Directory: Extension Agents & Lenders
Submit your farm plan below. We’ll connect you to the right Extension agent for support, or to an Ag Credit officer for projects.
""")
with gr.Row():
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
)
student_id = gr.Textbox(
label="Student ID (for saving work across Spaces)",
placeholder="e.g., netid or student number"
)
submit_btn = gr.Button("📡 Connect Me")
response = gr.Markdown() # Markdown so we can show clickable link
submit_btn.click(fn=dispatch, inputs=[farm_plan_input, student_id], outputs=response)
if __name__ == "__main__":
app.launch()