nickmuchi commited on
Commit
0681909
1 Parent(s): 64af83f

Update functions.py

Browse files
Files changed (1) hide show
  1. functions.py +48 -0
functions.py CHANGED
@@ -211,6 +211,54 @@ def embed_text(query,title,embedding_model,_docsearch):
211
  answer = chain({"query": query})
212
 
213
  return answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
 
215
  @st.cache_resource
216
  def get_spacy():
 
211
  answer = chain({"query": query})
212
 
213
  return answer
214
+
215
+ @st.cache_data
216
+ def gen_sentiment(text):
217
+ '''Generate sentiment of given text'''
218
+ return sent_pipe(text)[0]['label']
219
+
220
+ @st.cache_data
221
+ def gen_annotated_text(df):
222
+ '''Generate annotated text'''
223
+
224
+ tag_list=[]
225
+ for row in df.itertuples():
226
+ label = row[2]
227
+ text = row[1]
228
+ if label == 'Positive':
229
+ tag_list.append((text,label,'#8fce00'))
230
+ elif label == 'Negative':
231
+ tag_list.append((text,label,'#f44336'))
232
+ else:
233
+ tag_list.append((text,label,'#000000'))
234
+
235
+ return tag_list
236
+
237
+ @st.cache_data
238
+ def generate_eval(raw_text, N, chunk):
239
+
240
+ # Generate N questions from context of chunk chars
241
+ # IN: text, N questions, chunk size to draw question from in the doc
242
+ # OUT: eval set as JSON list
243
+
244
+ # raw_text = ','.join(raw_text)
245
+
246
+ st.info("`Generating sample questions ...`")
247
+ n = len(raw_text)
248
+ starting_indices = [random.randint(0, n-chunk) for _ in range(N)]
249
+ sub_sequences = [raw_text[i:i+chunk] for i in starting_indices]
250
+ chain = QAGenerationChain.from_llm(ChatOpenAI(temperature=0))
251
+ eval_set = []
252
+ for i, b in enumerate(sub_sequences):
253
+ try:
254
+ qa = chain.run(b)
255
+ eval_set.append(qa)
256
+ st.write("Creating Question:",i+1)
257
+ except Exception as e:
258
+ st.warning('Error generating question %s.' % str(i+1), icon="⚠️")
259
+ st.write(e)
260
+ eval_set_full = list(itertools.chain.from_iterable(eval_set))
261
+ return eval_set_full
262
 
263
  @st.cache_resource
264
  def get_spacy():