rohan13 commited on
Commit
9987dc1
1 Parent(s): c623d28

Making open_api_key static from env variable

Browse files
Files changed (2) hide show
  1. app.py +18 -32
  2. utils.py +1 -1
app.py CHANGED
@@ -19,8 +19,8 @@ index_file = "vector_stores/canvas-discussions.index"
19
  grading_model = 'gpt-4'
20
  qa_model = 'gpt-3.5-turbo-16k'
21
 
22
- llm = None
23
- embeddings = None
24
 
25
  grader = None
26
  grader_qa = None
@@ -40,7 +40,7 @@ def run_model(text):
40
  print("start time:" + str(start_time))
41
  if not grader_qa and not grader:
42
  if os.path.isfile(pickle_file) and os.path.isfile(index_file) and os.path.getsize(
43
- pickle_file) > 0 and os.path.isfile('docs/discussion_entries.json.json') and os.path.isfile(
44
  'docs/rubric-data.json') > 0:
45
  grader = Grader(qa_model)
46
  grader_qa = GraderQA(grader, embeddings)
@@ -69,10 +69,8 @@ def set_model(history):
69
  history = get_first_message(history)
70
  return history
71
 
72
-
73
- def ingest(url, canvas_api_key, openai_api_key, history):
74
  global grader, llm, embeddings
75
- set_key_and_llm(openai_api_key)
76
  text = f"Download data from {url} and ingest it to grade discussions"
77
  ingest_canvas_discussions(url, canvas_api_key)
78
  grader = Grader(grading_model)
@@ -80,18 +78,10 @@ def ingest(url, canvas_api_key, openai_api_key, history):
80
  history = history + [(text, response)]
81
  return get_grading_status(history)
82
 
83
-
84
- def set_key_and_llm(openai_api_key):
85
- global llm, embeddings
86
- os.environ['OPENAI_API_KEY'] = openai_api_key
87
- llm = ChatOpenAI(model_name=qa_model, temperature=0, verbose=True)
88
- embeddings = OpenAIEmbeddings(model='text-embedding-ada-002')
89
-
90
-
91
- def start_grading(url, canvas_api_key, openai_api_key, history):
92
  global grader, grader_qa
93
  text = f"Start grading discussions from {url}"
94
- if not url or not canvas_api_key or not openai_api_key:
95
  response = "Please enter all the fields to initiate grading"
96
  elif grader:
97
  # Create a new event loop
@@ -112,8 +102,12 @@ def start_grading(url, canvas_api_key, openai_api_key, history):
112
 
113
 
114
  def start_downloading():
115
- # grader.download()
116
- return "Downloaded"
 
 
 
 
117
 
118
 
119
  def get_first_message(history):
@@ -134,26 +128,25 @@ def get_grading_status(history):
134
  elif not grader_qa:
135
  grader_qa = GraderQA(grader, embeddings)
136
  history = history + [(None, 'Grading is already complete. You can now ask questions')]
137
- enable_fields(False, False, False, False, False, True, True, True)
138
  # Check if data is ingested
139
  elif len(glob.glob("docs/*.json")) > 0 and len(glob.glob("docs/*.html")):
140
  if not grader_qa:
141
  grader = Grader(qa_model)
142
  history = history + [(None, 'Canvas data is already ingested. You can grade discussions now')]
143
- enable_fields(False, False, False, False, True, True, False, False)
144
  else:
145
  history = history + [(None, 'Please ingest data and start grading')]
146
  url.disabled = True
147
- enable_fields(True, True, True, True, True, True, False, False)
148
  return history
149
 
150
 
151
  # handle enable/disable of fields
152
- def enable_fields(url_status, canvas_api_key_status, openai_api_key_status, submit_status, grade_status,
153
  download_status, chatbot_txt_status, chatbot_btn_status):
154
  url.interactive = url_status
155
  canvas_api_key.interactive = canvas_api_key_status
156
- openai_api_key.interactive = openai_api_key_status
157
  submit.interactive = submit_status
158
  grade.interactive = grade_status
159
  download.interactive = download_status
@@ -167,8 +160,6 @@ def enable_fields(url_status, canvas_api_key_status, openai_api_key_status, subm
167
  url.placeholder = "Data already ingested"
168
  if not canvas_api_key_status:
169
  canvas_api_key.placeholder = "Data already ingested"
170
- if not openai_api_key_status:
171
- openai_api_key.placeholder = "Data already ingested"
172
 
173
  def bot(history):
174
  return history
@@ -188,11 +179,6 @@ with gr.Blocks() as demo:
188
  placeholder="Enter your Canvas API Key", type="password"
189
  )
190
 
191
- openai_api_key = gr.Textbox(
192
- label="OpenAI API Key",
193
- placeholder="Enter your OpenAI API Key", type="password"
194
- )
195
-
196
  with gr.Row():
197
  submit = gr.Button(value="Submit", variant="secondary", )
198
  grade = gr.Button(value="Grade", variant="secondary")
@@ -210,12 +196,12 @@ with gr.Blocks() as demo:
210
  ask = gr.Button(value="Ask", variant="secondary", scale=1)
