Skip to content

Amr/gradio live deployment #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

Am64r
Copy link
Collaborator

@Am64r Am64r commented Apr 3, 2025

Pull Request: Enable Gradio Deployment on Hugging Face Spaces

Task Description

The goal was to deploy the Gradio UI application (gradio_ui.py), which uses Poetry for local dependency management, to Hugging Face Spaces using the gradio deploy command. Initial deployment attempts failed due to a series of issues:

  1. Dependency Mismatch: The deployment process defaults to using requirements.txt and pip, leading to ModuleNotFoundError errors as it didn't recognize dependencies specified only in pyproject.toml.
  2. Python Version Conflict: The default Hugging Face Spaces environment used Python 3.10, while the project requires Python 3.11+, causing incompatibility issues with dependencies specified using environment markers in the generated requirements.txt.
  3. Missing Files: Essential project files, specifically the character definition JSON files (e.g., characters/hyperbolic.json), were not included in the deployment image because they weren't tracked by Git, resulting in FileNotFoundError during agent initialization.
  4. Secrets Handling: Securely providing API keys (like CDP_API_KEY_PRIVATE) required moving away from local .env files or hardcoding, necessitating the use of Hugging Face Spaces secrets.
  5. Gradio Startup Errors: The application crashed during startup on Spaces due to:
    • Automatic execution of examples provided to gr.ChatInterface, which triggered agent logic unexpectedly.
    • ValidationError caused by Gradio attempting to process tool call outputs (formatted with metadata) as FileMessage objects during example caching.
  6. Gradio Runtime Errors: After fixing the startup crash, a ValueError: too many values to unpack occurred because the history processing loop in chat_with_agent incorrectly assumed a paired structure for the message history provided by gr.ChatInterface(type="messages").

Proposal

This PR implements the necessary configurations and code changes to address the deployment issues and ensure stable operation on Hugging Face Spaces:

  1. Dependency Management:

    • A requirements.txt file was generated using poetry export --without-hashes -f requirements.txt -o requirements.txt to provide a pip-compatible list of all project dependencies defined in pyproject.toml.
    • The separate requirements-build.txt file was deemed unnecessary and potentially confusing for this deployment method.
  2. Python Version Configuration:

    • Added a YAML metadata block to the top of README.md specifying python_version: 3.12 (or 3.11/compatible version) to instruct Hugging Face Spaces to use the correct Python runtime environment during build and execution.
  3. File Inclusion:

    • Ensured that the characters/ directory and its contents (e.g., hyperbolic.json) are tracked by Git, making them part of the deployed application image.
  4. Secrets Management:

    • Configured all required secrets (API keys, tokens) using the "Repository secrets" feature within the Hugging Face Space settings. The application now reads these secrets via environment variables (os.getenv) at runtime in the deployed environment.
  5. Gradio UI & Runtime Fixes (gradio_ui.py):

    • Commented out or removed the examples list argument from the gr.ChatInterface definition to prevent automatic execution and caching during application startup.
    • Modified the chat_with_agent function's history processing loop to correctly iterate over the flat list of message dictionaries provided by gr.ChatInterface when type="messages".
    • Updated chat_with_agent to format tool call outputs as part of the message content string (using Markdown) instead of using the metadata dictionary, resolving the Pydantic ValidationError.
    • Removed the timestamp-based checkpoint_id from the RunnableConfig in chat_with_agent to rely solely on thread_id and checkpoint_ns for state management within LangGraph.

Test Plan

The success of these changes was verified through the following steps:

  1. Executing gradio deploy without build errors related to dependencies or Python versions.
  2. Confirming the application starts successfully on Hugging Face Spaces by checking the runtime logs (no ModuleNotFoundError, FileNotFoundError, ValidationError, or ValueError).
  3. Accessing the public URL of the deployed Hugging Face Space.
  4. Observing that the application loads without executing any example prompts automatically.
  5. Sending various test messages (greetings, questions requiring tool use like checking balances or asking about GPUs) to the deployed Gradio chat interface.
  6. Verifying that the agent responds correctly, processes history across multiple turns, displays formatted tool call outputs within the chat, and does not crash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant