teymoor commited on
Commit
ecab5c0
·
0 Parent(s):

Initial commit: Add sentiment analysis application files

Browse files
Files changed (6) hide show
  1. PROGRESS.md +54 -0
  2. README.md +13 -0
  3. app.py +49 -0
  4. poetry.lock +0 -0
  5. pyproject.toml +17 -0
  6. requirements.txt +81 -0
PROGRESS.md ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Project Progress Log
2
+
3
+ This document tracks the step-by-step process of creating and deploying the Simple Sentiment Analyzer application.
4
+
5
+ ## 1. Prerequisites & Initial Setup
6
+
7
+ - **Goal:** Build and deploy a sentiment analysis web application using Hugging Face.
8
+ - **Accounts:** A new Hugging Face account was created and verified via email. A GitHub account was already available.
9
+ - **Tools:** Confirmed that `python` and `git` were installed locally on the Ubuntu system.
10
+
11
+ ## 2. Local Project Initialization
12
+
13
+ - **Directory:** Created a project directory named `sentiment-app`.
14
+ - **Dependency Management:** Chose to use `Poetry` for modern Python package and environment management.
15
+ - **Poetry Setup:**
16
+ - Initialized the project with `poetry init`, creating the `pyproject.toml` configuration file.
17
+ - Added the required libraries (`transformers`, `torch`, and `gradio`) using the `poetry add` command. This automatically created a virtual environment and installed the dependencies.
18
+
19
+ ## 3. Application Development
20
+
21
+ - **Main Application (`app.py`):**
22
+ - Created the `app.py` file.
23
+ - Wrote the Python code to load a pre-trained sentiment analysis model using the `transformers` pipeline.
24
+ - Wrote a function `analyze_sentiment` to process user input and return the model's prediction.
25
+ - Built a web user interface using `Gradio`, creating a title, description, and a simple textbox input/output.
26
+
27
+ - **Deployment Dependencies (`requirements.txt`):**
28
+ - To ensure compatibility with Hugging Face Spaces, a `requirements.txt` file was generated from the Poetry environment using the `poetry export` command.
29
+
30
+ ## 4. Local Testing
31
+
32
+ - **Execution:** The application was run locally from the terminal using `poetry run python app.py`.
33
+ - **Verification:** The Gradio server started successfully. The application was accessed via a local URL (`http://127.0.0.1:7860`) in a web browser.
34
+ - **Functionality Check:** Tested the app with both positive and negative sentences to confirm it was working as expected.
35
+ - **Shutdown:** The local server was stopped gracefully using `Ctrl+C` in the terminal.
36
+
37
+ ## 5. Deployment to Hugging Face Spaces
38
+
39
+ - **Git Initialization:** The project was already a Git repository. New files (`app.py`, `pyproject.toml`, `poetry.lock`, `requirements.txt`) were staged with `git add` and committed with `git commit`.
40
+
41
+ - **Space Creation:**
42
+ - A new "Space" was created on the Hugging Face website.
43
+ - The Space was configured with the `Gradio` SDK.
44
+
45
+ - **Connecting Local to Remote:**
46
+ - The new Hugging Face Space's Git URL was added as a remote to the local repository under the name `huggingface` (`git remote add ...`).
47
+
48
+ - **First Push & Reconciliation:**
49
+ - The initial `git push huggingface main` failed due to the remote Space having its own initial commit (with a `README.md` file), causing "divergent histories".
50
+ - This was resolved by pulling the remote changes first using `git pull huggingface main --allow-unrelated-histories`. This merged the remote's `README.md` into the local project.
51
+
52
+ - **Documentation:**
53
+ - The default `README.md` was updated with a detailed description of the project, its technologies, and usage instructions.
54
+ - This `PROGRESS.md` file was created to document the project's entire lifecycle.
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Simple Sentiment Analyzer
3
+ emoji: 🔥
4
+ colorFrom: green
5
+ colorTo: gray
6
+ sdk: gradio
7
+ sdk_version: 5.38.0
8
+ app_file: app.py
9
+ pinned: false
10
+ short_description: A simple web app to analyze text sentiment (Positive/Negativ
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import the necessary libraries
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+
5
+ # Load the sentiment analysis pipeline
6
+ # This will download the model and tokenizer for the first time.
7
+ # The model is 'distilbert-base-uncased-finetuned-sst-2-english'.
8
+ # It's a lightweight and fast model fine-tuned for sentiment analysis.
9
+ print("Loading sentiment analysis pipeline...")
10
+ sentiment_pipeline = pipeline("sentiment-analysis")
11
+ print("Pipeline loaded successfully.")
12
+
13
+ # Define the function that will perform the sentiment analysis
14
+ def analyze_sentiment(text):
15
+ """
16
+ Analyzes the sentiment of a given text.
17
+
18
+ Args:
19
+ text (str): The input text from the user.
20
+
21
+ Returns:
22
+ str: A formatted string with the prediction and score.
23
+ """
24
+ if text:
25
+ # The pipeline returns a list of dictionaries.
26
+ # e.g., [{'label': 'POSITIVE', 'score': 0.999}]
27
+ result = sentiment_pipeline(text)[0]
28
+ label = result['label']
29
+ score = result['score']
30
+
31
+ # Format the output for better readability
32
+ return f"Sentiment: {label} (Score: {score:.4f})"
33
+ else:
34
+ return "Please enter some text to analyze."
35
+
36
+ # Create the Gradio interface for the application
37
+ iface = gr.Interface(
38
+ fn=analyze_sentiment,
39
+ inputs=gr.Textbox(lines=5, placeholder="Enter text here..."),
40
+ outputs="text",
41
+ title="Simple Sentiment Analyzer",
42
+ description="Enter any English text to find out if its sentiment is POSITIVE or NEGATIVE. Powered by Hugging Face Transformers.",
43
+ allow_flagging="never" # Disables the flagging feature
44
+ )
45
+
46
+ # Launch the web application
47
+ if __name__ == "__main__":
48
+ print("Launching Gradio interface...")
49
+ iface.launch()
poetry.lock ADDED
The diff for this file is too large to render. See raw diff
 
pyproject.toml ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [tool.poetry]
2
+ name = "sentiment-app"
3
+ version = "0.1.0"
4
+ description = ""
5
+ authors = ["Teymoor <teymoor.k@gmail.com>"]
6
+ readme = "README.md"
7
+
8
+ [tool.poetry.dependencies]
9
+ python = "^3.12"
10
+ transformers = "^4.53.2"
11
+ torch = "^2.7.1"
12
+ gradio = "^5.38.0"
13
+
14
+
15
+ [build-system]
16
+ requires = ["poetry-core"]
17
+ build-backend = "poetry.core.masonry.api"
requirements.txt ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==24.1.0 ; python_version >= "3.12" and python_version < "4.0"
2
+ annotated-types==0.7.0 ; python_version >= "3.12" and python_version < "4.0"
3
+ anyio==4.9.0 ; python_version >= "3.12" and python_version < "4.0"
4
+ audioop-lts==0.2.1 ; python_version >= "3.13" and python_version < "4.0"
5
+ brotli==1.1.0 ; python_version >= "3.12" and python_version < "4.0"
6
+ certifi==2025.7.14 ; python_version >= "3.12" and python_version < "4.0"
7
+ charset-normalizer==3.4.2 ; python_version >= "3.12" and python_version < "4.0"
8
+ click==8.2.1 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
9
+ colorama==0.4.6 ; python_version >= "3.12" and python_version < "4.0" and platform_system == "Windows"
10
+ fastapi==0.116.1 ; python_version >= "3.12" and python_version < "4.0"
11
+ ffmpy==0.6.0 ; python_version >= "3.12" and python_version < "4.0"
12
+ filelock==3.18.0 ; python_version >= "3.12" and python_version < "4.0"
13
+ fsspec==2025.7.0 ; python_version >= "3.12" and python_version < "4.0"
14
+ gradio-client==1.11.0 ; python_version >= "3.12" and python_version < "4.0"
15
+ gradio==5.38.0 ; python_version >= "3.12" and python_version < "4.0"
16
+ groovy==0.1.2 ; python_version >= "3.12" and python_version < "4.0"
17
+ h11==0.16.0 ; python_version >= "3.12" and python_version < "4.0"
18
+ hf-xet==1.1.5 ; python_version >= "3.12" and python_version < "4.0" and (platform_machine == "x86_64" or platform_machine == "amd64" or platform_machine == "arm64" or platform_machine == "aarch64")
19
+ httpcore==1.0.9 ; python_version >= "3.12" and python_version < "4.0"
20
+ httpx==0.28.1 ; python_version >= "3.12" and python_version < "4.0"
21
+ huggingface-hub==0.33.4 ; python_version >= "3.12" and python_version < "4.0"
22
+ idna==3.10 ; python_version >= "3.12" and python_version < "4.0"
23
+ jinja2==3.1.6 ; python_version >= "3.12" and python_version < "4.0"
24
+ markdown-it-py==3.0.0 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
25
+ markupsafe==3.0.2 ; python_version >= "3.12" and python_version < "4.0"
26
+ mdurl==0.1.2 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
27
+ mpmath==1.3.0 ; python_version >= "3.12" and python_version < "4.0"
28
+ networkx==3.5 ; python_version >= "3.12" and python_version < "4.0"
29
+ numpy==2.3.1 ; python_version >= "3.12" and python_version < "4.0"
30
+ nvidia-cublas-cu12==12.6.4.1 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.12" and python_version < "4.0"
31
+ nvidia-cuda-cupti-cu12==12.6.80 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.12" and python_version < "4.0"
32
+ nvidia-cuda-nvrtc-cu12==12.6.77 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.12" and python_version < "4.0"
33
+ nvidia-cuda-runtime-cu12==12.6.77 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.12" and python_version < "4.0"
34
+ nvidia-cudnn-cu12==9.5.1.17 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.12" and python_version < "4.0"
35
+ nvidia-cufft-cu12==11.3.0.4 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.12" and python_version < "4.0"
36
+ nvidia-cufile-cu12==1.11.1.6 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.12" and python_version < "4.0"
37
+ nvidia-curand-cu12==10.3.7.77 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.12" and python_version < "4.0"
38
+ nvidia-cusolver-cu12==11.7.1.2 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.12" and python_version < "4.0"
39
+ nvidia-cusparse-cu12==12.5.4.2 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.12" and python_version < "4.0"
40
+ nvidia-cusparselt-cu12==0.6.3 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.12" and python_version < "4.0"
41
+ nvidia-nccl-cu12==2.26.2 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.12" and python_version < "4.0"
42
+ nvidia-nvjitlink-cu12==12.6.85 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.12" and python_version < "4.0"
43
+ nvidia-nvtx-cu12==12.6.77 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.12" and python_version < "4.0"
44
+ orjson==3.11.0 ; python_version >= "3.12" and python_version < "4.0"
45
+ packaging==25.0 ; python_version >= "3.12" and python_version < "4.0"
46
+ pandas==2.3.1 ; python_version >= "3.12" and python_version < "4.0"
47
+ pillow==11.3.0 ; python_version >= "3.12" and python_version < "4.0"
48
+ pydantic-core==2.33.2 ; python_version >= "3.12" and python_version < "4.0"
49
+ pydantic==2.11.7 ; python_version >= "3.12" and python_version < "4.0"
50
+ pydub==0.25.1 ; python_version >= "3.12" and python_version < "4.0"
51
+ pygments==2.19.2 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
52
+ python-dateutil==2.9.0.post0 ; python_version >= "3.12" and python_version < "4.0"
53
+ python-multipart==0.0.20 ; python_version >= "3.12" and python_version < "4.0"
54
+ pytz==2025.2 ; python_version >= "3.12" and python_version < "4.0"
55
+ pyyaml==6.0.2 ; python_version >= "3.12" and python_version < "4.0"
56
+ regex==2024.11.6 ; python_version >= "3.12" and python_version < "4.0"
57
+ requests==2.32.4 ; python_version >= "3.12" and python_version < "4.0"
58
+ rich==14.0.0 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
59
+ ruff==0.12.4 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
60
+ safehttpx==0.1.6 ; python_version >= "3.12" and python_version < "4.0"
61
+ safetensors==0.5.3 ; python_version >= "3.12" and python_version < "4.0"
62
+ semantic-version==2.10.0 ; python_version >= "3.12" and python_version < "4.0"
63
+ setuptools==80.9.0 ; python_version >= "3.12" and python_version < "4.0"
64
+ shellingham==1.5.4 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
65
+ six==1.17.0 ; python_version >= "3.12" and python_version < "4.0"
66
+ sniffio==1.3.1 ; python_version >= "3.12" and python_version < "4.0"
67
+ starlette==0.47.2 ; python_version >= "3.12" and python_version < "4.0"
68
+ sympy==1.14.0 ; python_version >= "3.12" and python_version < "4.0"
69
+ tokenizers==0.21.2 ; python_version >= "3.12" and python_version < "4.0"
70
+ tomlkit==0.13.3 ; python_version >= "3.12" and python_version < "4.0"
71
+ torch==2.7.1 ; python_version >= "3.12" and python_version < "4.0"
72
+ tqdm==4.67.1 ; python_version >= "3.12" and python_version < "4.0"
73
+ transformers==4.53.2 ; python_version >= "3.12" and python_version < "4.0"
74
+ triton==3.3.1 ; platform_system == "Linux" and platform_machine == "x86_64" and python_version >= "3.12" and python_version < "4.0"
75
+ typer==0.16.0 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
76
+ typing-extensions==4.14.1 ; python_version >= "3.12" and python_version < "4.0"
77
+ typing-inspection==0.4.1 ; python_version >= "3.12" and python_version < "4.0"
78
+ tzdata==2025.2 ; python_version >= "3.12" and python_version < "4.0"
79
+ urllib3==2.5.0 ; python_version >= "3.12" and python_version < "4.0"
80
+ uvicorn==0.35.0 ; python_version >= "3.12" and python_version < "4.0" and sys_platform != "emscripten"
81
+ websockets==15.0.1 ; python_version >= "3.12" and python_version < "4.0"