File size: 1,546 Bytes
0c8b033
9008d0c
 
 
9384e9a
9008d0c
0c8b033
9008d0c
0c8b033
 
 
9008d0c
0c8b033
 
9008d0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c8b033
9008d0c
 
 
 
 
0c8b033
9008d0c
 
 
 
 
 
 
 
 
0c8b033
 
9008d0c
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
import groq  # This assumes Groq provides a Python package named "groq"
import pdfplumber
import gradio as gr
import os
from dotenv import load_dotenv

load_dotenv()  # Loads environment variables from a .env file

# Replace with your Groq API key in your .env file as GROQ_API_KEY
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
groq.configure(api_key=GROQ_API_KEY)

# Load Groq's generative model (the model name 'groq-pro' is illustrative)
model = groq.GenerativeModel('groq-pro')

def extract_text_from_pdf(pdf_file):
    text = ""
    with pdfplumber.open(pdf_file) as pdf:
        for page in pdf.pages:
            page_text = page.extract_text()
            if page_text:
                text += page_text
    return text

def summarize_pdf(pdf_file):
    text = extract_text_from_pdf(pdf_file)
    if not text.strip():
        return "No extractable text found in the PDF."

    # Optional: Limit the text if needed for token limits
    text = text[:15000]

    prompt = f"Summarize the following PDF content:\n\n{text}"

    try:
        response = model.generate_content(prompt)  # Adjust parameters per Groq's API
        return response.text.strip()
    except Exception as e:
        return f"Error during summarization: {e}"

# Gradio interface
iface = gr.Interface(
    fn=summarize_pdf,
    inputs=gr.File(label="Upload PDF", file_types=[".pdf"]),
    outputs="text",
    title="PDF Summarizer with Groq",
    description="Upload a PDF and get a summary using Groq's generative AI API."
)

if __name__ == "__main__":
    iface.launch()