File size: 1,921 Bytes
07568cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Imports
import gradio as gr
from transformers import pipeline
import torch

print("Torch version:", torch.__version__)
# Create the sentiment analysis pipeline
sentiment_pipe = pipeline("sentiment-analysis")

# Define analysis function
def analyze_sentiment(text):
    result = sentiment_pipe(text)[0]
    label = result["label"]
    score = result["score"]

    if label == "POSITIVE":
        emoji = "😊"
        img_url = "https://thepreachersword.com/wp-content/uploads/2017/05/cheerful.jpg"  # cheerful
        sentiment_text = f"Positive sentiment detected! Confidence: {score:.2f} {emoji}"
    elif label == "NEGATIVE":
        emoji = "😞"
        img_url = "https://cdn.pixabay.com/photo/2024/04/24/14/24/ai-generated-8717915_640.png"  # sad
        sentiment_text = f"Negative sentiment detected! Confidence: {score:.2f} {emoji}"
    else:
        emoji = "😐"
        img_url = "https://media.istockphoto.com/id/1453968261/photo/thoughtful-senior-man-looks-into-copy-space-as-he-stands-outdoors-in-nature.jpg?s=612x612&w=0&k=20&c=V6KOTTki3thkrDNqIG4QBvmpCAoqO9aQ-9mtSy2BR9k="  # neutral
        sentiment_text = f"Neutral or other sentiment detected. Confidence: {score:.2f} {emoji}"

    return sentiment_text, img_url

# Create Gradio Blocks interface
with gr.Blocks(theme=gr.themes.Default(primary_hue="teal")) as demo:
    gr.Markdown(
        """
        # πŸ“ Sentiment Analysis App
        Enter any text below to analyze its sentiment and see a matching mood image!
        """
    )

    text_input = gr.Textbox(label="Enter your text", placeholder="Write something here...")
    analyze_button = gr.Button("Analyze Sentiment πŸ”")

    output_text = gr.Textbox(label="Sentiment Result")
    output_image = gr.Image(label="Mood Image", interactive=False)

    analyze_button.click(fn=analyze_sentiment, inputs=text_input, outputs=[output_text, output_image])

# Launch the app
demo.launch()