File size: 1,445 Bytes
a8039ff
 
 
 
 
 
 
 
 
 
 
96e4d99
a8039ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96e4d99
a8039ff
 
 
96e4d99
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
# 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)