|
|
import gradio as gr |
|
|
import PyPDF2 |
|
|
from groq import GroqClient |
|
|
|
|
|
|
|
|
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." |
|
|
|
|
|
|
|
|
extracted_text = extract_text_from_pdf(pdf_file) |
|
|
|
|
|
if not extracted_text.strip(): |
|
|
return "No readable text found in the PDF." |
|
|
|
|
|
|
|
|
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)}" |
|
|
|
|
|
|
|
|
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." |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
interface.launch(share=True) |