Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,450 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
-
import io
|
| 3 |
-
import openai
|
| 4 |
-
import datetime
|
| 5 |
-
import time
|
| 6 |
import gradio as gr
|
| 7 |
-
import json
|
| 8 |
-
from jinja2 import Template
|
| 9 |
import requests
|
| 10 |
-
import fitz
|
| 11 |
-
from xhtml2pdf import pisa
|
| 12 |
-
from io import BytesIO
|
| 13 |
-
#from dotenv import load_dotenv
|
| 14 |
-
from datetime import datetime
|
| 15 |
-
|
| 16 |
-
#load_dotenv()
|
| 17 |
-
|
| 18 |
-
# Initialize OpenAI
|
| 19 |
-
openai.api_key = os.environ.get('OPENAI_API_KEY')
|
| 20 |
-
|
| 21 |
-
# Configuration variables
|
| 22 |
-
airtable_api_key = os.environ.get('AIRTABLE_API_KEY')
|
| 23 |
-
|
| 24 |
-
# Airtable table names
|
| 25 |
-
prompts_table_name = 'tblYIZEB8m6JkGDEP'
|
| 26 |
-
users_table_name = 'tblLNe5ZL47SvrAEk'
|
| 27 |
-
user_log_table_name = 'tblrlTsRrkl6BqMAJ'
|
| 28 |
-
compliancelog_table_name = 'tblQMXWKGlOonkIw2'
|
| 29 |
-
policies_table_name = 'tbla6PC65qZfqdJhE'
|
| 30 |
-
compliance_history_table_name = 'tbltA3vCb2upKVeFT'
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# Define the style and content for the response field
|
| 34 |
-
label_text = "Contract Redline"
|
| 35 |
-
color = "#6562F4"
|
| 36 |
-
background_color = "white"
|
| 37 |
-
border_radius = "10px"
|
| 38 |
-
# response_label = f'<h3 style="color: {color}; background-color: {background_color}; border-radius: {border_radius}; padding: 10px;display: inline-block;">{label_text}</h3>'
|
| 39 |
-
response_label = f'<span style="display: inline-block; position: relative; z-index: var(--layer-4); border: solid var(--block-title-border-width) var(--block-title-border-color); border-radius: var(--block-title-radius); background: var(--block-title-background-fill); padding: var(--block-title-padding); color: var(--block-title-text-color); font-weight: var(--block-title-text-weight); font-size: var(--block-title-text-size); line-height: var(--line-sm); margin-bottom: var(--spacing-lg);">{label_text}</span>'
|
| 40 |
-
base_id = 'appcUK3hUWC7GM2Kb'
|
| 41 |
-
|
| 42 |
-
# App name for user login logging
|
| 43 |
-
app = "Compliance"
|
| 44 |
-
|
| 45 |
-
headers = {
|
| 46 |
-
"Authorization": f"Bearer {airtable_api_key}",
|
| 47 |
-
"Content-Type": "application/json",
|
| 48 |
-
"Accept": "application/json",
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
def prompt_trim(prompt: str) -> str:
|
| 53 |
-
lines = prompt.split('\n')
|
| 54 |
-
trimmed = '\n'.join([l.strip() for l in lines])
|
| 55 |
-
return trimmed
|
| 56 |
-
|
| 57 |
-
def get_policy_text(school):
|
| 58 |
-
|
| 59 |
-
airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{policies_table_name}'
|
| 60 |
-
|
| 61 |
-
# Parameters for the API request to select only the 'school' field
|
| 62 |
-
params = {
|
| 63 |
-
'filterByFormula': f'school="{school}"',
|
| 64 |
-
'fields[]': 'policy_text'
|
| 65 |
-
}
|
| 66 |
-
|
| 67 |
-
global policy_text
|
| 68 |
-
policy_text = ''
|
| 69 |
-
|
| 70 |
-
try:
|
| 71 |
-
# Send a GET request to the Airtable API
|
| 72 |
-
response = requests.get(airtable_endpoint, headers=headers, params=params)
|
| 73 |
-
|
| 74 |
-
# Check if the request was successful (status code 200)
|
| 75 |
-
if response.status_code == 200:
|
| 76 |
-
# Parse the JSON response
|
| 77 |
-
data = response.json()
|
| 78 |
-
|
| 79 |
-
# Check if there are records in the response
|
| 80 |
-
if data.get('records'):
|
| 81 |
-
# Extract the 'school' values from each record
|
| 82 |
-
policy_text = [record['fields']['policy_text'] for record in data['records']]
|
| 83 |
-
|
| 84 |
-
else:
|
| 85 |
-
print("No records found in the 'policies' table.")
|
| 86 |
-
else:
|
| 87 |
-
print(f"Failed to retrieve data. Status code: {response.status_code}")
|
| 88 |
-
except Exception as e:
|
| 89 |
-
print(f"An error occurred: {str(e)}")
|
| 90 |
-
|
| 91 |
-
#print(policy_text)
|
| 92 |
-
|
| 93 |
-
return policy_text
|
| 94 |
-
|
| 95 |
-
def get_schools():
|
| 96 |
-
|
| 97 |
-
airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{policies_table_name}'
|
| 98 |
-
|
| 99 |
-
# Parameters for the API request to select only the 'school' field
|
| 100 |
-
params = {
|
| 101 |
-
'fields[]': 'school', # Replace with the name of your field
|
| 102 |
-
'sort[0][field]': 'school', # Sort by the 'school' field
|
| 103 |
-
'sort[0][direction]': 'asc', # Sort in ascending order
|
| 104 |
-
}
|
| 105 |
-
|
| 106 |
-
schools = ''
|
| 107 |
-
|
| 108 |
-
try:
|
| 109 |
-
# Send a GET request to the Airtable API
|
| 110 |
-
response = requests.get(airtable_endpoint, headers=headers, params=params)
|
| 111 |
-
|
| 112 |
-
# Check if the request was successful (status code 200)
|
| 113 |
-
if response.status_code == 200:
|
| 114 |
-
# Parse the JSON response
|
| 115 |
-
data = response.json()
|
| 116 |
-
|
| 117 |
-
# Check if there are records in the response
|
| 118 |
-
if data.get('records'):
|
| 119 |
-
# Extract the 'school' values from each record
|
| 120 |
-
schools = [record['fields']['school'] for record in data['records']]
|
| 121 |
-
|
| 122 |
-
else:
|
| 123 |
-
print("No records found in the 'policies' table.")
|
| 124 |
-
else:
|
| 125 |
-
print(f"Failed to retrieve data. Status code: {response.status_code}")
|
| 126 |
-
except Exception as e:
|
| 127 |
-
print(f"An error occurred: {str(e)}")
|
| 128 |
-
|
| 129 |
-
return schools
|
| 130 |
-
|
| 131 |
-
def get_prompt(header, template_content):
|
| 132 |
-
airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{prompts_table_name}'
|
| 133 |
-
|
| 134 |
-
params = {
|
| 135 |
-
'filterByFormula': "prompt_name='Compliance_v1'",
|
| 136 |
-
}
|
| 137 |
-
|
| 138 |
-
response = requests.get(airtable_endpoint, headers=headers, params=params)
|
| 139 |
-
|
| 140 |
-
# Check for errors
|
| 141 |
-
response.raise_for_status()
|
| 142 |
-
|
| 143 |
-
data = response.json()
|
| 144 |
-
|
| 145 |
-
# Check if there is at least one record matching the condition
|
| 146 |
-
if data.get('records'):
|
| 147 |
-
# Get the first record (there should be only one)
|
| 148 |
-
record = data['records'][0]['fields']
|
| 149 |
-
|
| 150 |
-
# Assign system_prompt and user_prompt to variables
|
| 151 |
-
header = record.get('system_prompt', '')
|
| 152 |
-
template_content = record.get('user_prompt', '')
|
| 153 |
-
|
| 154 |
-
return header, template_content
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
def append_to_at_compliancelog(policy_name_dd,contract_text,gpt_response, response_time, question_cost, prompt_tokens, completion_tokens):
|
| 158 |
-
airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{compliancelog_table_name}'
|
| 159 |
-
|
| 160 |
-
# Organize data for Airtable
|
| 161 |
-
new_fields = {
|
| 162 |
-
'policy_name': str(policy_name_dd),
|
| 163 |
-
'contract_text': str(contract_text),
|
| 164 |
-
'gpt_response': str(gpt_response),
|
| 165 |
-
'response_time': str(response_time),
|
| 166 |
-
'question_cost': question_cost,
|
| 167 |
-
'user_name': str(logged_in_user),
|
| 168 |
-
'prompt_tokens': prompt_tokens,
|
| 169 |
-
'completion_tokens': completion_tokens
|
| 170 |
-
}
|
| 171 |
-
|
| 172 |
-
data = {
|
| 173 |
-
'fields': new_fields
|
| 174 |
-
}
|
| 175 |
-
try:
|
| 176 |
-
|
| 177 |
-
# Post data to Airtable
|
| 178 |
-
response = requests.post(airtable_endpoint, headers=headers, json=data)
|
| 179 |
-
|
| 180 |
-
# print(response.json())
|
| 181 |
-
|
| 182 |
-
# Check for errors
|
| 183 |
-
response.raise_for_status()
|
| 184 |
-
|
| 185 |
-
except requests.exceptions.HTTPError as http_error:
|
| 186 |
-
# Handle the HTTP error (e.g., log it or display an error message)
|
| 187 |
-
print(f"HTTP error occurred: {http_error}")
|
| 188 |
-
|
| 189 |
-
except Exception as e:
|
| 190 |
-
# Handle exceptions, log errors, or raise them as needed
|
| 191 |
-
print(f"An error occurred: {str(e)}")
|
| 192 |
-
|
| 193 |
-
def format_date(date_str):
|
| 194 |
-
# Convert YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS.000Z to MM/DD/YYYY HH:MM:SS format
|
| 195 |
-
try:
|
| 196 |
-
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
|
| 197 |
-
return date_obj.strftime("%m/%d/%Y")
|
| 198 |
-
except ValueError:
|
| 199 |
-
try:
|
| 200 |
-
date_obj = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S.%fZ")
|
| 201 |
-
return date_obj.strftime("%m/%d/%Y %H:%M")
|
| 202 |
-
except ValueError:
|
| 203 |
-
return date_str
|
| 204 |
-
|
| 205 |
-
def get_compliance_history():
|
| 206 |
-
airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{compliance_history_table_name}'
|
| 207 |
-
params = {
|
| 208 |
-
#'filterByFormula': f'school="{policy_name_dd}"',
|
| 209 |
-
'fields[]': ['sponsor', 'contract_value', 'student_name', 'student_email', 'start_date', 'end_date', 'status', 'Created'],
|
| 210 |
-
'sort[0][field]': 'Created',
|
| 211 |
-
'sort[0][direction]': 'desc',
|
| 212 |
-
}
|
| 213 |
-
|
| 214 |
-
compliance_fields = [
|
| 215 |
-
'sponsor', 'contract_value', 'student_name', 'student_email', 'start_date', 'end_date', 'status', 'Created'
|
| 216 |
-
]
|
| 217 |
-
compliance_records = []
|
| 218 |
-
|
| 219 |
-
try:
|
| 220 |
-
# Send a GET request to the Airtable API
|
| 221 |
-
response = requests.get(airtable_endpoint, headers=headers, params=params)
|
| 222 |
-
|
| 223 |
-
# Check if the request was successful (status code 200)
|
| 224 |
-
if response.status_code == 200:
|
| 225 |
-
# Parse the JSON response
|
| 226 |
-
data = response.json()
|
| 227 |
-
|
| 228 |
-
# Check if there are records in the response
|
| 229 |
-
if data.get('records'):
|
| 230 |
-
for record in data['records']:
|
| 231 |
-
# Use list comprehension to create the record in the correct field order
|
| 232 |
-
record_data = [
|
| 233 |
-
format_date(record['fields'][field]) if field in ['start_date', 'end_date', 'Created'] else
|
| 234 |
-
record['fields'][field]
|
| 235 |
-
for field in compliance_fields
|
| 236 |
-
]
|
| 237 |
-
compliance_records.append(record_data)
|
| 238 |
-
|
| 239 |
-
else:
|
| 240 |
-
print("No records found in the 'compliance history' table.")
|
| 241 |
-
else:
|
| 242 |
-
print(f"Failed to retrieve data. Status code: {response.status_code}")
|
| 243 |
-
except Exception as e:
|
| 244 |
-
print(f"An error occurred: {str(e)}")
|
| 245 |
-
|
| 246 |
-
#print(compliance_records)
|
| 247 |
-
|
| 248 |
-
return compliance_records
|
| 249 |
-
|
| 250 |
-
def append_to_at_compliance_history(policy_name_dd,contract_redline_html,compliance_comments_tbox,sponsor_tbox,compensation_num,status_ddss,name_tbox,email_tbox,start_date_tbox,end_date_tbox):
|
| 251 |
-
airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{compliance_history_table_name}'
|
| 252 |
-
|
| 253 |
-
# Organize data for Airtable
|
| 254 |
-
new_fields = {
|
| 255 |
-
'school': str(policy_name_dd),
|
| 256 |
-
'sponsor': str(sponsor_tbox),
|
| 257 |
-
'contract_redline': str(contract_redline_html),
|
| 258 |
-
'compliance_comments': str(compliance_comments_tbox),
|
| 259 |
-
'contract_value': compensation_num,
|
| 260 |
-
'status': str(status_ddss),
|
| 261 |
-
'student_name': str(name_tbox),
|
| 262 |
-
'student_email': str(email_tbox),
|
| 263 |
-
'start_date': str(start_date_tbox),
|
| 264 |
-
'end_date': str(end_date_tbox)
|
| 265 |
-
}
|
| 266 |
-
|
| 267 |
-
data = {
|
| 268 |
-
'fields': new_fields
|
| 269 |
-
}
|
| 270 |
-
if (
|
| 271 |
-
not policy_name_dd or
|
| 272 |
-
not contract_redline_html or
|
| 273 |
-
not compliance_comments_tbox or
|
| 274 |
-
not sponsor_tbox or
|
| 275 |
-
not compensation_num or
|
| 276 |
-
not status_ddss or
|
| 277 |
-
not name_tbox or
|
| 278 |
-
not email_tbox or
|
| 279 |
-
not start_date_tbox or
|
| 280 |
-
not end_date_tbox
|
| 281 |
-
):
|
| 282 |
-
gr.Warning("One or more fields are blank. Contract cannot be saved")
|
| 283 |
-
return
|
| 284 |
-
|
| 285 |
-
try:
|
| 286 |
-
|
| 287 |
-
# Post data to Airtable
|
| 288 |
-
response = requests.post(airtable_endpoint, headers=headers, json=data)
|
| 289 |
-
|
| 290 |
-
#print(response.json())
|
| 291 |
-
|
| 292 |
-
# Check for errors
|
| 293 |
-
response.raise_for_status()
|
| 294 |
-
|
| 295 |
-
gr.Info("Contract Saved")
|
| 296 |
-
|
| 297 |
-
compliance_history = get_compliance_history()
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
return {compliance_history_df: compliance_history }
|
| 301 |
-
|
| 302 |
-
except requests.exceptions.HTTPError as http_error:
|
| 303 |
-
# Handle the HTTP error (e.g., log it or display an error message)
|
| 304 |
-
print(f"HTTP error occurred: {http_error}")
|
| 305 |
-
|
| 306 |
-
except Exception as e:
|
| 307 |
-
# Handle exceptions, log errors, or raise them as needed
|
| 308 |
-
print(f"An error occurred: {str(e)}")
|
| 309 |
-
|
| 310 |
-
# Chatbot Function
|
| 311 |
-
def chatbot(policy_name_dd,contract_text,progress=gr.Progress()):
|
| 312 |
-
|
| 313 |
-
start_time = datetime.now()
|
| 314 |
-
|
| 315 |
-
progress(progress=None)
|
| 316 |
-
|
| 317 |
-
"""
|
| 318 |
-
time.sleep(10)
|
| 319 |
-
for i in progress.tqdm(range(100)):
|
| 320 |
-
time.sleep(1)
|
| 321 |
-
"""
|
| 322 |
-
|
| 323 |
-
#print(policy_name)
|
| 324 |
-
|
| 325 |
-
#students = get_students(school_selection)
|
| 326 |
-
get_policy_text(policy_name_dd)
|
| 327 |
-
|
| 328 |
-
#print(policy_text)
|
| 329 |
-
#print(contract_text)
|
| 330 |
-
|
| 331 |
-
template_content = ''
|
| 332 |
-
header = ''
|
| 333 |
-
|
| 334 |
-
header, template_content = get_prompt(header, template_content)
|
| 335 |
-
|
| 336 |
-
# print(header)
|
| 337 |
-
# print(template_content)
|
| 338 |
-
|
| 339 |
-
# Create a Jinja2 template from the content
|
| 340 |
-
template = Template(template_content)
|
| 341 |
-
|
| 342 |
-
# Render the template with the inputs
|
| 343 |
-
analysis_input = template.render(contract_text=contract_text,policy_text=policy_text)
|
| 344 |
-
|
| 345 |
-
trimmed_input = prompt_trim(analysis_input)
|
| 346 |
-
|
| 347 |
-
with open('analysis_input.txt', 'w', encoding='utf-8') as out_file:
|
| 348 |
-
out_file.write(trimmed_input)
|
| 349 |
-
|
| 350 |
-
gpt_model = "gpt-4-1106-preview"
|
| 351 |
-
response = openai.ChatCompletion.create(
|
| 352 |
-
model=gpt_model,
|
| 353 |
-
temperature=0,
|
| 354 |
-
messages=[
|
| 355 |
-
{
|
| 356 |
-
"role": "system",
|
| 357 |
-
"content": header
|
| 358 |
-
},
|
| 359 |
-
{
|
| 360 |
-
"role": "user",
|
| 361 |
-
"content": analysis_input
|
| 362 |
-
}
|
| 363 |
-
]
|
| 364 |
-
)
|
| 365 |
-
|
| 366 |
-
gpt_response = response.choices[0].message["content"]
|
| 367 |
-
|
| 368 |
-
tokens_used = response.usage
|
| 369 |
-
if gpt_model == "gpt-4":
|
| 370 |
-
question_cost = (tokens_used.get('total_tokens', 0) / 1000) * .03
|
| 371 |
-
prompt_tokens = tokens_used.get('prompt_tokens', )
|
| 372 |
-
completion_tokens = tokens_used.get('completion_tokens', 0)
|
| 373 |
-
|
| 374 |
-
else:
|
| 375 |
-
prompt_tokens = tokens_used.get('prompt_tokens', )
|
| 376 |
-
completion_tokens = tokens_used.get('completion_tokens', 0)
|
| 377 |
-
question_cost = ((prompt_tokens / 1000) * .01) + ((completion_tokens / 1000) * .03)
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
with open('response.txt', 'w', encoding='utf-8') as out_file:
|
| 381 |
-
out_file.write(gpt_response)
|
| 382 |
-
|
| 383 |
-
end_time = datetime.now()
|
| 384 |
-
|
| 385 |
-
response_time = end_time - start_time
|
| 386 |
-
|
| 387 |
-
data = json.loads(gpt_response.replace("```json", "").replace("```", "").strip())
|
| 388 |
-
|
| 389 |
-
global student_name, student_email, sponsor_name, start_date, end_date
|
| 390 |
-
student_name = data['header']['Student Name']
|
| 391 |
-
student_email = data['header']['Student Email']
|
| 392 |
-
sponsor_name = data['header']['Sponsor Name']
|
| 393 |
-
start_date = data['header']['Start Date']
|
| 394 |
-
end_date = data['header']['End Date']
|
| 395 |
-
|
| 396 |
-
# Extracting data from the 'body' node
|
| 397 |
-
html_content = data['body']
|
| 398 |
-
|
| 399 |
-
append_to_at_compliancelog(policy_name_dd,contract_text,html_content, response_time, question_cost, prompt_tokens, completion_tokens)
|
| 400 |
-
|
| 401 |
-
return {contract_redline_html: html_content, download_row: gr.Row(visible=True)}
|
| 402 |
-
|
| 403 |
-
def log_login(username):
|
| 404 |
-
airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{user_log_table_name}'
|
| 405 |
-
|
| 406 |
-
# Organize data for Airtable
|
| 407 |
-
new_fields = {
|
| 408 |
-
'user_name': str(username),
|
| 409 |
-
'app': str(app)
|
| 410 |
-
}
|
| 411 |
-
|
| 412 |
-
data = {
|
| 413 |
-
'fields': new_fields
|
| 414 |
-
}
|
| 415 |
-
try:
|
| 416 |
-
|
| 417 |
-
# Post data to Airtable
|
| 418 |
-
response = requests.post(airtable_endpoint, headers=headers, json=data)
|
| 419 |
-
|
| 420 |
-
# Check for errors
|
| 421 |
-
response.raise_for_status()
|
| 422 |
-
|
| 423 |
-
except requests.exceptions.HTTPError as http_error:
|
| 424 |
-
# Handle the HTTP error (e.g., log it or display an error message)
|
| 425 |
-
print(f"HTTP error occurred: {http_error}")
|
| 426 |
-
|
| 427 |
-
except Exception as e:
|
| 428 |
-
# Handle exceptions, log errors, or raise them as needed
|
| 429 |
-
print(f"An error occurred: {str(e)}")
|
| 430 |
-
|
| 431 |
|
| 432 |
def login_auth(username, password):
|
| 433 |
-
airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{users_table_name}'
|
| 434 |
|
| 435 |
-
|
| 436 |
-
params = {
|
| 437 |
-
'filterByFormula': f'AND(user_name = "{username}", password = "{password}")'
|
| 438 |
-
}
|
| 439 |
-
|
| 440 |
-
response = requests.get(airtable_endpoint, headers=headers, params=params)
|
| 441 |
-
|
| 442 |
-
if response.status_code == 200:
|
| 443 |
-
data = response.json()
|
| 444 |
-
if data.get('records'):
|
| 445 |
-
log_login(username)
|
| 446 |
-
global logged_in_user
|
| 447 |
-
logged_in_user = username
|
| 448 |
|
| 449 |
#gr.Info("Login Success")
|
| 450 |
|
|
@@ -455,105 +15,7 @@ def login_auth(username, password):
|
|
| 455 |
|
| 456 |
return {login_row: gr.Row(visible=True), app_row: gr.Row(visible=False)}
|
| 457 |
|
| 458 |
-
|
| 459 |
-
def extract_text_with_spacing(pdf_path):
|
| 460 |
-
document = fitz.open(pdf_path)
|
| 461 |
-
all_text = []
|
| 462 |
-
|
| 463 |
-
for page in document:
|
| 464 |
-
# Extract text in a dict structure
|
| 465 |
-
blocks = page.get_text("dict")["blocks"]
|
| 466 |
-
|
| 467 |
-
for b in blocks:
|
| 468 |
-
if "lines" in b: # Check if the block contains lines of text
|
| 469 |
-
for line in b["lines"]:
|
| 470 |
-
span_texts = [span["text"] for span in line["spans"]]
|
| 471 |
-
all_text.append(" ".join(span_texts))
|
| 472 |
-
all_text.append("\n") # Presume a new block is a new paragraph
|
| 473 |
-
|
| 474 |
-
document.close()
|
| 475 |
-
return "\n".join(all_text)
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
def pdf_to_text(contract_file_cmpt, contract_text_tbox, file_name_tbox):
|
| 479 |
-
|
| 480 |
-
file_text = extract_text_with_spacing(contract_file_cmpt.name)
|
| 481 |
-
|
| 482 |
-
#file_text = extract_text(contract_file_cmpt.name)
|
| 483 |
-
original_file_name = contract_file_cmpt.name.split("/")[-1]
|
| 484 |
-
redline_file_name = original_file_name.split(".")[0]+" Redline.pdf"
|
| 485 |
-
|
| 486 |
-
return file_text, redline_file_name, None, None, None
|
| 487 |
-
|
| 488 |
-
def convert_html_to_pdf(source_html, output_filename):
|
| 489 |
-
# Result file stream
|
| 490 |
-
result_file = open(output_filename, "w+b")
|
| 491 |
-
|
| 492 |
-
# Convert HTML to PDF
|
| 493 |
-
pisa_status = pisa.CreatePDF(
|
| 494 |
-
BytesIO(source_html.encode("UTF-8")), # HTML content
|
| 495 |
-
dest=result_file) # File handle to receive the result
|
| 496 |
-
|
| 497 |
-
# Close the result file
|
| 498 |
-
result_file.close()
|
| 499 |
-
|
| 500 |
-
# Return True on success and False on errors
|
| 501 |
-
return pisa_status.err
|
| 502 |
-
|
| 503 |
-
def download_pdf(compliance_comments,contract_redline_html,file_name_tbox):
|
| 504 |
-
#config = pdfkit.configuration(wkhtmltopdf="/usr/local/bin/wkhtmltopdf")
|
| 505 |
-
|
| 506 |
-
contract_redline_comments = "<h2><u>Contract Redline:</u></h2><br>"+contract_redline_html + "<br><h2><u>Compliance Comments:</u></h2><br>"+compliance_comments
|
| 507 |
-
|
| 508 |
-
#global pdf_download
|
| 509 |
-
#pdf_download = pdfkit.from_string(contract_redline_comments,file_name_tbox,configuration=config)
|
| 510 |
-
|
| 511 |
-
convert_html_to_pdf(contract_redline_comments, file_name_tbox)
|
| 512 |
-
|
| 513 |
-
return {pdf_download_file: file_name_tbox}
|
| 514 |
-
|
| 515 |
-
def change_tab(contract_text):
|
| 516 |
-
|
| 517 |
-
if (
|
| 518 |
-
not contract_text
|
| 519 |
-
):
|
| 520 |
-
gr.Warning("Contract Text is blank.")
|
| 521 |
-
return False
|
| 522 |
-
|
| 523 |
-
return gr.Tabs(selected=1)
|
| 524 |
-
|
| 525 |
-
def update_tboxes():
|
| 526 |
-
return {sponsor_tbox: sponsor_name, name_tbox: student_name, email_tbox: student_email, start_date_tbox: start_date, end_date_tbox: end_date}
|
| 527 |
-
|
| 528 |
-
def policy_name_change(policy_name_dd):
|
| 529 |
-
|
| 530 |
-
gr.Info(f'Policy Name Changed to {policy_name_dd}')
|
| 531 |
-
|
| 532 |
-
# Gradio UI
|
| 533 |
-
CIMStheme = gr.themes.Soft().set(
|
| 534 |
-
button_primary_background_fill='#6562F4',
|
| 535 |
-
)
|
| 536 |
-
|
| 537 |
-
schools = get_schools()
|
| 538 |
-
|
| 539 |
-
#compliance_history = get_compliance_history(["Wake Forest University"])
|
| 540 |
-
#compliance_history = get_compliance_history()
|
| 541 |
-
#policy_text = get_policy_text("LSU") #for testing the function call
|
| 542 |
-
|
| 543 |
-
#contract_redline = "<p><strong>Campaign:</strong></p><p><strong>Engagement Name:</strong> Applebee's Fall Burger Promo</p><p><strong>Engagement Id:</strong> 7015j000001DvCAAA0</p><p><strong>Sponsor:</strong> Applebee's</p><p><strong>Start Date:</strong> 2023-10-28</p><p><strong>End Date:</strong> 2023-11-11</p><p><strong>Engagement Description</strong></p><p>The goal of the engagement is to Increase Sales by having the Student-Athlete Social Media Post. For the Social Media Post, the sponsor is requesting the student athlete take a photo in front of the <span style='text-decoration: line-through;'>football stadium</span> in eating a Applebee's burger <span style='text-decoration: line-through;'>in your team jersey</span>. The Media rights for the content will be 90 Days.</p><p><strong>Engagement Compensation.</strong></p><p>For successful completion of the engagement the student-athlete will receive payment in the form of Cash.</p><p>Part or all of the payment will be in cash, paid via PayPal.</p><p>The total value of compensation will be 250.</p>"
|
| 544 |
-
#pdf_download = pdfkit.from_string(contract_redline, False)
|
| 545 |
-
|
| 546 |
-
#print(pdf_download)
|
| 547 |
-
|
| 548 |
-
logged_in_user = 'admin'
|
| 549 |
-
file_text = ''
|
| 550 |
-
contract_text = ''
|
| 551 |
-
policy_text = ''
|
| 552 |
-
compliance_comments = ''
|
| 553 |
-
file_name = 'redline.pdf'
|
| 554 |
-
pdf_download = ''
|
| 555 |
-
|
| 556 |
-
with gr.Blocks(CIMStheme) as iface:
|
| 557 |
with gr.Row(visible=False) as app_row:
|
| 558 |
with gr.Column():
|
| 559 |
with gr.Row():
|
|
@@ -564,93 +26,6 @@ with gr.Blocks(CIMStheme) as iface:
|
|
| 564 |
gr.Markdown(value="<H2 style='text-align: center;'>NILI Compliance Desktop</h2>")
|
| 565 |
with gr.Column(scale=2):
|
| 566 |
gr.Markdown("")
|
| 567 |
-
"""
|
| 568 |
-
with gr.Tabs() as tabs:
|
| 569 |
-
with gr.Tab(label="Contract Upload", id=0) as upload_tab:
|
| 570 |
-
with gr.Row():
|
| 571 |
-
with gr.Column(variant='panel',scale=1):
|
| 572 |
-
contract_file_cmpt = gr.File(label="Select Contract File",file_count="single",file_types=[".pdf"],height=150)
|
| 573 |
-
with gr.Column(variant='panel',scale=4):
|
| 574 |
-
with gr.Row():
|
| 575 |
-
with gr.Column(variant='panel'):
|
| 576 |
-
contract_text_tbox = gr.Textbox(label="Contract Text",interactive=True,info="Upload .pdf or paste in text. Shift-Enter to add a line")
|
| 577 |
-
with gr.Row():
|
| 578 |
-
with gr.Column(scale=1):
|
| 579 |
-
upload_btn = gr.components.Button(value="Upload Contract", size='sm', variant="primary")
|
| 580 |
-
with gr.Column(scale=2):
|
| 581 |
-
gr.Markdown("")
|
| 582 |
-
with gr.Column(scale=1):
|
| 583 |
-
redline_btn = gr.components.Button(value="Redline Contract", size='sm', variant="primary")
|
| 584 |
-
with gr.Column(scale=2):
|
| 585 |
-
gr.Markdown("")
|
| 586 |
-
with gr.Tab(label="Contract Redline", id=1) as redline_tab:
|
| 587 |
-
with gr.Row(variant='panel'):
|
| 588 |
-
with gr.Column():
|
| 589 |
-
sponsor_tbox = gr.Textbox(label="Sponsor:", interactive=True)
|
| 590 |
-
compensation_num = gr.Number(label="Contract Value",value=0)
|
| 591 |
-
status_ddss = gr.Dropdown(["Pending","Approved","Rejected"],multiselect=False,label="Status", value="Pending")
|
| 592 |
-
with gr.Column():
|
| 593 |
-
name_tbox = gr.Textbox(label="Name:", interactive=True)
|
| 594 |
-
email_tbox = gr.Textbox(label="Email:", interactive=True, type='email',placeholder='xxxxxxx@xxxxxx.xxx')
|
| 595 |
-
with gr.Column():
|
| 596 |
-
start_date_tbox = gr.Textbox(label="Start Date:", interactive=True, placeholder='MM/DD/YYYY')
|
| 597 |
-
end_date_tbox = gr.Textbox(label="End Date:", interactive=True, placeholder='MM/DD/YYYY')
|
| 598 |
-
with gr.Row():
|
| 599 |
-
with gr.Column(variant='panel'):
|
| 600 |
-
gr.components.Markdown(response_label)
|
| 601 |
-
contract_redline_html = gr.HTML(label="Contract Redline")
|
| 602 |
-
compliance_comments_tbox = gr.Textbox(interactive=True,label='Compliance Comments')
|
| 603 |
-
with gr.Row():
|
| 604 |
-
with gr.Column():
|
| 605 |
-
save_btn = gr.Button(value="Save Contract", size='sm', variant="primary")
|
| 606 |
-
with gr.Column():
|
| 607 |
-
gr.Markdown("")
|
| 608 |
-
with gr.Column():
|
| 609 |
-
gr.Markdown("")
|
| 610 |
-
with gr.Column():
|
| 611 |
-
gr.Markdown("")
|
| 612 |
-
with gr.Column():
|
| 613 |
-
gr.Markdown("")
|
| 614 |
-
with gr.Row(visible=False) as download_row:
|
| 615 |
-
with gr.Column(variant='panel'):
|
| 616 |
-
file_name_tbox = gr.Textbox(interactive=False,label='File Name',visible=False)
|
| 617 |
-
pdf_download_file = gr.File()
|
| 618 |
-
download_btn = gr.Button(value="Create Redline PDF", size='sm', variant="primary")
|
| 619 |
-
upload_btn.click(pdf_to_text,inputs=[contract_file_cmpt,contract_text_tbox,file_name_tbox],outputs=[contract_text_tbox,file_name_tbox,contract_redline_html,compliance_comments_tbox,pdf_download_file])
|
| 620 |
-
download_btn.click(download_pdf,inputs=[compliance_comments_tbox,contract_redline_html,file_name_tbox],outputs=pdf_download_file)
|
| 621 |
-
with gr.Column():
|
| 622 |
-
gr.Markdown("")
|
| 623 |
-
with gr.Column():
|
| 624 |
-
gr.Markdown("")
|
| 625 |
-
with gr.Column():
|
| 626 |
-
gr.Markdown("")
|
| 627 |
-
with gr.Column():
|
| 628 |
-
gr.Markdown("")
|
| 629 |
-
with gr.Tab(label="History", id=3) as history_tab:
|
| 630 |
-
with gr.Row():
|
| 631 |
-
with gr.Column(variant='panel'):
|
| 632 |
-
compliance_history_df = gr.Dataframe(get_compliance_history,
|
| 633 |
-
headers=["Sponsor", "Contract Value", "Student Name", "Student Email", "Start Date", "End Date", "Status","Created"],
|
| 634 |
-
datatype=["str", "number", "str", "str", "date", "date", "str","date"],
|
| 635 |
-
label="Contract History",
|
| 636 |
-
interactive=False
|
| 637 |
-
)
|
| 638 |
-
with gr.Tab(label="Settings", id=4) as settings_tab:
|
| 639 |
-
with gr.Row():
|
| 640 |
-
with gr.Column(variant='panel',scale=1):
|
| 641 |
-
policy_name_dd = gr.Dropdown(schools, multiselect=False,label="NIL Policy Selection", value="Wake Forest University")
|
| 642 |
-
policy_name_dd.change(policy_name_change,inputs=policy_name_dd,outputs=None)
|
| 643 |
-
redline_btn.click(change_tab, inputs=contract_text_tbox, outputs=tabs).success(chatbot,inputs=[policy_name_dd,contract_text_tbox],
|
| 644 |
-
outputs=[contract_redline_html,download_row]).then(update_tboxes,inputs=None,
|
| 645 |
-
outputs=[sponsor_tbox,name_tbox,email_tbox,start_date_tbox,end_date_tbox])
|
| 646 |
-
save_btn.click(append_to_at_compliance_history,inputs=[policy_name_dd,contract_redline_html,compliance_comments_tbox,sponsor_tbox,compensation_num,status_ddss,name_tbox,email_tbox,start_date_tbox,end_date_tbox],outputs=compliance_history_df)
|
| 647 |
-
with gr.Column():
|
| 648 |
-
gr.Markdown("")
|
| 649 |
-
with gr.Column():
|
| 650 |
-
gr.Markdown("")
|
| 651 |
-
with gr.Column():
|
| 652 |
-
gr.Markdown("")
|
| 653 |
-
"""
|
| 654 |
with gr.Row(visible=True) as login_row:
|
| 655 |
with gr.Column():
|
| 656 |
with gr.Row():
|
|
@@ -680,5 +55,4 @@ with gr.Blocks(CIMStheme) as iface:
|
|
| 680 |
gr.HTML('<center><i>© 2023 Collegiate Influencer Marketing Systems, Inc.</i><br>CIMS.AI, CIMS.AI logo, NILI, NILI logo, and EzNIL are trademarks of Collegiate Influencer Marketing Systems, Inc.</center>')
|
| 681 |
|
| 682 |
iface.queue()
|
| 683 |
-
#iface.queue(concurrency_count=20).launch(auth=login_auth, auth_message= "Enter your username and password that you received from CIMS.AI. To request a login, please email 'info@cims.ai'")
|
| 684 |
iface.launch(show_api=False)
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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=False)}
|
| 17 |
|
| 18 |
+
with gr.Blocks() as iface:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
with gr.Row(visible=False) as app_row:
|
| 20 |
with gr.Column():
|
| 21 |
with gr.Row():
|
|
|
|
| 26 |
gr.Markdown(value="<H2 style='text-align: center;'>NILI Compliance Desktop</h2>")
|
| 27 |
with gr.Column(scale=2):
|
| 28 |
gr.Markdown("")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
with gr.Row(visible=True) as login_row:
|
| 30 |
with gr.Column():
|
| 31 |
with gr.Row():
|
|
|
|
| 55 |
gr.HTML('<center><i>© 2023 Collegiate Influencer Marketing Systems, Inc.</i><br>CIMS.AI, CIMS.AI logo, NILI, NILI logo, and EzNIL are trademarks of Collegiate Influencer Marketing Systems, Inc.</center>')
|
| 56 |
|
| 57 |
iface.queue()
|
|
|
|
| 58 |
iface.launch(show_api=False)
|