Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -3,130 +3,119 @@ import gradio as gr
|
|
3 |
from PyPDF2 import PdfReader
|
4 |
import requests
|
5 |
from dotenv import load_dotenv
|
6 |
-
import
|
7 |
-
|
8 |
# Load environment variables
|
9 |
load_dotenv()
|
10 |
-
|
11 |
# Get the Hugging Face API token
|
12 |
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
13 |
-
|
14 |
# Initialize the tokenizer
|
15 |
-
tokenizer =
|
16 |
-
|
17 |
def count_tokens(text):
|
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 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
try:
|
109 |
-
print(f"Starting summarization process for file: {pdf_file.name}")
|
110 |
-
summary = process_pdf(pdf_file.name, chunk_instructions, window_instructions, final_instructions)
|
111 |
-
print("Summarization process completed successfully")
|
112 |
-
return summary
|
113 |
-
except Exception as e:
|
114 |
-
print(f"An error occurred: {str(e)}")
|
115 |
-
return f"An error occurred: {str(e)}"
|
116 |
-
|
117 |
# Gradio interface
|
118 |
iface = gr.Interface(
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
129 |
)
|
130 |
-
|
131 |
print("Launching Gradio interface")
|
132 |
iface.launch()
|
|
|
3 |
from PyPDF2 import PdfReader
|
4 |
import requests
|
5 |
from dotenv import load_dotenv
|
6 |
+
from transformers import AutoTokenizer
|
|
|
7 |
# Load environment variables
|
8 |
load_dotenv()
|
|
|
9 |
# Get the Hugging Face API token
|
10 |
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
|
|
11 |
# Initialize the tokenizer
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.3")
|
|
|
13 |
def count_tokens(text):
|
14 |
+
return len(tokenizer.encode(text))
|
15 |
+
def summarize_text(text, instructions, agent_name, max_length, temperature, repetition_penalty, top_p):
|
16 |
+
print(f"{agent_name}: Starting summarization")
|
17 |
+
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
|
18 |
+
headers = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"}
|
19 |
+
summaries = []
|
20 |
+
current_text = text
|
21 |
+
while len(current_text) > 0:
|
22 |
+
payload = {
|
23 |
+
"inputs": f"{instructions}\n\nText to summarize:\n{current_text}",
|
24 |
+
"parameters": {
|
25 |
+
"max_length": max_length,
|
26 |
+
"temperature": temperature,
|
27 |
+
"repetition_penalty": repetition_penalty,
|
28 |
+
"top_p": top_p
|
29 |
+
}
|
30 |
+
}
|
31 |
+
print(f"{agent_name}: Sending request to API")
|
32 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
33 |
+
print(f"{agent_name}: Received response from API")
|
34 |
+
generated_text = response.json()[0]["generated_text"]
|
35 |
+
# Split the generated text by the delimiter "\n\n" and take the last part as the summary
|
36 |
+
summary = generated_text.split("\n\n")[-1]
|
37 |
+
summaries.append(summary)
|
38 |
+
# Remove the summarized part from the current text
|
39 |
+
current_text = current_text[len(summary):].strip()
|
40 |
+
# Join all partial summaries into a final summary
|
41 |
+
final_summary = "\n\n".join(summaries)
|
42 |
+
return final_summary
|
43 |
+
def process_pdf(pdf_file, chunk_instructions, window_instructions, final_instructions, max_length, temperature, repetition_penalty, top_p):
|
44 |
+
print("Starting PDF processing")
|
45 |
+
# Read PDF
|
46 |
+
reader = PdfReader(pdf_file)
|
47 |
+
text = ""
|
48 |
+
for page in reader.pages:
|
49 |
+
text += page.extract_text() + "\n\n"
|
50 |
+
print(f"Extracted {len(reader.pages)} pages from PDF")
|
51 |
+
# Chunk the text (simple splitting by pages for this example)
|
52 |
+
chunks = text.split("\n\n")
|
53 |
+
print(f"Split text into {len(chunks)} chunks")
|
54 |
+
# Agent 1: Summarize each chunk
|
55 |
+
agent1_summaries = []
|
56 |
+
for i, chunk in enumerate(chunks):
|
57 |
+
print(f"Agent 1: Processing chunk {i+1}/{len(chunks)}")
|
58 |
+
summary = summarize_text(chunk, chunk_instructions, "Agent 1", max_length, temperature, repetition_penalty, top_p)
|
59 |
+
agent1_summaries.append(summary)
|
60 |
+
print("Agent 1: Finished processing all chunks")
|
61 |
+
# Concatenate Agent 1 summaries
|
62 |
+
concatenated_summary = "\n\n".join(agent1_summaries)
|
63 |
+
print(f"Concatenated Agent 1 summaries (length: {count_tokens(concatenated_summary)} tokens)")
|
64 |
+
print(f"Concatenated Summary: {concatenated_summary}")
|
65 |
+
# Sliding window approach
|
66 |
+
window_size = 3500 # in tokens
|
67 |
+
step_size = 3000 # overlap of 500 tokens
|
68 |
+
windows = []
|
69 |
+
current_position = 0
|
70 |
+
while current_position < len(concatenated_summary):
|
71 |
+
window_end = current_position
|
72 |
+
window_text = ""
|
73 |
+
while count_tokens(window_text) < window_size and window_end < len(concatenated_summary):
|
74 |
+
window_text += concatenated_summary[window_end]
|
75 |
+
window_end += 1
|
76 |
+
windows.append(window_text)
|
77 |
+
current_position += step_size
|
78 |
+
print(f"Created {len(windows)} windows for intermediate summarization")
|
79 |
+
# Intermediate summarization
|
80 |
+
intermediate_summaries = []
|
81 |
+
for i, window in enumerate(windows):
|
82 |
+
print(f"Processing window {i+1}/{len(windows)}")
|
83 |
+
summary = summarize_text(window, window_instructions, f"Window {i+1}", max_length, temperature, repetition_penalty, top_p)
|
84 |
+
intermediate_summaries.append(summary)
|
85 |
+
# Final summarization
|
86 |
+
final_input = "\n\n".join(intermediate_summaries)
|
87 |
+
print(f"Final input length: {count_tokens(final_input)} tokens")
|
88 |
+
final_summary = summarize_text(final_input, final_instructions, "Agent 2", max_length, temperature, repetition_penalty, top_p)
|
89 |
+
print("Agent 2: Finished final summarization")
|
90 |
+
return final_summary
|
91 |
+
def pdf_summarizer(pdf_file, chunk_instructions, window_instructions, final_instructions, max_length, temperature, repetition_penalty, top_p):
|
92 |
+
if pdf_file is None:
|
93 |
+
print("Error: No PDF file uploaded")
|
94 |
+
return "Please upload a PDF file."
|
95 |
+
try:
|
96 |
+
print(f"Starting summarization process for file: {pdf_file.name}")
|
97 |
+
summary = process_pdf(pdf_file.name, chunk_instructions, window_instructions, final_instructions, max_length, temperature, repetition_penalty, top_p)
|
98 |
+
print("Summarization process completed successfully")
|
99 |
+
return summary
|
100 |
+
except Exception as e:
|
101 |
+
print(f"An error occurred: {str(e)}")
|
102 |
+
return f"An error occurred: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
# Gradio interface
|
104 |
iface = gr.Interface(
|
105 |
+
fn=pdf_summarizer,
|
106 |
+
inputs=[
|
107 |
+
gr.File(label="Upload PDF"),
|
108 |
+
gr.Textbox(label="Chunk Instructions", placeholder="Instructions for summarizing each chunk"),
|
109 |
+
gr.Textbox(label="Window Instructions", placeholder="Instructions for summarizing each window"),
|
110 |
+
gr.Textbox(label="Final Instructions", placeholder="Instructions for final summarization"),
|
111 |
+
gr.Slider(label="Max Length", minimum=500, maximum=3500, step=100, value=2000),
|
112 |
+
gr.Slider(label="Temperature", minimum=0.1, maximum=1.0, step=0.1, value=0.7),
|
113 |
+
gr.Slider(label="Repetition Penalty", minimum=1.0, maximum=2.0, step=0.1, value=1.1),
|
114 |
+
gr.Slider(label="Top P", minimum=0.1, maximum=1.0, step=0.1, value=0.9)
|
115 |
+
],
|
116 |
+
outputs=gr.Textbox(label="Summary"),
|
117 |
+
title="PDF Earnings Summary Generator",
|
118 |
+
description="Upload a PDF of an earnings summary or transcript to generate a concise summary."
|
119 |
)
|
|
|
120 |
print("Launching Gradio interface")
|
121 |
iface.launch()
|