Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,17 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
import google.generativeai as genai
|
|
|
|
|
|
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
-
import fitz # PyMuPDF
|
| 8 |
-
import camelot
|
| 9 |
-
from pdf2image import convert_from_path
|
| 10 |
-
from pytesseract import image_to_string
|
| 11 |
-
import re
|
| 12 |
-
import spacy
|
| 13 |
-
import tempfile
|
| 14 |
|
| 15 |
# Load environment variables from a .env file
|
| 16 |
load_dotenv()
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
# Load SpaCy model
|
| 21 |
nlp = spacy.load("en_core_web_sm")
|
|
@@ -23,18 +19,19 @@ nlp = spacy.load("en_core_web_sm")
|
|
| 23 |
class ResearchPaperAnalyzer:
|
| 24 |
def __init__(self, model_name: str):
|
| 25 |
self.model = genai.GenerativeModel(model_name=model_name)
|
| 26 |
-
self.image_descriptions = {}
|
| 27 |
self.tables_data = {}
|
| 28 |
self.extracted_text = ""
|
| 29 |
|
| 30 |
def extract_text(self, pdf_path):
|
| 31 |
-
|
|
|
|
| 32 |
text = ""
|
| 33 |
-
for page in
|
| 34 |
-
text +=
|
| 35 |
return text
|
| 36 |
|
| 37 |
def segment_text(self, text):
|
|
|
|
| 38 |
heading_pattern = r"^\d+\.\s.+"
|
| 39 |
paragraphs = text.split("\n\n")
|
| 40 |
sections = []
|
|
@@ -80,86 +77,69 @@ class ResearchPaperAnalyzer:
|
|
| 80 |
return structured_text, analyzed_data
|
| 81 |
|
| 82 |
def extract_tables(self, pdf_path):
|
|
|
|
| 83 |
tables = camelot.read_pdf(pdf_path, pages='all')
|
| 84 |
return tables
|
| 85 |
|
| 86 |
def process_paper(self, pdf_path):
|
| 87 |
# Step 1: Extract and process text
|
| 88 |
self.extracted_text = self.extract_text(pdf_path)
|
| 89 |
-
|
| 90 |
-
# Process text (optional)
|
| 91 |
structured_text, _ = self.process_text(self.extracted_text)
|
| 92 |
|
| 93 |
# Step 2: Extract and process tables
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
st.error(f"Error extracting tables: {str(e)}")
|
| 101 |
|
| 102 |
def handle_query(self, query):
|
|
|
|
| 103 |
combined_info = f"{self.extracted_text}\n\nTables Data:\n{self.tables_data}"
|
| 104 |
|
| 105 |
-
|
|
|
|
| 106 |
|
|
|
|
|
|
|
|
|
|
| 107 |
try:
|
| 108 |
response = self.model.generate_content(prompt)
|
| 109 |
-
|
|
|
|
|
|
|
| 110 |
except Exception as e:
|
| 111 |
-
|
|
|
|
| 112 |
|
| 113 |
-
#
|
| 114 |
-
st.title("Research Paper Analyzer")
|
| 115 |
|
| 116 |
-
#
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
# Upload PDF file
|
| 120 |
-
pdf_file = st.file_uploader("Upload PDF", type="pdf")
|
| 121 |
-
|
| 122 |
-
if pdf_file is not None:
|
| 123 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
|
| 124 |
-
temp_file.write(pdf_file.getbuffer())
|
| 125 |
-
pdf_path = temp_file.name
|
| 126 |
-
|
| 127 |
-
if st.button("Process Text"):
|
| 128 |
-
analyzer.process_paper(pdf_path)
|
| 129 |
-
st.success("PDF processed successfully.")
|
| 130 |
|
| 131 |
-
if
|
| 132 |
-
analyzer
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
for table_name, csv_data in analyzer.tables_data.items():
|
| 136 |
-
st.write(f"Table {table_name}:")
|
| 137 |
-
st.text(csv_data)
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
st.
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
|
|
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
user_input = st.text_area("You:", "")
|
| 156 |
-
|
| 157 |
-
if st.button("Send") and user_input.strip():
|
| 158 |
-
add_message(user_input.strip(), is_user=True)
|
| 159 |
-
|
| 160 |
-
bot_response = analyzer.handle_query(user_input.strip())
|
| 161 |
-
|
| 162 |
-
add_message(bot_response, is_user=False)
|
| 163 |
-
|
| 164 |
-
# Rerun to update the chat with new messages
|
| 165 |
-
st.rerun()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import spacy
|
| 3 |
+
import camelot
|
| 4 |
+
import fitz # PyMuPDF
|
| 5 |
import google.generativeai as genai
|
| 6 |
+
import os
|
| 7 |
+
import re # For regular expressions
|
| 8 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Load environment variables from a .env file
|
| 11 |
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# Set the API key for Gemini
|
| 14 |
+
genai.configure(api_key=os.getenv('AIzaSyCYy1NLIOei1b6ZAG4Wtab4-md8MOW0yAk'))
|
| 15 |
|
| 16 |
# Load SpaCy model
|
| 17 |
nlp = spacy.load("en_core_web_sm")
|
|
|
|
| 19 |
class ResearchPaperAnalyzer:
|
| 20 |
def __init__(self, model_name: str):
|
| 21 |
self.model = genai.GenerativeModel(model_name=model_name)
|
|
|
|
| 22 |
self.tables_data = {}
|
| 23 |
self.extracted_text = ""
|
| 24 |
|
| 25 |
def extract_text(self, pdf_path):
|
| 26 |
+
# Extract text from the PDF using PyMuPDF
|
| 27 |
+
doc = fitz.open(pdf_path)
|
| 28 |
text = ""
|
| 29 |
+
for page in doc:
|
| 30 |
+
text += page.get_text("text")
|
| 31 |
return text
|
| 32 |
|
| 33 |
def segment_text(self, text):
|
| 34 |
+
# Segment the text into sections based on headings
|
| 35 |
heading_pattern = r"^\d+\.\s.+"
|
| 36 |
paragraphs = text.split("\n\n")
|
| 37 |
sections = []
|
|
|
|
| 77 |
return structured_text, analyzed_data
|
| 78 |
|
| 79 |
def extract_tables(self, pdf_path):
|
| 80 |
+
# Extract tables using Camelot
|
| 81 |
tables = camelot.read_pdf(pdf_path, pages='all')
|
| 82 |
return tables
|
| 83 |
|
| 84 |
def process_paper(self, pdf_path):
|
| 85 |
# Step 1: Extract and process text
|
| 86 |
self.extracted_text = self.extract_text(pdf_path)
|
|
|
|
|
|
|
| 87 |
structured_text, _ = self.process_text(self.extracted_text)
|
| 88 |
|
| 89 |
# Step 2: Extract and process tables
|
| 90 |
+
tables = self.extract_tables(pdf_path)
|
| 91 |
+
for i, table in enumerate(tables):
|
| 92 |
+
csv_data = table.df.to_csv(index=False)
|
| 93 |
+
self.tables_data[f"table_{i + 1}"] = csv_data
|
| 94 |
+
|
| 95 |
+
return structured_text, self.tables_data
|
|
|
|
| 96 |
|
| 97 |
def handle_query(self, query):
|
| 98 |
+
# Combine text and tables to create the context for Gemini
|
| 99 |
combined_info = f"{self.extracted_text}\n\nTables Data:\n{self.tables_data}"
|
| 100 |
|
| 101 |
+
# Send the query and combined info to Gemini for response
|
| 102 |
+
prompt = f"Given the following information, answer the query:\n\n{combined_info}\n\nQuery: {query}"
|
| 103 |
|
| 104 |
+
# Debugging: Display the prompt
|
| 105 |
+
st.write(f"Prompt sent to Gemini: {prompt}")
|
| 106 |
+
|
| 107 |
try:
|
| 108 |
response = self.model.generate_content(prompt)
|
| 109 |
+
# Debugging: Show the raw response
|
| 110 |
+
st.write(f"Full response from Gemini: {response.text}") # Debugging
|
| 111 |
+
return response.text # Return Gemini's generated answer
|
| 112 |
except Exception as e:
|
| 113 |
+
st.error(f"An error occurred: {str(e)}")
|
| 114 |
+
return None
|
| 115 |
|
| 116 |
+
# Streamlit Interface
|
| 117 |
+
st.title("Research Paper Analyzer with Chatbot")
|
| 118 |
|
| 119 |
+
# Upload PDF
|
| 120 |
+
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
+
if uploaded_file is not None:
|
| 123 |
+
# Create an instance of the analyzer, specify the model name
|
| 124 |
+
model_name = "gemini-1.5-pro-latest" # Update this to the appropriate model name
|
| 125 |
+
analyzer = ResearchPaperAnalyzer(model_name=model_name)
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
# Button to process the file
|
| 128 |
+
if st.button("Process Text and Tables"):
|
| 129 |
+
with open("uploaded_pdf.pdf", "wb") as f:
|
| 130 |
+
f.write(uploaded_file.getbuffer())
|
| 131 |
|
| 132 |
+
# Process the uploaded PDF
|
| 133 |
+
structured_text, tables_data = analyzer.process_paper("uploaded_pdf.pdf")
|
| 134 |
|
| 135 |
+
# Chatbot functionality: Ask a question
|
| 136 |
+
st.header("Ask a Question")
|
| 137 |
+
user_query = st.text_input("Enter your query:")
|
| 138 |
|
| 139 |
+
# Button to generate answer using Gemini
|
| 140 |
+
if st.button("Answer"):
|
| 141 |
+
if user_query:
|
| 142 |
+
answer = analyzer.handle_query(user_query)
|
| 143 |
+
st.write(f"Answer: {answer}")
|
| 144 |
+
else:
|
| 145 |
+
st.write("Please enter a query to get an answer.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|