File size: 2,991 Bytes
08a585e
 
 
 
 
 
 
 
 
 
 
 
622a2c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
---
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**.