akazmi commited on
Commit
74f331e
·
verified ·
1 Parent(s): ab4981a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -14
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  import pdfplumber
3
  import gradio as gr
4
  from groq import Groq
@@ -10,20 +11,29 @@ import numpy as np
10
  # Initialize Groq API
11
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
12
 
13
- # Path to the directory containing PDFs (using current working directory)
14
- PDF_DIR = "." # Access PDFs directly in the root directory
15
 
16
- # Function to list PDF files in the root directory
17
- def list_pdf_files():
18
- pdf_files = [f for f in os.listdir(PDF_DIR) if f.endswith('.pdf')]
19
- return pdf_files
20
 
21
- # Function to extract text and split into chunks using pdfplumber
 
 
 
 
 
 
 
 
 
 
 
 
22
  def extract_text_from_pdf(file_name):
23
  try:
24
- file_path = os.path.join(PDF_DIR, file_name)
25
  full_text = ""
26
- with pdfplumber.open(file_path) as pdf:
27
  for page_num, page in enumerate(pdf.pages):
28
  page_text = page.extract_text()
29
  if page_text:
@@ -31,6 +41,9 @@ def extract_text_from_pdf(file_name):
31
  else:
32
  print(f"Warning: No text found on page {page_num + 1}")
33
 
 
 
 
34
  if not full_text.strip():
35
  return "Error: PDF contains no extractable text."
36
 
@@ -59,7 +72,13 @@ def retrieve_relevant_chunks(chunks, question):
59
 
60
  # Function to answer the question using selected relevant chunks
61
  def answer_question(file_name, question):
62
- chunks = extract_text_from_pdf(file_name)
 
 
 
 
 
 
63
  if isinstance(chunks, str) and "Error" in chunks:
64
  return chunks
65
 
@@ -84,13 +103,10 @@ def answer_question(file_name, question):
84
  print(f"Error during API call: {e}")
85
  return "Error: Could not retrieve an answer from the Groq API."
86
 
87
- # List PDF files for selection
88
- pdf_files = list_pdf_files()
89
-
90
  # Create Gradio interface
91
  iface = gr.Interface(
92
  fn=answer_question,
93
- inputs=[gr.Dropdown(choices=pdf_files, label="Select PDF"), gr.Textbox(label="Ask a Question")],
94
  outputs="text",
95
  title="PDF Question Answering with RAG",
96
  description="Select a PDF document and ask any question related to its content. This uses Retrieval-Augmented Generation (RAG) for more accurate answers."
 
1
  import os
2
+ import requests
3
  import pdfplumber
4
  import gradio as gr
5
  from groq import Groq
 
11
  # Initialize Groq API
12
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
13
 
14
+ # Define the Hugging Face Space URL where the PDFs are stored
15
+ BASE_URL = "https://huggingface.co/spaces/akazmi/Legal2/raw/main"
16
 
17
+ # List of PDF filenames manually specified or obtained from a static list
18
+ PDF_FILES = ["document1.pdf", "document2.pdf"] # Replace with actual filenames
 
 
19
 
20
+ # Function to download a PDF file
21
+ def download_pdf(file_name):
22
+ url = f"{BASE_URL}/{file_name}"
23
+ response = requests.get(url)
24
+ if response.status_code == 200:
25
+ with open(file_name, 'wb') as f:
26
+ f.write(response.content)
27
+ return file_name
28
+ else:
29
+ print(f"Error: Could not download {file_name}. Status code: {response.status_code}")
30
+ return None
31
+
32
+ # Function to extract text from a downloaded PDF
33
  def extract_text_from_pdf(file_name):
34
  try:
 
35
  full_text = ""
36
+ with pdfplumber.open(file_name) as pdf:
37
  for page_num, page in enumerate(pdf.pages):
38
  page_text = page.extract_text()
39
  if page_text:
 
41
  else:
42
  print(f"Warning: No text found on page {page_num + 1}")
43
 
44
+ # Delete the downloaded PDF after extracting text
45
+ os.remove(file_name)
46
+
47
  if not full_text.strip():
48
  return "Error: PDF contains no extractable text."
49
 
 
72
 
73
  # Function to answer the question using selected relevant chunks
74
  def answer_question(file_name, question):
75
+ # Download the PDF file
76
+ downloaded_file = download_pdf(file_name)
77
+ if not downloaded_file:
78
+ return "Error: Could not download the PDF file."
79
+
80
+ # Extract chunks of text from the downloaded PDF
81
+ chunks = extract_text_from_pdf(downloaded_file)
82
  if isinstance(chunks, str) and "Error" in chunks:
83
  return chunks
84
 
 
103
  print(f"Error during API call: {e}")
104
  return "Error: Could not retrieve an answer from the Groq API."
105
 
 
 
 
106
  # Create Gradio interface
107
  iface = gr.Interface(
108
  fn=answer_question,
109
+ inputs=[gr.Dropdown(choices=PDF_FILES, label="Select PDF"), gr.Textbox(label="Ask a Question")],
110
  outputs="text",
111
  title="PDF Question Answering with RAG",
112
  description="Select a PDF document and ask any question related to its content. This uses Retrieval-Augmented Generation (RAG) for more accurate answers."