Spaces:
Sleeping
Sleeping
Update verifier.py
Browse files- verifier.py +19 -29
verifier.py
CHANGED
|
@@ -6,15 +6,14 @@ import PyPDF2
|
|
| 6 |
import google.generativeai as genai
|
| 7 |
|
| 8 |
def verifier_page():
|
| 9 |
-
#
|
| 10 |
cred = credentials.Certificate('serviceAccountKey.json')
|
| 11 |
if not firebase_admin._apps:
|
| 12 |
firebase_admin.initialize_app(cred)
|
| 13 |
|
| 14 |
-
# Create a Firestore client
|
| 15 |
db = firestore.client()
|
| 16 |
|
| 17 |
-
#
|
| 18 |
def fetch_data(selected_fields):
|
| 19 |
docs = db.collection('pdf_uploads').stream()
|
| 20 |
data = []
|
|
@@ -26,81 +25,72 @@ def verifier_page():
|
|
| 26 |
|
| 27 |
fields_to_fetch = ['filename', 'pdf_url', 'text']
|
| 28 |
|
| 29 |
-
#
|
| 30 |
if 'selected_file' not in st.session_state:
|
| 31 |
st.session_state.selected_file = ""
|
| 32 |
-
#if 'selected_text' not in st.session_state:
|
| 33 |
-
# st.session_state.selected_text = ""
|
| 34 |
-
#if 'selected_ai' not in st.session_state:
|
| 35 |
-
# st.session_state.selected_ai = ""
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
if st.session_state.selected_file:
|
| 40 |
-
# Process the selected file and generate AI response
|
| 41 |
pdf_path = 'VCS-Standard.pdf'
|
| 42 |
-
start_page = 0
|
| 43 |
-
end_page = 93
|
| 44 |
vcs_text = extract_text_from_pdf(pdf_path, start_page, end_page)
|
| 45 |
|
| 46 |
pdf_path = 'VCS-Methodology-Requirements.pdf'
|
| 47 |
-
start_page = 0
|
| 48 |
-
end_page = 89
|
| 49 |
methodology_text = extract_text_from_pdf(pdf_path, start_page, end_page)
|
| 50 |
|
| 51 |
pdf_path = 'VCS-Project-Description-Template-v4.4-FINAL2.docx.pdf'
|
| 52 |
-
start_page = 0
|
| 53 |
-
end_page = 34
|
| 54 |
template_text = extract_text_from_pdf(pdf_path, start_page, end_page)
|
| 55 |
|
| 56 |
-
|
| 57 |
GOOGLE_API_KEY = "AIzaSyC7TpzrIH_3-dppWE8exqdZX3DAdE6cy8w"
|
| 58 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 59 |
|
| 60 |
-
|
| 61 |
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 62 |
|
| 63 |
-
|
| 64 |
response = model.generate_content("You are a project verifier officer at Verra, the leading registry for projects used to generate carbon credits. Your job is to look into project submissions from project developers who implement nature-based solutions in order to generate carbon credits. You go through the content of the project submissions to investigate whether the submission fits into the vcs standards, methodology requirements, and touches everything on the project description template. A verifier has to compare the submission to these 3 main criteria. As a verifier, I want you to evaluate the project submission below based on the resources listed below. The output should be in the format of summary of the project submission, the level of adherence to the standards, what needs to be fixed, and notes for improvement for project developers. The level of adherence should have grading on selected criteria mentioned in the documentation. The goal here is to help other verifiers understand what do you think about this project submission and how much more improvement this work needs. What needs to be fixed should be detailed feedback and give action items. The output needs to have project-specific feedback. You can bolster your feedback with quotes from the submission or referencing numbers mentioned in the submission. Here is the project submission:" + st.session_state.selected_file + "Here is the vcs standards:" + vcs_text + "Here is the methodology requirement:" + methodology_text + "Here is the project description template:" + template_text)
|
| 65 |
|
| 66 |
-
#
|
| 67 |
st.session_state.selected_file = response.text
|
| 68 |
|
| 69 |
-
#
|
| 70 |
st.empty()
|
| 71 |
st.write(f"Selected File: {st.session_state.selected_file}")
|
| 72 |
#st.write(f"AI Response: {st.session_state.selected_ai}")
|
| 73 |
|
| 74 |
|
| 75 |
else:
|
| 76 |
-
# Automatically load and display the data table on page load
|
| 77 |
data = fetch_data(fields_to_fetch)
|
| 78 |
if data:
|
| 79 |
df = pd.DataFrame(data)
|
| 80 |
|
| 81 |
|
| 82 |
-
|
| 83 |
for index, row in df.iterrows():
|
| 84 |
col1, col2 = st.columns([4, 1])
|
| 85 |
with col1:
|
| 86 |
st.write(row['filename'])
|
| 87 |
|
| 88 |
with col2:
|
| 89 |
-
# Button to view details of each row
|
| 90 |
button_key = f"view_{index}"
|
| 91 |
if st.button("See details", key=button_key):
|
| 92 |
-
#
|
| 93 |
st.session_state.selected_file = row['text']
|
| 94 |
#st.session_state.selected_text = row['text']
|
| 95 |
|
| 96 |
-
#
|
| 97 |
st.rerun()
|
| 98 |
|
| 99 |
|
| 100 |
-
#
|
| 101 |
def extract_text_from_pdf(uploaded_file, start_page, end_page):
|
| 102 |
if uploaded_file is None:
|
| 103 |
-
return ""
|
| 104 |
|
| 105 |
reader = PyPDF2.PdfReader(uploaded_file)
|
| 106 |
num_pages = len(reader.pages)
|
|
|
|
| 6 |
import google.generativeai as genai
|
| 7 |
|
| 8 |
def verifier_page():
|
| 9 |
+
#initialize firebase
|
| 10 |
cred = credentials.Certificate('serviceAccountKey.json')
|
| 11 |
if not firebase_admin._apps:
|
| 12 |
firebase_admin.initialize_app(cred)
|
| 13 |
|
|
|
|
| 14 |
db = firestore.client()
|
| 15 |
|
| 16 |
+
#display submissions
|
| 17 |
def fetch_data(selected_fields):
|
| 18 |
docs = db.collection('pdf_uploads').stream()
|
| 19 |
data = []
|
|
|
|
| 25 |
|
| 26 |
fields_to_fetch = ['filename', 'pdf_url', 'text']
|
| 27 |
|
| 28 |
+
#initialize session state variables if not
|
| 29 |
if 'selected_file' not in st.session_state:
|
| 30 |
st.session_state.selected_file = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
if st.session_state.selected_file:
|
|
|
|
| 33 |
pdf_path = 'VCS-Standard.pdf'
|
| 34 |
+
start_page = 0
|
| 35 |
+
end_page = 93
|
| 36 |
vcs_text = extract_text_from_pdf(pdf_path, start_page, end_page)
|
| 37 |
|
| 38 |
pdf_path = 'VCS-Methodology-Requirements.pdf'
|
| 39 |
+
start_page = 0
|
| 40 |
+
end_page = 89
|
| 41 |
methodology_text = extract_text_from_pdf(pdf_path, start_page, end_page)
|
| 42 |
|
| 43 |
pdf_path = 'VCS-Project-Description-Template-v4.4-FINAL2.docx.pdf'
|
| 44 |
+
start_page = 0
|
| 45 |
+
end_page = 34
|
| 46 |
template_text = extract_text_from_pdf(pdf_path, start_page, end_page)
|
| 47 |
|
| 48 |
+
|
| 49 |
GOOGLE_API_KEY = "AIzaSyC7TpzrIH_3-dppWE8exqdZX3DAdE6cy8w"
|
| 50 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 51 |
|
| 52 |
+
|
| 53 |
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 54 |
|
| 55 |
+
|
| 56 |
response = model.generate_content("You are a project verifier officer at Verra, the leading registry for projects used to generate carbon credits. Your job is to look into project submissions from project developers who implement nature-based solutions in order to generate carbon credits. You go through the content of the project submissions to investigate whether the submission fits into the vcs standards, methodology requirements, and touches everything on the project description template. A verifier has to compare the submission to these 3 main criteria. As a verifier, I want you to evaluate the project submission below based on the resources listed below. The output should be in the format of summary of the project submission, the level of adherence to the standards, what needs to be fixed, and notes for improvement for project developers. The level of adherence should have grading on selected criteria mentioned in the documentation. The goal here is to help other verifiers understand what do you think about this project submission and how much more improvement this work needs. What needs to be fixed should be detailed feedback and give action items. The output needs to have project-specific feedback. You can bolster your feedback with quotes from the submission or referencing numbers mentioned in the submission. Here is the project submission:" + st.session_state.selected_file + "Here is the vcs standards:" + vcs_text + "Here is the methodology requirement:" + methodology_text + "Here is the project description template:" + template_text)
|
| 57 |
|
| 58 |
+
#save the response in session state
|
| 59 |
st.session_state.selected_file = response.text
|
| 60 |
|
| 61 |
+
#remove everything and show file details and response
|
| 62 |
st.empty()
|
| 63 |
st.write(f"Selected File: {st.session_state.selected_file}")
|
| 64 |
#st.write(f"AI Response: {st.session_state.selected_ai}")
|
| 65 |
|
| 66 |
|
| 67 |
else:
|
|
|
|
| 68 |
data = fetch_data(fields_to_fetch)
|
| 69 |
if data:
|
| 70 |
df = pd.DataFrame(data)
|
| 71 |
|
| 72 |
|
| 73 |
+
#add buttons for each row in the DataFrame
|
| 74 |
for index, row in df.iterrows():
|
| 75 |
col1, col2 = st.columns([4, 1])
|
| 76 |
with col1:
|
| 77 |
st.write(row['filename'])
|
| 78 |
|
| 79 |
with col2:
|
|
|
|
| 80 |
button_key = f"view_{index}"
|
| 81 |
if st.button("See details", key=button_key):
|
| 82 |
+
#update the session state with the selected file details
|
| 83 |
st.session_state.selected_file = row['text']
|
| 84 |
#st.session_state.selected_text = row['text']
|
| 85 |
|
| 86 |
+
#clear existing content on button click
|
| 87 |
st.rerun()
|
| 88 |
|
| 89 |
|
| 90 |
+
#helper function to extract text from PDF
|
| 91 |
def extract_text_from_pdf(uploaded_file, start_page, end_page):
|
| 92 |
if uploaded_file is None:
|
| 93 |
+
return ""
|
| 94 |
|
| 95 |
reader = PyPDF2.PdfReader(uploaded_file)
|
| 96 |
num_pages = len(reader.pages)
|