Spaces:
No application file
No application file
Download Core/app.py from CastroForGeorgia/Mantle: direct link, hf CLI and curl.
- Browser
- Download file 6.73 kB
-
https://huggingface.co/spaces/CastroForGeorgia/Mantle/resolve/main/Core/app.py
- Command line
-
hf download hf://spaces/CastroForGeorgia/Mantle/Core/app.py
-
curl -L -o app.py https://huggingface.co/spaces/CastroForGeorgia/Mantle/resolve/main/Core/app.py
6.73 kB
| import json | |
| import os | |
| import logging | |
| import chainlit as cl | |
| from autogen_agentchat.base import TaskResult | |
| from core.teams.teams import get_or_create_team | |
| # Set up logging | |
| logging.basicConfig(level=logging.DEBUG) | |
| logger = logging.getLogger(__name__) | |
| # Load translations | |
| current_dir = os.path.dirname(os.path.abspath(__file__)) | |
| translations_dir = os.path.join(current_dir, ".chainlit", "translations") | |
| translations_file = os.path.join(translations_dir, "en-US.json") | |
| if os.path.exists(translations_file): | |
| with open(translations_file, "r", encoding="utf-8") as f: | |
| translations = json.load(f) | |
| logger.debug("Translations loaded successfully.") | |
| else: | |
| translations = {} | |
| logger.warning("Translations file not found. Using default texts.") | |
| # Translation helper | |
| def t(key: str, default: str = "") -> str: | |
| keys = key.split(".") | |
| value = translations | |
| for k in keys: | |
| value = value.get(k, {}) | |
| if not isinstance(value, dict): | |
| break | |
| return value if isinstance(value, str) else default | |
| # Handle user messages | |
| async def on_message(message: cl.Message): | |
| logger.info(f"Received message from user: {message.content}") | |
| try: | |
| # Get or create the agent team | |
| team = await get_or_create_team() | |
| # Run the team stream for the provided message content | |
| response_stream = team.run_stream(task=message.content) | |
| async for msg in response_stream: | |
| logger.debug(f"Stream message received: {msg}") | |
| # Check if the message is a task result (termination condition) | |
| if isinstance(msg, TaskResult): | |
| await cl.Message( | |
| content=t("messages.termination", "The task is complete."), | |
| author="Agent Team", | |
| ).send() | |
| # Reset team to None when task completes | |
| cl.user_session.set("team", None) | |
| break | |
| # Handle regular messages in the stream | |
| if hasattr(msg, "content") and msg.content: | |
| source = getattr( | |
| msg, "source", "unknown" | |
| ) # Use 'source' to identify sender | |
| if source == "advisor_agent": | |
| logger.debug(f"Advisor Agent response: {msg.content}") | |
| await cl.Message( | |
| content=f"Thinking...", author="Advisor Agent" | |
| ).send() | |
| elif source in [ | |
| "housing_agent", | |
| "job_training_agent", | |
| "healthcare_agent", | |
| "language_access_agent", | |
| "civic_engagement_agent", | |
| "immigration_agent", | |
| ]: | |
| logger.debug( | |
| f"Specialized Agent ({source}) response: {msg.content}" | |
| ) | |
| await cl.Message(content=msg.content, author=source).send() | |
| elif source == "user_proxy": | |
| logger.debug(f"User proxy message: {msg.content}") | |
| # await cl.Message(content=msg.content, author="User").send() | |
| elif source == "TextMentionTermination": | |
| # Handle termination message | |
| logger.debug(f"Termination message received: {msg.content}") | |
| await cl.Message( | |
| content=t( | |
| "messages.termination_mentioned", | |
| "The task was terminated based on the conversation content.", | |
| ), | |
| author="System", | |
| ).send() | |
| cl.user_session.set("team", None) | |
| break | |
| else: | |
| logger.warning(f"Unhandled source type: {source}") | |
| else: | |
| logger.warning( | |
| "Message in stream has no content or unsupported structure." | |
| ) | |
| except Exception as e: | |
| logger.error(f"Error in on_message: {e}") | |
| await cl.Message( | |
| content=t("errors.unexpected", "An unexpected error occurred."), | |
| author="System", | |
| ).send() | |
| # Start session handlers | |
| async def on_chat_start(): | |
| logger.info("Chat session started.") | |
| async def on_stop(): | |
| cl.user_session.set("team", None) | |
| await cl.Message( | |
| content=t("messages.stopped", "The task has been stopped."), | |
| author="System", | |
| ).send() | |
| async def on_chat_end(): | |
| cl.user_session.set("team", None) | |
| logger.info("Chat session ended.") | |
| async def on_chat_resume(): | |
| await cl.Message( | |
| content=t( | |
| "messages.welcome_back", "Welcome back! How can I assist you further?" | |
| ), | |
| author="Assistant", | |
| ).send() | |
| # Define starter messages | |
| async def set_starters(): | |
| """Defines the starter messages available to users when initiating a chat.""" | |
| return [ | |
| cl.Starter( | |
| label=t("starters.affordable_housing.label", "Affordable Housing"), | |
| message=t("starters.affordable_housing.message", "Get help with housing."), | |
| icon="/public/housing-policy-header.jpeg", | |
| ), | |
| cl.Starter( | |
| label=t("starters.job_training.label", "Job Training"), | |
| message=t("starters.job_training.message", "Find job training resources."), | |
| icon="/public/economy-policy-header.jpeg", | |
| ), | |
| cl.Starter( | |
| label=t("starters.healthcare_access.label", "Healthcare Access"), | |
| message=t( | |
| "starters.healthcare_access.message", "Access healthcare services." | |
| ), | |
| icon="/public/medicare-for-all-header.jpeg", | |
| ), | |
| cl.Starter( | |
| label=t("starters.language_access.label", "Language Access"), | |
| message=t( | |
| "starters.language_access.message", "Language assistance services." | |
| ), | |
| icon="/public/immigration-policy-header.jpeg", | |
| ), | |
| cl.Starter( | |
| label=t("starters.immigration_access.label", "Immigration Access"), | |
| message=t( | |
| "starters.immigration_access.message", | |
| "Immigration assistance services.", | |
| ), | |
| icon="/public/immigration-policy-header.jpeg", | |
| ), | |
| cl.Starter( | |
| label=t("starters.civic_engagement.label", "Civic Engagement"), | |
| message=t( | |
| "starters.civic_engagement.message", "Get involved in your community." | |
| ), | |
| icon="/public/open-government-header.jpeg", | |
| ), | |
| ] | |