Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
import streamlit as st
|
4 |
+
import PyPDF2
|
5 |
+
|
6 |
+
# Set up OpenAI API key
|
7 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
8 |
+
|
9 |
+
# Streamlit app
|
10 |
+
def main():
|
11 |
+
st.title("EB2-NIW Petition Draft Generator")
|
12 |
+
st.write("Upload a research paper PDF and get a drafted petition addressing the three prongs for EB2-NIW.")
|
13 |
+
|
14 |
+
# File upload
|
15 |
+
uploaded_file = st.file_uploader("Upload PDF", type="pdf")
|
16 |
+
|
17 |
+
if uploaded_file is not None:
|
18 |
+
# Extract text from PDF
|
19 |
+
pdf_text = extract_text_from_pdf(uploaded_file)
|
20 |
+
|
21 |
+
# Button to extract and show text from PDF
|
22 |
+
if st.button("Extract Text"):
|
23 |
+
if pdf_text:
|
24 |
+
st.subheader("Extracted Text:")
|
25 |
+
st.write(pdf_text)
|
26 |
+
else:
|
27 |
+
st.error("Failed to extract text from the PDF.")
|
28 |
+
|
29 |
+
# Button to generate petition draft
|
30 |
+
if st.button("Generate Petition Draft"):
|
31 |
+
if pdf_text.strip() == "":
|
32 |
+
st.error("No text extracted from the PDF. Please check the file and try again.")
|
33 |
+
else:
|
34 |
+
# Generate the petition draft based on the extracted text
|
35 |
+
petition_draft = generate_petition_draft(pdf_text)
|
36 |
+
st.success("Petition Draft:")
|
37 |
+
st.write(petition_draft)
|
38 |
+
|
39 |
+
|
40 |
+
# Function to extract text from a PDF
|
41 |
+
def extract_text_from_pdf(pdf_file):
|
42 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
43 |
+
text = ""
|
44 |
+
for page in pdf_reader.pages:
|
45 |
+
text += page.extract_text()
|
46 |
+
return text
|
47 |
+
|
48 |
+
# Function to generate EB2-NIW petition draft using GPT-3.5
|
49 |
+
def generate_petition_draft(research_text):
|
50 |
+
# Prompt for GPT-3.5
|
51 |
+
prompt = (
|
52 |
+
"A researcher has asked you to draft an EB2-NIW petition. "
|
53 |
+
"Based on the following research paper text, draft a 2-page petition that proves the following three prongs:\n\n"
|
54 |
+
"Prong 1: The applicant's proposed endeavor has substantial merit and national importance.\n"
|
55 |
+
"Prong 2: The applicant is well-positioned to advance the proposed endeavor.\n"
|
56 |
+
"Prong 3: On balance, it would be beneficial to the United States to waive the requirements of the PERM labor certification.\n\n"
|
57 |
+
"Research paper text:\n\n" + research_text
|
58 |
+
)
|
59 |
+
|
60 |
+
# Generate response from GPT-3.5
|
61 |
+
response = openai.ChatCompletion.create(
|
62 |
+
model="gpt-3.5-turbo",
|
63 |
+
messages=[
|
64 |
+
{"role": "system", "content": "You are an experienced immigration lawyer."},
|
65 |
+
{"role": "user", "content": prompt}
|
66 |
+
],
|
67 |
+
max_tokens=5000,
|
68 |
+
temperature=0.1
|
69 |
+
)
|
70 |
+
|
71 |
+
petition_draft = response.choices[0].message['content'].strip()
|
72 |
+
return petition_draft
|
73 |
+
|
74 |
+
if __name__ == "__main__":
|
75 |
+
main()
|