File size: 1,587 Bytes
cd3cab0
c4eca14
 
 
cd3cab0
c4eca14
 
 
 
 
ad12f7d
c4eca14
 
4d3dd74
 
 
 
c4eca14
ad12f7d
c4eca14
ad12f7d
 
c4eca14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
import matplotlib.pyplot as plt

# Load model and tokenizer
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)

# Streamlit interface
st.title("Sentiment Analysis with Hugging Face Transformers")

prompt_text = "create a nlp transformer example using pytorch that will run hugging face, put a streamlit interface on it that will take the appropriate inputs and outputs include a matplotlib graph if necessary with the output. The code should be all together to make it easy to cut and paste."
st.write(f"**Prompt:** {prompt_text}")

st.write("Enter text to analyze its sentiment:")

input_text = st.text_area("Input Text", height=200)

if st.button("Analyze"):
    if input_text:
        # Perform sentiment analysis
        results = classifier(input_text)
        
        # Display results
        st.write("Results:")
        st.write(results)
        
        # Extract scores for plotting
        scores = results[0]['score']
        labels = results[0]['label']
        
        # Plotting
        fig, ax = plt.subplots()
        ax.bar(labels, scores, color='skyblue')
        ax.set_ylabel('Score')
        ax.set_title('Sentiment Analysis Result')
        
        st.pyplot(fig)
    else:
        st.write("Please enter text to analyze.")