Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,15 +4,12 @@ import gradio as gr
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from groq import Groq
|
| 6 |
|
| 7 |
-
# Load environment variables
|
| 8 |
load_dotenv()
|
| 9 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 10 |
-
print("Groq API Key:", GROQ_API_KEY)
|
| 11 |
|
| 12 |
-
# Instantiate Groq client
|
| 13 |
client = Groq(api_key=GROQ_API_KEY)
|
| 14 |
|
| 15 |
-
# Function to extract text from PDF
|
| 16 |
def extract_text_from_pdf(pdf_file):
|
| 17 |
text = ""
|
| 18 |
with pdfplumber.open(pdf_file.name) as pdf:
|
|
@@ -22,7 +19,6 @@ def extract_text_from_pdf(pdf_file):
|
|
| 22 |
text += page_text
|
| 23 |
return text
|
| 24 |
|
| 25 |
-
# Split text into manageable chunks (by character count)
|
| 26 |
def split_text_into_chunks(text, max_chars=2000):
|
| 27 |
words = text.split()
|
| 28 |
chunks = []
|
|
@@ -39,7 +35,6 @@ def split_text_into_chunks(text, max_chars=2000):
|
|
| 39 |
|
| 40 |
return chunks
|
| 41 |
|
| 42 |
-
# Summarize a single chunk using Groq
|
| 43 |
def summarize_chunk(chunk):
|
| 44 |
prompt = f"Summarize the following PDF section:\n\n{chunk}"
|
| 45 |
try:
|
|
@@ -51,7 +46,6 @@ def summarize_chunk(chunk):
|
|
| 51 |
except Exception as e:
|
| 52 |
return f"Error during summarization: {e}"
|
| 53 |
|
| 54 |
-
# Main summarization function
|
| 55 |
def summarize_pdf(pdf_file):
|
| 56 |
text = extract_text_from_pdf(pdf_file)
|
| 57 |
if not text.strip():
|
|
@@ -67,7 +61,6 @@ def summarize_pdf(pdf_file):
|
|
| 67 |
final_summary = "\n".join(summaries)
|
| 68 |
return final_summary
|
| 69 |
|
| 70 |
-
# Gradio interface
|
| 71 |
iface = gr.Interface(
|
| 72 |
fn=summarize_pdf,
|
| 73 |
inputs=gr.File(label="Upload PDF", file_types=[".pdf"]),
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from groq import Groq
|
| 6 |
|
|
|
|
| 7 |
load_dotenv()
|
| 8 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 9 |
+
print("Groq API Key:", GROQ_API_KEY)
|
| 10 |
|
|
|
|
| 11 |
client = Groq(api_key=GROQ_API_KEY)
|
| 12 |
|
|
|
|
| 13 |
def extract_text_from_pdf(pdf_file):
|
| 14 |
text = ""
|
| 15 |
with pdfplumber.open(pdf_file.name) as pdf:
|
|
|
|
| 19 |
text += page_text
|
| 20 |
return text
|
| 21 |
|
|
|
|
| 22 |
def split_text_into_chunks(text, max_chars=2000):
|
| 23 |
words = text.split()
|
| 24 |
chunks = []
|
|
|
|
| 35 |
|
| 36 |
return chunks
|
| 37 |
|
|
|
|
| 38 |
def summarize_chunk(chunk):
|
| 39 |
prompt = f"Summarize the following PDF section:\n\n{chunk}"
|
| 40 |
try:
|
|
|
|
| 46 |
except Exception as e:
|
| 47 |
return f"Error during summarization: {e}"
|
| 48 |
|
|
|
|
| 49 |
def summarize_pdf(pdf_file):
|
| 50 |
text = extract_text_from_pdf(pdf_file)
|
| 51 |
if not text.strip():
|
|
|
|
| 61 |
final_summary = "\n".join(summaries)
|
| 62 |
return final_summary
|
| 63 |
|
|
|
|
| 64 |
iface = gr.Interface(
|
| 65 |
fn=summarize_pdf,
|
| 66 |
inputs=gr.File(label="Upload PDF", file_types=[".pdf"]),
|