Bikas0 commited on
Commit
1705208
·
1 Parent(s): 64efe54

contract verification

Browse files
Files changed (1) hide show
  1. app.py +47 -22
app.py CHANGED
@@ -13,6 +13,20 @@ load_dotenv()
13
  api_key = os.getenv("OPENAI_API_KEY")
14
  openai.api_key = api_key
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def extract_text_from_docx(docx_path):
17
  doc = Document(docx_path)
18
  return "\n".join([para.text for para in doc.paragraphs])
@@ -30,7 +44,8 @@ def extract_terms_from_contract(contract_text):
30
  "Provide the extracted terms in JSON format."
31
  )
32
 
33
- retries = 5
 
34
  for i in range(retries):
35
  try:
36
  response = openai.ChatCompletion.create(
@@ -45,11 +60,11 @@ def extract_terms_from_contract(contract_text):
45
  temperature=0.1,
46
  )
47
  return response.choices[0].message["content"]
48
- except openai.error.RateLimitError as e:
49
  if i < retries - 1:
50
- wait_time = 2 ** i
51
  st.warning(f"Rate limit exceeded. Retrying in {wait_time} seconds...")
52
  time.sleep(wait_time)
 
53
  else:
54
  st.error("Rate limit exceeded. Please try again later.")
55
  return None
@@ -67,6 +82,7 @@ def analyze_task_compliance(task_description, cost_estimate, contract_terms):
67
  )
68
 
69
  retries = 5
 
70
  for i in range(retries):
71
  try:
72
  response = openai.ChatCompletion.create(
@@ -79,32 +95,38 @@ def analyze_task_compliance(task_description, cost_estimate, contract_terms):
79
  n=1,
80
  stop=None,
81
  temperature=0.1,
 
82
  )
83
- response_content = response.choices[0].message["content"]
84
- compliance_analysis = json.loads(response_content)
85
- return compliance_analysis
86
- except openai.error.RateLimitError as e:
 
 
 
 
 
 
87
  if i < retries - 1:
88
- wait_time = 2 ** i
89
  st.warning(f"Rate limit exceeded. Retrying in {wait_time} seconds...")
90
  time.sleep(wait_time)
 
91
  else:
92
  st.error("Rate limit exceeded. Please try again later.")
93
  return None
94
 
95
  def main():
96
- st.title("Contract Compliance Analyzer")
97
-
98
- # File upload buttons in the same row
99
- col1, col2 = st.columns(2)
100
-
101
- with col1:
102
- docx_file = st.file_uploader("Upload Contract Document (DOCX)", type="docx")
103
-
104
- with col2:
105
- xlsx_file = st.file_uploader("Upload Task Descriptions (XLSX)", type="xlsx")
106
-
107
- if docx_file and xlsx_file:
108
  # Clear previous information
109
  st.session_state.clear()
110
 
@@ -121,8 +143,11 @@ def main():
121
  st.error(f"JSON decoding error: {e}")
122
  return
123
 
124
- # Read task descriptions and cost estimates from XLSX
125
- tasks_df = pd.read_excel(xlsx_file)
 
 
 
126
 
127
  compliance_results = []
128
  futures = []
 
13
  api_key = os.getenv("OPENAI_API_KEY")
14
  openai.api_key = api_key
15
 
16
+ # Streamlit app layout
17
+ st.set_page_config(layout="wide")
18
+
19
+ # Add custom CSS for center alignment
20
+ st.markdown("""
21
+ <style>
22
+ .centered-title {
23
+ text-align: center;
24
+ font-size: 2.5em;
25
+ margin-top: 0;
26
+ }
27
+ </style>
28
+ """, unsafe_allow_html=True)
29
+
30
  def extract_text_from_docx(docx_path):
31
  doc = Document(docx_path)
32
  return "\n".join([para.text for para in doc.paragraphs])
 
44
  "Provide the extracted terms in JSON format."
45
  )
46
 
47
+ retries = 2
48
+ wait_time = 1
49
  for i in range(retries):
50
  try:
51
  response = openai.ChatCompletion.create(
 
60
  temperature=0.1,
61
  )
62
  return response.choices[0].message["content"]
63
+ except openai.error.RateLimitError:
64
  if i < retries - 1:
 
65
  st.warning(f"Rate limit exceeded. Retrying in {wait_time} seconds...")
66
  time.sleep(wait_time)
67
+ wait_time *= 2 # Exponential backoff
68
  else:
69
  st.error("Rate limit exceeded. Please try again later.")
70
  return None
 
82
  )
83
 
84
  retries = 5
85
+ wait_time = 1
86
  for i in range(retries):
87
  try:
88
  response = openai.ChatCompletion.create(
 
95
  n=1,
96
  stop=None,
97
  temperature=0.1,
98
+ stream=True,
99
  )
100
+
101
+ compliance_analysis = ""
102
+ for chunk in response:
103
+ chunk_text = chunk['choices'][0]['delta'].get('content', '')
104
+ compliance_analysis += chunk_text
105
+ st.write(chunk_text)
106
+ st.json(chunk_text)
107
+
108
+ return json.loads(compliance_analysis)
109
+ except openai.error.RateLimitError:
110
  if i < retries - 1:
 
111
  st.warning(f"Rate limit exceeded. Retrying in {wait_time} seconds...")
112
  time.sleep(wait_time)
113
+ wait_time *= 2 # Exponential backoff
114
  else:
115
  st.error("Rate limit exceeded. Please try again later.")
116
  return None
117
 
118
  def main():
119
+ st.markdown("<h1 class='centered-title'>Contract Compliance Analyzer</h1>", unsafe_allow_html=True)
120
+
121
+ # File upload buttons one after another
122
+ st.sidebar.file_uploader("Upload Contract Document (DOCX)", type="docx", key="docx_file")
123
+ st.sidebar.file_uploader("Upload Task Descriptions (XLSX or CSV)", type=["xlsx", "csv"], key="data_file")
124
+ submit_button = st.sidebar.button("Submit")
125
+
126
+ docx_file = st.session_state.get("docx_file")
127
+ data_file = st.session_state.get("data_file")
128
+
129
+ if submit_button and docx_file and data_file:
 
130
  # Clear previous information
131
  st.session_state.clear()
132
 
 
143
  st.error(f"JSON decoding error: {e}")
144
  return
145
 
146
+ # Read task descriptions and cost estimates from XLSX or CSV
147
+ if data_file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
148
+ tasks_df = pd.read_excel(data_file)
149
+ else:
150
+ tasks_df = pd.read_csv(data_file)
151
 
152
  compliance_results = []
153
  futures = []