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.")