Spaces:
Sleeping
Sleeping
File size: 16,525 Bytes
289132f 60ab3b8 289132f 60ab3b8 289132f 67b54e2 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 9d6ebfa 289132f 67b54e2 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 67b54e2 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 67b54e2 289132f 60ab3b8 67b54e2 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 67b54e2 60ab3b8 289132f fa4d5f0 60ab3b8 289132f 60ab3b8 289132f 67b54e2 289132f 60ab3b8 289132f 60ab3b8 289132f fa4d5f0 289132f 67b54e2 289132f 60ab3b8 67b54e2 60ab3b8 67b54e2 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 907f33c 289132f 907f33c 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 2c17158 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 289132f 60ab3b8 fa4d5f0 60ab3b8 fa4d5f0 60ab3b8 fa4d5f0 e9a3875 60ab3b8 289132f 60ab3b8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
import os
import openai
import datetime
import time
import gradio as gr
import json
from jinja2 import Template
import requests
from pdfminer.high_level import extract_text
import pdfkit
# Initialize OpenAI
openai.api_key = os.environ.get('OPENAI_API_KEY')
# Configuration variables
airtable_api_key = os.environ.get('AIRTABLE_API_KEY')
# Airtable table names
prompts_table_name = 'tblYIZEB8m6JkGDEP'
users_table_name = 'tblLNe5ZL47SvrAEk'
user_log_table_name = 'tblrlTsRrkl6BqMAJ'
scoutlog_table_name = 'tblF30p0krA1FwOiD'
policies_table_name = 'tbla6PC65qZfqdJhE'
# Define the style and content for the response field
label_text = "Contract Redline"
color = "#6562F4"
background_color = "white"
border_radius = "10px"
# response_label = f'<h3 style="color: {color}; background-color: {background_color}; border-radius: {border_radius}; padding: 10px;display: inline-block;">{label_text}</h3>'
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>'
base_id = 'appcUK3hUWC7GM2Kb'
# App name for user login logging
app = "Scout"
headers = {
"Authorization": f"Bearer {airtable_api_key}",
"Content-Type": "application/json",
"Accept": "application/json",
}
def prompt_trim(prompt: str) -> str:
lines = prompt.split('\n')
trimmed = '\n'.join([l.strip() for l in lines])
return trimmed
def get_policy_text(school):
airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{policies_table_name}'
# Parameters for the API request to select only the 'school' field
params = {
'filterByFormula': f'school="{school}"',
'fields[]': 'policy_text'
}
global policy_text
policy_text = ''
try:
# Send a GET request to the Airtable API
response = requests.get(airtable_endpoint, headers=headers, params=params)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse the JSON response
data = response.json()
# Check if there are records in the response
if data.get('records'):
# Extract the 'school' values from each record
policy_text = [record['fields']['policy_text'] for record in data['records']]
else:
print("No records found in the 'policies' table.")
else:
print(f"Failed to retrieve data. Status code: {response.status_code}")
except Exception as e:
print(f"An error occurred: {str(e)}")
#print(policy_text)
return policy_text
def get_schools():
airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{policies_table_name}'
# Parameters for the API request to select only the 'school' field
params = {
'fields[]': 'school', # Replace with the name of your field
'sort[0][field]': 'school', # Sort by the 'school' field
'sort[0][direction]': 'asc', # Sort in ascending order
}
schools = ''
try:
# Send a GET request to the Airtable API
response = requests.get(airtable_endpoint, headers=headers, params=params)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse the JSON response
data = response.json()
# Check if there are records in the response
if data.get('records'):
# Extract the 'school' values from each record
schools = [record['fields']['school'] for record in data['records']]
else:
print("No records found in the 'policies' table.")
else:
print(f"Failed to retrieve data. Status code: {response.status_code}")
except Exception as e:
print(f"An error occurred: {str(e)}")
return schools
def get_prompt(header, template_content):
airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{prompts_table_name}'
params = {
'filterByFormula': "prompt_name='Compliance_v1'",
}
response = requests.get(airtable_endpoint, headers=headers, params=params)
# Check for errors
response.raise_for_status()
data = response.json()
# Check if there is at least one record matching the condition
if data.get('records'):
# Get the first record (there should be only one)
record = data['records'][0]['fields']
# Assign system_prompt and user_prompt to variables
header = record.get('system_prompt', '')
template_content = record.get('user_prompt', '')
return header, template_content
def append_to_at_scountlog(school_selection, output_format, gender, interests, sports, gpt_response, number_of_athletes,
social_media_channel, response_time, question_cost, prompt_tokens, completion_tokens):
airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{scoutlog_table_name}'
# Organize data for Airtable
new_fields = {
'school_selection': str(school_selection),
'gender': str(gender),
'social_media_channel': str(social_media_channel),
'sports': str(sports),
'interests': str(interests),
'number_of_athletes': int(number_of_athletes),
'output_format': str(output_format),
'gpt_response': str(gpt_response),
'response_time': str(response_time),
'question_cost': question_cost,
'user_name': str(logged_in_user),
'prompt_tokens': prompt_tokens,
'completion_tokens': completion_tokens
}
data = {
'fields': new_fields
}
try:
# Post data to Airtable
response = requests.post(airtable_endpoint, headers=headers, json=data)
# print(response.json())
# Check for errors
response.raise_for_status()
except requests.exceptions.HTTPError as http_error:
# Handle the HTTP error (e.g., log it or display an error message)
print(f"HTTP error occurred: {http_error}")
except Exception as e:
# Handle exceptions, log errors, or raise them as needed
print(f"An error occurred: {str(e)}")
# Chatbot Function
def chatbot(policy_name,contract_text,progress=gr.Progress()):
start_time = datetime.datetime.now()
progress(progress=None)
"""
time.sleep(10)
for i in progress.tqdm(range(100)):
time.sleep(1)
"""
#print(policy_name)
#students = get_students(school_selection)
get_policy_text(policy_name)
#print(policy_text)
#print(contract_text)
template_content = ''
header = ''
header, template_content = get_prompt(header, template_content)
# print(header)
# print(template_content)
# Create a Jinja2 template from the content
template = Template(template_content)
# Render the template with the inputs
analysis_input = template.render(contract_text=contract_text,policy_text=policy_text)
trimmed_input = prompt_trim(analysis_input)
with open('analysis_input.txt', 'w', encoding='utf-8') as out_file:
out_file.write(trimmed_input)
response = openai.ChatCompletion.create(
model="gpt-4",
# model="gpt-3.5-turbo",
temperature=0,
messages=[
{
"role": "system",
"content": header
},
{
"role": "user",
"content": analysis_input
}
]
)
gpt_response = response.choices[0].message["content"]
tokens_used = response.usage
question_cost = (tokens_used.get('total_tokens', 0) / 1000) * .03
prompt_tokens = tokens_used.get('prompt_tokens', )
completion_tokens = tokens_used.get('completion_tokens', 0)
with open('response.txt', 'w', encoding='utf-8') as out_file:
out_file.write(gpt_response)
end_time = datetime.datetime.now()
response_time = end_time - start_time
contract_redline = gpt_response
#print(contract_redline)
#append_to_at_scountlog(school_selection, output_format, gender, interests, sports, gpt_response, number_of_athletes,social_media_channel, response_time, question_cost, prompt_tokens, completion_tokens)
return {contract_redline_html: contract_redline, download_row: gr.Row(visible=True)}
def log_login(username):
airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{user_log_table_name}'
# Organize data for Airtable
new_fields = {
'user_name': str(username),
'app': str(app)
}
data = {
'fields': new_fields
}
try:
# Post data to Airtable
response = requests.post(airtable_endpoint, headers=headers, json=data)
# Check for errors
response.raise_for_status()
except requests.exceptions.HTTPError as http_error:
# Handle the HTTP error (e.g., log it or display an error message)
print(f"HTTP error occurred: {http_error}")
except Exception as e:
# Handle exceptions, log errors, or raise them as needed
print(f"An error occurred: {str(e)}")
def login_auth(username, password):
airtable_endpoint = f'https://api.airtable.com/v0/{base_id}/{users_table_name}'
# Query the 'users' table to check for a match with the provided username and password
params = {
'filterByFormula': f'AND(user_name = "{username}", password = "{password}")'
}
response = requests.get(airtable_endpoint, headers=headers, params=params)
if response.status_code == 200:
data = response.json()
if data.get('records'):
log_login(username)
global logged_in_user
logged_in_user = username
return True
print(f"Invalid user/password combination")
return False
def pdf_to_text(contract_file_cmpt):
file_text = extract_text(contract_file_cmpt.name)
original_file_name = contract_file_cmpt.name.split("/")[-1]
redline_file_name = original_file_name.split(".")[0]+" Redline.pdf"
return file_text,redline_file_name
def download_pdf(file_name,compliance_comments,contract_redline_html):
contract_redline_comments = "<h2><u>Contract Redline:</u></h2><br>"+contract_redline + "<br><h2><u>Compliance Comments:</u></h2><br>"+compliance_comments
pdf_download = pdfkit.from_string(contract_redline_comments,False)
with open(file_name, 'wb') as f:
f.write(pdf_download)
return file_name
# Gradio UI
CIMStheme = gr.themes.Soft().set(
button_primary_background_fill='#6562F4',
)
schools = get_schools()
#policy_text = get_policy_text("LSU") #for testing the function call
# scout_response = "<!DOCTYPEhtml><html><head><title>HTMLTableGenerator</title><style>table{width:100%;border:2pxsolid#000000;padding:5px;}tableth{border:2pxsolid#000000;padding:5px;background:#f0f0f0;color:#000000;}tabletd{border:2pxsolid#000000;text-align:left;padding:5px;background:#ffffff;color:#000000;}</style></head><body><table><thead><tr><th>Rank</th><th>CIMS.AI <br>Scout Ranking</th><th>Athlete Name</th><th>Gender</th><th>Primary Sport</th><th>Social Media <br>Fan Total</th><th>Interests</th><th>Commentary on Ranking</th></tr></thead><tbody><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></tbody></table></body></html>"
scout_response = "<!DOCTYPEhtml><html><head><title>HTMLTableGenerator</title><style>table{width:100%;border:2pxsolid#000000;padding:5px;background:#ffffff}tableth{border:2pxsolid#000000;padding:5px;background:#f0f0f0;color:#000000;}tabletd{border:2pxsolid#000000;text-align:left;padding:5px;background:#ffffff;color:#000000;}</style></head><body><table><thead><tr><th>Rank</th><th>CIMS.AI<br>Scout<br>Ranking</th><th>Athlete Name</th><th>Gender</th><th>Primary Sport</th><th>Social Media<br>Fan Total</th><th>Interests</th><th>Commentary on Ranking</th></tr></thead><tbody><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></tbody></table></body></html>"
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>"
pdf_download = pdfkit.from_string(contract_redline, False)
#print(pdf_download)
logged_in_user = 'admin'
file_text = ''
contract_text = ''
policy_text = ''
compliance_comments = ''
file_name = 'redline.pdf'
with gr.Blocks(CIMStheme) as iface:
with gr.Row():
with gr.Column(scale=2):
gr.Image(label="Logo", value="CIMS Logo Purple.png", width=10, show_download_button=False,
interactive=False, show_label=False, elem_id="logo", container=False)
with gr.Column(scale=2):
gr.Markdown(value="<H1 style='text-align: center;'>Compliance Desktop - Powered by CIMS.AI</h1>")
with gr.Column(scale=2):
gr.Markdown("")
with gr.Row():
with gr.Column(variant='panel',scale=1):
policy_name = gr.components.Dropdown(schools, multiselect=False,label="NIL Policy Selection")
contract_file_cmpt = gr.File(label="Select Contract File",file_count="single",file_types=[".pdf"],height=150)
with gr.Column(variant='panel',scale=3):
contract_text_tbox = gr.Textbox(label="Contract Text",interactive=True,info="Upload .pdf or paste in text")
with gr.Row():
with gr.Column():
btn_upload = gr.components.Button(value="Upload Contract", size='sm', variant="primary")
with gr.Column():
gr.Markdown("")
with gr.Column():
btn_redline = gr.components.Button(value="Redline Contract", size='sm', variant="primary")
with gr.Column():
gr.Markdown("")
with gr.Row():
with gr.Column(variant='panel'):
gr.components.Markdown(response_label)
contract_redline_html = gr.components.HTML(label="Contract Redline")
compliance_comments_tbox = gr.Textbox(interactive=True,label='Compliance Comments')
with gr.Row(visible=False) as download_row:
with gr.Column(variant='panel'):
file_name_tbox = gr.Textbox(interactive=True,label='File Name')
btn_download = gr.components.Button(value="Download Redline", size='sm', variant="primary")
#pdf_download = gr.File(label="Download Redline File",height=150)
btn_download.click(download_pdf,inputs=[file_name_tbox,compliance_comments_tbox,contract_redline_html])
btn_redline.click(chatbot,inputs=[policy_name,contract_text_tbox],outputs=[contract_redline_html,download_row])
btn_upload.click(pdf_to_text,inputs=contract_file_cmpt,outputs=[contract_text_tbox,file_name_tbox])
with gr.Column():
gr.Markdown("")
with gr.Column():
gr.Markdown("")
with gr.Column():
gr.Markdown("")
with gr.Row():
with gr.Column():
gr.HTML('<center><i>CIMS.AI Confidential 2023</i></center>')
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'")
#iface.queue(concurrency_count=20).launch()
|