SyedMohammedSathiq commited on
Commit
4492537
·
verified ·
1 Parent(s): 7635e33
Files changed (1) hide show
  1. app.py +37 -26
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 (without float16 for CPU)
6
  pipe = pipeline("summarization", model="Falconsai/text_summarization")
7
 
8
- # Store the history
9
- history = []
10
 
11
  # Define the summarize function
12
- def summarize(input, clear_history=False):
13
- # Clear history if the flag is set
 
 
14
  if clear_history:
15
- history.clear()
16
- return "History cleared!"
17
 
18
- # Get the summary
19
- output = pipe(input)
20
  summary = output[0]['summary_text']
21
-
22
- # Store the summary in history
23
- history.append(summary)
24
-
25
- # Return the summary along with the history
26
- return summary, history
27
 
28
  # Define the Gradio interface
29
- iface = gr.Interface(
30
- fn=summarize,
31
- inputs=[
32
- gr.Textbox(lines=10, placeholder="Enter text to summarize here..."),
33
- gr.Checkbox(label="Clear history", value=False) # Checkbox to clear history
34
- ],
35
- outputs=["text", "json"], # Show summary and history in json format
36
- title="Text Summarizer",
37
- description="Enter a long piece of text, and the summarizer will provide a concise summary. You can also clear the history of summaries."
38
- )
 
 
 
 
 
 
 
 
 
39
 
40
  # Launch the interface
41
  if __name__ == "__main__":
42
- iface.launch()
 
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()