File size: 2,057 Bytes
3dee2a1
d388fd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3dee2a1
d388fd7
 
 
3dee2a1
 
d388fd7
3dee2a1
d388fd7
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
import gradio as gr
import PyPDF2
from groq import GroqClient

# Groq API Setup
GROQ_API_KEY = "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"
GROQ_ENDPOINT = "your_groq_model_endpoint_here"
groq_client = GroqClient(api_key=GROQ_API_KEY)

def extract_text_from_pdf(pdf_file):
    """Extract text from the uploaded PDF."""
    text = ""
    try:
        pdf_reader = PyPDF2.PdfReader(pdf_file)
        for page in pdf_reader.pages:
            text += page.extract_text()
    except Exception as e:
        return f"Error extracting text: {str(e)}"
    return text

def generate_recommendations_groq(pdf_file, prompt):
    """Extract text from PDF and generate recommendations using Groq."""
    if pdf_file is None:
        return "Please upload a valid PDF file."

    # Extract text from the PDF
    extracted_text = extract_text_from_pdf(pdf_file)

    if not extracted_text.strip():
        return "No readable text found in the PDF."

    # Send the extracted text and prompt to Groq for processing
    try:
        response = groq_client.request(
            endpoint=GROQ_ENDPOINT,
            inputs={
                "text": extracted_text,
                "prompt": prompt,
            },
        )
        return response.get("result", "No result returned from Groq.")
    except Exception as e:
        return f"Error generating recommendations: {str(e)}"

# Gradio Interface
def process_pdf(file, user_prompt):
    return generate_recommendations_groq(file.name, user_prompt)

interface = gr.Interface(
    fn=process_pdf,
    inputs=[
        gr.inputs.File(label="Upload PDF", type="file"),
        gr.inputs.Textbox(label="Custom Prompt for Analysis", placeholder="Enter your prompt...")
    ],
    outputs=gr.outputs.Textbox(label="Recommendations and Insights"),
    title="PDF Recommendation and Analysis Generator (Groq)",
    description="Upload a PDF file and provide a custom prompt to generate insights and recommendations using Groq."
)

# Launch the Gradio app
if __name__ == "__main__":
    interface.launch(share=True)