I built a simple sentiment analysis app that utilizes the Hugging Face Inference API provided by the Gradio client on Hugging Face. The code for the API is running on Hugging Face Spaces, and it offers an API endpoint provided by the Gradio client. I've used the Streamlit library to create a straightforward front end for this app. You can explore the backend code here, which leverages the Hugging Face Transformers library and pipelines to implement text sentiment analysis with just a few lines of code. The code for the front end is hosted in this repository. If you want to run it on your local machine, start by installing the dependencies listed in the requirements.txt file and running app.py. I've also provided an example of using my API endpoint for your own projects, which you can find in the example.py file.
This project started with a question: "How can I run, train, and host AI and machine learning models for free, as I don't have a powerful enough GPU?" Google Colab was not supporting certain AI models like Stable Diffusion on their GPUs. While doing research, I discovered Hugging Face Spaces and their Transformers and Diffusers libraries, which essentially allow you to use AI models without worrying about the underlying details. This was exactly what I needed! I watched a quick 15-minute crash course video on Hugging Face libraries here and built a simple sentiment analysis app. However, there was one more problem: "How do I use these models in my own applications?" I solved this problem almost by coincidence. 😂 This image and screenshot you see in the README provided the example code for using the API, and that convinced me that I could reliably use this API endpoint in my own applications.
Additionally, I watched some code challenge videos by Nicholas Renotte where I was introduced to the Streamlit library, a Python frontend framework. Streamlit allows you to easily build and host user interfaces for machine learning and AI applications. So, I used the Python template for the API along with the Streamlit library to build this app. I utilized a textarea for user input, which I then passed to the API model according to the provided example. I added a function to extract and display only the label and score from the output string. In the end, I hosted this project on Streamlit itself since it offered free hosting.
In the future, I plan to use what I've learned from this project to build my own image generation app using my fine-tuned Stable Diffusion model. This app will generate pictures with specific themes, depending on the dataset of pictures I use. I'll also employ another AI model to classify all these images because, well, I'm a bit lazy! 😂 I'd also like to explore technologies like ControlNet and others to achieve better results.