Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
CHANGED
@@ -2,41 +2,52 @@ import torch
|
|
2 |
import gradio as gr
|
3 |
from transformers import pipeline
|
4 |
|
5 |
-
# Initialize the summarization pipeline
|
6 |
pipe = pipeline("summarization", model="Falconsai/text_summarization")
|
7 |
|
8 |
-
# Store
|
9 |
-
|
10 |
|
11 |
# Define the summarize function
|
12 |
-
def summarize(
|
13 |
-
|
|
|
|
|
14 |
if clear_history:
|
15 |
-
|
16 |
-
return
|
17 |
|
18 |
-
#
|
19 |
-
output = pipe(
|
20 |
summary = output[0]['summary_text']
|
21 |
-
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
# Return the
|
26 |
-
return
|
27 |
|
28 |
# Define the Gradio interface
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
# Launch the interface
|
41 |
if __name__ == "__main__":
|
42 |
-
|
|
|
2 |
import gradio as gr
|
3 |
from transformers import pipeline
|
4 |
|
5 |
+
# Initialize the summarization pipeline
|
6 |
pipe = pipeline("summarization", model="Falconsai/text_summarization")
|
7 |
|
8 |
+
# Store chat history as a list of tuples [(user_input, summary), ...]
|
9 |
+
chat_history = []
|
10 |
|
11 |
# Define the summarize function
|
12 |
+
def summarize(input_text, clear_history=False):
|
13 |
+
global chat_history
|
14 |
+
|
15 |
+
# Clear history if requested
|
16 |
if clear_history:
|
17 |
+
chat_history = []
|
18 |
+
return chat_history
|
19 |
|
20 |
+
# Generate the summary
|
21 |
+
output = pipe(input_text)
|
22 |
summary = output[0]['summary_text']
|
23 |
+
|
24 |
+
# Append the user's input and the summary to chat history
|
25 |
+
chat_history.append(("User: " + input_text, "Summarizer: " + summary))
|
26 |
+
|
27 |
+
# Return the updated chat history
|
28 |
+
return chat_history
|
29 |
|
30 |
# Define the Gradio interface
|
31 |
+
with gr.Blocks() as interface:
|
32 |
+
# Title and description
|
33 |
+
gr.Markdown("# ChatGPT-like Text Summarizer")
|
34 |
+
gr.Markdown("Enter a long piece of text, and the summarizer will provide a concise summary. History will appear like a chat interface.")
|
35 |
+
|
36 |
+
# Input section
|
37 |
+
with gr.Row():
|
38 |
+
input_text = gr.Textbox(lines=10, placeholder="Enter text to summarize here...", label="Input Text")
|
39 |
+
clear_history_btn = gr.Button("Clear History")
|
40 |
+
|
41 |
+
# Chatbot-style output
|
42 |
+
chatbot = gr.Chatbot(label="History")
|
43 |
+
|
44 |
+
# Submit button for summarization
|
45 |
+
submit_button = gr.Button("Summarize")
|
46 |
+
|
47 |
+
# Functionality for buttons
|
48 |
+
submit_button.click(summarize, inputs=[input_text, gr.State(False)], outputs=chatbot)
|
49 |
+
clear_history_btn.click(summarize, inputs=["", gr.State(True)], outputs=chatbot)
|
50 |
|
51 |
# Launch the interface
|
52 |
if __name__ == "__main__":
|
53 |
+
interface.launch()
|