Skip to content

Commit

Permalink
added python poc
Browse files Browse the repository at this point in the history
  • Loading branch information
kadenbking committed Feb 22, 2024
1 parent d926f49 commit 19ea4b4
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 4 deletions.
2 changes: 1 addition & 1 deletion backend/.idea/.idea.Palindrome/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions frontend/src/models/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const dotnetApiUrl = "https://cf-contract-analysis-mvp.azurewebsites.net";
// export const dotnetApiUrl = "https://localhost:7254";
3 changes: 2 additions & 1 deletion frontend/src/pages/ContractAnalysis.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState } from "react";
import axios, { AxiosResponse } from "axios";
import ContractResults from "../components/ContractResults";
import { dotnetApiUrl } from "../models/constants";

const ContractAnalysis = () => {
const [loading, setLoading] = useState<boolean>(false);
Expand Down Expand Up @@ -31,7 +32,7 @@ const ContractAnalysis = () => {
formData.append("file", file);

axios
.post("https://cf-contract-analysis-mvp.azurewebsites.net/api/azure/analyze-contract", formData, {
.post(`${dotnetApiUrl}/api/azure/analyze-contract`, formData, {
headers: {
"Content-Type": "multipart/form-data",
"Access-Control-Allow-Origin": "https://cf-contract-analysis-mvp.azurewebsites.net",
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/pages/DocumentAnalysis.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState } from "react";
import axios, { AxiosResponse } from "axios";
import DocumentResults from "../components/DocumentResults";
import { dotnetApiUrl } from "../models/constants";

const DocumentAnalysis = () => {
const [loading, setLoading] = useState<boolean>(false);
Expand Down Expand Up @@ -31,7 +32,7 @@ const DocumentAnalysis = () => {
formData.append("file", file);

axios
.post("https://cf-contract-analysis-mvp.azurewebsites.net/api/azure/analyze-document", formData, {
.post(`${dotnetApiUrl}/api/azure/analyze-document`, formData, {
headers: {
"Content-Type": "multipart/form-data",
},
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/pages/OpenAiAnalysis.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState } from "react";
import axios, { AxiosResponse } from "axios";
import { CopyBlock, dracula } from "react-code-blocks";
import { dotnetApiUrl } from "../models/constants";

const OpenAiAnalysis = () => {
const [loading, setLoading] = useState<boolean>(false);
Expand Down Expand Up @@ -31,7 +32,7 @@ const OpenAiAnalysis = () => {
formData.append("file", file);

axios
.post("https://cf-contract-analysis-mvp.azurewebsites.net/api/open-ai/analyze-contract", formData, {
.post(`${dotnetApiUrl}/api/open-ai/analyze-contract`, formData, {
headers: {
"Content-Type": "multipart/form-data",
},
Expand Down
Binary file added python-poc/__pycache__/prompt.cpython-311.pyc
Binary file not shown.
62 changes: 62 additions & 0 deletions python-poc/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import os
from dotenv import load_dotenv
from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer import DocumentAnalysisClient
from openai import OpenAI

from prompt import system_prompt

load_dotenv()
azure_key = os.getenv("AZURE_KEY")
azure_service_endpoint = os.getenv("AZURE_SERVICE_ENDPOINT")
open_ai_org_id = os.getenv("OPEN_AI_ORG_ID")

document_analysis_client = DocumentAnalysisClient(
endpoint=azure_service_endpoint, credential=AzureKeyCredential(azure_key)
)

client = OpenAI()


def analyze_contract(path_to_file):
print("Uploading to Azure Document AI...")
print("________________________________________")

with open(path_to_file, "rb") as f:
poller = document_analysis_client.begin_analyze_document(
"prebuilt-read", document=f
)

result = poller.result()

print("File read by Azure Document AI")
print("Uploading file content to Open AI API...")
print("________________________________________")

messages = system_prompt

messages.append({"role": "user", "content": "Upload Starting Now."})
for paragraph in result.paragraphs:
messages.append({"role": "user", "content": paragraph.content})

messages.append({"role": "user", "content": "Please Start Summary Results."})

response = client.chat.completions.create(model="gpt-3.5-turbo", messages=messages)

print("Contract Summary Results:")
print("________________________________________")

print(response.choices[0].message.content)


if __name__ == "__main__":
analyze_contract(
"/Users/kaden/dev/cf/sample-contract-files/AmendmentToContract.pdf"
)
# analyze_contract(
# "/Users/kaden/dev/cf/sample-contract-files/FarmAndRanchContract.pdf"
# )
# analyze_contract(
# "/Users/kaden/dev/cf/sample-contract-files/Sample_Utah_Real_Estate_Contract.pdf"
# )
# analyze_contract("/Users/kaden/dev/cf/sample-contract-files/SimpleContract.pdf")
17 changes: 17 additions & 0 deletions python-poc/parse_attempts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import PyPDF2


def try_parse_pdf(path_to_file):
pdfFileObj = open(path_to_file, "rb")

pdfReader = PyPDF2.PdfReader(pdfFileObj)
print(len(pdfReader.pages))

pageObj = pdfReader.pages[0]
print(pageObj.extract_text())

pdfFileObj.close()


if __name__ == "__main__":
try_parse_pdf("/Users/kaden/dev/cf/sample-contract-files/AmendmentToContract.pdf")
26 changes: 26 additions & 0 deletions python-poc/prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
system_prompt = [
{
"role": "system",
"content": "You are a document research assistant that reads long files and summarizes them.",
},
{
"role": "system",
"content": "The conversation will start by the user saying 'Upload Starting Now.' The user will then provide you with document paragraphs.",
},
{
"role": "system",
"content": "You will not answer until the user has asked, 'Please Start Summary Results.'",
},
{
"role": "system",
"content": "You will then provide the most accurate details about the current information given to you.",
},
{
"role": "system",
"content": "You will start by listing in bullet points the following information: Buyer Name (if applicable), Seller Name (if applicable), Contract Amount (if applicable), Contract Key Dates, and Contract Property Address (if applicable).",
},
{
"role": "system",
"content": "After providing that information, you will give a summary of all other major details in paragraph form.",
},
]

0 comments on commit 19ea4b4

Please sign in to comment.