Spaces:
Sleeping
Sleeping
| import os | |
| import pdfplumber | |
| import gradio as gr | |
| from dotenv import load_dotenv | |
| from groq import Groq | |
| # Load environment variables from a .env file | |
| load_dotenv() | |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") | |
| # Instantiate the Groq client | |
| client = Groq(api_key=GROQ_API_KEY) | |
| def extract_text_from_pdf(pdf_file): | |
| text = "" | |
| with pdfplumber.open(pdf_file.name) 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." | |
| prompt = f"Summarize the following PDF content:\n\n{text}" | |
| try: | |
| response = client.chat.completions.create( | |
| messages=[ | |
| { | |
| "role": "user", | |
| "content": prompt | |
| } | |
| ], | |
| model="llama3-8b-8192", # Replace with your desired model ID | |
| ) | |
| return response.choices[0].message.content.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() | |