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+
from transformers import T5Tokenizer, T5ForConditionalGeneration, BartTokenizer, BartForConditionalGeneration
|
3 |
+
import requests
|
4 |
+
from bs4 import BeautifulSoup
|
5 |
+
import gtts
|
6 |
+
from io import BytesIO
|
7 |
+
import base64
|
8 |
+
import os
|
9 |
+
|
10 |
+
# Function to fetch text from a URL
|
11 |
+
def fetch_text_from_url(url):
|
12 |
+
try:
|
13 |
+
response = requests.get(url)
|
14 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
15 |
+
paragraphs = soup.find_all('p')
|
16 |
+
text = ' '.join([para.get_text() for para in paragraphs])
|
17 |
+
return text, None
|
18 |
+
except Exception as e:
|
19 |
+
return None, f"Error fetching URL: {e}"
|
20 |
+
|
21 |
+
# Function to summarize text using T5
|
22 |
+
def summarize_t5(text, size):
|
23 |
+
model_name = "t5-small"
|
24 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
25 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
26 |
+
|
27 |
+
input_text = f"summarize: {text}"
|
28 |
+
inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
|
29 |
+
|
30 |
+
if size == "Short":
|
31 |
+
max_len = 50
|
32 |
+
elif size == "Medium":
|
33 |
+
max_len = 100
|
34 |
+
else: # Long
|
35 |
+
max_len = 200
|
36 |
+
|
37 |
+
summary_ids = model.generate(inputs["input_ids"], max_length=max_len, min_length=10, length_penalty=2.0, num_beams=4)
|
38 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
39 |
+
return summary
|
40 |
+
|
41 |
+
# Function to summarize text using BART
|
42 |
+
def summarize_bart(text, size):
|
43 |
+
model_name = "facebook/bart-large-cnn"
|
44 |
+
tokenizer = BartTokenizer.from_pretrained(model_name)
|
45 |
+
model = BartForConditionalGeneration.from_pretrained(model_name)
|
46 |
+
|
47 |
+
inputs = tokenizer(text, return_tensors="pt", max_length=1024, truncation=True)
|
48 |
+
|
49 |
+
if size == "Short":
|
50 |
+
max_len = 50
|
51 |
+
elif size == "Medium":
|
52 |
+
max_len = 100
|
53 |
+
else: # Long
|
54 |
+
max_len = 200
|
55 |
+
|
56 |
+
summary_ids = model.generate(inputs["input_ids"], max_length=max_len, min_length=10, length_penalty=2.0, num_beams=4)
|
57 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
58 |
+
return summary
|
59 |
+
|
60 |
+
# Function to convert text to speech and save as a file
|
61 |
+
def text_to_speech(text):
|
62 |
+
tts = gtts.gTTS(text)
|
63 |
+
audio_file_path = "summary_audio.mp3"
|
64 |
+
tts.save(audio_file_path)
|
65 |
+
return audio_file_path
|
66 |
+
|
67 |
+
# Main function to handle summarization
|
68 |
+
def summarize_news(input_type, text_input, url_input, model_choice, size_choice):
|
69 |
+
# Determine the input text based on the input type
|
70 |
+
if input_type == "Text":
|
71 |
+
if not text_input:
|
72 |
+
return "Please provide text to summarize.", None, None
|
73 |
+
input_text = text_input
|
74 |
+
else: # URL
|
75 |
+
if not url_input:
|
76 |
+
return "Please provide a URL to summarize.", None, None
|
77 |
+
input_text, error = fetch_text_from_url(url_input)
|
78 |
+
if error:
|
79 |
+
return error, None, None
|
80 |
+
|
81 |
+
# Summarize the text
|
82 |
+
if model_choice == "T5":
|
83 |
+
summary = summarize_t5(input_text, size_choice)
|
84 |
+
else: # BART
|
85 |
+
summary = summarize_bart(input_text, size_choice)
|
86 |
+
|
87 |
+
# Generate audio for the summary
|
88 |
+
audio_file = text_to_speech(summary)
|
89 |
+
|
90 |
+
return summary, audio_file, None
|
91 |
+
|
92 |
+
# Custom CSS for the design
|
93 |
+
custom_css = """
|
94 |
+
<style>
|
95 |
+
/* Background for the entire app */
|
96 |
+
body {
|
97 |
+
background: linear-gradient(135deg, #E6E6FA 0%, #D8BFD8 100%) !important;
|
98 |
+
font-family: 'Arial', sans-serif;
|
99 |
+
min-height: 100vh;
|
100 |
+
margin: 0;
|
101 |
+
display: flex;
|
102 |
+
justify-content: center;
|
103 |
+
align-items: center;
|
104 |
+
}
|
105 |
+
/* White container for all elements */
|
106 |
+
.container {
|
107 |
+
background-color: #FFFFFF !important;
|
108 |
+
border-radius: 15px !important;
|
109 |
+
padding: 30px !important;
|
110 |
+
margin: 20px auto !important;
|
111 |
+
max-width: 800px !important;
|
112 |
+
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1) !important;
|
113 |
+
width: 100%;
|
114 |
+
}
|
115 |
+
/* Title styling */
|
116 |
+
.title {
|
117 |
+
font-size: 36px;
|
118 |
+
color: #000000 !important;
|
119 |
+
text-align: center;
|
120 |
+
font-weight: bold;
|
121 |
+
margin-bottom: 10px;
|
122 |
+
}
|
123 |
+
/* Subtitle styling */
|
124 |
+
.subtitle {
|
125 |
+
font-size: 18px;
|
126 |
+
color: #000000 !important;
|
127 |
+
text-align: center;
|
128 |
+
margin-bottom: 20px;
|
129 |
+
}
|
130 |
+
/* Labels for inputs */
|
131 |
+
label {
|
132 |
+
color: #000000 !important;
|
133 |
+
}
|
134 |
+
/* Input and textarea styling */
|
135 |
+
input, textarea {
|
136 |
+
background-color: #F5F5F5 !important;
|
137 |
+
color: #000000 !important;
|
138 |
+
border: 1px solid #D3D3D3 !important;
|
139 |
+
border-radius: 5px !important;
|
140 |
+
}
|
141 |
+
/* Dropdown styling */
|
142 |
+
select {
|
143 |
+
background-color: #F5F5F5 !important;
|
144 |
+
color: #000000 !important;
|
145 |
+
border: 1px solid #D3D3D3 !important;
|
146 |
+
border-radius: 5px !important;
|
147 |
+
padding: 5px !important;
|
148 |
+
}
|
149 |
+
/* Button styling */
|
150 |
+
button {
|
151 |
+
background-color: #9370DB !important;
|
152 |
+
color: white !important;
|
153 |
+
border-radius: 10px !important;
|
154 |
+
padding: 8px 20px !important;
|
155 |
+
border: none !important;
|
156 |
+
display: block !important;
|
157 |
+
margin: 20px auto !important;
|
158 |
+
cursor: pointer !important;
|
159 |
+
}
|
160 |
+
button:hover {
|
161 |
+
background-color: #4B0082 !important;
|
162 |
+
}
|
163 |
+
/* Footer styling */
|
164 |
+
.footer {
|
165 |
+
text-align: center;
|
166 |
+
color: #000000 !important;
|
167 |
+
font-size: 14px;
|
168 |
+
margin-top: 30px;
|
169 |
+
}
|
170 |
+
.footer-heart {
|
171 |
+
color: #FF0000 !important;
|
172 |
+
}
|
173 |
+
/* Output text and error messages */
|
174 |
+
.output-text, .error-text {
|
175 |
+
color: #000000 !important;
|
176 |
+
}
|
177 |
+
</style>
|
178 |
+
"""
|
179 |
+
|
180 |
+
# Gradio app
|
181 |
+
with gr.Blocks() as app:
|
182 |
+
# Inject custom CSS
|
183 |
+
gr.HTML(custom_css)
|
184 |
+
|
185 |
+
# Main container
|
186 |
+
with gr.Column(elem_classes=["container"]):
|
187 |
+
# Title and subtitle
|
188 |
+
gr.HTML('<p class="title">BBC News Summarizer</p>')
|
189 |
+
gr.HTML('<p class="subtitle">Summarize news articles with T5 or BART in your preferred length!</p>')
|
190 |
+
|
191 |
+
# Input section
|
192 |
+
input_type = gr.Radio(choices=["Text", "URL"], label="Choose input type:", value="Text")
|
193 |
+
|
194 |
+
with gr.Row():
|
195 |
+
text_input = gr.Textbox(label="Enter news text here:", lines=5, visible=True, placeholder="Paste your news text here...")
|
196 |
+
url_input = gr.Textbox(label="Enter news URL here:", visible=False, placeholder="Enter a news article URL...")
|
197 |
+
|
198 |
+
# Show/hide text input or URL input based on input type
|
199 |
+
def update_input_visibility(input_type):
|
200 |
+
return (
|
201 |
+
gr.update(visible=(input_type == "Text")),
|
202 |
+
gr.update(visible=(input_type == "URL"))
|
203 |
+
)
|
204 |
+
|
205 |
+
input_type.change(
|
206 |
+
fn=update_input_visibility,
|
207 |
+
inputs=input_type,
|
208 |
+
outputs=[text_input, url_input]
|
209 |
+
)
|
210 |
+
|
211 |
+
# Model selection
|
212 |
+
model_choice = gr.Dropdown(choices=["T5", "BART"], label="Choose summarization model:", value="T5")
|
213 |
+
|
214 |
+
# Summary size selection
|
215 |
+
size_choice = gr.Dropdown(choices=["Short", "Medium", "Long"], label="Choose summary size:", value="Short")
|
216 |
+
|
217 |
+
# Summarize button
|
218 |
+
summarize_button = gr.Button("Get Summary")
|
219 |
+
|
220 |
+
# Outputs
|
221 |
+
summary_output = gr.Textbox(label="Summary:", elem_classes=["output-text"])
|
222 |
+
audio_output = gr.Audio(label="Listen to the Summary:")
|
223 |
+
error_output = gr.Textbox(label="Error:", elem_classes=["error-text"], visible=False)
|
224 |
+
|
225 |
+
# Footer
|
226 |
+
gr.HTML('<p class="footer">Powered by xAI\'s Grok | Made with <span class="footer-heart">❤️</span> for news enthusiasts</p>')
|
227 |
+
|
228 |
+
# Button click event
|
229 |
+
summarize_button.click(
|
230 |
+
fn=summarize_news,
|
231 |
+
inputs=[input_type, text_input, url_input, model_choice, size_choice],
|
232 |
+
outputs=[summary_output, audio_output, error_output]
|
233 |
+
)
|
234 |
+
|
235 |
+
# Launch the app
|
236 |
+
app.launch()
|