Spaces:
Sleeping
Sleeping
Update developer.py
Browse files- developer.py +65 -89
developer.py
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
-
|
| 2 |
import PyPDF2
|
| 3 |
import streamlit as st
|
| 4 |
import firebase_admin
|
| 5 |
from firebase_admin import credentials, firestore, storage
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def project_developer_page():
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
st.subheader('NBS Project Submission Evaluation')
|
| 13 |
|
| 14 |
def initialize_firebase():
|
|
@@ -17,10 +15,9 @@ def project_developer_page():
|
|
| 17 |
firebase_admin.initialize_app(cred, {
|
| 18 |
'storageBucket': 'verifyx-a1164.appspot.com' # Replace with your actual bucket name
|
| 19 |
})
|
| 20 |
-
|
| 21 |
# Initialize Firebase
|
| 22 |
initialize_firebase()
|
| 23 |
-
# Firestore and Storage clients
|
| 24 |
db = firestore.client()
|
| 25 |
bucket = storage.bucket()
|
| 26 |
|
|
@@ -31,17 +28,17 @@ def project_developer_page():
|
|
| 31 |
|
| 32 |
reader = PyPDF2.PdfReader(uploaded_file)
|
| 33 |
num_pages = len(reader.pages)
|
| 34 |
-
|
| 35 |
if start_page < 0 or start_page >= num_pages:
|
| 36 |
start_page = 0
|
| 37 |
if end_page < start_page or end_page >= num_pages:
|
| 38 |
end_page = num_pages - 1
|
| 39 |
-
|
| 40 |
text = ''
|
| 41 |
for page_num in range(start_page, end_page + 1):
|
| 42 |
page = reader.pages[page_num]
|
| 43 |
text += page.extract_text()
|
| 44 |
-
|
| 45 |
return text
|
| 46 |
|
| 47 |
def upload_pdf_to_storage(pdf_file):
|
|
@@ -50,84 +47,63 @@ def project_developer_page():
|
|
| 50 |
blob.upload_from_file(pdf_file)
|
| 51 |
blob.make_public() # Make the file publicly accessible
|
| 52 |
return blob.public_url # Return the public URL of the uploaded PDF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
pdf_file = st.file_uploader("Upload a project submission", type="pdf")
|
| 54 |
-
|
| 55 |
if pdf_file is not None:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
'
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
from IPython.display import display
|
| 109 |
-
from IPython.display import Markdown
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
def to_markdown(text):
|
| 113 |
-
text = text.replace('•', ' *')
|
| 114 |
-
return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
GOOGLE_API_KEY="AIzaSyC7TpzrIH_3-dppWE8exqdZX3DAdE6cy8w"
|
| 119 |
-
genai.configure(api_key=GOOGLE_API_KEY)
|
| 120 |
-
|
| 121 |
-
for m in genai.list_models():
|
| 122 |
-
if 'generateContent' in m.supported_generation_methods:
|
| 123 |
-
print(m.name)
|
| 124 |
-
|
| 125 |
-
#For text-only prompts, use a Gemini 1.5 model or the Gemini 1.0 Pro model:
|
| 126 |
-
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 127 |
-
|
| 128 |
-
# Commented out IPython magic to ensure Python compatibility.
|
| 129 |
-
if st.button("Evaluate", 2):
|
| 130 |
-
# %%time
|
| 131 |
-
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 create an 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 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:" + submission_text + "Here is the vcs standards:" + vcs_text + "Here is the methodology requirement:" + methodology_text + "Here is the project description template:" + template_text)
|
| 132 |
-
to_markdown(response.text)
|
| 133 |
-
st.write(response.text)
|
|
|
|
|
|
|
| 1 |
import PyPDF2
|
| 2 |
import streamlit as st
|
| 3 |
import firebase_admin
|
| 4 |
from firebase_admin import credentials, firestore, storage
|
| 5 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 6 |
+
import google.generativeai as genai
|
| 7 |
+
import textwrap
|
| 8 |
|
| 9 |
def project_developer_page():
|
|
|
|
|
|
|
|
|
|
| 10 |
st.subheader('NBS Project Submission Evaluation')
|
| 11 |
|
| 12 |
def initialize_firebase():
|
|
|
|
| 15 |
firebase_admin.initialize_app(cred, {
|
| 16 |
'storageBucket': 'verifyx-a1164.appspot.com' # Replace with your actual bucket name
|
| 17 |
})
|
| 18 |
+
|
| 19 |
# Initialize Firebase
|
| 20 |
initialize_firebase()
|
|
|
|
| 21 |
db = firestore.client()
|
| 22 |
bucket = storage.bucket()
|
| 23 |
|
|
|
|
| 28 |
|
| 29 |
reader = PyPDF2.PdfReader(uploaded_file)
|
| 30 |
num_pages = len(reader.pages)
|
| 31 |
+
|
| 32 |
if start_page < 0 or start_page >= num_pages:
|
| 33 |
start_page = 0
|
| 34 |
if end_page < start_page or end_page >= num_pages:
|
| 35 |
end_page = num_pages - 1
|
| 36 |
+
|
| 37 |
text = ''
|
| 38 |
for page_num in range(start_page, end_page + 1):
|
| 39 |
page = reader.pages[page_num]
|
| 40 |
text += page.extract_text()
|
| 41 |
+
|
| 42 |
return text
|
| 43 |
|
| 44 |
def upload_pdf_to_storage(pdf_file):
|
|
|
|
| 47 |
blob.upload_from_file(pdf_file)
|
| 48 |
blob.make_public() # Make the file publicly accessible
|
| 49 |
return blob.public_url # Return the public URL of the uploaded PDF
|
| 50 |
+
|
| 51 |
+
# Load PDF files for evaluation
|
| 52 |
+
vcs_text = extract_text_from_pdf('VCS-Standard.pdf', 0, 93)
|
| 53 |
+
methodology_text = extract_text_from_pdf('VCS-Methodology-Requirements.pdf', 0, 89)
|
| 54 |
+
template_text = extract_text_from_pdf('VCS-Project-Description-Template-v4.4-FINAL2.docx.pdf', 0, 34)
|
| 55 |
+
|
| 56 |
pdf_file = st.file_uploader("Upload a project submission", type="pdf")
|
| 57 |
+
|
| 58 |
if pdf_file is not None:
|
| 59 |
+
if 'evaluated' not in st.session_state:
|
| 60 |
+
st.session_state.evaluated = False
|
| 61 |
+
|
| 62 |
+
# Handle PDF upload
|
| 63 |
+
if st.button("Upload") and not st.session_state.evaluated:
|
| 64 |
+
with st.spinner("Uploading and processing..."):
|
| 65 |
+
pdf_url = upload_pdf_to_storage(pdf_file)
|
| 66 |
+
submission_text = extract_text_from_pdf(pdf_file, 0, 117) # Adjust page numbers as necessary
|
| 67 |
+
|
| 68 |
+
# Save to Firestore
|
| 69 |
+
db.collection('pdf_uploads').add({
|
| 70 |
+
'filename': pdf_file.name,
|
| 71 |
+
'text': submission_text,
|
| 72 |
+
'pdf_url': pdf_url,
|
| 73 |
+
'upload_time': firestore.SERVER_TIMESTAMP
|
| 74 |
+
})
|
| 75 |
+
|
| 76 |
+
st.session_state.evaluated = True # Mark as evaluated
|
| 77 |
+
st.success("File uploaded successfully!")
|
| 78 |
+
|
| 79 |
+
# Disable the evaluate button if the submission has been evaluated
|
| 80 |
+
if st.session_state.evaluated:
|
| 81 |
+
st.button("Evaluate", disabled=True)
|
| 82 |
+
st.warning("Submission has already been evaluated. Please upload a new file to evaluate again.")
|
| 83 |
+
else:
|
| 84 |
+
if st.button("Evaluate"):
|
| 85 |
+
with st.spinner("Evaluating..."):
|
| 86 |
+
with ThreadPoolExecutor() as executor:
|
| 87 |
+
future = executor.submit(upload_pdf_to_storage, pdf_file)
|
| 88 |
+
pdf_url = future.result()
|
| 89 |
+
submission_text = extract_text_from_pdf(pdf_file, 0, 117) # Adjust as necessary
|
| 90 |
+
|
| 91 |
+
# Set up Google Generative AI
|
| 92 |
+
GOOGLE_API_KEY = "YOUR_API_KEY_HERE" # Replace with your actual API key
|
| 93 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 94 |
+
model = genai.GenerativeModel('gemini-1.5-flash-latest')
|
| 95 |
+
|
| 96 |
+
# Generate evaluation
|
| 97 |
+
response = model.generate_content(
|
| 98 |
+
"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 create and 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 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: " + submission_text +
|
| 99 |
+
" Here is the vcs standards: " + vcs_text +
|
| 100 |
+
" Here is the methodology requirement: " + methodology_text +
|
| 101 |
+
" Here is the project description template: " + template_text
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
st.write(response.text)
|
| 105 |
+
st.success("Evaluation complete!")
|
| 106 |
+
|
| 107 |
+
# Run the app
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
project_developer_page()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|