Tulika2000's picture
Upload app.py
ee04366 verified
raw
history blame
2.39 kB
# -*- coding: utf-8 -*-
"""app.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1XblbxoRxB4XOHixjGij789FPD9KjKdhi
"""
import os
import pdfplumber
import gradio as gr
from langchain_groq.chat_models import ChatGroq
# Set Groq API key securely
GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Fetch from environment variables
if not GROQ_API_KEY:
raise ValueError("GROQ_API_KEY is not set. Add it in Hugging Face Secrets.")
# Initialize LLM (Mistral-8x7B)
llm = ChatGroq(model_name="mixtral-8x7b-32768")
def extract_text_from_pdf(pdf_file):
"""Extract text from a PDF file using pdfplumber."""
text = ""
with pdfplumber.open(pdf_file) as pdf:
for page in pdf.pages:
text += page.extract_text() or "" # Extract text safely
return text.strip()
def summarize_text(text, length, style):
"""Summarize text with adjustable length and style."""
prompt = (
f"""
Read the following document and summarize it in {style.lower()} format.
Keep the summary {length.lower()}.
Follow this step-by-step approach:
1. Identify key sections.
2. Extract essential points.
3. Remove unnecessary details.
4. Ensure factual accuracy without adding extra information.
Document:
{text[:10000]} # Limit input to 10,000 characters for efficiency
"""
)
response = llm.predict(prompt)
return response.strip()
def process_pdf(file, length, style):
"""Extract text and summarize PDF using Mistral-8x7B with customization."""
if not file:
return "No file uploaded."
text = extract_text_from_pdf(file.name) # Extract text
if not text:
return "Could not extract text from the PDF. Try another file."
return summarize_text(text, length, style)
# Create Gradio Interface
interface = gr.Interface(
fn=process_pdf,
inputs=[
gr.File(label="Upload a PDF"),
gr.Radio(["Short", "Medium", "Long"], label="Summary Length", value="Medium"),
gr.Radio(["Bullets", "Key Takeaways", "Concise Paragraph"], label="Summary Style", value="Key Takeaways"),
],
outputs="text",
title="πŸ“„ Advanced PDF Summarizer",
description="Upload a PDF file and get a structured summary with customization options."
)
# Run the app
interface.launch()