Spaces:
Runtime error
Runtime error
Create NILI.py
Browse files
NILI.py
ADDED
@@ -0,0 +1,218 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
import datetime
|
4 |
+
import utilities.parsed_policies as policies
|
5 |
+
import utilities.engagements as engagements
|
6 |
+
import utilities.pickfromlist as pick
|
7 |
+
import gradio as gr
|
8 |
+
import json
|
9 |
+
from jinja2 import Template
|
10 |
+
import csv
|
11 |
+
|
12 |
+
# openai.api_key = os.getenv("OPENAI_API_KEY")
|
13 |
+
openai.api_key = "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"
|
14 |
+
|
15 |
+
#Write log file function
|
16 |
+
def append_to_qalog(your_role, school_selection, output_format, input_text, gpt_response,response_time,question_cost):
|
17 |
+
# Define the filename
|
18 |
+
filename = "qalog.csv"
|
19 |
+
|
20 |
+
# Check if file exists to decide if headers are needed
|
21 |
+
file_exists = os.path.isfile(filename)
|
22 |
+
|
23 |
+
# Open the file in append mode
|
24 |
+
with open(filename, 'a', newline='') as csvfile:
|
25 |
+
writer = csv.DictWriter(csvfile, fieldnames=["your_role", "school_selection", "output_format", "input_text", "gpt_response","response_time","question_cost"])
|
26 |
+
|
27 |
+
# If the file didn't exist, write the headers first
|
28 |
+
if not file_exists:
|
29 |
+
writer.writeheader()
|
30 |
+
|
31 |
+
# Append the data
|
32 |
+
writer.writerow({
|
33 |
+
"your_role": your_role,
|
34 |
+
"school_selection": school_selection,
|
35 |
+
"output_format": output_format,
|
36 |
+
"input_text": input_text,
|
37 |
+
"gpt_response": gpt_response,
|
38 |
+
"response_time": response_time,
|
39 |
+
"question_cost": question_cost
|
40 |
+
})
|
41 |
+
|
42 |
+
#Chatbot Function
|
43 |
+
def chatbot(your_role,school_selection,output_format,input_text):
|
44 |
+
|
45 |
+
start_time = datetime.datetime.now()
|
46 |
+
|
47 |
+
# Read in the prompt header for engagement analysis
|
48 |
+
with open('prompts/nili_analysis_header.txt', 'r') as file:
|
49 |
+
header = file.read()
|
50 |
+
|
51 |
+
# school_selection holds an array of one or more schools, so create a comma delimited str to process.
|
52 |
+
#print(school_selection)
|
53 |
+
str=",".join(school_selection)
|
54 |
+
|
55 |
+
# Read the Hydrated policies
|
56 |
+
|
57 |
+
# Split the input string by commas to get the list of university names
|
58 |
+
university_names = str.split(',')
|
59 |
+
|
60 |
+
policies = ''
|
61 |
+
|
62 |
+
for university in university_names:
|
63 |
+
# Construct the file path
|
64 |
+
file_path = f"nili_hydrated_policies/{university}.txt"
|
65 |
+
|
66 |
+
try:
|
67 |
+
# Open the file and read its contents
|
68 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
69 |
+
content = file.read()
|
70 |
+
|
71 |
+
# If 'policies' already has content, add separator before appending
|
72 |
+
if policies:
|
73 |
+
policies += "\n----------\n"
|
74 |
+
|
75 |
+
policies += content
|
76 |
+
|
77 |
+
except FileNotFoundError:
|
78 |
+
|
79 |
+
print(f"File for {university} not found!")
|
80 |
+
|
81 |
+
with open('combined_policies.txt', 'w', encoding='utf-8') as out_file:
|
82 |
+
out_file.write(policies)
|
83 |
+
|
84 |
+
#print(policies)
|
85 |
+
|
86 |
+
# Insert into prompt
|
87 |
+
with open("prompts/nili_engagement_user_v2.j2", "r") as file:
|
88 |
+
#with open("prompts/nili_engagement_user_v3.j2", "r") as file:
|
89 |
+
template_content = file.read()
|
90 |
+
|
91 |
+
# Create a Jinja2 template from the content
|
92 |
+
template = Template(template_content)
|
93 |
+
|
94 |
+
# Render the template with the policy JSON
|
95 |
+
analysis_input = template.render(policies=policies, question=input_text,format=output_format)
|
96 |
+
|
97 |
+
with open('analysis_input.txt', 'w', encoding='utf-8') as out_file:
|
98 |
+
out_file.write(analysis_input)
|
99 |
+
|
100 |
+
response = openai.ChatCompletion.create(
|
101 |
+
model="gpt-4",
|
102 |
+
# model="gpt-3.5-turbo",
|
103 |
+
temperature=0,
|
104 |
+
messages=[
|
105 |
+
{
|
106 |
+
"role": "system",
|
107 |
+
"content": header
|
108 |
+
},
|
109 |
+
{
|
110 |
+
"role": "user",
|
111 |
+
"content": analysis_input
|
112 |
+
}
|
113 |
+
]
|
114 |
+
)
|
115 |
+
|
116 |
+
gpt_response = response.choices[0].message["content"]
|
117 |
+
|
118 |
+
tokens_used = response.usage
|
119 |
+
|
120 |
+
question_cost = (tokens_used.get('total_tokens', 0) / 1000) * .03
|
121 |
+
|
122 |
+
#print(question_cost)
|
123 |
+
|
124 |
+
with open('response.txt', 'w', encoding='utf-8') as out_file:
|
125 |
+
out_file.write(gpt_response)
|
126 |
+
|
127 |
+
"""print(ui_examples)
|
128 |
+
|
129 |
+
# Read the csv file
|
130 |
+
with open('examples/examples_v3.csv', 'r') as csvfile:
|
131 |
+
reader = csv.reader(csvfile)
|
132 |
+
next(reader) # Skip the header row
|
133 |
+
for row in reader:
|
134 |
+
ui_examples.append([convert_to_none(item) for item in row])
|
135 |
+
|
136 |
+
print(ui_examples)"""
|
137 |
+
|
138 |
+
# Define the style and content
|
139 |
+
label_text = "NILI Response"
|
140 |
+
color = "#6562F4"
|
141 |
+
background_color = "white"
|
142 |
+
border_radius = "10px" # You can adjust this value as needed
|
143 |
+
|
144 |
+
response_label = f'<h3 style="color: {color}; background-color: {background_color}; border-radius: {border_radius}; padding: 10px;display: inline-block;">{label_text}</h3>'
|
145 |
+
|
146 |
+
end_time = datetime.datetime.now()
|
147 |
+
|
148 |
+
response_time = end_time - start_time
|
149 |
+
|
150 |
+
append_to_qalog(your_role, school_selection, output_format, input_text, gpt_response,response_time,question_cost)
|
151 |
+
|
152 |
+
return response_label,gpt_response
|
153 |
+
|
154 |
+
def load_example(example_id):
|
155 |
+
global ui_examples
|
156 |
+
return ui_examples[example_id][0]
|
157 |
+
|
158 |
+
|
159 |
+
|
160 |
+
#Gradio UI
|
161 |
+
CIMStheme = gr.themes.Soft().set(button_primary_background_fill='#6562F4')
|
162 |
+
|
163 |
+
# Initialize an empty list to store the examples
|
164 |
+
ui_examples = []
|
165 |
+
|
166 |
+
nili_response = "Test"
|
167 |
+
|
168 |
+
# Function to convert 'None' string to None object
|
169 |
+
def convert_to_none(value):
|
170 |
+
return None if value == 'None' else value
|
171 |
+
|
172 |
+
# Read the csv file
|
173 |
+
with open('examples/examples_v2.csv', 'r') as csvfile:
|
174 |
+
reader = csv.reader(csvfile)
|
175 |
+
next(reader) # Skip the header row
|
176 |
+
for row in reader:
|
177 |
+
ui_examples.append([convert_to_none(item) for item in row])
|
178 |
+
|
179 |
+
#response_label='<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Formatted Text</title><style>.formatted-text {color: black;font-weight: bold;text-align: left;}</style></head><body><div class="formatted-text">NILI Response</div></body></html>'
|
180 |
+
# Define the style and content
|
181 |
+
label_text = "NILI Response"
|
182 |
+
color = "#6562F4"
|
183 |
+
background_color = "white"
|
184 |
+
border_radius = "10px" # You can adjust this value as needed
|
185 |
+
|
186 |
+
response_label = f'<h3 style="color: {color}; background-color: {background_color}; border-radius: {border_radius}; padding: 10px;display: inline-block;">{label_text}</h3>'
|
187 |
+
|
188 |
+
with gr.Blocks(CIMStheme) as iface:
|
189 |
+
with gr.Row():
|
190 |
+
with gr.Column(scale=2):
|
191 |
+
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)
|
192 |
+
with gr.Column(scale=2):
|
193 |
+
#gr.Textbox(value="# NILI - Powered by CIMS.AI",show_label=False,interactive=False,text_align="center",elem_id="CIMSTitle")
|
194 |
+
gr.Markdown(value="# NILI - Powered by CIMS.AI")
|
195 |
+
with gr.Column(scale=2):
|
196 |
+
gr.Markdown("")
|
197 |
+
with gr.Row():
|
198 |
+
with gr.Column():
|
199 |
+
gr.Interface(fn=chatbot,
|
200 |
+
inputs=[
|
201 |
+
gr.components.Dropdown(["Student Athlete","Parent","Athletic Director"],multiselect=False,info="Select a role.",label="User Role"),
|
202 |
+
gr.components.Dropdown(["Stanford","CU","LSU","CMU","Tulane","Cal Poly","Long Beach State University","Auburn"],multiselect=True,info="Select one or more schools. This will help set the context of your question.",label="School Context"),
|
203 |
+
gr.components.Dropdown(["Summary","Detailed Analysis","Table"],multiselect=False,info="Select the desired output format.",label="Output Format"),
|
204 |
+
gr.components.Textbox(lines=5, placeholder="Enter your question here", label="NIL Question")],
|
205 |
+
outputs=[
|
206 |
+
gr.components.Markdown(response_label),
|
207 |
+
gr.components.HTML(label="NILI Response")
|
208 |
+
],
|
209 |
+
description="Ask any question about Name, Image, Likeness (NIL)",
|
210 |
+
allow_flagging="manual",
|
211 |
+
examples=ui_examples,
|
212 |
+
flagging_options=["The response is incorrect","The response is inappropriate","The response doesn't make sense"]
|
213 |
+
)
|
214 |
+
with gr.Row():
|
215 |
+
with gr.Column():
|
216 |
+
gr.HTML('<center><i>CIMS.AI Confidential 2023</i></center>')
|
217 |
+
|
218 |
+
iface.launch(share=True)
|