Spaces:
Paused
Paused
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475import gradio as grfrom functions import extract_text_from_pdf, format_content, split_into_snippets, build_promptsdef process_inputs(pdf_file, model_choice, output_format, oauth_token: gr.OAuthToken | None = None):"""Process PDF and generate summary"""if oauth_token is None:return "### Please log in to use this service"if not pdf_file:return "### Please upload a PDF file"try:text = extract_text_from_pdf(pdf_file.name)return f"### Processing successful with {model_choice}!"except Exception as e:return f"### Error: {str(e)}"# Define core interface componentsiface = gr.Interface(fn=process_inputs,inputs=[gr.File(label="Upload PDF",file_types=[".pdf"]),gr.Dropdown(choices=["GPT-3.5","GPT-4","Claude-3","Mistral"],label="Model",value="GPT-3.5"),gr.Radio(choices=["TXT", "MD", "HTML"],label="Format",value="TXT")],outputs=gr.Markdown(label="Output",value="### Upload your PDF to begin"),flagging_mode="never",css=""".gradio-container {max-width: 800px !important;margin: 0 auto !important;}.container {max-width: 800px !important;margin: 0 auto !important;padding: 2rem !important;}""")# Create main appwith gr.Blocks(theme=gr.themes.Default()) as demo:gr.Markdown("## π PDF to LLM Summarizer")with gr.Row():with gr.Column():gr.Markdown("π Extract and summarize text from PDFs using state-of-the-art language models")with gr.Column():gr.LoginButton(min_width=200)iface.render()gr.Markdown("Made with Gradio")if __name__ == "__main__":demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334import osimport reimport tempfileimport requestsimport gradio as grfrom PyPDF2 import PdfReaderimport openaiimport logging# Set up logginglogging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')# Initialize Hugging Face modelsHUGGINGFACE_MODELS = {"Phi-3 Mini 128k Instruct by EswardiVI": "eswardivi/Phi-3-mini-128k-instruct","Phi-3 Mini 128k Instruct by TaufiqDP": "taufiqdp/phi-3-mini-128k-instruct"}# Utility Functionsdef extract_text_from_pdf(pdf_path):"""Extract text content from PDF file."""try:reader = PdfReader(pdf_path)text = ""for page_num, page in enumerate(reader.pages, start=1):page_text = page.extract_text()if page_text:text += page_text + "\n"else:logging.warning(f"No text found on page {page_num}.")if not text.strip():return "Error: No extractable text found in the PDF."return textexcept Exception as e:logging.error(f"Error reading PDF file: {e}")return f"Error reading PDF file: {e}"def format_content(text, format_type):"""Format extracted text according to specified format."""if format_type == 'txt':return textelif format_type == 'md':paragraphs = text.split('\n\n')return '\n\n'.join(paragraphs)elif format_type == 'html':paragraphs = text.split('\n\n')return ''.join([f'<p>{para.strip()}</p>' for para in paragraphs if para.strip()])else:logging.error(f"Unsupported format: {format_type}")return f"Unsupported format: {format_type}"def split_into_snippets(text, context_size):"""Split text into manageable snippets based on context size."""sentences = re.split(r'(?<=[.!?]) +', text)snippets = []current_snippet = ""for sentence in sentences:if len(current_snippet) + len(sentence) + 1 > context_size:if current_snippet:snippets.append(current_snippet.strip())current_snippet = sentence + " "else:snippets.append(sentence.strip())current_snippet = ""else:current_snippet += sentence + " "if current_snippet.strip():snippets.append(current_snippet.strip())return snippetsdef build_prompts(snippets, prompt_instruction, custom_prompt):"""Build formatted prompts from text snippets."""prompts = []for idx, snippet in enumerate(snippets, start=1):current_prompt = custom_prompt if custom_prompt else prompt_instructionframed_prompt = f"---\nPart {idx} of {len(snippets)}:\n{current_prompt}\n\n{snippet}\n\nEnd of Part {idx}.\n---"prompts.append(framed_prompt)return promptsdef send_to_huggingface(prompt, model_name):"""Send prompt to Hugging Face model."""try:payload = {"inputs": prompt}response = requests.post(f"https://api-inference.huggingface.co/models/{model_name}",json=payload)if response.status_code == 200:return response.json()[0].get('generated_text', 'No generated text found.')else:error_info = response.json()error_message = error_info.get('error', 'Unknown error occurred.')logging.error(f"Error from Hugging Face model: {error_message}")return f"Error from Hugging Face model: {error_message}"except Exception as e:logging.error(f"Error interacting with Hugging Face model: {e}")return f"Error interacting with Hugging Face model: {e}"def authenticate_openai(api_key):"""Authenticate with OpenAI API."""if api_key:try:openai.api_key = api_keyopenai.Model.list()return "OpenAI Authentication Successful!"except Exception as e:logging.error(f"OpenAI API Key Error: {e}")return f"OpenAI API Key Error: {e}"return "No OpenAI API key provided."# Main Interfacewith gr.Blocks(theme=gr.themes.Default()) as demo:# Headergr.Markdown("# π Smart PDF Summarizer")gr.Markdown("Upload a PDF document and get AI-powered summaries using OpenAI or Hugging Face models.")# Authentication Sectionwith gr.Row():with gr.Column(scale=1):openai_api_key = gr.Textbox(label="π OpenAI API Key",type="password",placeholder="Enter your OpenAI API key (optional)")auth_status = gr.Textbox(label="Authentication Status",interactive=False)auth_button = gr.Button("π Authenticate", variant="primary")# Main Contentwith gr.Row():# Left Column - Input Optionswith gr.Column(scale=1):pdf_input = gr.File(label="π Upload PDF",file_types=[".pdf"])with gr.Row():format_type = gr.Radio(choices=["txt", "md", "html"],value="txt",label="π Output Format")context_size = gr.Slider(minimum=4000,maximum=128000,step=4000,value=32000,label="π Context Window Size")snippet_number = gr.Number(label="π’ Snippet Number (Optional)",value=None,precision=0)custom_prompt = gr.Textbox(label="βοΈ Custom Prompt",placeholder="Enter your custom prompt here...",lines=2)model_choice = gr.Radio(choices=["OpenAI ChatGPT", "Hugging Face Model"],value="OpenAI ChatGPT",label="π€ Model Selection")hf_model = gr.Dropdown(choices=list(HUGGINGFACE_MODELS.keys()),label="π§ Hugging Face Model",visible=False)# Right Column - Outputwith gr.Column(scale=1):with gr.Row():process_button = gr.Button("π Process PDF", variant="primary")progress_status = gr.Textbox(label="π Progress",interactive=False)generated_prompt = gr.Textbox(label="π Generated Prompt",lines=10)summary_output = gr.Textbox(label="π Summary",lines=15)with gr.Row():download_prompt = gr.File(label="π₯ Download Prompt")download_summary = gr.File(label="π₯ Download Summary")# Event Handlersdef toggle_hf_model(choice):return gr.update(visible=choice == "Hugging Face Model")def handle_authentication(api_key):return authenticate_openai(api_key)def process_pdf(pdf, fmt, ctx_size, snippet_num, prompt, model_selection, hf_model_choice, api_key):try:if not pdf:return "Please upload a PDF file.", "", "", None, None# Extract texttext = extract_text_from_pdf(pdf.name)if text.startswith("Error"):return text, "", "", None, None# Format contentformatted_text = format_content(text, fmt)# Split into snippetssnippets = split_into_snippets(formatted_text, ctx_size)# Process specific snippet or allif snippet_num is not None:if 1 <= snippet_num <= len(snippets):selected_snippets = [snippets[snippet_num - 1]]else:return f"Invalid snippet number. Please choose between 1 and {len(snippets)}.", "", "", None, Noneelse:selected_snippets = snippets# Build promptsdefault_prompt = "Summarize the following text:"prompts = build_prompts(selected_snippets, default_prompt, prompt)full_prompt = "\n".join(prompts)# Generate summaryif model_selection == "OpenAI ChatGPT":if not api_key:return "OpenAI API key required.", full_prompt, "", None, Nonetry:openai.api_key = api_keyresponse = openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=[{"role": "user", "content": full_prompt}])summary = response.choices[0].message.contentexcept Exception as e:return f"OpenAI API error: {str(e)}", full_prompt, "", None, Noneelse:summary = send_to_huggingface(full_prompt, HUGGINGFACE_MODELS[hf_model_choice])# Save files for downloadwith tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as prompt_file:prompt_file.write(full_prompt)prompt_path = prompt_file.namewith tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as summary_file:summary_file.write(summary)summary_path = summary_file.namereturn "Processing complete!", full_prompt, summary, prompt_path, summary_pathexcept Exception as e:logging.error(f"Error processing PDF: {e}")return f"Error processing PDF: {str(e)}", "", "", None, None# Connect event handlersmodel_choice.change(toggle_hf_model,inputs=[model_choice],outputs=[hf_model])auth_button.click(handle_authentication,inputs=[openai_api_key],outputs=[auth_status])process_button.click(process_pdf,inputs=[pdf_input,format_type,context_size,snippet_number,custom_prompt,model_choice,hf_model,openai_api_key],outputs=[progress_status,generated_prompt,summary_output,download_prompt,download_summary])# Instructionsgr.Markdown("""### π Instructions:1. (Optional) Enter your OpenAI API key and authenticate2. Upload a PDF document3. Choose output format and context window size4. Optionally specify a snippet number or custom prompt5. Select between OpenAI ChatGPT or Hugging Face model6. Click 'Process PDF' to generate summary7. Download the generated prompt and summary as needed### βοΈ Features:- Support for multiple PDF formats- Flexible text formatting options- Custom prompt creation- Multiple AI model options- Snippet-based processing- Downloadable outputs""")# Launch the interfaceif __name__ == "__main__":demo.launch(share=False, debug=True)
app.py+322-63