<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Alienimnida</title>
    <description>The latest articles on Forem by Alienimnida (@alienimnida).</description>
    <link>https://forem.com/alienimnida</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1008176%2F537cf845-7c8c-4c37-ab63-0d1e5678ec2b.gif</url>
      <title>Forem: Alienimnida</title>
      <link>https://forem.com/alienimnida</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/alienimnida"/>
    <language>en</language>
    <item>
      <title>Deploy YOLOv5 + FastAPI with Custom Weights on Hugging Face Spaces (Step-by-Step)</title>
      <dc:creator>Alienimnida</dc:creator>
      <pubDate>Sun, 20 Jul 2025 17:22:40 +0000</pubDate>
      <link>https://forem.com/alienimnida/deploy-yolov5-fastapi-with-custom-weights-on-hugging-face-spaces-step-by-step-31ob</link>
      <guid>https://forem.com/alienimnida/deploy-yolov5-fastapi-with-custom-weights-on-hugging-face-spaces-step-by-step-31ob</guid>
      <description>&lt;p&gt;Have you ever looked at impressive ML app demos and wondered, “How on earth do people even manage to deploy such heavy projects?” Yeah, I’ve been there too…&lt;/p&gt;

&lt;h2&gt;
  
  
  Why HF Spaces?
&lt;/h2&gt;

&lt;p&gt;When I set out to deploy my YOLOv5 + FastAPI app, I was simply looking for a straightforward platform where I could test and iterate quickly, without worrying about infrastructure or complex deployment pipelines. I tried platforms like Render, but their free tier couldn’t handle my large custom model weights. That’s when I came across Hugging Face Spaces. It turned out to be exactly what I needed — fast, free, and beginner-friendly. With support for Docker and generous limits (16 GB RAM, 30 min idle timeout), Spaces made deployment as easy as pushing code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Creating a Space
&lt;/h2&gt;

&lt;p&gt;Go to &lt;a href="https://huggingface.co/spaces" rel="noopener noreferrer"&gt;https://huggingface.co/spaces&lt;/a&gt;. Here you will see a lot of spaces created by others and you can also search for a space by category. Make sure to create a new account if you don't have one then click on &lt;strong&gt;New Space&lt;/strong&gt; button in the top right corner.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fly1si7pa7cvqe9uwo9ga.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fly1si7pa7cvqe9uwo9ga.png" alt="HF spaces" width="800" height="383"&gt;&lt;/a&gt;&lt;br&gt;
Once you click new space you'll be directed to a page like this-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fldn1m3rl18o5y1hqou8a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fldn1m3rl18o5y1hqou8a.png" alt="Create new space page" width="800" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Give a suitable name and description to your space and you can choose license too if your project uses any then you'll come across &lt;strong&gt;Select the Space SDK&lt;/strong&gt; option for my case I went with Docker as I already had a Dockerfile ready then I selected &lt;strong&gt;Blank&lt;/strong&gt; as Docker template as for the Space Hardware and leave the option for Space Hardware as it is. While using the free tier your space will be public.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm761lj3vhma8winka4ai.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm761lj3vhma8winka4ai.png" alt="Space Configurations" width="800" height="383"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Pushing your files to your HF space
&lt;/h2&gt;

&lt;p&gt;After creating your Space, Hugging Face provides clear step-by-step instructions to set it up — including sample templates like Dockerfile, requirements.txt, and app.py. While you can manually upload files through the web UI, using Git makes the process much smoother and keeps your workflow version-controlled.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqldj57zwv2b7e7zomghd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqldj57zwv2b7e7zomghd.png" alt="Get started with templates" width="800" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before pushing your code to Hugging Face Spaces, there are a few important details to get right to ensure your deployment works smoothly.&lt;/p&gt;

&lt;p&gt;First, your project directory must contain a README.md file — this isn’t just for documentation, it’s required by HF Spaces to properly render your app’s landing page. Include a short description of what your app does and any helpful usage notes. If your project doesn't contain a README.md file with the following details, you'll most probably run into &lt;strong&gt;Configuration Error&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---
title: "Your App Title"
emoji: 🚀
colorFrom: blue
colorTo: purple
sdk: docker
app_file: app.py
pinned: false
---

# Your App Title

A brief description of what your app does.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second, since Spaces run in a restricted environment, you won’t have write access to arbitrary directories like /.cache or /uploads. Always use the &lt;strong&gt;/tmp directory&lt;/strong&gt; for saving temporary files, such as image uploads or processed outputs.&lt;/p&gt;

&lt;p&gt;Third, avoid using torch.load() directly on models trained with YOLOv5 — it often fails when dependencies like custom modules are missing. Instead, prefer loading the model using torch.hub or the ultralytics.YOLO interface for better compatibility. Something like this-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', trust_repo=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, make sure your FastAPI app runs on port &lt;strong&gt;7860&lt;/strong&gt;. Hugging Face expects this specific port for Spaces to detect and serve your app correctly. You can do this by setting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uvicorn.run(app, host="0.0.0.0", port=7860)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Will all this changes you can start with pushing your code to your HF space. In my case, I already had the code in a local Git repository that was also connected to GitHub. I didn’t want to disturb my existing GitHub remote, so I added Hugging Face as a second remote to the same repository.&lt;/p&gt;

&lt;p&gt;Here’s how I did it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, initialize Git if your project isn’t already a Git repo:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Then add Hugging Face as a new remote (replace &lt;code&gt;&amp;lt;your-space-name&amp;gt;&lt;/code&gt; with your actual Space name):
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git remote add hf https://huggingface.co/spaces/&amp;lt;your-username&amp;gt;/&amp;lt;your-space-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now commit your files:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .
git commit -m "Initial commit for HF Space"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Push to Hugging Face:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git push hf main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before pushing your code, make sure to handle large files like best.pt properly. Hugging Face Spaces has a file size limit for regular Git pushes, so you'll either need to use Git LFS (Large File Storage) or manually upload the large files via the Hugging Face Space's GUI by clicking on the &lt;strong&gt;Contribute&lt;/strong&gt; button under the Files tab&lt;/p&gt;

&lt;p&gt;That’s it — once pushed, Hugging Face Spaces automatically starts building your app based on the Dockerfile and displays logs in real time.&lt;/p&gt;

&lt;p&gt;Once the deployment is successful, your FastAPI app will be accessible at:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://&amp;lt;username&amp;gt;-&amp;lt;space_name&amp;gt;.hf.space/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you found this blog helpful, feel free to leave a ❤️ and drop your thoughts or questions in the comments. I'd love to hear your feedback and help if you get stuck anywhere!&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>yolov5</category>
      <category>huggingface</category>
    </item>
  </channel>
</rss>
