yukta1 commited on
Commit
258a52c
·
verified ·
1 Parent(s): df6e58e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -11
app.py CHANGED
@@ -1,21 +1,44 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load Hugging Face sentiment analysis pipeline
5
  sentiment_pipeline = pipeline("sentiment-analysis")
6
 
7
- # Define function to wrap the model
8
  def analyze_sentiment(text):
 
 
9
  result = sentiment_pipeline(text)[0]
10
- return f"Label: {result['label']}, Confidence: {round(result['score'], 3)}"
 
11
 
12
- # Create Gradio interface
13
- interface = gr.Interface(fn=analyze_sentiment,
14
- inputs="text",
15
- outputs="text",
16
- title="Sentiment Analysis App",
17
- description="Enter a sentence to analyze its sentiment.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- # Launch app (not needed on Spaces, but useful for local testing)
 
 
 
 
 
20
  if __name__ == "__main__":
21
- interface.launch()
 
1
  import gradio as gr
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()