211
 
212
  chatbot.value = get_first_message([])
213
- submit.click(ingest, inputs=[url, canvas_api_key, openai_api_key, chatbot], outputs=[chatbot],
214
  postprocess=False).then(
215
  bot, chatbot, chatbot
216
  )
217
 
218
- grade.click(start_grading, inputs=[url, canvas_api_key, openai_api_key, chatbot], outputs=[chatbot],
219
  postprocess=False).then(
220
  bot, chatbot, chatbot
221
  )
 
19
  grading_model = 'gpt-4'
20
  qa_model = 'gpt-3.5-turbo-16k'
21
 
22
+ llm = ChatOpenAI(model_name=qa_model, temperature=0, verbose=True)
23
+ embeddings = OpenAIEmbeddings(model='text-embedding-ada-002')
24
 
25
  grader = None
26
  grader_qa = None
 
40
  print("start time:" + str(start_time))
41
  if not grader_qa and not grader:
42
  if os.path.isfile(pickle_file) and os.path.isfile(index_file) and os.path.getsize(
43
+ pickle_file) > 0 and os.path.isfile('docs/discussion_entries.json') and os.path.isfile(
44
  'docs/rubric-data.json') > 0:
45
  grader = Grader(qa_model)
46
  grader_qa = GraderQA(grader, embeddings)
 
69
  history = get_first_message(history)
70
  return history
71
 
72
+ def ingest(url, canvas_api_key, history):
 
73
  global grader, llm, embeddings
 
74
  text = f"Download data from {url} and ingest it to grade discussions"
75
  ingest_canvas_discussions(url, canvas_api_key)
76
  grader = Grader(grading_model)
 
78
  history = history + [(text, response)]
79
  return get_grading_status(history)
80
 
81
+ def start_grading(url, canvas_api_key, history):
 
 
 
 
 
 
 
 
82
  global grader, grader_qa
83
  text = f"Start grading discussions from {url}"
84
+ if not url or not canvas_api_key:
85
  response = "Please enter all the fields to initiate grading"
86
  elif grader:
87
  # Create a new event loop
 
102
 
103
 
104
  def start_downloading():
105
+ files = glob.glob("output/*.csv")
106
+ if files:
107
+ file = files[0]
108
+ return gr.outputs.File(file)
109
+ else:
110
+ return "File not found"
111
 
112
 
113
  def get_first_message(history):
 
128
  elif not grader_qa:
129
  grader_qa = GraderQA(grader, embeddings)
130
  history = history + [(None, 'Grading is already complete. You can now ask questions')]
131
+ enable_fields(False, False, False, False, True, True, True)
132
  # Check if data is ingested
133
  elif len(glob.glob("docs/*.json")) > 0 and len(glob.glob("docs/*.html")):
134
  if not grader_qa:
135
  grader = Grader(qa_model)
136
  history = history + [(None, 'Canvas data is already ingested. You can grade discussions now')]
137
+ enable_fields(False, False, False, True, True, False, False)
138
  else:
139
  history = history + [(None, 'Please ingest data and start grading')]
140
  url.disabled = True
141
+ enable_fields(True, True, True, True, True, False, False)
142
  return history
143
 
144
 
145
  # handle enable/disable of fields
146
+ def enable_fields(url_status, canvas_api_key_status, submit_status, grade_status,
147
  download_status, chatbot_txt_status, chatbot_btn_status):
148
  url.interactive = url_status
149
  canvas_api_key.interactive = canvas_api_key_status
 
150
  submit.interactive = submit_status
151
  grade.interactive = grade_status
152
  download.interactive = download_status
 
160
  url.placeholder = "Data already ingested"
161
  if not canvas_api_key_status:
162
  canvas_api_key.placeholder = "Data already ingested"
 
 
163
 
164
  def bot(history):
165
  return history
 
179
  placeholder="Enter your Canvas API Key", type="password"
180
  )
181
 
 
 
 
 
 
182
  with gr.Row():
183
  submit = gr.Button(value="Submit", variant="secondary", )
184
  grade = gr.Button(value="Grade", variant="secondary")
 
196
  ask = gr.Button(value="Ask", variant="secondary", scale=1)
197
 
198
  chatbot.value = get_first_message([])
199
+ submit.click(ingest, inputs=[url, canvas_api_key, chatbot], outputs=[chatbot],
200
  postprocess=False).then(
201
  bot, chatbot, chatbot
202
  )
203
 
204
+ grade.click(start_grading, inputs=[url, canvas_api_key, chatbot], outputs=[chatbot],
205
  postprocess=False).then(
206
  bot, chatbot, chatbot
207
  )
utils.py CHANGED
@@ -24,7 +24,7 @@ def get_chat_history(inputs) -> str:
24
  return "\n".join(res)
25
 
26
 
27
- class GraderQA():
28
  def __init__(self, grader, embeddings):
29
  self.grader = grader
30
  self.llm = self.grader.llm
 
24
  return "\n".join(res)
25
 
26
 
27
+ class GraderQA:
28
  def __init__(self, grader, embeddings):
29
  self.grader = grader
30
  self.llm = self.grader.llm