Hugging Face & DeepSeek: The AI Power Duo

Using Hugging Face Spaces with DeepSeek: A Practical Alternative

Direct API integration isn’t always the smoothest path when working with large language models. Recently, I encountered some challenges with DeepSeek’s upload functionality, which led me to discover a more robust solution using Hugging Face Spaces and their new inference provider API. Here’s my journey and what I learned along the way. 

The Challenge

While trying to implement direct file uploads to DeepSeek, I ran into some issues — possibly due to rate limiting or other technical constraints. Instead of getting stuck or implementing complex retry logic, this presented an opportunity to explore alternative approaches.

Hugging Face Spaces

Hugging Face Spaces turned out to be an elegant solution, offering several advantages:

  • Streamlit Integration: The implementation uses Streamlit to create a user-friendly interface with straightforward file upload capabilities and chat functionality.
  • Robust File Handling: The code includes proper file processing with error handling:
def process_file(file) -> str:
    if the file is None:
        return ""
   
    try:
        content = file.getvalue().decode('utf-8')
        return content
    except Exception as e:
        return f"Error reading file: {str(e)}"

Inference Providers API

Hugging Face’s new inference providers API allows seamless integration with multiple AI providers, including DeepSeek. The code demonstrates this with the following:

client = InferenceClient(
    provider="together",
    api_key=API_KEY
)

Key Features of the Implementation

Streamlined Chat Interface

The implementation includes a complete chat interface with:

  • Message history management
  • Stream-based responses for better UX
  • File upload support
  • Customizable system messages

Configuration Options

Users can adjust various parameters through the sidebar:

  • System message customization
  • Maximum token limit
  • Temperature settings
  • Top-p sampling

Error Handling

The code includes robust error handling for file processing and API interactions, ensuring a smooth user experience even when things go wrong.

Benefits of This Approach

  • Simplified Deployment: Hugging Face Spaces provides a ready-to-use infrastructure for deploying ML applications.
  • Provider Flexibility: The inference providers API allows easy switching between AI providers without changing the core application code.
  • Built-in UI: Streamlit provides a clean, functional interface with minimal coding required.
  • Cost Control: Hugging Face’s infrastructure can help manage costs and provide better monitoring.

Technical Implementation Highlights:

The core of the implementation revolves around the generate_response function:

def generate_response(
    message: str,
    history: list[tuple[str, str]],
    system_message: str,
    max_tokens: int,
    temperature: float,
    top_p: float,
    file=None
) -> Iterator[str]:
    client = get_client()
   
    # Process file if uploaded
    file_content = process_file(file) if file else ""
   
    # Combine file content with user message if present
    if file_content:
        message = f"File content:\n{file_content}\n\nUser message:\n{message}" 

This function handles:

  • File content processing
  • Message history management
  • Streaming responses
  • Parameter customization

element61 perspective

While direct API integration might seem like the obvious path, leveraging Hugging Face Spaces with their inference providers’ API proved to be a game-changer. Not only did it resolve immediate file upload challenges, but it also laid a stronger, more flexible foundation for future AI development.

By combining Streamlit’s intuitive UI, Hugging Face’s powerful infrastructure, and cutting-edge inference provider APIs, businesses can build AI-driven applications faster and more efficiently than ever. Whether you're rapidly prototyping or scaling to production, this approach offers a smarter, more future-proof alternative to traditional API integration.

The Generative AI landscape is evolving at lightning speed, with innovations like DeepSeek reshaping how we think about inference and model computing. In such a fast-moving industry, staying ahead isn’t optional—it’s essential. That’s why at element61, we continuously explore, test, and develop the latest AI capabilities, ensuring our clients stay ahead of the curve.

Ready to future-proof your AI strategy? Let’s build something incredible together. 🚀

Start developing yourself

To try this yourself:

  1. Create a new Space on the Hugging Face.
  2. Set up your API keys in the Space’s secrets.
  3. Deploy the provided code 
  4. Start chatting with your documents!

Remember to check out Hugging Face’s documentation on inference providers for the latest updates and features.

If you want to try it yourself, go to my space: DeepSeek Chat with File Upload.

More information

For more information, feel free to contact us!