Spaces:
Runtime error
Runtime error
- __pycache__/app.cpython-311.pyc +0 -0
- app.py +50 -33
__pycache__/app.cpython-311.pyc
CHANGED
Binary files a/__pycache__/app.cpython-311.pyc and b/__pycache__/app.cpython-311.pyc differ
|
|
app.py
CHANGED
@@ -16,38 +16,48 @@ from datetime import datetime
|
|
16 |
class GradioArchitectureApp:
|
17 |
def __init__(self):
|
18 |
load_dotenv()
|
19 |
-
|
20 |
-
|
21 |
-
raise ValueError("Please set OPENAI_API_KEY in .env file")
|
22 |
|
|
|
23 |
self.api_key = api_key
|
24 |
self.assistant = None
|
25 |
self.current_user_id = None
|
26 |
self.conversation_history = []
|
27 |
self.state_manager = user_state_manager
|
28 |
|
29 |
-
def initialize_user_session(self, user_identifier: str = None):
|
30 |
"""Initialize user session with persistent state"""
|
31 |
if not user_identifier:
|
32 |
# Generate user ID from timestamp for demo
|
33 |
user_identifier = hashlib.md5(str(datetime.now()).encode()).hexdigest()[:12]
|
34 |
|
|
|
|
|
|
|
|
|
|
|
35 |
self.current_user_id = user_identifier
|
36 |
-
self.assistant = ArchitectureAssistant(
|
37 |
|
38 |
return user_identifier
|
39 |
|
40 |
-
def change_user_id(self, new_user_id: str):
|
41 |
"""Change user ID and auto-load their conversation history"""
|
42 |
if not new_user_id.strip():
|
43 |
return [], "β Please enter a valid User ID", "", "Start chatting to see your requirements here!"
|
44 |
|
45 |
try:
|
|
|
|
|
|
|
|
|
|
|
46 |
# Set new user ID
|
47 |
self.current_user_id = new_user_id.strip()
|
48 |
|
49 |
# Create new assistant instance for this user
|
50 |
-
self.assistant = ArchitectureAssistant(
|
51 |
|
52 |
# Try to load their latest conversation
|
53 |
history = self.state_manager.get_user_history(self.current_user_id)
|
@@ -89,18 +99,25 @@ class GradioArchitectureApp:
|
|
89 |
except Exception as e:
|
90 |
return [], f"β Error loading user {new_user_id}: {str(e)}", "", "Error loading user"
|
91 |
|
92 |
-
def chat_with_assistant(self, message, history, user_id_input=""):
|
93 |
"""Process user message and return response with state saving"""
|
94 |
if not message.strip():
|
95 |
return history, "", user_id_input, self.get_conversation_summary()
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
# Ensure we have an assistant instance
|
98 |
if not self.assistant:
|
99 |
if user_id_input.strip():
|
100 |
self.current_user_id = user_id_input.strip()
|
101 |
-
self.assistant = ArchitectureAssistant(
|
102 |
else:
|
103 |
-
user_id = self.initialize_user_session()
|
104 |
return history, "", user_id, self.get_conversation_summary()
|
105 |
|
106 |
# Handle special commands
|
@@ -308,7 +325,15 @@ def create_gradio_interface():
|
|
308 |
</div>
|
309 |
""")
|
310 |
|
311 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
312 |
with gr.Row():
|
313 |
with gr.Column(scale=4):
|
314 |
user_id_input = gr.Textbox(
|
@@ -421,11 +446,11 @@ def create_gradio_interface():
|
|
421 |
)
|
422 |
|
423 |
# Event handlers
|
424 |
-
def send_message(message, history, user_id):
|
425 |
-
return app.chat_with_assistant(message, history, user_id)
|
426 |
|
427 |
-
def change_user(user_id):
|
428 |
-
return app.change_user_id(user_id)
|
429 |
|
430 |
def view_user_history(user_id):
|
431 |
return app.get_user_history_display(user_id)
|
@@ -434,10 +459,10 @@ def create_gradio_interface():
|
|
434 |
return app.get_all_users_summary()
|
435 |
|
436 |
# Wire up the events
|
437 |
-
load_user_btn.click(change_user, [user_id_input], [chatbot, status_display, user_id_input, summary_display])
|
438 |
|
439 |
-
msg.submit(send_message, [msg, chatbot, user_id_input], [chatbot, msg, user_id_input, summary_display])
|
440 |
-
send_btn.click(send_message, [msg, chatbot, user_id_input], [chatbot, msg, user_id_input, summary_display])
|
441 |
|
442 |
clear_btn.click(lambda: app.assistant.reset_conversation() if app.assistant else None, outputs=[chatbot, summary_display])
|
443 |
|
@@ -457,25 +482,17 @@ def create_gradio_interface():
|
|
457 |
|
458 |
|
459 |
def main():
|
460 |
-
"""Launch the
|
461 |
try:
|
462 |
interface = create_gradio_interface()
|
463 |
-
print("π Starting
|
464 |
print("π₯ Features: Multi-user support, auto-loading conversations, precise floorplans")
|
465 |
-
print("
|
466 |
-
print("
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
server_port=7861,
|
471 |
-
share=False,
|
472 |
-
show_error=True,
|
473 |
-
quiet=False
|
474 |
-
)
|
475 |
|
476 |
-
except ValueError as e:
|
477 |
-
print(f"β Configuration Error: {e}")
|
478 |
-
print("Please copy .env.example to .env and add your OpenAI API key")
|
479 |
except Exception as e:
|
480 |
print(f"β Error starting the application: {e}")
|
481 |
|
|
|
16 |
class GradioArchitectureApp:
|
17 |
def __init__(self):
|
18 |
load_dotenv()
|
19 |
+
# For Hugging Face Spaces deployment - get API key from environment
|
20 |
+
api_key = os.environ.get("OPENAI_API_KEY")
|
|
|
21 |
|
22 |
+
# Make API key optional for HF Spaces - users can provide their own
|
23 |
self.api_key = api_key
|
24 |
self.assistant = None
|
25 |
self.current_user_id = None
|
26 |
self.conversation_history = []
|
27 |
self.state_manager = user_state_manager
|
28 |
|
29 |
+
def initialize_user_session(self, user_identifier: str = None, api_key: str = None):
|
30 |
"""Initialize user session with persistent state"""
|
31 |
if not user_identifier:
|
32 |
# Generate user ID from timestamp for demo
|
33 |
user_identifier = hashlib.md5(str(datetime.now()).encode()).hexdigest()[:12]
|
34 |
|
35 |
+
# Use provided API key or fallback to environment
|
36 |
+
effective_api_key = api_key or self.api_key
|
37 |
+
if not effective_api_key:
|
38 |
+
raise ValueError("No API key provided")
|
39 |
+
|
40 |
self.current_user_id = user_identifier
|
41 |
+
self.assistant = ArchitectureAssistant(effective_api_key, user_identifier)
|
42 |
|
43 |
return user_identifier
|
44 |
|
45 |
+
def change_user_id(self, new_user_id: str, api_key: str = None):
|
46 |
"""Change user ID and auto-load their conversation history"""
|
47 |
if not new_user_id.strip():
|
48 |
return [], "β Please enter a valid User ID", "", "Start chatting to see your requirements here!"
|
49 |
|
50 |
try:
|
51 |
+
# Use provided API key or fallback to environment
|
52 |
+
effective_api_key = api_key or self.api_key
|
53 |
+
if not effective_api_key:
|
54 |
+
return [], "β Please provide your OpenAI API key", "", "API key required"
|
55 |
+
|
56 |
# Set new user ID
|
57 |
self.current_user_id = new_user_id.strip()
|
58 |
|
59 |
# Create new assistant instance for this user
|
60 |
+
self.assistant = ArchitectureAssistant(effective_api_key, self.current_user_id)
|
61 |
|
62 |
# Try to load their latest conversation
|
63 |
history = self.state_manager.get_user_history(self.current_user_id)
|
|
|
99 |
except Exception as e:
|
100 |
return [], f"β Error loading user {new_user_id}: {str(e)}", "", "Error loading user"
|
101 |
|
102 |
+
def chat_with_assistant(self, message, history, user_id_input="", api_key=""):
|
103 |
"""Process user message and return response with state saving"""
|
104 |
if not message.strip():
|
105 |
return history, "", user_id_input, self.get_conversation_summary()
|
106 |
|
107 |
+
# Check for API key first
|
108 |
+
effective_api_key = api_key or self.api_key
|
109 |
+
if not effective_api_key:
|
110 |
+
error_msg = "β Please provide your OpenAI API key to use the Architecture Assistant."
|
111 |
+
history.append([message, error_msg])
|
112 |
+
return history, "", user_id_input, "API key required"
|
113 |
+
|
114 |
# Ensure we have an assistant instance
|
115 |
if not self.assistant:
|
116 |
if user_id_input.strip():
|
117 |
self.current_user_id = user_id_input.strip()
|
118 |
+
self.assistant = ArchitectureAssistant(effective_api_key, self.current_user_id)
|
119 |
else:
|
120 |
+
user_id = self.initialize_user_session(api_key=effective_api_key)
|
121 |
return history, "", user_id, self.get_conversation_summary()
|
122 |
|
123 |
# Handle special commands
|
|
|
325 |
</div>
|
326 |
""")
|
327 |
|
328 |
+
# API Key input for Hugging Face Spaces
|
329 |
+
api_key_input = gr.Textbox(
|
330 |
+
label="π OpenAI API Key",
|
331 |
+
placeholder="Enter your OpenAI API key (sk-...)",
|
332 |
+
type="password",
|
333 |
+
info="Required for AI functionality. Not stored or logged."
|
334 |
+
)
|
335 |
+
|
336 |
+
# User ID input
|
337 |
with gr.Row():
|
338 |
with gr.Column(scale=4):
|
339 |
user_id_input = gr.Textbox(
|
|
|
446 |
)
|
447 |
|
448 |
# Event handlers
|
449 |
+
def send_message(message, history, user_id, api_key):
|
450 |
+
return app.chat_with_assistant(message, history, user_id, api_key)
|
451 |
|
452 |
+
def change_user(user_id, api_key):
|
453 |
+
return app.change_user_id(user_id, api_key)
|
454 |
|
455 |
def view_user_history(user_id):
|
456 |
return app.get_user_history_display(user_id)
|
|
|
459 |
return app.get_all_users_summary()
|
460 |
|
461 |
# Wire up the events
|
462 |
+
load_user_btn.click(change_user, [user_id_input, api_key_input], [chatbot, status_display, user_id_input, summary_display])
|
463 |
|
464 |
+
msg.submit(send_message, [msg, chatbot, user_id_input, api_key_input], [chatbot, msg, user_id_input, summary_display])
|
465 |
+
send_btn.click(send_message, [msg, chatbot, user_id_input, api_key_input], [chatbot, msg, user_id_input, summary_display])
|
466 |
|
467 |
clear_btn.click(lambda: app.assistant.reset_conversation() if app.assistant else None, outputs=[chatbot, summary_display])
|
468 |
|
|
|
482 |
|
483 |
|
484 |
def main():
|
485 |
+
"""Launch the enhanced Gradio interface for Hugging Face Spaces"""
|
486 |
try:
|
487 |
interface = create_gradio_interface()
|
488 |
+
print("π Starting Architecture Assistant for Hugging Face Spaces...")
|
489 |
print("π₯ Features: Multi-user support, auto-loading conversations, precise floorplans")
|
490 |
+
print("π Users provide their own OpenAI API key via the interface")
|
491 |
+
print("π Ready for Hugging Face Spaces deployment!")
|
492 |
+
|
493 |
+
# Simple launch for HF Spaces
|
494 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
495 |
|
|
|
|
|
|
|
496 |
except Exception as e:
|
497 |
print(f"β Error starting the application: {e}")
|
498 |
|