Summariser / app.py
MrJShen's picture
Update app.py
3d36732
# app.py
from wordcloud import WordCloud ## Mr.Shen: Using a wordcloud library to show the key-words of the article
import streamlit as st
from utils import *
#### Define some sidebar functions and layouts here ####
with st.sidebar:
openai_api_key = st.text_input("OpenAI API Key", key="chatbot_api_key", type="password")
"[How to get your openai API key?](https://elephas.app/blog/how-to-get-chatgpt-api-key-clh93ii2e1642073tpacu6w934j)" ## Mr.Shen: Add a superlink to show how to get an Openai API key
"[![Visit out Huggingface Space](https://huggingface.co/datasets/huggingface/badges/raw/main/powered-by-huggingface-light.svg)](https://huggingface.co/TGSAI)"
st.markdown("""
##### Maintainers:
- Gavin Wu
- Samuel Moffitt
##### Contributors:
- David Pan
- Wei Wang
""")
#### The main layouts start here ####
### Input section start ###
st.title("📝 Summarizer")
txt = st.text_area('Copy and paste your text here',
height = 20,
max_chars = 3000,
key="txt")
topics = st.text_input('Choose topics, seperated by ,', value= '', key='topics')
word_count = st.slider('The maximum number of words of summary.', min_value = 10, max_value = 200, value = 20, key = 'word_count')
## We will need two buttons, one for chat with ChatGPT, another for the free hugchat with lower quality. ##
col1, col2 = st.columns(2) ## This is how we put widgets in one row.
with col1:
st.button('with ChatGPT', key='chatgpt')
with col2:
st.button('with HugChat', key='hugchat')
### Input section end ###
### Function section Start ###
## Now we need to pass those input variables to our prompt ##
prompt = f"""
Your task is to generate a short summary from text below, delimited by triple backticks, in at most {word_count} words,\
Firstly, extract relevant information and create a list of keywords without response,\
Then, check if {topics} is in your list, if not, just response no relevent topics about {topics} to summarise,\
if it is in your list, focusing on any aspects that mention {topics},
```{txt}```
"""
response = '' ## Initilise the response as an empty string
# Handling the "chat with ChatGPT" button clicked
if st.session_state.chatgpt:
# Making sure user entering the OpenaAI API Key
if not st.session_state.chatbot_api_key:
st.info("Please add your OpenAI API key to continue.")
st.stop()
openai.api_key = st.session_state.chatbot_api_key
response, token_dic, moderation = get_completion(prompt) ## Get the moderation, cost, and response from one function
# Compute cost
input_cost = token_dic['prompt_tokens']*0.0015/1000;
output_cost=token_dic['completion_tokens']*0.002/1000
total_cost = input_cost + output_cost
# Moderation
mod = pd.DataFrame(moderation)
fig_r = get_radar(mod) ## Generate the moderation radar graph
# Wordcloud and sentiment analysis:
if txt:
wordcloud = WordCloud(background_color='white', min_font_size=3, mode ='RGBA',min_word_length=1).generate(txt)
df, fig_s = get_sentiment(txt)
### Function section End ###
### Output section Start ###
## Wordcloud and Sentiment Analysis##
if txt:
st.markdown(":violet[Wordcloud: ]")
st.pyplot(plot_wordcloud(wordcloud))
st.write("##")
st.markdown(":violet[Sentiment Analysis: ]")
st.plotly_chart(fig_s)
st.dataframe(df)
## Moderation ##
if st.session_state.chatgpt:
st.markdown("##### :blue[Content moderation: ]")
st.plotly_chart(fig_r)
## Cost of summarisation ##
st.text("Total cost for this query: " + "$" + str(total_cost))
st.divider()
### Summarise with ChatGPT ###
st.markdown("##### :blue[Summarizer with ChatGPT: ]")
st.markdown(f"###### :black[{response}]")
if st.session_state.hugchat:
st.write("coming soon ...")
st.divider()
st.markdown("##### :black[Other NLP servers are coming soon ... ]")
### Output section End ###