File size: 4,127 Bytes
c7dea47
9eba384
 
c7dea47
26e2707
c7dea47
 
9eba384
 
 
 
 
 
3d36732
 
 
 
 
 
 
 
 
 
 
9eba384
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74a38b8
 
 
9eba384
74a38b8
9eba384
74a38b8
9eba384
 
 
 
d11d806
 
1772b07
c7dea47
9eba384
 
c7dea47
d11d806
2b79d0e
9eba384
 
 
 
 
37148d1
dfffa12
 
 
 
 
9eba384
80fe636
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
# 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 ###