| --- |
| title: Advanced Sentiment Classifier |
| emoji: ๐ |
| colorFrom: green |
| colorTo: green |
| sdk: gradio |
| sdk_version: 4.36.1 |
| app_file: app.py |
| pinned: false |
| license: apache-2.0 |
| --- |
| |
| # Sentiment Analyzer |
|
|
| This project provides a web-based tool to analyze the sentiment of reviews using a pre-trained model from Hugging Face's `transformers` library. Users can upload a text file containing reviews, and the tool will display a DataFrame with the sentiment analysis results and a pie chart visualizing the sentiment distribution. |
|
|
| ## Features |
|
|
| - **Sentiment Analysis**: Uses a pre-trained model to classify the sentiment of each review as positive or negative. |
| - **File Upload**: Allows users to upload a text file containing reviews. |
| - **DataFrame Display**: Shows a DataFrame with the original reviews, sentiment labels, and confidence scores. |
| - **Pie Chart Visualization**: Displays a pie chart showing the percentage of positive and negative reviews. |
|
|
| ## Requirements |
|
|
| - Python 3.6 or higher |
| - `transformers` library |
| - `pandas` library |
| - `matplotlib` library |
| - `gradio` library |
|
|
| ## Installation |
|
|
| 1. Clone the repository: |
| ```bash |
| git clone https://github.com/your-username/sentiment-analyzer.git |
| cd sentiment-analyzer |
| ``` |
| |
| 2. Install the required libraries: |
| ```bash |
| pip install transformers pandas matplotlib gradio |
| ``` |
| |
| ## Usage |
|
|
| 1. Save your reviews in a text file named `reviews.txt`, with each review on a new line. |
|
|
| 2. Run the script: |
| ```bash |
| python app.py |
| ``` |
| |
| 3. Open your web browser and go to the local server URL provided by Gradio (usually `http://127.0.0.1:7860`). |
|
|
| 4. Upload the `reviews.txt` file. |
|
|
| 5. The tool will display a DataFrame with the sentiment analysis results and a pie chart visualizing the sentiment distribution. |
|
|
| ## Code Explanation |
|
|
| - **read_reviews_to_dataframe**: Reads reviews from a text file and converts them into a pandas DataFrame. |
| - **analyzer**: Applies sentiment analysis to a given text and returns the label and score. |
| - **evaluate_reviews**: Adds sentiment evaluation to each review in the DataFrame and splits it into 'Sentiment' and 'Score' columns. |
| - **create_pie_chart**: Creates and saves a pie chart showing the distribution of sentiments. |
| - **process_reviews**: Processes the uploaded file, evaluates reviews, and generates the pie chart. |
| - **gradio_interface**: Function for Gradio interface that processes the file and returns the DataFrame and chart path. |
|
|
| ## Example |
|
|
| 1. Prepare a text file `reviews.txt` with the following content: |
| ``` |
| The product quality is excellent! |
| The battery life is awful. |
| Very satisfied with the purchase. |
| Will not buy again, very disappointed. |
| ``` |
| |
| 2. Run the script: |
| ```bash |
| python app.py |
| ``` |
| |
| 3. Upload `reviews.txt` via the Gradio interface. |
|
|
| 4. View the DataFrame and pie chart displaying the sentiment analysis results. |
|
|
| ## License |
|
|
| This project is licensed under the MIT License. |
|
|
| ## Author |
|
|
| This tool was created by **Walid Ahmed**. |
|
|