MrJShen commited on
Commit
9eba384
·
1 Parent(s): 5684259

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -14
app.py CHANGED
@@ -1,26 +1,116 @@
1
  # app.py
 
 
2
  import streamlit as st
3
  from utils import *
4
 
5
- st.header("AI Output: Summarizer")
6
 
7
- txt = st.text_area('Copy and paste your text here',
8
- max_chars = 4000,
9
- key="input_text")
10
- st.write(txt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- topics = st.text_input('Choose topics, seperated by ,', '')
13
- st.write(topics)
 
 
 
14
 
 
 
 
 
 
 
 
15
 
16
- st.subheader("Word count")
17
- word_count = st.slider("How long would you like your summary?", 10, 200)
18
- st.write("Word count:", word_count)
19
 
20
- password = st.text_input('Enter your password', max_chars=50, type="password") # changed st.text_input with type="password"
21
- st.write(password)
22
 
 
 
 
 
 
 
23
 
24
- ## st.header("Output:", divider=True ## seems like a version conflict here, to-do
25
 
26
- st.button("ChatGPT summariser")
 
1
  # app.py
2
+
3
+ from wordcloud import WordCloud ## Mr.Shen: Using a wordcloud library to show the key-words of the article
4
  import streamlit as st
5
  from utils import *
6
 
 
7
 
8
+ #### Define some sidebar functions and layouts here ####
9
+ with st.sidebar:
10
+ openai_api_key = st.text_input("OpenAI API Key", key="chatbot_api_key", type="password")
11
+ "[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
12
+ "[![Visit out Huggingface Space](https://huggingface.co/datasets/huggingface/badges/raw/main/powered-by-huggingface-light.svg)](https://huggingface.co/TGSAI)"
13
+
14
+
15
+ #### The main layouts start here ####
16
+
17
+ ### Input section start ###
18
+
19
+ st.title("📝 Summarizer")
20
+
21
+ txt = st.text_area('Copy and paste your text here',
22
+ height = 20,
23
+ max_chars = 3000,
24
+ key="txt")
25
+
26
+ topics = st.text_input('Choose topics, seperated by ,', value= '', key='topics')
27
+
28
+ word_count = st.slider('The maximum number of words of summary.', min_value = 10, max_value = 200, value = 20, key = 'word_count')
29
+
30
+ ## We will need two buttons, one for chat with ChatGPT, another for the free hugchat with lower quality. ##
31
+ col1, col2 = st.columns(2) ## This is how we put widgets in one row.
32
+
33
+ with col1:
34
+ st.button('with ChatGPT', key='chatgpt')
35
+ with col2:
36
+ st.button('with HugChat', key='hugchat')
37
+
38
+ ### Input section end ###
39
+
40
+ ### Function section Start ###
41
+
42
+ ## Now we need to pass those input variables to our prompt ##
43
+ prompt = f"""
44
+ Your task is to generate a short summary from text below, delimited by triple backticks, in at most {word_count} words,\
45
+ Firstly, extract relevant information and create a list of keywords without response,\
46
+ Then, check if {topics} is in your list, if not, just response no relevent topics about {topics} to summarise,\
47
+ if it is in your list, focusing on any aspects that mention {topics},
48
+ ```{txt}```
49
+ """
50
+ response = '' ## Initilise the response as an empty string
51
+
52
+ # Handling the "chat with ChatGPT" button clicked
53
+ if st.session_state.chatgpt:
54
+
55
+ # Making sure user entering the OpenaAI API Key
56
+ if not st.session_state.chatbot_api_key:
57
+ st.info("Please add your OpenAI API key to continue.")
58
+ st.stop()
59
+
60
+ openai.api_key = st.session_state.chatbot_api_key
61
+ response, token_dic, moderation = get_completion(prompt) ## Get the moderation, cost, and response from one function
62
+
63
+ # Compute cost
64
+ input_cost = token_dic['prompt_tokens']*0.0015/1000;
65
+ output_cost=token_dic['completion_tokens']*0.002/1000
66
+ total_cost = input_cost + output_cost
67
+
68
+ # Moderation
69
+ mod = pd.DataFrame(moderation)
70
+ fig_r = get_radar(mod) ## Generate the moderation radar graph
71
+
72
+ # Wordcloud and sentiment analysis:
73
+ if txt:
74
+ wordcloud = WordCloud(background_color='white', min_font_size=3, mode ='RGBA',min_word_length=1).generate(txt)
75
+ df, fig_s = get_sentiment(txt)
76
+
77
+ ### Function section End ###
78
+
79
+ ### Output section Start ###
80
+
81
+ ## Wordcloud ##
82
+ st.markdown(":violet[Wordcloud: ]")
83
+ if wordcloud:
84
+ st.pyplot(plot_wordcloud(wordcloud))
85
+
86
+ st.write("##")
87
 
88
+ ## Sentiment Analysis ##
89
+ st.markdown(":violet[Sentiment Analysis: ]")
90
+ if df:
91
+ st.plotly_chart(fig_s)
92
+ st.dataframe(df)
93
 
94
+ st.markdown("##### :blue[Summarizer with ChatGPT: ]")
95
+ st.markdown(f"###### :black[{response}]")
96
+
97
+ ## Moderation ##
98
+ st.markdown("##### :blue[Content moderation: ]")
99
+ if fig_r:
100
+ st.plotly_chart(fig)
101
 
102
+ ## Cost of summarisation ##
103
+ if total_cost:
104
+ st.text("Total cost for this query: " + "$" + str(total_cost))
105
 
106
+ st.divider()
 
107
 
108
+ ### Summarise with ChatGPT ###
109
+ if st.session_state.chatgpt:
110
+ st.markdown("##### :blue[Summarizer with ChatGPT: ]")
111
+ st.markdown(f"###### :black[{response}]")
112
+ if st.session_state.hugchat:
113
+ st.write("coming soon ...")
114
 
115
+ ### Output section End ###
116