Duplicate from chinhon/headline_writer
Browse filesCo-authored-by: Chua chin Hon <[email protected]>
- .gitattributes +27 -0
- README.md +38 -0
- app.py +62 -0
- requirements.txt +4 -0
.gitattributes
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bin.* filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
20 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Headline_writer
|
3 |
+
emoji: 👁
|
4 |
+
colorFrom: pink
|
5 |
+
colorTo: purple
|
6 |
+
sdk: gradio
|
7 |
+
app_file: app.py
|
8 |
+
pinned: false
|
9 |
+
duplicated_from: chinhon/headline_writer
|
10 |
+
---
|
11 |
+
|
12 |
+
# Configuration
|
13 |
+
|
14 |
+
`title`: _string_
|
15 |
+
Display title for the Space
|
16 |
+
|
17 |
+
`emoji`: _string_
|
18 |
+
Space emoji (emoji-only character allowed)
|
19 |
+
|
20 |
+
`colorFrom`: _string_
|
21 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
22 |
+
|
23 |
+
`colorTo`: _string_
|
24 |
+
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
25 |
+
|
26 |
+
`sdk`: _string_
|
27 |
+
Can be either `gradio` or `streamlit`
|
28 |
+
|
29 |
+
`sdk_version` : _string_
|
30 |
+
Only applicable for `streamlit` SDK.
|
31 |
+
See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
|
32 |
+
|
33 |
+
`app_file`: _string_
|
34 |
+
Path to your main application file (which contains either `gradio` or `streamlit` Python code).
|
35 |
+
Path is relative to the root of the repository.
|
36 |
+
|
37 |
+
`pinned`: _boolean_
|
38 |
+
Whether the Space stays on top of your list.
|
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import re
|
3 |
+
|
4 |
+
from transformers import (
|
5 |
+
AutoTokenizer,
|
6 |
+
AutoModelForSeq2SeqLM,
|
7 |
+
)
|
8 |
+
|
9 |
+
def clean_text(text):
|
10 |
+
text = text.encode("ascii", errors="ignore").decode(
|
11 |
+
"ascii"
|
12 |
+
) # remove non-ascii, Chinese characters
|
13 |
+
text = re.sub(r"http\S+", "", text)
|
14 |
+
text = re.sub(r"\n", " ", text)
|
15 |
+
text = re.sub(r"\n\n", " ", text)
|
16 |
+
text = re.sub(r"\t", " ", text)
|
17 |
+
text = re.sub(r"ADVERTISEMENT", " ", text)
|
18 |
+
text = text.strip(" ")
|
19 |
+
text = re.sub(
|
20 |
+
" +", " ", text
|
21 |
+
).strip() # get rid of multiple spaces and replace with a single
|
22 |
+
return text
|
23 |
+
|
24 |
+
|
25 |
+
model_name = "chinhon/pegasus-newsroom-headline_writer_57k"
|
26 |
+
|
27 |
+
def headline_writer(text):
|
28 |
+
input_text = clean_text(text)
|
29 |
+
|
30 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
31 |
+
|
32 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
33 |
+
|
34 |
+
with tokenizer.as_target_tokenizer():
|
35 |
+
batch = tokenizer(
|
36 |
+
input_text,
|
37 |
+
truncation=True,
|
38 |
+
padding="longest",
|
39 |
+
return_tensors="pt",
|
40 |
+
)
|
41 |
+
|
42 |
+
raw_write = model.generate(**batch)
|
43 |
+
|
44 |
+
headline = tokenizer.batch_decode(
|
45 |
+
raw_write, skip_special_tokens=True, min_length=200, length_penalty=50.5
|
46 |
+
)
|
47 |
+
|
48 |
+
return headline[0]
|
49 |
+
|
50 |
+
|
51 |
+
gradio_ui = gr.Interface(
|
52 |
+
fn=headline_writer,
|
53 |
+
title="Generate News Headlines with AI",
|
54 |
+
description="Too busy or tired to write a headline? Try this instead.",
|
55 |
+
inputs=gr.inputs.Textbox(
|
56 |
+
lines=20, label="Paste the first few paras of your news story here"
|
57 |
+
),
|
58 |
+
outputs=gr.outputs.Textbox(label="Suggested Headline"),
|
59 |
+
theme="darkdefault"
|
60 |
+
)
|
61 |
+
|
62 |
+
gradio_ui.launch(enable_queue=True)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
regex
|
3 |
+
torch
|
4 |
+
transformers
|