![](https://crypto4nerd.com/wp-content/uploads/2023/05/1Nny5hUWPPDxiEvg-8dvY4g-1024x526.png)
For this use case, we will create a simple sentiment analysis application using HuggingFace Transformers and Streamlit. Follow the steps below:
Step 1: Install the Required Libraries
In your Space, click on files and add a new file called requirements.txt
and add the following libraries:
streamlit
transformers
torch
These libraries will be automatically installed in the environment when your Space is launched.
Step 2: Write Your Application Code
Edit the app.py
file and replace the default code with the following:
import streamlit as st
from transformers import pipelinesentiment_pipeline = pipeline("sentiment-analysis")
st.title("Sentiment Analysis with HuggingFace Spaces")
st.write("Enter a sentence to analyze its sentiment:")
user_input = st.text_input("")
if user_input:
result = sentiment_pipeline(user_input)
sentiment = result[0]["label"]
confidence = result[0]["score"]
st.write(f"Sentiment: {sentiment}")
st.write(f"Confidence: {confidence:.2f}")
Step 3: Save Your Changes
Click on the “Save changes” button in the top-right corner of the screen. Your application will automatically reload and display the Sentiment Analysis interface.
Step 4: Interact with Your Application
With your Sentiment Analysis application live, you can now interact with it:
- Type a sentence into the text input field.
- Watch as your application analyzes the sentiment and displays the result.
Feel free to experiment with different sentences and observe the sentiment analysis results.