-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
77 lines (59 loc) · 2.23 KB
/
Copy pathapp.py
File metadata and controls
77 lines (59 loc) · 2.23 KB
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
import gradio as gr
from transformers import pipeline
# Load the model
model = pipeline("summarization", model="facebook/bart-large-cnn")
# Load example text
with open("example.txt", "r", encoding="utf-8") as file:
example_text = file.read()
def predict(text, max_length=200):
summary = model(text, max_length=max_length)[0]["summary_text"]
return summary
def reset():
return "", "", "", gr.update(interactive=True)
def get_token_length(text):
token_length = len(text.split())
color = "red" if token_length > 750 else "green"
button_state = gr.update(interactive=token_length <= 750)
return (
f'<span style="color:{color}">Input Token Length: {token_length}</span>',
button_state,
)
def load_example():
return example_text
# Create a Gradio interface to summarize text with a text box input
with gr.Blocks() as demo:
gr.Markdown("# 📝 Text Summarization App")
gr.Markdown("### ✨ Enter text to get a summary")
load_example_button = gr.Button("📄 Load Example", size="sm")
token_length_label = gr.HTML(value="Input Token Length: 0")
input_text = gr.Textbox(label="Input Text")
with gr.Row():
summarize_button = gr.Button("🔍 Summarize", interactive=True, size="medium")
reset_button = gr.Button("🔄 Reset", size="medium")
output_text = gr.Textbox(label="Summary")
with gr.Row():
feedback_button = gr.Button("👍 Provide Feedback", size="medium")
input_text.change( # pylint: disable=E1101
fn=get_token_length,
inputs=input_text,
outputs=[token_length_label, summarize_button],
)
summarize_button.click( # pylint: disable=E1101
fn=predict,
inputs=input_text,
outputs=output_text,
)
reset_button.click( # pylint: disable=E1101
fn=reset,
inputs=None,
outputs=[input_text, output_text, token_length_label, summarize_button],
)
load_example_button.click( # pylint: disable=E1101
fn=load_example,
inputs=None,
outputs=input_text,
)
gr.Markdown(
"[Visit Bart Model](https://huggingface.co/facebook/bart-large-cnn) | [Visit Project](https://github.com/jaypat7828/summarization)"
)
demo.launch(share=True)