Omnibus commited on
Commit
7df3040
1 Parent(s): 36efd7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -189
app.py CHANGED
@@ -124,6 +124,7 @@ def compress_history(formatted_prompt):
124
 
125
  def comment_generate(prompt, history,post_check,full_conv,persona2, agent_name=agents[0], sys_prompt="", temperature=0.9, max_new_tokens=1028, top_p=0.95, repetition_penalty=1.3,):
126
  #def question_generate(prompt, history):
 
127
  uid=uuid.uuid4()
128
  print(post_check)
129
  #full_conv=history
@@ -164,14 +165,25 @@ def comment_generate(prompt, history,post_check,full_conv,persona2, agent_name=a
164
  comment_cnt=post_check['comment']
165
  print(type(comment_cnt))
166
  post_check['comment']=comment_cnt+1
 
 
 
 
 
 
 
 
 
 
167
  #out_json = {'user':"",'datetime':current_time,'title':title,'blog':1,'comment':0,'reply':0,"prompt":prompt,"output":output}
168
  #full_conv[-1]+=(output,)
169
  full_conv.append((None,output,None))
170
- html_out=load_html(full_conv,post_check['title'],post_check['user'])
171
 
172
  #out_json = {'user':list_of_users[0],'datetime':current_time,'file_name':filename,'title':title,'blog':1,'comment':0,'reply':0,"prompt":prompt,"output":output,'comment_list':[]}
173
  file_n = f'{post_check["filename"]}.json'
174
  print(file_n)
 
175
  r = requests.get(f'{save_data}book1/{file_n}')
176
  print(f'status code main:: {r.status_code}')
177
  if r.status_code==200:
@@ -183,26 +195,30 @@ def comment_generate(prompt, history,post_check,full_conv,persona2, agent_name=a
183
  #hist_out.append(out_json)
184
  #try:
185
  # for ea in
186
- with open(f'{uid}.json', 'w') as f:
187
- json_hist=json.dumps(lod, indent=4)
188
- f.write(json_hist)
189
- f.close()
190
-
191
- upload_file(
192
- path_or_fileobj =f"{uid}.json",
193
- path_in_repo = f"book1/{file_n}",
194
- repo_id =f"{username}/{dataset_name}",
195
- repo_type = "dataset",
196
- token=token,
197
- )
198
- except Exception as e:
199
- print(e)
200
- return "",history,lod[0],lod[0],lod[0],html_out
 
 
201
 
202
 
203
 
204
  def reply_generate(prompt, history,post_check,full_conv,persona1, agent_name=agents[0], sys_prompt="", temperature=0.9, max_new_tokens=1028, top_p=0.95, repetition_penalty=1.0,):
205
  #def question_generate(prompt, history):
 
 
206
  uid=uuid.uuid4()
207
  print(post_check)
208
  #full_conv=history
@@ -239,10 +255,34 @@ def reply_generate(prompt, history,post_check,full_conv,persona1, agent_name=age
239
 
240
  reply_cnt=post_check['reply']
241
  post_check['reply']=reply_cnt+1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
  #out_json = {'user':"",'datetime':current_time,'title':title,'blog':1,'comment':0,'reply':0,"prompt":prompt,"output":output}
243
  #full_conv[-1]+=(output,)
244
  full_conv.append((None,None,output))
245
- html_out=load_html(full_conv,post_check['title'],post_check['user'])
246
 
247
  file_n = f'{post_check["filename"]}.json'
248
  print(file_n)
@@ -274,38 +314,6 @@ def reply_generate(prompt, history,post_check,full_conv,persona1, agent_name=age
274
  return "",history,lod[0],lod[0],lod[0],html_out
275
 
276
 
277
-
278
-
279
- def reply_generate_OG(prompt, history, agent_name=agents[0], sys_prompt="", temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,):
280
- #def question_generate(prompt, history):
281
- print("###############\nRUNNING BLOG POSTER REPLY\n###############\n")
282
- seed = random.randint(1,1111111111111111)
283
- agent=prompts.REPLY_TO_COMMENTER.format(focus=main_point[0])
284
- system_prompt=agent
285
- temperature = float(temperature)
286
- if temperature < 1e-2:
287
- temperature = 1e-2
288
- top_p = float(top_p)
289
-
290
- generate_kwargs = dict(
291
- temperature=temperature,
292
- max_new_tokens=max_new_tokens,
293
- top_p=top_p,
294
- repetition_penalty=repetition_penalty,
295
- do_sample=True,
296
- seed=seed,
297
- )
298
- #history.append((prompt,""))
299
- formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
300
- client=client_z[0]
301
- stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
302
- output = ""
303
-
304
- for response in stream:
305
- output += response.token.text
306
- #history.append((output,history))
307
-
308
- return output
309
 
310
 
311
 
@@ -325,8 +333,21 @@ def create_valid_filename(invalid_filename: str) -> str:
325
 
326
 
327
 
328
- def load_html(inp,title,user_name="None"):
329
  ht=""
 
 
 
 
 
 
 
 
 
 
 
 
 
330
  if inp:
331
  for i,ea in enumerate(inp):
332
 
@@ -336,12 +357,14 @@ def load_html(inp,title,user_name="None"):
336
  ht+=f"""<div class="div_box">"""
337
  if blog:
338
  #ht+=f"""<div class="bhead"><div><h1>$btitle</h1></div><div>$user_name</div></div>"""
339
- ht+=f"""<pre class="bpost"><div class="bhead"><h2>$title</h2><br><h5>$user_name</h5></div>{blog}</pre>"""
340
  if comm:
341
  ht+=f"""<pre class="resp1"><div class="bhead"></div>{comm}</pre>"""
342
  if repl:
343
  ht+=f"""<pre class="resp2"><div class="bhead"></div>{repl}</pre>"""
344
  ht+=f"""</div>"""
 
 
345
  with open('index.html','r') as h:
346
  html=h.read()
347
  html = html.replace("$body",f"{ht}")
@@ -452,9 +475,19 @@ def generate(prompt, history, post_check,full_conv,persona1, agent_name=agents[0
452
  print(f'title:: {title}')
453
  filename=create_valid_filename(f'{current_time}---{title}')
454
 
455
- out_json = {'user':persona[persona1]['name'],'datetime':current_time,'file_name':filename,'title':title,'blog':1,'comment':0,'reply':0,"prompt":prompt,"output":output,'comment_list':[]}
 
 
 
 
 
 
 
 
 
 
456
 
457
- hist_out.append(out_json)
458
  #try:
459
  # for ea in
460
  with open(f'{uid}.json', 'w') as f:
@@ -497,145 +530,12 @@ def generate(prompt, history, post_check,full_conv,persona1, agent_name=agents[0
497
  full_conv.append((output,None,None))
498
 
499
 
500
- html_out=load_html(full_conv,title,persona[persona1]['name'])
501
- post_check={'filename':filename,'user':persona[persona1]['name'],'datetime':current_time,'title':title,'blog':1,'comment':0,'reply':0}
502
- yield prompt, history,post_check,full_conv,summary[0],json_obj,json_hist,html_out
503
  else:
504
  print("passing blog")
505
 
506
- def generate_OG(prompt, history, agent_name=agents[0], sys_prompt="", temperature=0.9, max_new_tokens=1048, top_p=0.95, repetition_penalty=1.0):
507
- html_out=""
508
- #main_point[0]=prompt
509
- #print(datetime.datetime.now())
510
- uid=uuid.uuid4()
511
- current_time = str(datetime.datetime.now())
512
- title=""
513
- filename=create_valid_filename(f'{current_time}---{title}')
514
-
515
- current_time=current_time.replace(":","-")
516
- current_time=current_time.replace(".","-")
517
- print (current_time)
518
- agent=prompts.BLOG_POSTER
519
- system_prompt=agent
520
- temperature = float(temperature)
521
- if temperature < 1e-2:
522
- temperature = 1e-2
523
- top_p = float(top_p)
524
- hist_out=[]
525
- sum_out=[]
526
- json_hist={}
527
- json_obj={}
528
- full_conv=[]
529
- post_cnt=1
530
- while True:
531
- seed = random.randint(1,1111111111111111)
532
- if post_cnt==1:
533
- generate_kwargs = dict(
534
- temperature=temperature,
535
- max_new_tokens=max_new_tokens2,
536
- top_p=top_p,
537
- repetition_penalty=repetition_penalty,
538
- do_sample=True,
539
- seed=seed,
540
- )
541
- if prompt.startswith(' \"'):
542
- prompt=prompt.strip(' \"')
543
-
544
- formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
545
-
546
- post_cnt+=1
547
- else:
548
- system_prompt=prompts.REPLY_TO_COMMENTER.format(focus=main_point[0])
549
-
550
- generate_kwargs = dict(
551
- temperature=temperature,
552
- max_new_tokens=max_new_tokens2,
553
- top_p=top_p,
554
- repetition_penalty=repetition_penalty,
555
- do_sample=True,
556
- seed=seed,
557
- )
558
- if prompt.startswith(' \"'):
559
- prompt=prompt.strip(' \"')
560
-
561
- formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
562
- print("###############\nRUNNING REPLY TO COMMENTER\n###############\n")
563
- print (system_prompt)
564
- if len(formatted_prompt) < (40000):
565
- print(len(formatted_prompt))
566
-
567
- client=client_z[0]
568
- stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
569
- output = ""
570
- #if history:
571
- # yield history
572
-
573
- for response in stream:
574
- output += response.token.text
575
- yield '', [(prompt,output)],summary[0],json_obj, json_hist,html_out
576
-
577
- if not title:
578
- for line in output.split("\n"):
579
- if "title" in line.lower() and ":" in line.lower():
580
- title = line.split(":")[1]
581
- print(f'title:: {title}')
582
- filename=create_valid_filename(f'{current_time}---{title}')
583
-
584
- out_json = {"prompt":prompt,"output":output}
585
-
586
- prompt = question_generate(output, history)
587
- #output += prompt
588
- history.append((prompt,output))
589
- print ( f'Prompt:: {len(prompt)}')
590
- #print ( f'output:: {output}')
591
- print ( f'history:: {len(formatted_prompt)}')
592
- hist_out.append(out_json)
593
- #try:
594
- # for ea in
595
- with open(f'{uid}.json', 'w') as f:
596
- json_hist=json.dumps(hist_out, indent=4)
597
- f.write(json_hist)
598
- f.close()
599
-
600
- upload_file(
601
- path_or_fileobj =f"{uid}.json",
602
- path_in_repo = f"book1/{filename}.json",
603
- repo_id =f"{username}/{dataset_name}",
604
- repo_type = "dataset",
605
- token=token,
606
- )
607
- else:
608
- formatted_prompt = format_prompt(f"{prompts.COMPRESS_HISTORY_PROMPT.format(history=summary[0],focus=main_point[0])}, {summary[0]}", history)
609
-
610
- #current_time = str(datetime.datetime.now().timestamp()).split(".",1)[0]
611
- #filename=f'{filename}-{current_time}'
612
- history = []
613
- output = compress_history(formatted_prompt)
614
- summary[0]=output
615
- sum_json = {"summary":summary[0]}
616
- sum_out.append(sum_json)
617
- with open(f'{uid}-sum.json', 'w') as f:
618
- json_obj=json.dumps(sum_out, indent=4)
619
- f.write(json_obj)
620
- f.close()
621
- upload_file(
622
- path_or_fileobj =f"{uid}-sum.json",
623
- path_in_repo = f"book1/{filename}-summary.json",
624
- repo_id =f"{username}/{dataset_name}",
625
- repo_type = "dataset",
626
- token=token,
627
- )
628
-
629
-
630
- prompt = question_generate(output, history)
631
- main_point[0]=prompt
632
- full_conv.append((output,prompt))
633
-
634
-
635
- html_out=load_html(full_conv,title)
636
- yield prompt, history, summary[0],json_obj,json_hist,html_out
637
- return prompt, history, summary[0],json_obj,json_hist,html_out
638
-
639
 
640
 
641
 
 
124
 
125
  def comment_generate(prompt, history,post_check,full_conv,persona2, agent_name=agents[0], sys_prompt="", temperature=0.9, max_new_tokens=1028, top_p=0.95, repetition_penalty=1.3,):
126
  #def question_generate(prompt, history):
127
+ current_time = str(datetime.datetime.now())
128
  uid=uuid.uuid4()
129
  print(post_check)
130
  #full_conv=history
 
165
  comment_cnt=post_check['comment']
166
  print(type(comment_cnt))
167
  post_check['comment']=comment_cnt+1
168
+
169
+ comment_json= {'user':persona[persona2]['name'],'datetime':current_time,'comment':output,'reply_list':[]}
170
+
171
+ out_json = {'user':persona[persona1]['name'],'datetime':post_check['datetime'],'file_name':post_check['filename'],
172
+ 'title':post_check['title'],'blog':1,'comment':post_check['comment']+=1,'reply':post_check['reply'],
173
+ "prompt":post_check['prompt'],"output":post_check['output'],'comment_list':post_check['comment_list'].append(comment_json)}
174
+
175
+
176
+
177
+
178
  #out_json = {'user':"",'datetime':current_time,'title':title,'blog':1,'comment':0,'reply':0,"prompt":prompt,"output":output}
179
  #full_conv[-1]+=(output,)
180
  full_conv.append((None,output,None))
181
+ html_out=load_html(full_conv,out_json)
182
 
183
  #out_json = {'user':list_of_users[0],'datetime':current_time,'file_name':filename,'title':title,'blog':1,'comment':0,'reply':0,"prompt":prompt,"output":output,'comment_list':[]}
184
  file_n = f'{post_check["filename"]}.json'
185
  print(file_n)
186
+ '''
187
  r = requests.get(f'{save_data}book1/{file_n}')
188
  print(f'status code main:: {r.status_code}')
189
  if r.status_code==200:
 
195
  #hist_out.append(out_json)
196
  #try:
197
  # for ea in
198
+ '''
199
+
200
+ with open(f'{uid}.json', 'w') as f:
201
+ json_hist=json.dumps(out_json, indent=4)
202
+ f.write(json_hist)
203
+ f.close()
204
+
205
+ upload_file(
206
+ path_or_fileobj =f"{uid}.json",
207
+ path_in_repo = f"book1/{file_n}",
208
+ repo_id =f"{username}/{dataset_name}",
209
+ repo_type = "dataset",
210
+ token=token,
211
+ )
212
+ #except Exception as e:
213
+ # print(e)
214
+ return "",history,out_json,out_json,out_json,html_out
215
 
216
 
217
 
218
  def reply_generate(prompt, history,post_check,full_conv,persona1, agent_name=agents[0], sys_prompt="", temperature=0.9, max_new_tokens=1028, top_p=0.95, repetition_penalty=1.0,):
219
  #def question_generate(prompt, history):
220
+ current_time = str(datetime.datetime.now())
221
+
222
  uid=uuid.uuid4()
223
  print(post_check)
224
  #full_conv=history
 
255
 
256
  reply_cnt=post_check['reply']
257
  post_check['reply']=reply_cnt+1
258
+
259
+
260
+
261
+
262
+ out_json = {'user':persona[persona1]['name'],'datetime':post_check['datetime'],'file_name':post_check['filename'],
263
+ 'title':post_check['title'],'blog':1,'comment':0,'reply':post_check['reply']+=1,"prompt":post_check['prompt'],"output":post_check['output'],'comment_list':post_check['comment_list']}
264
+
265
+ hist_out.append(out_json)
266
+ #try:
267
+ # for ea in
268
+ with open(f'{uid}.json', 'w') as f:
269
+ json_hist=json.dumps(hist_out, indent=4)
270
+ f.write(json_hist)
271
+ f.close()
272
+
273
+ upload_file(
274
+ path_or_fileobj =f"{uid}.json",
275
+ path_in_repo = f"book1/{filename}.json",
276
+ repo_id =f"{username}/{dataset_name}",
277
+ repo_type = "dataset",
278
+ token=token,
279
+ )
280
+
281
+
282
  #out_json = {'user':"",'datetime':current_time,'title':title,'blog':1,'comment':0,'reply':0,"prompt":prompt,"output":output}
283
  #full_conv[-1]+=(output,)
284
  full_conv.append((None,None,output))
285
+ html_out=load_html(full_conv,post_check)
286
 
287
  file_n = f'{post_check["filename"]}.json'
288
  print(file_n)
 
314
  return "",history,lod[0],lod[0],lod[0],html_out
315
 
316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
 
318
 
319
 
 
333
 
334
 
335
 
336
+ def load_html(inp,conv):
337
  ht=""
338
+ for i,ea in enumerate(inp):
339
+
340
+ blog,comm,repl=ea
341
+ #print(f'outp:: {outp}')
342
+ #print(f'prom:: {prom}')
343
+ ht+=f"""<div class="div_box">"""
344
+ ht+=f"""<pre class="bpost"><div class="bhead"><h2>{conv['title']}</h2><br><h5>{conv['user']}</h5></div>{conv['output']}</pre>"""
345
+ for
346
+ ht+=f"""<pre class="resp1"><div class="bhead"></div>{comm}</pre>"""
347
+ if repl:
348
+ ht+=f"""<pre class="resp2"><div class="bhead"></div>{repl}</pre>"""
349
+ ht+=f"""</div>"""
350
+ '''
351
  if inp:
352
  for i,ea in enumerate(inp):
353
 
 
357
  ht+=f"""<div class="div_box">"""
358
  if blog:
359
  #ht+=f"""<div class="bhead"><div><h1>$btitle</h1></div><div>$user_name</div></div>"""
360
+ ht+=f"""<pre class="bpost"><div class="bhead"><h2>{conv['title']}</h2><br><h5>{conv['user']}</h5></div>{blog}</pre>"""
361
  if comm:
362
  ht+=f"""<pre class="resp1"><div class="bhead"></div>{comm}</pre>"""
363
  if repl:
364
  ht+=f"""<pre class="resp2"><div class="bhead"></div>{repl}</pre>"""
365
  ht+=f"""</div>"""
366
+
367
+ '''
368
  with open('index.html','r') as h:
369
  html=h.read()
370
  html = html.replace("$body",f"{ht}")
 
475
  print(f'title:: {title}')
476
  filename=create_valid_filename(f'{current_time}---{title}')
477
 
478
+ #out_json = {'user':persona[persona1]['name'],'datetime':current_time,'file_name':filename,'title':title,'blog':1,'comment':0,'reply':0,"prompt":prompt,"output":output,'comment_list':[]}
479
+
480
+
481
+
482
+ reply_json= {'user':'','datetime':'','reply':''}
483
+
484
+ comment_json= {'user':'','datetime':'','comment':'','reply_list':[reply_json]}
485
+
486
+ out_json = {'user':persona[persona1]['name'],'datetime':current_time,'file_name':filename,
487
+ 'title':title,'blog':1,'comment':0,'reply':0,
488
+ "prompt":prompt,"output":output,'comment_list':[comment_json]}
489
 
490
+ #hist_out.append(out_json)
491
  #try:
492
  # for ea in
493
  with open(f'{uid}.json', 'w') as f:
 
530
  full_conv.append((output,None,None))
531
 
532
 
533
+ html_out=load_html(full_conv,out_json)
534
+ #post_check={'filename':filename,'user':persona[persona1]['name'],'datetime':current_time,'title':title,'blog':1,'comment':0,'reply':0}
535
+ yield prompt, history,out_json,full_conv,summary[0],out_json,json_hist,html_out
536
  else:
537
  print("passing blog")
538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
539
 
540
 
541