Harshavarma04's picture
Create app.py
b836427 verified
import streamlit as st
from transformers import pipeline
# Initialize sentiment analysis pipeline
sentiment_pipeline = pipeline("sentiment-analysis")
# Use a sarcasm detection model
sarcasm_model_name = "mrm8488/t5-base-finetuned-sarcasm-twitter" # Correct model name for sarcasm detection
# Create the sarcasm detection pipeline
sarcasm_pipeline = pipeline("text2text-generation", model=sarcasm_model_name)
def classify_sentence(sentence):
# Detect sarcasm
sarcasm_result = sarcasm_pipeline(sentence)[0]['generated_text']
is_sarcastic = sarcasm_result.strip().lower() == 'true'
# Detect sentiment
sentiment_result = sentiment_pipeline(sentence)[0]
sentiment_label = sentiment_result['label']
sentiment_score = sentiment_result['score']
# Determine sentiment
if sentiment_label == "NEGATIVE":
sentiment = "negative"
elif sentiment_label == "POSITIVE":
sentiment = "positive"
else:
sentiment = "neutral"
# Handle sarcasm
if is_sarcastic:
sentiment += " (sarcastic)"
return sentiment
# Streamlit app
st.title("Sentence Analyzer")
# User input
sentence = st.text_input("Enter a sentence:", "")
if st.button("Analyze"):
if sentence:
classification = classify_sentence(sentence)
st.write(f"Sentence: {sentence}")
st.write(f"Classification: {classification}")
else:
st.write("Please enter a sentence to analyze.")
# Example sentences
st.subheader("Example Sentences")
example_sentences = [
"they are so beautiful",
"This is the best day of my life.",
"I'm not happy with your work.",
"Yeah,you are not a good person!"
]
if st.button("Analyze Example Sentences"):
for sentence in example_sentences:
st.write(f"Sentence: {sentence}")
st.write(f"Classification: {classify_sentence(sentence)}")
st.write()