Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from utils import get_textrank_summary
|
4 |
+
|
5 |
+
def textrank_summarizer(text, level, max_tokens, verbose):
|
6 |
+
summary, verbose_info = get_textrank_summary(text=text, level=level, token_count=max_tokens, verbose=verbose)
|
7 |
+
return summary, verbose_info
|
8 |
+
|
9 |
+
def option2_summarizer(text, max_tokens):
|
10 |
+
return f"[Option2 Summary]\n{text[:max_tokens]}...", ""
|
11 |
+
|
12 |
+
def option3_summarizer(text, max_tokens):
|
13 |
+
return f"[Option3 Summary]\n{text[:max_tokens]}...", ""
|
14 |
+
|
15 |
+
def summarize_text(text, method, level, max_tokens, verbose):
|
16 |
+
if method == "TextRank":
|
17 |
+
return textrank_summarizer(text, level, max_tokens, verbose)
|
18 |
+
elif method == "Option2":
|
19 |
+
return option2_summarizer(text, max_tokens)
|
20 |
+
elif method == "Option3":
|
21 |
+
return option3_summarizer(text, max_tokens)
|
22 |
+
return "Invalid method selected.", "Invalid method selected."
|
23 |
+
|
24 |
+
def process_file(file, method, level, max_tokens, verbose):
|
25 |
+
try:
|
26 |
+
if file.name.endswith(".csv"):
|
27 |
+
df = pd.read_csv(file.name)
|
28 |
+
elif file.name.endswith(".xlsx"):
|
29 |
+
df = pd.read_excel(file.name)
|
30 |
+
else:
|
31 |
+
return "Unsupported file format.", ""
|
32 |
+
|
33 |
+
summaries = []
|
34 |
+
logs = []
|
35 |
+
for column in df.columns:
|
36 |
+
column_summary, verbose_log = summarize_text(
|
37 |
+
" ".join(df[column].astype(str).tolist()),
|
38 |
+
method, level, max_tokens, verbose
|
39 |
+
)
|
40 |
+
summaries.append({"Column": column, "Summary": column_summary})
|
41 |
+
logs.append(f"Column: {column}\n{verbose_log}")
|
42 |
+
|
43 |
+
return pd.DataFrame(summaries), "\n\n".join(logs)
|
44 |
+
except Exception as e:
|
45 |
+
return f"Error processing file: {e}", ""
|
46 |
+
|
47 |
+
with gr.Blocks() as demo:
|
48 |
+
with gr.Tabs():
|
49 |
+
with gr.TabItem("Text Summarization"):
|
50 |
+
text_input = gr.TextArea(label="Text", placeholder="Enter your text here...", lines=5)
|
51 |
+
summarization_method = gr.Radio([
|
52 |
+
"TextRank", "Option2", "Option3"
|
53 |
+
], value="TextRank", label="Summarization Method")
|
54 |
+
level_selector = gr.Radio([
|
55 |
+
"sentence", "paragraph"
|
56 |
+
], value="sentence", label="Level", visible=True)
|
57 |
+
max_tokens_slider = gr.Slider(0, 4096, value=100, label="Max Tokens")
|
58 |
+
verbose_checkbox = gr.Checkbox(label="Verbose Mode", value=False)
|
59 |
+
summarize_button = gr.Button("Summarize", interactive=False)
|
60 |
+
output = gr.TextArea(label="Summary", interactive=False, lines=5, show_copy_button=True)
|
61 |
+
verbose_output = gr.Markdown(label="Verbose Logs")
|
62 |
+
|
63 |
+
def toggle_level_selector(method):
|
64 |
+
return gr.update(visible=(method == "TextRank"))
|
65 |
+
|
66 |
+
def toggle_summarize_button(method):
|
67 |
+
return gr.update(interactive=(len(method) != 0))
|
68 |
+
|
69 |
+
def toggle_verbose_logs(verbose_mode):
|
70 |
+
return gr.update(visible=verbose_mode)
|
71 |
+
|
72 |
+
|
73 |
+
summarization_method.change(toggle_level_selector, inputs=summarization_method, outputs=level_selector)
|
74 |
+
text_input.change(toggle_summarize_button, inputs=text_input, outputs=summarize_button)
|
75 |
+
verbose_checkbox.change(toggle_verbose_logs, inputs=verbose_checkbox, outputs=verbose_output)
|
76 |
+
|
77 |
+
summarize_button.click(
|
78 |
+
summarize_text,
|
79 |
+
inputs=[text_input, summarization_method, level_selector, max_tokens_slider, verbose_checkbox],
|
80 |
+
outputs=[output, verbose_output],
|
81 |
+
show_progress=True
|
82 |
+
)
|
83 |
+
|
84 |
+
with gr.TabItem("File Processing"):
|
85 |
+
file_input = gr.File(label="Upload File (xlsx, csv)")
|
86 |
+
summarization_method_file = gr.Radio([
|
87 |
+
"TextRank", "Option2", "Option3"
|
88 |
+
], value="TextRank", label="Summarization Method")
|
89 |
+
level_selector_file = gr.Radio([
|
90 |
+
"sentence", "paragraph"
|
91 |
+
], value="sentence", label="Level", visible=True)
|
92 |
+
max_tokens_slider_file = gr.Slider(0, 4096, value=100, label="Max Tokens")
|
93 |
+
verbose_checkbox_file = gr.Checkbox(label="Verbose Mode", value=False)
|
94 |
+
load_button = gr.Button("Load and Process")
|
95 |
+
file_output = gr.Dataframe()
|
96 |
+
verbose_file_output = gr.TextArea(label="Verbose Logs", interactive=False, lines=10)
|
97 |
+
|
98 |
+
def toggle_level_selector_file(method):
|
99 |
+
return gr.update(visible=(method == "TextRank"))
|
100 |
+
|
101 |
+
summarization_method_file.change(toggle_level_selector_file, inputs=summarization_method_file, outputs=level_selector_file)
|
102 |
+
|
103 |
+
load_button.click(
|
104 |
+
process_file,
|
105 |
+
inputs=[file_input, summarization_method_file, level_selector_file, max_tokens_slider_file, verbose_checkbox_file],
|
106 |
+
outputs=[file_output, verbose_file_output],
|
107 |
+
show_progress=True
|
108 |
+
)
|
109 |
+
|
110 |
+
demo.launch()
|