spaces-trial / app.py
ranga-godhandaraman's picture
Update app.py
a8039ff verified
# import streamlit as st
# from transformers import pipeline
# pipe = pipeline('sentiment-analysis')
# text= st.text_area('enter some text')
# if st.button('Submit'):
# if text:
# out = pipe(text)
# st.write(out)
import streamlit as st
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
# Load the GPT tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("sshleifer/tiny-gpt2")
model = AutoModelForCausalLM.from_pretrained("sshleifer/tiny-gpt2")
# Load the sentiment analysis pipeline
sentiment_pipeline = pipeline("sentiment-analysis")
# Text input field
user_input = st.text_area("Enter your prompt:")
if st.button("Submit"):
if user_input:
# Generate text using the GPT model
inputs = tokenizer(user_input, return_tensors="pt")
generated_ids = model.generate(**inputs)
generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
# Perform sentiment analysis on both original and generated text
original_sentiment = sentiment_pipeline(user_input)
generated_sentiment = sentiment_pipeline(generated_text)
# Display the results
st.write("Original Text:")
st.write(user_input)
st.write("Original Text Sentiment:", original_sentiment)
st.write("Generated Text:")
st.write(generated_text)
st.write("Generated Text Sentiment:", generated_sentiment)