Update app.py
Browse files215 unmodified lines216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284215 unmodified lines# Define font size and line spacingfont_size = 9line_spacing = 1 * font_sizemax_lines_per_page = int(text_height // line_spacing)# Load a built-in fontfont = "helv"# Split the text into lineslines = text.split("\n")current_line = 0for line in lines:if current_line >= max_lines_per_page:page = doc.new_page() # Add a new pagecurrent_line = 0rect = fitz.Rect(margin, margin + current_line * line_spacing, text_width, margin + (current_line + 1) * line_spacing)page.insert_textbox(rect, line, fontsize=font_size, fontname=font, align=fitz.TEXT_ALIGN_LEFT)current_line += 1doc.save(output_path)print(f"Text saved to PDF at {output_path}.")# Function to handle user queriesdef handle_query(query, is_read_pdf, instructions, pdf_file=None):print("Handling user query...")max_chars_per_chunk = 1000 # Adjust this value as needed to control chunk sizeif is_read_pdf and pdf_file:pdf_text = read_pdf(pdf_file)text_chunks = [pdf_text[i:i+max_chars_per_chunk] for i in range(0, len(pdf_text), max_chars_per_chunk)]else:search_results = google_search(query)text_chunks = []for result in search_results:if result["text"]:text_chunks.extend([result["text"][i:i+max_chars_per_chunk] for i in range(0, len(result["text"]), max_chars_per_chunk)])summaries = []for chunk in text_chunks:formatted_prompt = format_prompt_with_instructions(chunk, instructions)summary = generate_text(formatted_prompt)if summary:summaries.append(summary)combined_summary = " ".join(summaries)save_text_to_pdf(combined_summary, "output_summary.pdf")return combined_summarydef run_app():with gr.Blocks() as demo:gr.Markdown("# Web and PDF Summarizer")query = gr.Textbox(label="Enter your query", placeholder="Enter query here")pdf_file = gr.File(label="Upload PDF", file_types=[".pdf"])is_read_pdf = gr.Checkbox(label="Read PDF", value=False)instructions = gr.Textbox(label="Enter instructions", placeholder="Enter instructions here")output = gr.Textbox(label="Summary")clear_cache_btn = gr.Button("Clear Cache")clear_cache_btn.click(fn=clear_cache, outputs=output)generate_btn = gr.Button("Generate Summary")generate_btn.click(fn=handle_query, inputs=[query, is_read_pdf, instructions, pdf_file], outputs=output)demo.launch()run_app()215 unmodified lines216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333215 unmodified lines# Define font size and line spacingfont_size = 9line_spacing = 1 * font_sizefontname = "times-roman" # Use a supported font name# Process the textinto lines that fit within the text_widthlines = []current_line = ""current_line_width = 0words = text.split(" ")for word in words:word_width = fitz.get_text_length(word, fontname, font_size)if current_line_width + word_width <= text_width:current_line += word + " "current_line_width += word_width + fitz.get_text_length(" ", fontname, font_size)else:lines.append(current_line.strip())current_line = word + " "current_line_width = word_width + fitz.get_text_length(" ", fontname, font_size)if current_line:lines.append(current_line.strip())# Add the lines to the page with marginsx = marginy = marginfor line in lines:if y + line_spacing > text_height:# Create a new page if text exceeds the page heightpage = doc.new_page()y = margin # Reset y-coordinate for the new pagepage.insert_text((x, y), line, fontname=fontname, fontsize=font_size)y += line_spacingdoc.save(output_path) # Save the PDF to the specified output pathprint(f"Text saved to PDF at {output_path}")# Function to process the PDF or search query and generate a summarydef process_input(query_or_file, is_pdf, instructions, api_key):load_dotenv() # Load environment variables from .env fileHUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_TOKEN")if is_pdf:print(f"Processing PDF: {query_or_file.name}")input_text = read_pdf(query_or_file)else:print(f"Processing search query: {query_or_file}")search_results = google_search(query_or_file)input_text = "\n\n".join(result["text"] for result in search_results if result["text"])# Split the input text into smaller chunks to fit within the token limitchunk_size = 1024 # Adjust as needed to stay within the token limittext_chunks = [input_text[i:i + chunk_size] for i in range(0, len(input_text), chunk_size)]print(f"Total number of chunks: {len(text_chunks)}")# Generate summaries for each chunk and concatenate themconcatenated_summary = ""for chunk in text_chunks:prompt = format_prompt_with_instructions(chunk, instructions)chunk_summary = generate_text(prompt)concatenated_summary += f"{chunk_summary}\n\n"print("Final concatenated summary generated.")return concatenated_summary# Function to clear cachedef clear_cache():try:# Clear Gradio cachecache_dir = tempfile.gettempdir()shutil.rmtree(os.path.join(cache_dir, "gradio"), ignore_errors=True)# Clear any custom cache you might have# For example, if you're caching PDF files or search results:if os.path.exists("output_summary.pdf"):os.remove("output_summary.pdf")# Add any other cache clearing operations hereprint("Cache cleared successfully.")return "Cache cleared successfully."except Exception as e:print(f"Error clearing cache: {e}")return f"Error clearing cache: {e}"def summarization_interface():with gr.Blocks() as demo:gr.Markdown("# PDF and Web Summarization Tool")with gr.Tab("Summarize PDF"):pdf_file = gr.File(label="Upload PDF", file_types=[".pdf"])pdf_instructions = gr.Textbox(label="Instructions for Summarization", placeholder="Enter instructions for summarization", lines=3)pdf_summary_output = gr.Textbox(label="Summary Output")pdf_api_key = gr.Textbox(label="Hugging Face API Key", type="password")pdf_summarize_button = gr.Button("Generate Summary")pdf_clear_cache_button = gr.Button("Clear Cache")with gr.Tab("Summarize Web Search"):search_query = gr.Textbox(label="Enter Search Query", placeholder="Enter search query")search_instructions = gr.Textbox(label="Instructions for Summarization", placeholder="Enter instructions for summarization", lines=3)search_summary_output = gr.Textbox(label="Summary Output")search_api_key = gr.Textbox(label="Hugging Face API Key", type="password")search_summarize_button = gr.Button("Generate Summary")search_clear_cache_button = gr.Button("Clear Cache")# Bind functions to button clickspdf_summarize_button.click(fn=lambda file, instructions, api_key: process_input(file, True, instructions, api_key), inputs=[pdf_file, pdf_instructions, pdf_api_key], outputs=pdf_summary_output)search_summarize_button.click(fn=lambda query, instructions, api_key: process_input(query, False, instructions, api_key), inputs=[search_query, search_instructions, search_api_key], outputs=search_summary_output)pdf_clear_cache_button.click(fn=clear_cache, inputs=None, outputs=pdf_summary_output)search_clear_cache_button.click(fn=clear_cache, inputs=None, outputs=search_summary_output)return demo# Launch the Gradio interfacedemo = summarization_interface()demo.launch()
app.py+102-53