AhsanSaghir commited on
Commit
6615a41
·
verified ·
1 Parent(s): e865a90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -34
app.py CHANGED
@@ -1,47 +1,60 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
- # Initialize a sentiment analysis pipeline
5
  sentiment_analyzer = pipeline("sentiment-analysis")
 
6
 
7
  def analyze_sentiment(text):
8
- if not text.strip():
9
- return "Please enter some text to analyze"
10
-
11
  result = sentiment_analyzer(text)[0]
12
  return {
13
- "Sentiment": result["label"],
14
- "Confidence": f"{result['score']*100:.1f}%"
 
15
  }
16
 
17
- # Create the Gradio interface
18
- with gr.Blocks(title="Sentiment Analysis App") as demo:
19
- gr.Markdown("# 🎭 Text Sentiment Analysis")
20
- gr.Markdown("Enter some text and I'll analyze its sentiment using a Hugging Face model.")
21
-
22
- with gr.Row():
23
- with gr.Column():
24
- text_input = gr.Textbox(label="Your Text", placeholder="Type something here...")
25
- analyze_btn = gr.Button("Analyze Sentiment")
26
- with gr.Column():
27
- json_output = gr.JSON(label="Analysis Results")
28
-
29
- # Additional examples
30
- gr.Examples(
31
- examples=[
32
- "I love this product! It's amazing!",
33
- "I'm really disappointed with the service.",
34
- "The weather is nice today."
35
- ],
36
- inputs=text_input
37
- )
38
 
39
- analyze_btn.click(
40
- fn=analyze_sentiment,
41
- inputs=text_input,
42
- outputs=json_output
43
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- # Launch the app
46
  if __name__ == "__main__":
47
- demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import numpy as np
4
 
5
+ # Initialize models
6
  sentiment_analyzer = pipeline("sentiment-analysis")
7
+ text_generator = pipeline("text-generation", model="gpt2")
8
 
9
  def analyze_sentiment(text):
 
 
 
10
  result = sentiment_analyzer(text)[0]
11
  return {
12
+ "text": text,
13
+ "sentiment": result["label"],
14
+ "confidence": f"{result['score']*100:.1f}%"
15
  }
16
 
17
+ def generate_text(prompt, length=50):
18
+ generated = text_generator(prompt, max_length=length, num_return_sequences=1)
19
+ return generated[0]["generated_text"]
20
+
21
+ with gr.Blocks(title="Multi-Function NLP App", theme=gr.themes.Soft()) as demo:
22
+ gr.Markdown("""
23
+ # 🚀 Advanced NLP Playground
24
+ *Powered by Hugging Face Transformers*
25
+ """)
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ with gr.Tab("Sentiment Analysis"):
28
+ with gr.Row():
29
+ with gr.Column():
30
+ sentiment_input = gr.Textbox(label="Input Text", placeholder="Enter text to analyze...")
31
+ sentiment_button = gr.Button("Analyze")
32
+ with gr.Column():
33
+ sentiment_output = gr.JSON(label="Results")
34
+
35
+ sentiment_examples = gr.Examples(
36
+ examples=[
37
+ "I'm absolutely thrilled with this service!",
38
+ "The product didn't meet my expectations.",
39
+ "It's okay, nothing special."
40
+ ],
41
+ inputs=sentiment_input
42
+ )
43
+ sentiment_button.click(analyze_sentiment, inputs=sentiment_input, outputs=sentiment_output)
44
+
45
+ with gr.Tab("Text Generation"):
46
+ with gr.Row():
47
+ with gr.Column():
48
+ gen_input = gr.Textbox(label="Prompt", placeholder="Start typing your idea...")
49
+ gen_slider = gr.Slider(20, 100, value=50, label="Output Length")
50
+ gen_button = gr.Button("Generate")
51
+ with gr.Column():
52
+ gen_output = gr.Textbox(label="Generated Text", lines=5)
53
+
54
+ gen_button.click(generate_text, inputs=[gen_input, gen_slider], outputs=gen_output)
55
+
56
+ gr.Markdown("---")
57
+ gr.HTML("<center>Built with ❤️ using Gradio and Hugging Face</center>")
58
 
 
59
  if __name__ == "__main__":
60
+ demo.launch(share=True)