File size: 4,630 Bytes
ec929e1 32c9084 a906ccd 32c9084 35ef636 32c9084 a56fc14 ec929e1 b1eb8cd f54fc6b b1eb8cd f54fc6b b1eb8cd f54fc6b b1eb8cd f54fc6b 8c3201a f54fc6b 0b5bcd1 f54fc6b b1eb8cd ec929e1 a906ccd 0e5f8d9 32c9084 a906ccd ec929e1 2c0ab44 f1b2a26 0262183 a906ccd f7d4880 9e19736 0262183 9e19736 ec929e1 a906ccd 9e19736 f35d308 ec929e1 f35d308 2c5fe61 f35d308 ec929e1 492c1a0 f35d308 f1b2a26 f35d308 2c5fe61 492c1a0 2c5fe61 dbdc1d5 f35d308 8cf4aa8 ec929e1 f35d308 ec929e1 8cf4aa8 |
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# Natural Language Tools
# Richard Orama - September 2024
#x = st.slider('Select a value')
#st.write(x, 'squared is', x * x)
import streamlit as st
from transformers import pipeline
import ast
st.title("Assorted Language Tools - Orama's AI Craze")
################ CHAT BOT #################
# Load the GPT model
generator = pipeline("text-generation", model="EleutherAI/gpt-neo-2.7B")
# Streamlit chat UI
#st.title("GPT-3 Chatbox")
# user_input = st.text_input("You: ", "Hello, how are you?")
# if user_input:
# response = generator(user_input, max_length=100, num_return_sequences=1)[0]['generated_text']
# st.write(f"GPT-3: {response}")
# Define the summarization function
def chat(txt):
st.write('\n\n')
#st.write(txt[:100]) # Display the first 100 characters of the article
#st.write('--------------------------------------------------------------')
#summary = summarizer(txt, max_length=500, min_length=30, do_sample=False)
#st.write(summary[0]['summary_text'])
response = generator(txt, max_length=500, num_return_sequences=1)[0]['generated_text']
st.write(f"GPT-3: {response}")
DEFAULT_CHAT = ""
# Create a text area for user input
CHAT = st.sidebar.text_area('Enter Chat (String)', DEFAULT_CHAT, height=150)
# Enable the button only if there is text in the CHAT variable
if CHAT:
if st.sidebar.button('Chat Statement'):
# Call your Summarize function here
chat(CHAT) # Directly pass the your
else:
st.sidebar.button('Chat Statement', disabled=True)
st.warning('π Please enter Chat!')
################ STATEMENT SUMMARIZATION #################
# Load the summarization model
#summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6") # smaller version of the model
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
# Define the summarization function
def summarize_statement(txt):
st.write('\n\n')
#st.write(txt[:100]) # Display the first 100 characters of the article
#st.write('--------------------------------------------------------------')
summary = summarizer(txt, max_length=500, min_length=30, do_sample=False)
st.write(summary[0]['summary_text'])
DEFAULT_STATEMENT = ""
# Create a text area for user input
STATEMENT = st.sidebar.text_area('Enter Statement (String)', DEFAULT_STATEMENT, height=150)
# Enable the button only if there is text in the SENTIMENT variable
if STATEMENT:
if st.sidebar.button('Summarize Statement'):
# Call your Summarize function here
summarize_statement(STATEMENT) # Directly pass the STATEMENT
else:
st.sidebar.button('Summarize Statement', disabled=True)
st.warning('π Please enter Statement!')
################ SENTIMENT ANALYSIS #################
# Initialize the sentiment analysis pipeline
# No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english
sentiment_pipeline = pipeline("sentiment-analysis")
def is_valid_list_string(string):
try:
result = ast.literal_eval(string)
return isinstance(result, list)
except (ValueError, SyntaxError):
return False
# Define the summarization function
def analyze_sentiment(txt):
st.write('\n\n')
#st.write(txt[:100]) # Display the first 100 characters of the article
#st.write('--------------------------------------------------------------')
# Display the results
if is_valid_list_string(txt):
txt_converted = ast.literal_eval(txt) #convert string to actual content, e.g. list
# Perform Hugging sentiment analysis on multiple texts
results = sentiment_pipeline(txt_converted)
for i, text in enumerate(txt_converted):
st.write(f"Text: {text}")
st.write(f"Sentiment: {results[i]['label']}, Score: {results[i]['score']:.2f}\n")
else:
# Perform Hugging sentiment analysis on multiple texts
results = sentiment_pipeline(txt)
st.write(f"Text: {txt}")
st.write(f"Sentiment: {results[0]['label']}, Score: {results[0]['score']:.2f}\n")
DEFAULT_SENTIMENT = ""
# Create a text area for user input
SENTIMENT = st.sidebar.text_area('Enter Sentiment (String or List of Strings)', DEFAULT_SENTIMENT, height=150)
# Enable the button only if there is text in the SENTIMENT variable
if SENTIMENT:
if st.sidebar.button('Analyze Sentiment'):
analyze_sentiment(SENTIMENT) # Directly pass the SENTIMENT
else:
st.sidebar.button('Analyze Sentiment', disabled=True)
st.warning('π Please enter Sentiment!')
|