File size: 2,662 Bytes
02d81ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
"""app.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1XblbxoRxB4XOHixjGij789FPD9KjKdhi
"""

import os
import PyPDF2
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

# Ensure API key is available
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."""
    text = ""
    reader = PyPDF2.PdfReader(pdf_file)
    for page in reader.pages:
        page_text = page.extract_text()
        if page_text:
            text += page_text + "\n"
    return text

def summarize_text(text, length='Medium', style='Concise Paragraph'):
    """Summarize the text with CoT and adjustable format."""

    # Adjust summary length
    length_map = {
        'Short': 'Summarize in 3-4 lines.',
        'Medium': 'Summarize in 6-8 lines.',
        'Long': 'Provide a detailed summary in multiple paragraphs.'
    }

    # Adjust summary style
    style_map = {
        'Bulleted List': 'Format the summary as a list of key points.',
        'Key Takeaways': 'Extract the most important insights as key takeaways.',
        'Concise Paragraph': 'Write the summary as a structured paragraph.'
    }

    prompt = f"""
    Step 1: Identify the main topics covered in the document.
    Step 2: Extract key facts, arguments, and conclusions.
    Step 3: Generate a structured summary based on extracted information.
    {length_map[length]} {style_map[style]}

    Document:
    {text[:10000]}
    """

    response = llm.predict(prompt)
    return response

def process_pdf(file, length, style):
    """Extract text and summarize PDF using Mistral-8x7B with customization."""
    if file is None:
        return "No file uploaded."

    text = extract_text_from_pdf(file)
    summary = summarize_text(text, length, style)
    return summary

# Create Gradio Interface
interface = gr.Interface(
    fn=process_pdf,
    inputs=[
        gr.File(label="Upload a PDF"),
        gr.Radio(["Short", "Medium", "Long"], value="Medium", label="Summary Length"),
        gr.Radio(["Bulleted List", "Key Takeaways", "Concise Paragraph"], value="Concise Paragraph", label="Summary Style")
    ],
    outputs="text",
    title="📄 AI-Powered PDF Summarizer",
    description="Upload a PDF file and customize the summary format and length."
)

# Run the app
interface.launch()