Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,52 @@
|
|
1 |
-
|
|
|
2 |
from bs4 import BeautifulSoup
|
3 |
import requests
|
4 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
search_results.append(url)
|
17 |
-
except TypeError:
|
18 |
-
for url in search(query, num_results=num_results):
|
19 |
-
if any(domain in url for domain in ["tesla.com", "cnbc.com", "reuters.com", "bloomberg.com", "investopedia.com"]):
|
20 |
-
search_results.append(url)
|
21 |
-
return search_results
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
soup = BeautifulSoup(response.text, 'html.parser')
|
29 |
-
|
30 |
-
# Extract relevant content (e.g., paragraphs or sections)
|
31 |
-
paragraphs = [p.text for p in soup.find_all('p')]
|
32 |
-
combined_text = " ".join(paragraphs[:3]) # Combine first few paragraphs for summary
|
33 |
-
|
34 |
-
# Tokenize the text
|
35 |
-
inputs = tokenizer.encode("summarize: " + combined_text, return_tensors="pt", max_length=1024, truncation=True)
|
36 |
-
|
37 |
-
# Generate summary
|
38 |
-
summary_ids = model.generate(inputs, max_length=150, min_length=40, length_penalty=2.0, num_beams=4, early_stopping=True)
|
39 |
-
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
40 |
-
|
41 |
-
return summary
|
42 |
-
except requests.RequestException as e:
|
43 |
-
return None
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
if summaries:
|
58 |
-
return "\n\n".join(summaries)
|
59 |
-
else:
|
60 |
-
return "No relevant information found."
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
print("Intelligent Assistant")
|
65 |
-
question = input("Enter your query: ")
|
66 |
-
keywords = input("Enter specific keywords (e.g., 'Q1 2024 financial results Tesla'): ")
|
67 |
-
answer = google_search_and_answer(question, keywords)
|
68 |
-
print("Answer:", answer)
|
69 |
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
from bs4 import BeautifulSoup
|
4 |
import requests
|
|
|
5 |
|
6 |
+
def summarize_blog_post(url):
|
7 |
+
# Load summarization pipeline
|
8 |
+
summarizer = pipeline("summarization")
|
9 |
|
10 |
+
# Get blog post content
|
11 |
+
r = requests.get(url)
|
12 |
+
soup = BeautifulSoup(r.text, 'html.parser')
|
13 |
+
results = soup.find_all(['h1', 'p'])
|
14 |
+
text = [result.text for result in results]
|
15 |
+
ARTICLE = ' '.join(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
# Chunk text
|
18 |
+
max_chunk = 500
|
19 |
+
ARTICLE = ARTICLE.replace('.', '.<eos>')
|
20 |
+
ARTICLE = ARTICLE.replace('?', '?<eos>')
|
21 |
+
ARTICLE = ARTICLE.replace('!', '!<eos>')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
sentences = ARTICLE.split('<eos>')
|
24 |
+
current_chunk = 0
|
25 |
+
chunks = []
|
26 |
+
for sentence in sentences:
|
27 |
+
if len(chunks) == current_chunk + 1:
|
28 |
+
if len(chunks[current_chunk]) + len(sentence.split(' ')) <= max_chunk:
|
29 |
+
chunks[current_chunk].extend(sentence.split(' '))
|
30 |
+
else:
|
31 |
+
current_chunk += 1
|
32 |
+
chunks.append(sentence.split(' '))
|
33 |
+
else:
|
34 |
+
chunks.append(sentence.split(' '))
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
for chunk_id in range(len(chunks)):
|
37 |
+
chunks[chunk_id] = ' '.join(chunks[chunk_id])
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
# Summarize text
|
40 |
+
summaries = summarizer(chunks, max_length=120, min_length=30, do_sample=False)
|
41 |
+
summary_text = " ".join([summary['summary_text'] for summary in summaries])
|
42 |
+
return summary_text
|
43 |
+
|
44 |
+
iface = gr.Interface(
|
45 |
+
fn=summarize_blog_post,
|
46 |
+
inputs="text",
|
47 |
+
outputs="text",
|
48 |
+
title="Medium Blog Post Summarizer",
|
49 |
+
description="Enter the URL of a Medium blog post to get a summarized version of the content."
|
50 |
+
)
|
51 |
+
|
52 |
+
iface.launch()
|