falconX90 commited on
Commit
a97d0de
·
1 Parent(s): 83d684f
Files changed (1) hide show
  1. app.py +64 -60
app.py CHANGED
@@ -1,17 +1,17 @@
1
  import streamlit as st
2
  import requests
3
  import os
 
4
 
 
5
 
6
- huggingface_token = os.getenv('HF_TOKEN')
7
 
 
 
8
 
9
- API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
10
- headers = {"Authorization": f"Bearer {huggingface_token}"}
11
-
12
- def query(payload):
13
- response = requests.post(API_URL, headers=headers, json=payload)
14
- return response.json()
15
 
16
  # output = query({
17
  # "inputs": "The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.",
@@ -20,62 +20,66 @@ def query(payload):
20
  # print(output)
21
 
22
 
23
- st.title("Text Summarization App")
24
- sentence = st.text_area('Please paste your article :', height=68)
25
- output = query({
26
- "inputs": sentence,
27
- })
28
- button = st.button("Summarize", type="primary")
29
- # st.write(output[0]["summary_text"])
 
 
 
 
 
 
30
 
31
- if button and sentence:
32
- st.write(output[0]["summary_text"])
33
 
34
  # @st.cache_data(allow_output_mutation=True)
35
- # def load_summarizer():
36
- # model = pipeline("summarization", device=0)
37
- # return model
38
 
39
 
40
- # def generate_chunks(inp_str):
41
- # max_chunk = 500
42
- # inp_str = inp_str.replace('.', '.<eos>')
43
- # inp_str = inp_str.replace('?', '?<eos>')
44
- # inp_str = inp_str.replace('!', '!<eos>')
45
 
46
- # sentences = inp_str.split('<eos>')
47
- # current_chunk = 0
48
- # chunks = []
49
- # for sentence in sentences:
50
- # if len(chunks) == current_chunk + 1:
51
- # if len(chunks[current_chunk]) + len(sentence.split(' ')) <= max_chunk:
52
- # chunks[current_chunk].extend(sentence.split(' '))
53
- # else:
54
- # current_chunk += 1
55
- # chunks.append(sentence.split(' '))
56
- # else:
57
- # chunks.append(sentence.split(' '))
58
-
59
- # for chunk_id in range(len(chunks)):
60
- # chunks[chunk_id] = ' '.join(chunks[chunk_id])
61
- # return chunks
62
-
63
-
64
- # summarizer = load_summarizer()
65
- # st.title("Summarize Text")
66
- # sentence = st.text_area('Please paste your article :', height=30)
67
- # button = st.button("Summarize")
68
-
69
- # max = st.sidebar.slider('Select max', 50, 500, step=10, value=150)
70
- # min = st.sidebar.slider('Select min', 10, 450, step=10, value=50)
71
- # do_sample = st.sidebar.checkbox("Do sample", value=False)
72
- # with st.spinner("Generating Summary.."):
73
- # if button and sentence:
74
- # chunks = generate_chunks(sentence)
75
- # res = summarizer(chunks,
76
- # max_length=max,
77
- # min_length=min,
78
- # do_sample=do_sample)
79
- # text = ' '.join([summ['summary_text'] for summ in res])
80
- # # st.write(result[0]['summary_text'])
81
- # st.write(text)
 
1
  import streamlit as st
2
  import requests
3
  import os
4
+ from transformers import pipeline
5
 
6
+ # huggingface_token = os.getenv('HF_TOKEN')
7
 
 
8
 
9
+ # API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
10
+ # headers = {"Authorization": f"Bearer {huggingface_token}"}
11
 
12
+ # def query(payload):
13
+ # response = requests.post(API_URL, headers=headers, json=payload)
14
+ # return response.json()
 
 
 
15
 
16
  # output = query({
17
  # "inputs": "The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.",
 
20
  # print(output)
21
 
22
 
23
+ # st.title("Text Summarization App")
24
+ # sentence = st.text_area('Please paste your article :', height=68)
25
+ # output = query({
26
+ # "inputs": sentence,
27
+ # })
28
+ # button = st.button("Summarize", type="primary")
29
+ # # st.write(output[0]["summary_text"])
30
+
31
+ # if button and sentence:
32
+ # st.write(output[0]["summary_text"])
33
+
34
+
35
+
36
 
 
 
37
 
38
  # @st.cache_data(allow_output_mutation=True)
39
+ def load_summarizer():
40
+ model = pipeline("summarization", device=0)
41
+ return model
42
 
43
 
44
+ def generate_chunks(inp_str):
45
+ max_chunk = 500
46
+ inp_str = inp_str.replace('.', '.<eos>')
47
+ inp_str = inp_str.replace('?', '?<eos>')
48
+ inp_str = inp_str.replace('!', '!<eos>')
49
 
50
+ sentences = inp_str.split('<eos>')
51
+ current_chunk = 0
52
+ chunks = []
53
+ for sentence in sentences:
54
+ if len(chunks) == current_chunk + 1:
55
+ if len(chunks[current_chunk]) + len(sentence.split(' ')) <= max_chunk:
56
+ chunks[current_chunk].extend(sentence.split(' '))
57
+ else:
58
+ current_chunk += 1
59
+ chunks.append(sentence.split(' '))
60
+ else:
61
+ chunks.append(sentence.split(' '))
62
+
63
+ for chunk_id in range(len(chunks)):
64
+ chunks[chunk_id] = ' '.join(chunks[chunk_id])
65
+ return chunks
66
+
67
+
68
+ summarizer = load_summarizer()
69
+ st.title("Summarize Text")
70
+ sentence = st.text_area('Please paste your article :', height=30)
71
+ button = st.button("Summarize")
72
+
73
+ max = st.sidebar.slider('Select max', 50, 500, step=10, value=150)
74
+ min = st.sidebar.slider('Select min', 10, 450, step=10, value=50)
75
+ do_sample = st.sidebar.checkbox("Do sample", value=False)
76
+ with st.spinner("Generating Summary.."):
77
+ if button and sentence:
78
+ chunks = generate_chunks(sentence)
79
+ res = summarizer(chunks,
80
+ max_length=max,
81
+ min_length=min,
82
+ do_sample=do_sample)
83
+ text = ' '.join([summ['summary_text'] for summ in res])
84
+ # st.write(result[0]['summary_text'])
85
+ st.write(text)