Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
from crewai import Agent, Task, Crew, Process
|
| 5 |
+
from crewai_tools import SerperDevTool, BaseTool
|
| 6 |
+
|
| 7 |
+
# 1. Page Configuration
|
| 8 |
+
st.set_page_config(page_title="Smart Relocation Assistant", page_icon="🏘️")
|
| 9 |
+
|
| 10 |
+
st.title("🏘️ Smart Relocation Assistant")
|
| 11 |
+
st.markdown("Use AI Agents to find your perfect neighborhood based on budget and lifestyle.")
|
| 12 |
+
|
| 13 |
+
# 2. Sidebar for API Keys (Optional but recommended for public spaces)
|
| 14 |
+
with st.sidebar:
|
| 15 |
+
st.header("API Configuration")
|
| 16 |
+
st.info("If running locally, set keys in .env. On Hugging Face, set them in 'Settings' -> 'Secrets'.")
|
| 17 |
+
|
| 18 |
+
# Check if keys are already in environment (HF Secrets)
|
| 19 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 20 |
+
serper_api_key = os.getenv("SERPER_API_KEY")
|
| 21 |
+
|
| 22 |
+
# If not found in env, ask user to input them
|
| 23 |
+
if not openai_api_key:
|
| 24 |
+
openai_api_key = st.text_input("OpenAI API Key", type="password")
|
| 25 |
+
if not serper_api_key:
|
| 26 |
+
serper_api_key = st.text_input("Serper API Key", type="password")
|
| 27 |
+
|
| 28 |
+
# 3. Define Tools
|
| 29 |
+
class CalculatorTool(BaseTool):
|
| 30 |
+
name: str = "Budget Calculator"
|
| 31 |
+
description: str = "Useful for calculating if a rent price fits within a budget."
|
| 32 |
+
|
| 33 |
+
def _run(self, expression: str) -> str:
|
| 34 |
+
try:
|
| 35 |
+
return str(eval(expression))
|
| 36 |
+
except:
|
| 37 |
+
return "Error in calculation"
|
| 38 |
+
|
| 39 |
+
# 4. Main Application Logic
|
| 40 |
+
if openai_api_key and serper_api_key:
|
| 41 |
+
# Set keys for the libraries
|
| 42 |
+
os.environ["OPENAI_API_KEY"] = openai_api_key
|
| 43 |
+
os.environ["SERPER_API_KEY"] = serper_api_key
|
| 44 |
+
|
| 45 |
+
# Initialize Tools
|
| 46 |
+
search_tool = SerperDevTool()
|
| 47 |
+
calc_tool = CalculatorTool()
|
| 48 |
+
|
| 49 |
+
# Inputs
|
| 50 |
+
col1, col2 = st.columns(2)
|
| 51 |
+
with col1:
|
| 52 |
+
city = st.text_input("Target City", "Austin, TX")
|
| 53 |
+
budget = st.number_input("Max Monthly Rent ($)", min_value=500, value=2500)
|
| 54 |
+
with col2:
|
| 55 |
+
work_location = st.text_input("Work Location", "Downtown")
|
| 56 |
+
|
| 57 |
+
preferences = st.text_area("Lifestyle Preferences", "Walkable, near coffee shops, safe, good for young professionals")
|
| 58 |
+
|
| 59 |
+
if st.button("Start Research"):
|
| 60 |
+
with st.spinner('🤖 AI Agents are scouting neighborhoods and crunching numbers...'):
|
| 61 |
+
try:
|
| 62 |
+
# --- AGENT DEFINITIONS ---
|
| 63 |
+
city_scout = Agent(
|
| 64 |
+
role='City Neighborhood Scout',
|
| 65 |
+
goal=f'Identify the top 3 neighborhoods in {city} that match: {preferences}.',
|
| 66 |
+
backstory="You are a local expert who knows every corner of the city. You prioritize safety, commute, and lifestyle fit.",
|
| 67 |
+
tools=[search_tool],
|
| 68 |
+
verbose=True,
|
| 69 |
+
allow_delegation=False
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
budget_analyst = Agent(
|
| 73 |
+
role='Relocation Financial Advisor',
|
| 74 |
+
goal=f'Assess affordability and ensure rent is within {budget}.',
|
| 75 |
+
backstory="You are a pragmatic financial advisor. You find average rent prices and filter out options that exceed the budget.",
|
| 76 |
+
tools=[search_tool, calc_tool],
|
| 77 |
+
verbose=True,
|
| 78 |
+
allow_delegation=False
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# --- TASK DEFINITIONS ---
|
| 82 |
+
scout_task = Task(
|
| 83 |
+
description=f"""
|
| 84 |
+
1. Search for neighborhoods in {city} that are: {preferences}.
|
| 85 |
+
2. Consider commute to {work_location}.
|
| 86 |
+
3. Select top 5 candidates.
|
| 87 |
+
""",
|
| 88 |
+
expected_output="List of 5 neighborhoods with vibe descriptions.",
|
| 89 |
+
agent=city_scout
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
analysis_task = Task(
|
| 93 |
+
description=f"""
|
| 94 |
+
1. For the neighborhoods found, search current average rent for 1-bedroom.
|
| 95 |
+
2. Filter out any where rent > {budget}.
|
| 96 |
+
3. Calculate remaining budget.
|
| 97 |
+
""",
|
| 98 |
+
expected_output="Filtered list of affordable neighborhoods with costs.",
|
| 99 |
+
agent=budget_analyst
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
report_task = Task(
|
| 103 |
+
description="""
|
| 104 |
+
Compile a final report. Rank top 3 affordable neighborhoods.
|
| 105 |
+
Include: Neighborhood Name, Vibe, Rent Cost, Remaining Budget, and one Local Highlight.
|
| 106 |
+
Format as clear Markdown.
|
| 107 |
+
""",
|
| 108 |
+
expected_output="Markdown formatted relocation guide.",
|
| 109 |
+
agent=budget_analyst
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# --- CREW EXECUTION ---
|
| 113 |
+
relocation_crew = Crew(
|
| 114 |
+
agents=[city_scout, budget_analyst],
|
| 115 |
+
tasks=[scout_task, analysis_task, report_task],
|
| 116 |
+
process=Process.sequential
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
result = relocation_crew.kickoff()
|
| 120 |
+
|
| 121 |
+
# Display Results
|
| 122 |
+
st.success("Research Complete!")
|
| 123 |
+
st.markdown("### 📋 Your Relocation Plan")
|
| 124 |
+
st.markdown(result)
|
| 125 |
+
|
| 126 |
+
except Exception as e:
|
| 127 |
+
st.error(f"An error occurred: {e}")
|
| 128 |
+
|
| 129 |
+
else:
|
| 130 |
+
st.warning("Please provide your API Keys to proceed.")
|