Update app.py
Browse files2 unmodified lines34567891011126 unmodified lines1920212223242514 unmodified lines4041424344454647484950515253545556575 unmodified lines6364656667682 unmodified linesfrom PyPDF2 import PdfReaderimport requestsfrom dotenv import load_dotenv# Load environment variablesload_dotenv()# Get the Hugging Face API tokenHUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")def summarize_text(text, instructions, agent_name):print(f"{agent_name}: Starting summarization")API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"6 unmodified linesresponse = requests.post(API_URL, headers=headers, json=payload)print(f"{agent_name}: Received response from API")return response.json()[0]["generated_text"]def process_pdf(pdf_file, chunk_instructions, final_instructions):print("Starting PDF processing")# Read PDFreader = PdfReader(pdf_file)14 unmodified lines# Concatenate Agent 1 summariesconcatenated_summary = "\n\n".join(agent1_summaries)print(f"Concatenated Agent 1 summaries (length: {len(concatenated_summary)})")# Agent 2: Final summarizationprint("Agent 2: Starting final summarization")final_summary = summarize_text(concatenated_summary, final_instructions, "Agent 2")print("Agent 2: Finished final summarization")return final_summarydef pdf_summarizer(pdf_file, chunk_instructions, final_instructions):if pdf_file is None:print("Error: No PDF file uploaded")return "Please upload a PDF file."try:print(f"Starting summarization process for file: {pdf_file.name}")summary = process_pdf(pdf_file.name, chunk_instructions, final_instructions)print("Summarization process completed successfully")return summaryexcept Exception as e:5 unmodified linesinputs=[gr.File(label="Upload PDF"),gr.Textbox(label="Chunk Instructions", placeholder="Instructions for summarizing each chunk"),gr.Textbox(label="Final Instructions", placeholder="Instructions for final summarization")],outputs=gr.Textbox(label="Summary"),2 unmodified lines345678910111213141516176 unmodified lines2425262728293014 unmodified lines454647484950515253545556575859606162636465666768697071727374757677787980818283845 unmodified lines909192939495962 unmodified linesfrom PyPDF2 import PdfReaderimport requestsfrom dotenv import load_dotenvimport tiktoken# Load environment variablesload_dotenv()# Get the Hugging Face API tokenHUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")# Initialize the tokenizertokenizer = tiktoken.get_encoding("cl100k_base")def count_tokens(text):return len(tokenizer.encode(text))def summarize_text(text, instructions, agent_name):print(f"{agent_name}: Starting summarization")API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"6 unmodified linesresponse = requests.post(API_URL, headers=headers, json=payload)print(f"{agent_name}: Received response from API")return response.json()[0]["generated_text"]def process_pdf(pdf_file, chunk_instructions, window_instructions, final_instructions):print("Starting PDF processing")# Read PDFreader = PdfReader(pdf_file)14 unmodified lines# Concatenate Agent 1 summariesconcatenated_summary = "\n\n".join(agent1_summaries)print(f"Concatenated Agent 1 summaries (length: {len(concatenated_summary)})")print(f"Concatenated Summary:{concatenated_summary}")# Sliding window approachwindow_size = 3500 # in tokensstep_size = 3000 # overlap of 500 tokenswindows = []current_position = 0while current_position < len(concatenated_summary):window_end = current_positionwindow_text = ""while count_tokens(window_text) < window_size and window_end < len(concatenated_summary):window_text += concatenated_summary[window_end]window_end += 1windows.append(window_text)current_position += step_sizeprint(f"Created {len(windows)} windows for intermediate summarization")# Intermediate summarizationintermediate_summaries = []for i, window in enumerate(windows):print(f"Processing window {i+1}/{len(windows)}")summary = summarize_text(window, window_instructions, f"Window {i+1}")intermediate_summaries.append(summary)# Final summarizationfinal_input = "\n\n".join(intermediate_summaries)print(f"Final input length: {count_tokens(final_input)} tokens")final_summary = summarize_text(final_input, final_instructions, "Agent 2")print("Agent 2: Finished final summarization")return final_summarydef pdf_summarizer(pdf_file, chunk_instructions, window_instructions, final_instructions):if pdf_file is None:print("Error: No PDF file uploaded")return "Please upload a PDF file."try:print(f"Starting summarization process for file: {pdf_file.name}")summary = process_pdf(pdf_file.name, chunk_instructions, window_instructions, final_instructions)print("Summarization process completed successfully")return summaryexcept Exception as e:5 unmodified linesinputs=[gr.File(label="Upload PDF"),gr.Textbox(label="Chunk Instructions", placeholder="Instructions for summarizing each chunk"),gr.Textbox(label="Window Instructions", placeholder="Instructions for summarizing each window"),gr.Textbox(label="Final Instructions", placeholder="Instructions for final summarization")],outputs=gr.Textbox(label="Summary"),
app.py+34-6