Spaces:
Runtime error
Runtime error
#!/usr/bin/env python3 | |
""" | |
Hugging Face Spaces deployment for Residential Architecture Assistant | |
Standalone version that avoids all LangGraph imports at startup | |
""" | |
import os | |
import gradio as gr | |
def create_interface(): | |
"""Create interface that works reliably in HF Spaces""" | |
def handle_chat(message, history, api_key, user_id=""): | |
if not api_key or not api_key.strip(): | |
error_msg = "β Please provide your OpenAI API key to start using the Architecture Assistant." | |
history.append([message, error_msg]) | |
return history, "" | |
if not message.strip(): | |
return history, "" | |
try: | |
# Only import when actually needed to avoid schema issues | |
from graph import ArchitectureAssistant | |
# Create assistant instance | |
assistant = ArchitectureAssistant( | |
openai_api_key=api_key.strip(), | |
user_id=user_id.strip() if user_id.strip() else f"hf_user_{len(history)}" | |
) | |
# Get response | |
response = assistant.chat(message) | |
history.append([message, response]) | |
except ImportError as e: | |
error_msg = f"""β **System modules not available**: {str(e)} | |
**This should be the Residential Architecture Assistant** with: | |
- ποΈ Architecture design guidance | |
- π° Montreal market cost analysis | |
- π Professional floorplan generation | |
- π Building codes & permit requirements | |
The system modules couldn't be loaded in this environment. Please try again or contact support.""" | |
history.append([message, error_msg]) | |
except Exception as e: | |
error_msg = f"""β **Error**: {str(e)} | |
**Troubleshooting:** | |
1. **Check your OpenAI API key** - Ensure it's valid and has credits | |
2. **Try a simpler question** - Start with basic architecture questions | |
3. **Wait and retry** - There might be temporary connectivity issues | |
**Expected functionality:** | |
- Multi-agent architecture consultation | |
- Montreal-specific building analysis | |
- Professional floorplan generation | |
- Building code guidance | |
Please try again or contact support if issues persist.""" | |
history.append([message, error_msg]) | |
return history, "" | |
with gr.Blocks(title="π Architecture Assistant") as interface: | |
gr.HTML(""" | |
<div style="text-align: center; padding: 20px;"> | |
<h1>π Residential Architecture Assistant</h1> | |
<p><strong>Multi-Agent LangGraph System for Professional Architecture Consultation</strong></p> | |
<p>β¨ 7 AI Specialists | π Professional Floorplans | π° Montreal Market Analysis | π Building Codes</p> | |
</div> | |
""") | |
# API Key input | |
api_key = gr.Textbox( | |
label="π OpenAI API Key", | |
type="password", | |
placeholder="Enter your OpenAI API key (sk-...)", | |
info="Required for AI functionality. Not stored or logged." | |
) | |
# Optional User ID | |
user_id = gr.Textbox( | |
label="π€ User ID (Optional)", | |
placeholder="Enter a unique ID to save your conversation (e.g., 'john_house_project')", | |
info="Leave blank for anonymous session" | |
) | |
# Chat interface | |
chatbot = gr.Chatbot( | |
label="π¬ Architecture Consultation", | |
height=400 | |
) | |
msg = gr.Textbox( | |
label="Your Message", | |
placeholder="Ask about home design, budgets, floorplans, Montreal building codes...", | |
lines=2 | |
) | |
with gr.Row(): | |
send_btn = gr.Button("π€ Send", variant="primary") | |
clear_btn = gr.Button("π Clear", variant="secondary") | |
gr.HTML(""" | |
<div style="background: #f8f9fa; padding: 15px; margin: 20px 0; border-radius: 8px;"> | |
<h4>π‘ Example Questions:</h4> | |
<ul style="margin: 10px 0;"> | |
<li>"I want to design a home but don't know where to start"</li> | |
<li>"I have a $800,000 budget for Montreal - is that realistic?"</li> | |
<li>"We're a family of 4, what size house do we need?"</li> | |
<li>"Can you generate a floorplan for a 2500 sq ft house?"</li> | |
<li>"What building permits do I need in Montreal?"</li> | |
</ul> | |
<h4>π€ Our AI Specialists:</h4> | |
<ul style="margin: 10px 0;"> | |
<li><strong>RouterAgent:</strong> Intelligent conversation routing</li> | |
<li><strong>GeneralDesignAgent:</strong> Architecture principles & design guidance</li> | |
<li><strong>BudgetAnalysisAgent:</strong> Montreal market cost analysis</li> | |
<li><strong>FloorplanAgent:</strong> Spatial planning & requirements</li> | |
<li><strong>FloorplanGeneratorAgent:</strong> Detailed architectural specifications</li> | |
<li><strong>DetailedBudgetAgent:</strong> Comprehensive cost breakdowns</li> | |
<li><strong>RegulationAgent:</strong> Montreal building codes & permits</li> | |
</ul> | |
</div> | |
""") | |
# Event handlers | |
msg.submit(handle_chat, [msg, chatbot, api_key, user_id], [chatbot, msg]) | |
send_btn.click(handle_chat, [msg, chatbot, api_key, user_id], [chatbot, msg]) | |
clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg]) | |
gr.HTML(""" | |
<div style="text-align: center; padding: 20px; border-top: 1px solid #ddd; margin-top: 20px;"> | |
<p><strong>π Residential Architecture Assistant v3.0</strong></p> | |
<p>Built with <a href="https://langchain.ai/langgraph">LangGraph</a> β’ | |
Powered by <a href="https://openai.com">OpenAI</a> β’ | |
Interface by <a href="https://gradio.app">Gradio</a></p> | |
<p><em>Professional architecture consultation from concept to construction</em></p> | |
</div> | |
""") | |
return interface | |
if __name__ == "__main__": | |
demo = create_interface() | |
demo.launch() |