changes to the ui
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import streamlit as st
|
|
| 4 |
import requests
|
| 5 |
import torch
|
| 6 |
import requests
|
| 7 |
-
import gradio as gr
|
| 8 |
from bs4 import BeautifulSoup
|
| 9 |
from transformers import pipeline, AutoTokenizer
|
| 10 |
|
|
@@ -13,9 +12,9 @@ if torch.cuda.is_available():
|
|
| 13 |
print("GPU is available")
|
| 14 |
device = 0
|
| 15 |
else:
|
| 16 |
-
print("GPU is
|
|
|
|
| 17 |
device = -1
|
| 18 |
-
Gradio = True
|
| 19 |
|
| 20 |
url="https://text.npr.org"
|
| 21 |
def get_headlines_links():
|
|
@@ -52,11 +51,14 @@ def get_paragraphs(soup):
|
|
| 52 |
|
| 53 |
def get_hindex(headlines):
|
| 54 |
global url
|
| 55 |
-
|
| 56 |
-
sbox = st.selectbox(f"\n\n \nPick a story from NPR {url}",
|
| 57 |
options=headlines)
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
def get_story(index):
|
| 62 |
global links
|
|
@@ -103,7 +105,7 @@ def chunkify(paras, pstart):
|
|
| 103 |
raise RuntimeError("paragraph too large, tokens: {num_tokens}")
|
| 104 |
|
| 105 |
tokens = tokenizer(long_text)
|
| 106 |
-
print(f" cur_p {cur_p} tokens {len(tokens.input_ids)} ")
|
| 107 |
chunks.append(long_text)
|
| 108 |
long_text = ''
|
| 109 |
num_tokens = 0
|
|
@@ -115,7 +117,7 @@ def chunkify(paras, pstart):
|
|
| 115 |
tokens = tokenizer(chunk)
|
| 116 |
num_tokens = len(tokens[0])
|
| 117 |
words = [x for x in chunk.split() if x]
|
| 118 |
-
print(f" {ci} has {len(words)} words")
|
| 119 |
if num_tokens >= HARD_MAX_TOKENS:
|
| 120 |
raise ValueError(f"{ci} {num_tokens}")
|
| 121 |
return chunks
|
|
@@ -126,11 +128,11 @@ def summarize_chunks(chunks):
|
|
| 126 |
for ci, chunk in enumerate(chunks):
|
| 127 |
tokens = tokenizer(chunk)
|
| 128 |
num_tokens = len(tokens.input_ids)
|
| 129 |
-
print( f"chunk {ci} has {num_tokens} tokens")
|
| 130 |
psummary = pipe(chunk, max_length=140, min_length=20, do_sample=False)
|
| 131 |
psummary = psummary[0]["summary_text"]
|
| 132 |
psummary_words = len(psummary.split())
|
| 133 |
-
print(f" {psummary_words}")
|
| 134 |
full_summary += psummary
|
| 135 |
except Exception:
|
| 136 |
exc_type, exc_val, exc_tb = sys.exc_info()
|
|
@@ -139,21 +141,9 @@ def summarize_chunks(chunks):
|
|
| 139 |
print(msg)
|
| 140 |
raise RuntimeError(msg)
|
| 141 |
return full_summary
|
| 142 |
-
|
| 143 |
-
def get_summary(paras, pstart):
|
| 144 |
-
chunks = chunkify(paras, pstart)
|
| 145 |
-
summary = summarize_chunks(chunks)
|
| 146 |
-
chunks = chunkify(paras, pstart)
|
| 147 |
-
summary = summarize_chunks(chunks)
|
| 148 |
-
with st.container(border=True):
|
| 149 |
-
st.subheader("Summary")
|
| 150 |
-
st.text_area(" ",summary, height=60)
|
| 151 |
-
return (summary)
|
| 152 |
-
|
| 153 |
if True:
|
| 154 |
pipe = pipeline("summarization", device=device)
|
| 155 |
tokenizer = pipe.tokenizer
|
| 156 |
-
|
| 157 |
if True:
|
| 158 |
headlines, links = get_headlines_links()
|
| 159 |
hindex = get_hindex(headlines)
|
|
@@ -163,13 +153,10 @@ if True:
|
|
| 163 |
with st.container(border=True):
|
| 164 |
st.subheader("Full Story")
|
| 165 |
st.text_area(" ",output, height=400)
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
get_summary(paras, pstart)
|
| 174 |
-
st.write("Done")
|
| 175 |
-
|
|
|
|
| 4 |
import requests
|
| 5 |
import torch
|
| 6 |
import requests
|
|
|
|
| 7 |
from bs4 import BeautifulSoup
|
| 8 |
from transformers import pipeline, AutoTokenizer
|
| 9 |
|
|
|
|
| 12 |
print("GPU is available")
|
| 13 |
device = 0
|
| 14 |
else:
|
| 15 |
+
print("GPU is NOT available")
|
| 16 |
+
st.write("be patient, this will take some time on the CPU")
|
| 17 |
device = -1
|
|
|
|
| 18 |
|
| 19 |
url="https://text.npr.org"
|
| 20 |
def get_headlines_links():
|
|
|
|
| 51 |
|
| 52 |
def get_hindex(headlines):
|
| 53 |
global url
|
| 54 |
+
sbox = st.selectbox(f"Pick a headline here from NPR and get a summary below",
|
|
|
|
| 55 |
options=headlines)
|
| 56 |
+
hindex = headlines.index(sbox)
|
| 57 |
+
paras, pstart = get_story(hindex)
|
| 58 |
+
#st.text(paras)
|
| 59 |
+
return hindex
|
| 60 |
+
|
| 61 |
+
|
| 62 |
|
| 63 |
def get_story(index):
|
| 64 |
global links
|
|
|
|
| 105 |
raise RuntimeError("paragraph too large, tokens: {num_tokens}")
|
| 106 |
|
| 107 |
tokens = tokenizer(long_text)
|
| 108 |
+
#print(f" cur_p {cur_p} tokens {len(tokens.input_ids)} ")
|
| 109 |
chunks.append(long_text)
|
| 110 |
long_text = ''
|
| 111 |
num_tokens = 0
|
|
|
|
| 117 |
tokens = tokenizer(chunk)
|
| 118 |
num_tokens = len(tokens[0])
|
| 119 |
words = [x for x in chunk.split() if x]
|
| 120 |
+
#print(f" {ci} has {len(words)} words")
|
| 121 |
if num_tokens >= HARD_MAX_TOKENS:
|
| 122 |
raise ValueError(f"{ci} {num_tokens}")
|
| 123 |
return chunks
|
|
|
|
| 128 |
for ci, chunk in enumerate(chunks):
|
| 129 |
tokens = tokenizer(chunk)
|
| 130 |
num_tokens = len(tokens.input_ids)
|
| 131 |
+
#print( f"chunk {ci} has {num_tokens} tokens")
|
| 132 |
psummary = pipe(chunk, max_length=140, min_length=20, do_sample=False)
|
| 133 |
psummary = psummary[0]["summary_text"]
|
| 134 |
psummary_words = len(psummary.split())
|
| 135 |
+
#print(f" {psummary_words}")
|
| 136 |
full_summary += psummary
|
| 137 |
except Exception:
|
| 138 |
exc_type, exc_val, exc_tb = sys.exc_info()
|
|
|
|
| 141 |
print(msg)
|
| 142 |
raise RuntimeError(msg)
|
| 143 |
return full_summary
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
if True:
|
| 145 |
pipe = pipeline("summarization", device=device)
|
| 146 |
tokenizer = pipe.tokenizer
|
|
|
|
| 147 |
if True:
|
| 148 |
headlines, links = get_headlines_links()
|
| 149 |
hindex = get_hindex(headlines)
|
|
|
|
| 153 |
with st.container(border=True):
|
| 154 |
st.subheader("Full Story")
|
| 155 |
st.text_area(" ",output, height=400)
|
| 156 |
+
chunks = chunkify(paras, pstart)
|
| 157 |
+
summary = summarize_chunks(chunks)
|
| 158 |
+
chunks = chunkify(paras, pstart)
|
| 159 |
+
summary = summarize_chunks(chunks)
|
| 160 |
+
with st.container(border=True):
|
| 161 |
+
st.subheader("Summary")
|
| 162 |
+
st.text_area(" ",summary, height=200)
|
|
|
|
|
|
|
|
|