Dhruv Singh commited on
Commit
1db0c1f
·
verified ·
1 Parent(s): 5655af0

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. .gitignore +7 -0
  2. README.md +30 -7
  3. app.py +71 -0
  4. requirements.txt +77 -0
.gitignore ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ .venv/
2
+ __pycache__/
3
+ *.py[cod]
4
+ .env
5
+ .DS_Store
6
+ .idea/
7
+ .vscode/
README.md CHANGED
@@ -1,14 +1,37 @@
1
  ---
2
  title: Beginner Sentiment Analyzer
3
- emoji: 🏆
4
- colorFrom: pink
5
- colorTo: green
6
  sdk: gradio
7
- sdk_version: 6.20.0
8
- python_version: '3.12'
9
  app_file: app.py
10
  pinned: false
11
- short_description: This is beggineers project to learn about hugging afce
12
  ---
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  title: Beginner Sentiment Analyzer
3
+ emoji: 🤗
4
+ colorFrom: blue
5
+ colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 5.0.0
 
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
+ # Beginner Sentiment Analyzer
13
+
14
+ A beginner-friendly sentiment analysis project built using Hugging Face
15
+ Transformers, DistilBERT, PyTorch, and Gradio.
16
+
17
+ ## Features
18
+
19
+ - Classifies English text as positive or negative
20
+ - Shows confidence scores
21
+ - Includes example sentences
22
+ - Handles empty and excessively long input
23
+ - Runs locally or on Hugging Face Spaces
24
+
25
+ ## Model
26
+
27
+ This project uses:
28
+
29
+ `distilbert/distilbert-base-uncased-finetuned-sst-2-english`
30
+
31
+ ## Run locally
32
+
33
+ ```bash
34
+ python -m venv .venv
35
+ source .venv/bin/activate
36
+ pip install -r requirements.txt
37
+ python app.py
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any
2
+
3
+ import gradio as gr
4
+ from transformers import Pipeline, pipeline
5
+
6
+
7
+ MODEL_NAME = "distilbert/distilbert-base-uncased-finetuned-sst-2-english"
8
+
9
+ classifier: Pipeline = pipeline(
10
+ task="sentiment-analysis",
11
+ model=MODEL_NAME,
12
+ )
13
+
14
+
15
+ def analyze_sentiment(text: str) -> dict[str, float]:
16
+ """Analyze text and return probabilities for Gradio's Label component."""
17
+
18
+ cleaned_text = text.strip()
19
+
20
+ if not cleaned_text:
21
+ raise gr.Error("Please enter a sentence before analyzing.")
22
+
23
+ if len(cleaned_text) > 1000:
24
+ raise gr.Error("Please keep the text below 1,000 characters.")
25
+
26
+ predictions: list[dict[str, Any]] = classifier(
27
+ cleaned_text,
28
+ top_k=None,
29
+ )
30
+
31
+ return {
32
+ prediction["label"].title(): float(prediction["score"])
33
+ for prediction in predictions
34
+ }
35
+
36
+
37
+ examples = [
38
+ ["I loved working on this machine-learning project."],
39
+ ["The application was confusing and frustrating."],
40
+ ["The workshop was useful, but it was quite long."],
41
+ ]
42
+
43
+ demo = gr.Interface(
44
+ fn=analyze_sentiment,
45
+ inputs=gr.Textbox(
46
+ lines=5,
47
+ max_lines=10,
48
+ label="Your text",
49
+ placeholder="Example: Learning Hugging Face is exciting!",
50
+ ),
51
+ outputs=gr.Label(
52
+ label="Sentiment prediction",
53
+ num_top_classes=2,
54
+ ),
55
+ examples=examples,
56
+ title="🤗 Beginner Sentiment Analyzer",
57
+ description=(
58
+ "Enter an English sentence and let a pretrained Hugging Face "
59
+ "model classify its sentiment."
60
+ ),
61
+ article=(
62
+ "This beginner project uses DistilBERT, Transformers, "
63
+ "PyTorch, and Gradio."
64
+ ),
65
+ submit_btn="Analyze sentiment",
66
+ clear_btn="Clear",
67
+ )
68
+
69
+
70
+ if __name__ == "__main__":
71
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ annotated-doc==0.0.5
2
+ annotated-types==0.8.0
3
+ anyio==4.14.2
4
+ audioop-lts==0.2.2
5
+ brotli==1.2.0
6
+ certifi==2026.7.22
7
+ click==8.4.2
8
+ cuda-bindings==13.3.1
9
+ cuda-pathfinder==1.6.0
10
+ cuda-toolkit==13.0.3.0
11
+ fastapi==0.140.13
12
+ filelock==3.32.0
13
+ fsspec==2026.7.0
14
+ gradio==6.20.0
15
+ gradio_client==2.5.0
16
+ groovy==0.1.2
17
+ h11==0.16.0
18
+ hf-gradio==0.4.1
19
+ hf-xet==1.5.2
20
+ httpcore==1.0.9
21
+ httpx==0.28.1
22
+ huggingface_hub==1.25.1
23
+ idna==3.18
24
+ Jinja2==3.1.6
25
+ markdown-it-py==4.2.0
26
+ MarkupSafe==3.0.3
27
+ mdurl==0.1.2
28
+ mpmath==1.3.0
29
+ networkx==3.6.1
30
+ numpy==2.5.1
31
+ nvidia-cublas==13.1.1.3
32
+ nvidia-cuda-cupti==13.0.85
33
+ nvidia-cuda-nvrtc==13.0.88
34
+ nvidia-cuda-runtime==13.0.96
35
+ nvidia-cudnn-cu13==9.20.0.48
36
+ nvidia-cufft==12.0.0.61
37
+ nvidia-cufile==1.15.1.6
38
+ nvidia-curand==10.4.0.35
39
+ nvidia-cusolver==12.0.4.66
40
+ nvidia-cusparse==12.6.3.3
41
+ nvidia-cusparselt-cu13==0.8.1
42
+ nvidia-nccl-cu13==2.29.7
43
+ nvidia-nvjitlink==13.3.33
44
+ nvidia-nvshmem-cu13==3.4.5
45
+ nvidia-nvtx==13.0.85
46
+ orjson==3.11.9
47
+ packaging==26.2
48
+ pandas==3.0.5
49
+ pillow==12.3.0
50
+ pydantic==2.13.4
51
+ pydantic_core==2.46.4
52
+ pydub==0.25.1
53
+ Pygments==2.20.0
54
+ python-dateutil==2.9.0.post0
55
+ python-multipart==0.0.32
56
+ pytz==2026.3.post1
57
+ PyYAML==6.0.3
58
+ regex==2026.7.19
59
+ rich==15.0.0
60
+ safehttpx==0.1.7
61
+ safetensors==0.8.0
62
+ semantic-version==2.10.0
63
+ setuptools==83.0.0
64
+ shellingham==1.5.4
65
+ six==1.17.0
66
+ starlette==1.3.1
67
+ sympy==1.14.0
68
+ tokenizers==0.22.2
69
+ tomlkit==0.14.0
70
+ torch==2.13.0
71
+ tqdm==4.70.0
72
+ transformers==5.14.1
73
+ triton==3.7.1
74
+ typer==0.27.0
75
+ typing-inspection==0.4.2
76
+ typing_extensions==4.16.0
77
+ uvicorn==0.51.0