How to deploy a Streamlit Application on Hugging Face
In this guide, we'll walk through the steps to deploy a Streamlit app using the Hugging Face platform. For demonstration purposes, we'll create an app that utilizes the Python module Pix2Tex. Users will be able to upload an image and get the corresponding LaTeX formula along with a rendered version.
Prerequisites¶
- Create a virtual environment:
-
Install dependencies:
Creating the Streamlit App¶
-
Create the
app.pyfile with your Streamlit app code.# app.py import streamlit as st from PIL import Image from pix2tex.cli import LatexOCR def main(): st.title("Image to LaTeX Formula Parser") # Upload image through Streamlit uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) if uploaded_image is not None: # Display the uploaded image st.image(uploaded_image, caption="Uploaded Image", use_column_width=True) # Perform LaTeX OCR on the uploaded image latex_formula = process_image(uploaded_image) # Display LaTeX formula st.subheader("LaTeX Formula:") st.text(latex_formula) # Display parsed Markdown parsed_md = parse_to_md(latex_formula) st.subheader("Parsed Markdown:") st.latex(f"\n{latex_formula}\n") def process_image(image): # Perform LaTeX OCR on the image img = Image.open(image) model = LatexOCR() latex_formula = model(img) return latex_formula def parse_to_md(latex_formula): # You can implement your own logic to parse LaTeX to Markdown # Here's a simple example for demonstration purposes parsed_md = f"**Parsed Formula:** *{latex_formula}*" return parsed_md if __name__ == "__main__": main() -
Test the app locally:
Deploying on Hugging Face¶
-
Create a new Hugging Face space:
- Visit Hugging Face Spaces.
- Name your repository and choose the Streamlit option.

-
Create a Hugging Face token:
- Visit Hugging Face Token Settings.
- Create a new token. the format is
hf_****

-
Clone the repository and push your app:
-
clone the repo
the token is from step 6 - add
app.pyandrequirements.txtinto the folder - push the changes to huggingface```bash git add . git commit -m "Add application" git push ```-
example
-
-
Check your deployed application online:
- Visit Hugging Face Repository
https://huggingface.co/<user_name>/<repo_name>.

- Visit Hugging Face Repository
Congratulations! Your Streamlit app is now deployed on Hugging Face. You can find a live example on https://huggingface.co/spaces/hermann-web/pix2tex.