yukta1 commited on
Commit
354f3db
Β·
verified Β·
1 Parent(s): 1fa2e81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -16
app.py CHANGED
@@ -2,24 +2,43 @@ import gradio as gr
2
  from transformers import pipeline
3
 
4
  # Load the sentiment analysis pipeline
5
- classifier = pipeline("sentiment-analysis")
6
 
7
- # Define the prediction function
8
  def analyze_sentiment(text):
9
- result = classifier(text)[0]
10
- label = result['label']
11
- score = round(result['score'], 3)
12
- return f"Sentiment: {label} (Confidence: {score})"
 
13
 
14
- # Create the Gradio interface
15
- interface = gr.Interface(
16
- fn=analyze_sentiment,
17
- inputs=gr.Textbox(lines=4, placeholder="Enter some text here..."),
18
- outputs="text",
19
- title="Sentiment Analysis with Hugging Face πŸ€—",
20
- description="Enter text to find out if the sentiment is positive, negative, or neutral."
21
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # Run the app
 
 
 
 
 
24
  if __name__ == "__main__":
25
- interface.launch()
 
2
  from transformers import pipeline
3
 
4
  # Load the sentiment analysis pipeline
5
+ sentiment_pipeline = pipeline("sentiment-analysis")
6
 
7
+ # Function to process input text
8
  def analyze_sentiment(text):
9
+ if not text.strip():
10
+ return "⚠️ Please enter some text."
11
+ result = sentiment_pipeline(text)[0]
12
+ label_emoji = "😊" if result["label"] == "POSITIVE" else "😞"
13
+ return f"{label_emoji} **{result['label']}** (confidence: `{round(result['score'], 3)}`)"
14
 
15
+ # Create a more styled interface using Gradio Blocks
16
+ with gr.Blocks(title="Sentiment Analyzer") as demo:
17
+ gr.Markdown(
18
+ """
19
+ # 🧠 Sentiment Analysis App
20
+ _Enter a sentence to discover its emotional tone!_
21
+ Uses `distilbert-base-uncased-finetuned-sst-2-english` from Hugging Face πŸ€—
22
+ ---
23
+ """
24
+ )
25
+
26
+ with gr.Row():
27
+ with gr.Column():
28
+ input_text = gr.Textbox(
29
+ placeholder="Type something like 'I love this app!'",
30
+ label="Your Text",
31
+ lines=3
32
+ )
33
+ submit_btn = gr.Button("πŸ” Analyze")
34
+ with gr.Column():
35
+ output = gr.Markdown(label="Sentiment Result")
36
 
37
+ submit_btn.click(fn=analyze_sentiment, inputs=input_text, outputs=output)
38
+
39
+ gr.Markdown("---")
40
+ gr.Markdown("Made with ❀️ using [Gradio](https://gradio.app) and [Hugging Face Transformers](https://huggingface.co/transformers/)")
41
+
42
+ # For local testing; not required for Spaces
43
  if __name__ == "__main__":
44
+ demo.launch()