Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,15 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
import sys
|
| 4 |
-
|
| 5 |
-
# FIX 1: Corrected imports for CrewAI 2.0+
|
| 6 |
from crewai import Agent, Task, Crew, Process
|
| 7 |
from crewai.tools import BaseTool
|
| 8 |
from crewai_tools import SerperDevTool
|
| 9 |
from langchain_openai import ChatOpenAI
|
| 10 |
|
| 11 |
-
|
| 12 |
st.set_page_config(page_title="Smart Relocation Assistant", page_icon="ποΈ", layout="wide")
|
| 13 |
|
| 14 |
-
st.title("
|
| 15 |
st.markdown("Use AI Agents to find your perfect neighborhood based on budget and lifestyle.")
|
| 16 |
|
| 17 |
# --- 2. API KEY SETUP ---
|
|
@@ -20,7 +18,7 @@ openai_api_key = os.getenv("OPENAI_API_KEY")
|
|
| 20 |
serper_api_key = os.getenv("SERPER_API_KEY")
|
| 21 |
|
| 22 |
if not (openai_api_key and serper_api_key):
|
| 23 |
-
st.error("
|
| 24 |
st.warning("Please set OPENAI_API_KEY and SERPER_API_KEY in your Hugging Face Space 'Settings' -> 'Variables and secrets'.")
|
| 25 |
st.stop()
|
| 26 |
|
|
@@ -33,7 +31,7 @@ os.environ["SERPER_API_KEY"] = serper_api_key
|
|
| 33 |
# The Search Tool is now initialized here
|
| 34 |
search_tool = SerperDevTool()
|
| 35 |
|
| 36 |
-
# Custom Calculator Tool
|
| 37 |
class CalculatorTool(BaseTool):
|
| 38 |
name: str = "Budget Calculator"
|
| 39 |
description: str = "Useful for calculating if a rent price fits within a budget. Input must be a mathematical expression."
|
|
@@ -47,8 +45,7 @@ class CalculatorTool(BaseTool):
|
|
| 47 |
|
| 48 |
calc_tool = CalculatorTool()
|
| 49 |
|
| 50 |
-
# --- 4. LLM DEFINITION
|
| 51 |
-
# FIX 2: Explicitly define the gpt-4o-mini model
|
| 52 |
gpt_4o_mini = ChatOpenAI(
|
| 53 |
model="gpt-4o-mini",
|
| 54 |
temperature=0.7
|
|
@@ -69,13 +66,12 @@ preferences = st.text_area("Lifestyle Preferences", "Walkable, near coffee shops
|
|
| 69 |
# --- 6. AGENT & TASK DEFINITIONS (Defined in the function for cleaner scope) ---
|
| 70 |
|
| 71 |
def run_crew_analysis(city, work_location, budget, preferences):
|
| 72 |
-
# FIX 2 (CONT.): Passing the LLM instance to the Agents
|
| 73 |
city_scout = Agent(
|
| 74 |
role='City Neighborhood Scout',
|
| 75 |
goal=f'Identify the top 3 neighborhoods in {city} that match these preferences: {preferences}.',
|
| 76 |
backstory="You are a local expert who knows every corner of the city. You prioritize safety, commute, and lifestyle fit.",
|
| 77 |
tools=[search_tool],
|
| 78 |
-
llm=gpt_4o_mini,
|
| 79 |
verbose=True,
|
| 80 |
allow_delegation=False
|
| 81 |
)
|
|
@@ -85,7 +81,7 @@ def run_crew_analysis(city, work_location, budget, preferences):
|
|
| 85 |
goal=f'Assess the affordability of specific neighborhoods and ensure rent is within {budget}.',
|
| 86 |
backstory="You are a pragmatic financial advisor. You strictly filter out any options that exceed the user's budget.",
|
| 87 |
tools=[search_tool, calc_tool],
|
| 88 |
-
llm=gpt_4o_mini,
|
| 89 |
verbose=True,
|
| 90 |
allow_delegation=False
|
| 91 |
)
|
|
@@ -113,20 +109,20 @@ def run_crew_analysis(city, work_location, budget, preferences):
|
|
| 113 |
agents=[city_scout, budget_analyst],
|
| 114 |
tasks=[scout_task, analysis_task, report_task],
|
| 115 |
process=Process.sequential,
|
| 116 |
-
llm=gpt_4o_mini
|
| 117 |
)
|
| 118 |
|
| 119 |
return relocation_crew.kickoff()
|
| 120 |
|
| 121 |
# --- 7. BUTTON TRIGGER ---
|
| 122 |
if st.button("Start Research", type="primary"):
|
| 123 |
-
with st.spinner('
|
| 124 |
try:
|
| 125 |
result = run_crew_analysis(city, work_location, budget, preferences)
|
| 126 |
|
| 127 |
# Display Results
|
| 128 |
st.success("Research Complete!")
|
| 129 |
-
st.markdown("###
|
| 130 |
st.markdown(result)
|
| 131 |
|
| 132 |
except Exception as e:
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
import sys
|
|
|
|
|
|
|
| 4 |
from crewai import Agent, Task, Crew, Process
|
| 5 |
from crewai.tools import BaseTool
|
| 6 |
from crewai_tools import SerperDevTool
|
| 7 |
from langchain_openai import ChatOpenAI
|
| 8 |
|
| 9 |
+
|
| 10 |
st.set_page_config(page_title="Smart Relocation Assistant", page_icon="ποΈ", layout="wide")
|
| 11 |
|
| 12 |
+
st.title("Smart Relocation Assistant")
|
| 13 |
st.markdown("Use AI Agents to find your perfect neighborhood based on budget and lifestyle.")
|
| 14 |
|
| 15 |
# --- 2. API KEY SETUP ---
|
|
|
|
| 18 |
serper_api_key = os.getenv("SERPER_API_KEY")
|
| 19 |
|
| 20 |
if not (openai_api_key and serper_api_key):
|
| 21 |
+
st.error(" API Keys not found!")
|
| 22 |
st.warning("Please set OPENAI_API_KEY and SERPER_API_KEY in your Hugging Face Space 'Settings' -> 'Variables and secrets'.")
|
| 23 |
st.stop()
|
| 24 |
|
|
|
|
| 31 |
# The Search Tool is now initialized here
|
| 32 |
search_tool = SerperDevTool()
|
| 33 |
|
| 34 |
+
# Custom Calculator Tool
|
| 35 |
class CalculatorTool(BaseTool):
|
| 36 |
name: str = "Budget Calculator"
|
| 37 |
description: str = "Useful for calculating if a rent price fits within a budget. Input must be a mathematical expression."
|
|
|
|
| 45 |
|
| 46 |
calc_tool = CalculatorTool()
|
| 47 |
|
| 48 |
+
# --- 4. LLM DEFINITION
|
|
|
|
| 49 |
gpt_4o_mini = ChatOpenAI(
|
| 50 |
model="gpt-4o-mini",
|
| 51 |
temperature=0.7
|
|
|
|
| 66 |
# --- 6. AGENT & TASK DEFINITIONS (Defined in the function for cleaner scope) ---
|
| 67 |
|
| 68 |
def run_crew_analysis(city, work_location, budget, preferences):
|
|
|
|
| 69 |
city_scout = Agent(
|
| 70 |
role='City Neighborhood Scout',
|
| 71 |
goal=f'Identify the top 3 neighborhoods in {city} that match these preferences: {preferences}.',
|
| 72 |
backstory="You are a local expert who knows every corner of the city. You prioritize safety, commute, and lifestyle fit.",
|
| 73 |
tools=[search_tool],
|
| 74 |
+
llm=gpt_4o_mini,
|
| 75 |
verbose=True,
|
| 76 |
allow_delegation=False
|
| 77 |
)
|
|
|
|
| 81 |
goal=f'Assess the affordability of specific neighborhoods and ensure rent is within {budget}.',
|
| 82 |
backstory="You are a pragmatic financial advisor. You strictly filter out any options that exceed the user's budget.",
|
| 83 |
tools=[search_tool, calc_tool],
|
| 84 |
+
llm=gpt_4o_mini,
|
| 85 |
verbose=True,
|
| 86 |
allow_delegation=False
|
| 87 |
)
|
|
|
|
| 109 |
agents=[city_scout, budget_analyst],
|
| 110 |
tasks=[scout_task, analysis_task, report_task],
|
| 111 |
process=Process.sequential,
|
| 112 |
+
llm=gpt_4o_mini
|
| 113 |
)
|
| 114 |
|
| 115 |
return relocation_crew.kickoff()
|
| 116 |
|
| 117 |
# --- 7. BUTTON TRIGGER ---
|
| 118 |
if st.button("Start Research", type="primary"):
|
| 119 |
+
with st.spinner('AI Agents are scouting neighborhoods and crunching numbers...'):
|
| 120 |
try:
|
| 121 |
result = run_crew_analysis(city, work_location, budget, preferences)
|
| 122 |
|
| 123 |
# Display Results
|
| 124 |
st.success("Research Complete!")
|
| 125 |
+
st.markdown("### Your Relocation Plan")
|
| 126 |
st.markdown(result)
|
| 127 |
|
| 128 |
except Exception as e:
|