Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,218 +1,72 @@
|
|
1 |
-
import requests
|
2 |
-
from bs4 import BeautifulSoup
|
3 |
-
import gradio as gr
|
4 |
-
from huggingface_hub import InferenceClient
|
5 |
-
import random
|
6 |
-
import urllib.parse
|
7 |
-
from datetime import datetime, timedelta
|
8 |
-
import re
|
9 |
import os
|
10 |
-
import
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
soup = BeautifulSoup(resp.text, "html.parser")
|
69 |
-
result_block = soup.find_all("div", attrs={"class": "g"})
|
70 |
-
if not result_block:
|
71 |
-
print("No more results found.")
|
72 |
-
break
|
73 |
-
for result in result_block:
|
74 |
-
if len(all_results) >= num_results:
|
75 |
-
break
|
76 |
-
link = result.find("a", href=True)
|
77 |
-
if link:
|
78 |
-
link = link["href"]
|
79 |
-
print(f"Found link: {link}")
|
80 |
-
try:
|
81 |
-
webpage = session.get(link, headers=headers, timeout=timeout)
|
82 |
-
webpage.raise_for_status()
|
83 |
-
visible_text = extract_text_from_webpage(webpage.text)
|
84 |
-
all_results.append({"link": link, "text": visible_text})
|
85 |
-
except requests.exceptions.HTTPError as e:
|
86 |
-
if e.response.status_code == 403:
|
87 |
-
print(f"403 Forbidden error for {link}, skipping...")
|
88 |
-
else:
|
89 |
-
print(f"HTTP error {e.response.status_code} for {link}, skipping...")
|
90 |
-
except requests.exceptions.RequestException as e:
|
91 |
-
print(f"Error fetching or processing {link}: {e}")
|
92 |
-
else:
|
93 |
-
print("No link found in result.")
|
94 |
-
start += len(result_block)
|
95 |
-
attempts += 1
|
96 |
-
except requests.exceptions.RequestException as e:
|
97 |
-
print(f"Error fetching search results: {e}")
|
98 |
-
attempts += 1
|
99 |
-
print(f"Total results fetched: {len(all_results)}")
|
100 |
-
return all_results
|
101 |
-
def extract_text_from_webpage(html_content):
|
102 |
-
"""Extract visible text from HTML content"""
|
103 |
-
soup = BeautifulSoup(html_content, 'html.parser')
|
104 |
-
# Remove script and style elements
|
105 |
-
for script in soup(["script", "style"]):
|
106 |
-
script.decompose()
|
107 |
-
# Get text
|
108 |
-
text = soup.get_text()
|
109 |
-
# Break into lines and remove leading and trailing space on each
|
110 |
-
lines = (line.strip() for line in text.splitlines())
|
111 |
-
# Break multi-headlines into a line each
|
112 |
-
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
|
113 |
-
# Drop blank lines
|
114 |
-
text = '\n'.join(chunk for chunk in chunks if chunk)
|
115 |
-
return text
|
116 |
-
def filter_relevant_content(text):
|
117 |
-
"""Filter out irrelevant content"""
|
118 |
-
# List of keywords related to financial reports
|
119 |
-
keywords = ['revenue', 'profit', 'earnings', 'financial', 'quarter', 'fiscal', 'growth', 'income', 'loss', 'dividend']
|
120 |
-
# Split the text into sentences
|
121 |
-
sentences = re.split(r'(?<=[.!?])\s+', text)
|
122 |
-
# Filter sentences containing at least one keyword
|
123 |
-
relevant_sentences = [sentence for sentence in sentences if any(keyword in sentence.lower() for keyword in keywords)]
|
124 |
-
# Join the relevant sentences back into a single string
|
125 |
-
filtered_text = ' '.join(relevant_sentences)
|
126 |
-
return filtered_text
|
127 |
-
def chunk_text(text, max_chunk_size=1000, overlap=100):
|
128 |
-
# List of keywords that might indicate new sections
|
129 |
-
section_keywords = ["revenue", "income", "profit", "loss", "expenses", "outlook", "forecast", "quarter", "year"]
|
130 |
-
# Split text into sentences
|
131 |
-
sentences = re.split(r'(?<=[.!?])\s+', text)
|
132 |
-
chunks = []
|
133 |
-
current_chunk = ""
|
134 |
-
for sentence in sentences:
|
135 |
-
if len(current_chunk) + len(sentence) > max_chunk_size:
|
136 |
-
# If adding this sentence exceeds max_chunk_size, start a new chunk
|
137 |
-
chunks.append(current_chunk.strip())
|
138 |
-
current_chunk = sentence + " "
|
139 |
-
elif any(keyword in sentence.lower() for keyword in section_keywords):
|
140 |
-
# If sentence contains a section keyword, start a new chunk
|
141 |
-
if current_chunk:
|
142 |
-
chunks.append(current_chunk.strip())
|
143 |
-
current_chunk = sentence + " "
|
144 |
-
else:
|
145 |
-
current_chunk += sentence + " "
|
146 |
-
# Add the last chunk if it's not empty
|
147 |
-
if current_chunk:
|
148 |
-
chunks.append(current_chunk.strip())
|
149 |
-
# Add overlap
|
150 |
-
overlapped_chunks = []
|
151 |
-
for i, chunk in enumerate(chunks):
|
152 |
-
if i > 0:
|
153 |
-
chunk = chunks[i-1][-overlap:] + chunk
|
154 |
-
if i < len(chunks) - 1:
|
155 |
-
chunk = chunk + chunks[i+1][:overlap]
|
156 |
-
overlapped_chunks.append(chunk)
|
157 |
-
return overlapped_chunks
|
158 |
-
def summarize_text(text, context_instructions):
|
159 |
-
chunks = chunk_text(text, max_chunk_size=3000, overlap=200)
|
160 |
-
summaries = []
|
161 |
-
for chunk in chunks:
|
162 |
-
prompt = f"""You are a financial analyst. Summarize the following text from a financial perspective:
|
163 |
-
{chunk}
|
164 |
-
{context_instructions}"""
|
165 |
-
summary = query_llama({"inputs": prompt, "parameters": {"max_length": 1000}})
|
166 |
-
if summary and isinstance(summary, list) and 'generated_text' in summary[0]:
|
167 |
-
summaries.append(summary[0]['generated_text'])
|
168 |
-
# Combine summaries
|
169 |
-
combined_summary = "\n\n".join(summaries)
|
170 |
-
# Final summarization of combined summaries
|
171 |
-
final_prompt = f"""As a financial analyst, provide a coherent and comprehensive summary of the following financial information:
|
172 |
-
{combined_summary}
|
173 |
-
Focus on the most important financial implications and analysis."""
|
174 |
-
final_summary = query_llama({"inputs": final_prompt, "parameters": {"max_length": 3000}})
|
175 |
-
if final_summary and isinstance(final_summary, list) and 'generated_text' in final_summary[0]:
|
176 |
-
return final_summary[0]['generated_text']
|
177 |
-
else:
|
178 |
-
return "Unable to generate summary due to an error."
|
179 |
-
def summarize_financial_news(query, read_pdf=False, pdf=None):
|
180 |
-
"""Search for financial news, extract relevant content
|
181 |
-
, and summarize"""
|
182 |
-
all_filtered_text = ""
|
183 |
-
if read_pdf and pdf is not None:
|
184 |
-
pdf_text = extract_text_from_pdf(pdf)
|
185 |
-
all_filtered_text += pdf_text + "\n\n"
|
186 |
-
else:
|
187 |
-
search_results = google_search(query, num_results=1)
|
188 |
-
for result in search_results:
|
189 |
-
if result['text']:
|
190 |
-
filtered_text = filter_relevant_content(result['text'])
|
191 |
-
all_filtered_text += filtered_text + "\n\n"
|
192 |
-
if not all_filtered_text:
|
193 |
-
return "No relevant financial information found."
|
194 |
-
context_instructions = "Provide a detailed, coherent summary focusing on financial implications and analysis."
|
195 |
-
return summarize_text(all_filtered_text, context_instructions)
|
196 |
-
def extract_text_from_pdf(pdf):
|
197 |
-
"""Extract text from each page of the PDF"""
|
198 |
-
reader = PyPDF2.PdfFileReader(pdf)
|
199 |
-
text = ""
|
200 |
-
for page_num in range(reader.getNumPages()):
|
201 |
-
page = reader.getPage(page_num)
|
202 |
-
text += page.extract_text() + "\n"
|
203 |
-
return text
|
204 |
-
# Gradio Interface
|
205 |
-
def interface_function(query, read_pdf, pdf):
|
206 |
-
return summarize_financial_news(query, read_pdf, pdf)
|
207 |
iface = gr.Interface(
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
)
|
|
|
218 |
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import gradio as gr
|
3 |
+
from PyPDF2 import PdfReader
|
4 |
+
import requests
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
# Load environment variables
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# Get the Hugging Face API token
|
11 |
+
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
12 |
+
|
13 |
+
def summarize_text(text, instructions):
|
14 |
+
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf"
|
15 |
+
headers = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"}
|
16 |
+
|
17 |
+
payload = {
|
18 |
+
"inputs": f"{instructions}\n\nText to summarize:\n{text}",
|
19 |
+
"parameters": {"max_length": 500}
|
20 |
+
}
|
21 |
+
|
22 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
23 |
+
return response.json()[0]["generated_text"]
|
24 |
+
|
25 |
+
def process_pdf(pdf_file, chunk_instructions, final_instructions):
|
26 |
+
# Read PDF
|
27 |
+
reader = PdfReader(pdf_file)
|
28 |
+
text = ""
|
29 |
+
for page in reader.pages:
|
30 |
+
text += page.extract_text() + "\n\n"
|
31 |
+
|
32 |
+
# Chunk the text (simple splitting by pages for this example)
|
33 |
+
chunks = text.split("\n\n")
|
34 |
+
|
35 |
+
# Agent 1: Summarize each chunk
|
36 |
+
agent1_summaries = []
|
37 |
+
for chunk in chunks:
|
38 |
+
summary = summarize_text(chunk, chunk_instructions)
|
39 |
+
agent1_summaries.append(summary)
|
40 |
+
|
41 |
+
# Concatenate Agent 1 summaries
|
42 |
+
concatenated_summary = "\n\n".join(agent1_summaries)
|
43 |
+
|
44 |
+
# Agent 2: Final summarization
|
45 |
+
final_summary = summarize_text(concatenated_summary, final_instructions)
|
46 |
+
|
47 |
+
return final_summary
|
48 |
+
|
49 |
+
def pdf_summarizer(pdf_file, chunk_instructions, final_instructions):
|
50 |
+
if pdf_file is None:
|
51 |
+
return "Please upload a PDF file."
|
52 |
+
|
53 |
+
try:
|
54 |
+
summary = process_pdf(pdf_file.name, chunk_instructions, final_instructions)
|
55 |
+
return summary
|
56 |
+
except Exception as e:
|
57 |
+
return f"An error occurred: {str(e)}"
|
58 |
+
|
59 |
+
# Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
iface = gr.Interface(
|
61 |
+
fn=pdf_summarizer,
|
62 |
+
inputs=[
|
63 |
+
gr.File(label="Upload PDF"),
|
64 |
+
gr.Textbox(label="Chunk Instructions", placeholder="Instructions for summarizing each chunk"),
|
65 |
+
gr.Textbox(label="Final Instructions", placeholder="Instructions for final summarization")
|
66 |
+
],
|
67 |
+
outputs=gr.Textbox(label="Summary"),
|
68 |
+
title="PDF Earnings Summary Generator",
|
69 |
+
description="Upload a PDF of an earnings summary or transcript to generate a concise summary."
|
70 |
)
|
71 |
+
|
72 |
iface.launch()
|