File size: 25,514 Bytes
e72d7b7
 
b73125b
b133255
e72d7b7
 
 
 
 
 
 
da76781
da8ab41
 
e72d7b7
99cd660
e72d7b7
 
 
 
 
 
 
564d1d3
e72d7b7
f3d467a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99cd660
95ce061
 
99cd660
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0d8821
3dea939
 
 
c0d8821
99cd660
 
e72d7b7
 
 
 
 
 
 
 
 
 
 
a2f95d7
e72d7b7
 
 
 
 
c0d8821
e72d7b7
 
 
 
08a3a8b
e72d7b7
 
 
 
 
 
 
 
 
 
c0d8821
e72d7b7
 
 
 
 
 
 
 
99cd660
e72d7b7
 
 
 
 
 
 
 
 
 
 
5509f2d
e72d7b7
ce331ff
95ce061
a055bad
74644ec
08a3a8b
e72d7b7
8d5f956
c58ef48
7a99d6a
e72d7b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e9992f
f5e6ef6
 
99cd660
 
e72d7b7
99cd660
e72d7b7
 
 
 
49eebbd
e72d7b7
b67a2f5
 
 
49eebbd
eba8b1d
e5b450b
92a9cbb
49eebbd
da8ab41
d4a6a70
5c6de90
23dbed5
da8ab41
 
a12425d
 
43f0c7f
564d1d3
7a99d6a
a12425d
 
 
 
 
 
 
da8ab41
a12425d
 
43f0c7f
a12425d
 
 
 
 
 
564d1d3
a2f95d7
a66d7ff
 
5509f2d
a66d7ff
5987484
a66d7ff
 
 
dc56529
a66d7ff
7a99d6a
a66d7ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eed2a2b
f5e6ef6
a66d7ff
 
 
 
 
 
 
 
 
 
2a53bd8
 
a66d7ff
 
2a53bd8
92a9cbb
a66d7ff
564d1d3
 
 
 
 
 
 
 
 
7a99d6a
564d1d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc1802e
564d1d3
a66d7ff
 
 
 
 
a2f95d7
08a3a8b
a2f95d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99cd660
a2f95d7
 
 
 
 
 
 
 
 
 
 
e72d7b7
 
 
 
 
 
 
 
 
 
 
bbd06c4
 
 
 
 
dc56529
3add84f
 
 
 
 
 
 
a055bad
 
dc56529
a055bad
 
 
 
 
 
3add84f
 
 
dc56529
73598d5
dc56529
3add84f
 
 
 
5ec32be
58ded98
13d4e12
 
58ded98
644e87c
 
13d4e12
 
9543448
 
13d4e12
 
 
 
 
 
bbd06c4
 
5ec32be
4563510
bbd06c4
 
 
 
564d1d3
bbd06c4
5509f2d
684ba89
3e1a621
a2f95d7
e72d7b7
 
 
010002c
 
e72d7b7
1fcc518
 
 
7a99d6a
1fcc518
 
 
 
 
 
 
 
 
 
 
2f29eaf
 
 
7556fb9
2f29eaf
 
1fcc518
f29440c
d3708e0
1fcc518
 
 
 
 
 
 
 
 
 
 
 
2a53bd8
1fcc518
 
 
 
 
 
 
 
 
2a53bd8
9f71767
2a53bd8
 
1fcc518
 
2a53bd8
1fcc518
 
 
 
 
2a53bd8
 
1fcc518
 
 
92a9cbb
1fcc518
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ffd975
1fcc518
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a53bd8
7556fb9
1fcc518
 
7a99d6a
 
a055bad
d3708e0
 
1fcc518
 
 
 
 
 
 
 
 
 
e72d7b7
 
 
 
 
 
 
 
 
 
 
 
 
5ec32be
a2f95d7
e72d7b7
 
a2f95d7
 
 
 
 
 
 
 
 
 
 
e72d7b7
a2f95d7
010002c
a712c99
a2f95d7
bbd06c4
a2f95d7
 
 
 
 
 
 
 
 
 
 
 
 
644e87c
 
eaa17b0
e72d7b7
a2f95d7
99cd660
e72d7b7
 
 
 
 
 
 
bbd06c4
010002c
 
c1ae534
010002c
 
 
 
 
e72d7b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8addf64
e72d7b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2f95d7
e72d7b7
 
 
 
 
a712c99
5ec32be
a2f95d7
5ec32be
b73125b
bbd06c4
05ada67
bbd06c4
 
 
 
f3d467a
37686b3
176bcef
c58ef48
3405e06
1ffd975
37686b3
0fcf8a1
4bff41f
0fcf8a1
 
1fcc518
 
 
 
0fcf8a1
 
1fcc518
c0d8821
 
 
4bff41f
 
 
5509f2d
 
4bff41f
0fcf8a1
 
 
 
99cd660
 
cb7e6ed
3edcc5a
 
0fcf8a1
684ba89
5509f2d
 
684ba89
1fcc518
0fcf8a1
dac269f
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
from huggingface_hub import InferenceClient, HfApi, upload_file
import datetime
import gradio as gr
import requests
import random
import prompts
import json
import uuid
import os
token=os.environ.get("HF_TOKEN")
username="omnibus"
dataset_name="tmp"
save_data=f'https://huggingface.co/datasets/{username}/{dataset_name}/raw/main/'

api=HfApi(token="")
VERBOSE=False

history = []
hist_out= []
summary =[]
main_point=[]
summary.append("")
main_point.append("")
list_of_users=["user1","user2","user3"]

persona=[
    {"name":"Mr. Nice Guy", "description":"Nice","personality":"friendly, caring, helpful and informative.  You always compliment people, and stick up for them, and you have no patience for bullies."},
    {"name":"Mr. Mean Guy", "description":"Mean","personality":"a total asshole.  You think you are really smart, but really you are just ignorant and mean.  You don't have time for everybodies stupidity, and you let them know that in the comments."},
    {"name":"Smarty Pants", "description":"Genius","personality":"intelligent, informative, know-it-all.  You are the smartest guy in the room and always one-up the blog poster to show how mart you are."},
    {"name":"Try Hard", "description":"Not Genius","personality":"dimwitted, lacking understanding about any topic.  You always ask really irrelevant questions about the post."},
    {"name":"Class Clown", "description":"Humerous","personality":"humerous, funny.  You turn everything into a joke.  Make a joke about the post."},
]
persona_names=[]
for ea in persona:
    persona_names.append(ea['name'])






models=[
    "mistralai/Mixtral-8x7B-Instruct-v0.1",
    "mistralai/Mixtral-8x7B-Instruct-v0.2",
    "google/gemma-7b",
    "google/gemma-7b-it",
    "google/gemma-2b",
    "google/gemma-2b-it",
    "meta-llama/Llama-2-7b-chat-hf",
    "codellama/CodeLlama-70b-Instruct-hf",
    "openchat/openchat-3.5-0106",
    "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
]

client_z=[]

def load_models(inp):
    if VERBOSE==True:    
        print(type(inp))
        print(inp)
        print(models[inp])
    client_z.clear()
    client_z.append(InferenceClient(models[inp]))

    #if "mistralai" in models[inp]:
    #    tokens = gr.Slider(label="Max new tokens",value=1600,minimum=0,maximum=8000,step=64,interactive=True, visible=True,info="The maximum number of tokens")
    
    
    return gr.update(label=models[inp])

def format_prompt(message, history):
  prompt = "<s>"
  for user_prompt, bot_response in history:
    prompt += f"[INST] {user_prompt} [/INST]"
    prompt += f" {bot_response}</s> "
  prompt += f"[INST] {message} [/INST]"
  return prompt

agents =[
    "COMMENTER",
    "BLOG_POSTER",
    "REPLY_TO_COMMENTER",
    "COMPRESS_HISTORY_PROMPT"
]

temperature=0.9
max_new_tokens=256
max_new_tokens2=4000
top_p=0.95
repetition_penalty=1.0,

def compress_history(formatted_prompt):
    print("###############\nRUNNING COMPRESS HISTORY\n###############\n")
    seed = random.randint(1,1111111111111111)
    agent=prompts.COMPRESS_HISTORY_PROMPT.format(history=summary[0],focus=main_point[0])
    
    system_prompt=agent
    temperature = 0.9
    if temperature < 1e-2:
        temperature = 1e-2

    generate_kwargs = dict(
        temperature=temperature,
        max_new_tokens=1048,
        top_p=0.95,
        repetition_penalty=1.0,
        do_sample=True,
        seed=seed,
    )
    #history.append((prompt,""))
    #formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
    formatted_prompt = formatted_prompt
    client=client_z[0]
    stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
    output = ""

    for response in stream:
        output += response.token.text
    #history.append((output,history))
    print(output)
    print(main_point[0])
    return output


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,):
#def question_generate(prompt, history):
    uid=uuid.uuid4()
    print(post_check)
    #full_conv=history
    print(f'full_conv::\n{full_conv}')
    print("###############\nRUNNING QUESTION GENERATOR\n###############\n")
    seed = random.randint(1,1111111111111111)
    #agent=prompts.COMMENTER.format(focus=main_point[0])
    #bp,_,_=full_conv[0]
    agent=prompts.COMMENTER.format(focus=full_conv[0][0],persona=persona[persona2]['personality'])
    system_prompt=agent
    temperature = float(temperature)
    if temperature < 1e-2:
        temperature = 1e-2
    top_p = float(top_p)

    generate_kwargs = dict(
        temperature=temperature,
        max_new_tokens=max_new_tokens,
        top_p=top_p,
        repetition_penalty=repetition_penalty,
        do_sample=True,
        seed=seed,
    )
    #history.append((prompt,""))
    #formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
    #[INST] {message} [/INST]
    formatted_prompt = f"[INST] {system_prompt}, {prompt} [/INST]"
    client=client_z[0]

    stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
        
    output = ""

    for response in stream:
        output += response.token.text
    history.append((output,None))

    comment_cnt=post_check['comment']
    print(type(comment_cnt))
    post_check['comment']=comment_cnt+1
    #out_json = {'user':"",'datetime':current_time,'title':title,'blog':1,'comment':0,'reply':0,"prompt":prompt,"output":output}
    #full_conv[-1]+=(output,)
    full_conv.append((None,output,None))
    html_out=load_html(full_conv,post_check['title'],post_check['user'])

    #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':[]}
    file_n = f'{post_check["filename"]}.json'
    print(file_n)
    r = requests.get(f'{save_data}book1/{file_n}') 
    print(f'status code main:: {r.status_code}')
    if r.status_code==200:
        try:
            lod = json.loads(r.text)
            print(f'lod:: {lod}')
            lod[0]['comment']=lod[0]['comment']+1
            lod[0]['comment_list'].append({'user':persona[persona2]['name'],'datetime':'','comment':output,'reply_list':[]})
            #hist_out.append(out_json)
            #try:
            #    for ea in 
            with open(f'{uid}.json', 'w') as f:
                json_hist=json.dumps(lod, indent=4)
                f.write(json_hist)
            f.close()
        
            upload_file(
                path_or_fileobj =f"{uid}.json", 
                path_in_repo = f"book1/{file_n}", 
                repo_id =f"{username}/{dataset_name}", 
                repo_type = "dataset", 
                token=token,
            )
        except Exception as e:
            print(e)
    return "",history,lod[0],lod[0],lod[0],html_out



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,):
#def question_generate(prompt, history):
    uid=uuid.uuid4()
    print(post_check)
    #full_conv=history
    print(f'full_conv::\n{full_conv}')
    print("###############\nRUNNING REPLY GENERATOR\n###############\n")
    seed = random.randint(1,1111111111111111)
    agent=prompts.REPLY_TO_COMMENTER.format(focus=full_conv[0][0],comment=full_conv[1][1],persona=persona[persona1]['personality'])
    system_prompt=agent
    temperature = float(temperature)
    if temperature < 1e-2:
        temperature = 1e-2
    top_p = float(top_p)

    generate_kwargs = dict(
        temperature=temperature,
        max_new_tokens=max_new_tokens,
        top_p=top_p,
        repetition_penalty=repetition_penalty,
        do_sample=True,
        seed=seed,
    )
    #history.append((prompt,""))
    #formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
    formatted_prompt = f"[INST] {system_prompt}, {prompt} [/INST]"
    client=client_z[0]

    stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
        
    output = ""

    for response in stream:
        output += response.token.text
    history.append((output,None))

    reply_cnt=post_check['reply']
    post_check['reply']=reply_cnt+1
    #out_json = {'user':"",'datetime':current_time,'title':title,'blog':1,'comment':0,'reply':0,"prompt":prompt,"output":output}
    #full_conv[-1]+=(output,)
    full_conv.append((None,None,output))
    html_out=load_html(full_conv,post_check['title'],post_check['user'])

    file_n = f'{post_check["filename"]}.json'
    print(file_n)
    r = requests.get(f'{save_data}book1/{file_n}') 
    print(f'status code main:: {r.status_code}')
    if r.status_code==200:
        try:
            lod = json.loads(r.text)
            print(f'lod:: {lod}')
            lod[0]['reply']=lod[0]['reply']+1
            lod[0]['comment_list'][0]['reply_list'].append({'user':persona[persona1]['name'],'datetime':'','reply':output})
            #hist_out.append(out_json)
            #try:
            #    for ea in 
            with open(f'{uid}.json', 'w') as f:
                json_hist=json.dumps(lod, indent=4)
                f.write(json_hist)
            f.close()
        
            upload_file(
                path_or_fileobj =f"{uid}.json", 
                path_in_repo = f"book1/{file_n}", 
                repo_id =f"{username}/{dataset_name}", 
                repo_type = "dataset", 
                token=token,
            )
        except Exception as e:
            print(e)
    return "",history,lod[0],lod[0],lod[0],html_out


    

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,):
#def question_generate(prompt, history):
    print("###############\nRUNNING BLOG POSTER REPLY\n###############\n")
    seed = random.randint(1,1111111111111111)
    agent=prompts.REPLY_TO_COMMENTER.format(focus=main_point[0])
    system_prompt=agent
    temperature = float(temperature)
    if temperature < 1e-2:
        temperature = 1e-2
    top_p = float(top_p)

    generate_kwargs = dict(
        temperature=temperature,
        max_new_tokens=max_new_tokens,
        top_p=top_p,
        repetition_penalty=repetition_penalty,
        do_sample=True,
        seed=seed,
    )
    #history.append((prompt,""))
    formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
    client=client_z[0]
    stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
    output = ""

    for response in stream:
        output += response.token.text
    #history.append((output,history))

    return output


    
def create_valid_filename(invalid_filename: str) -> str:
    """Converts invalid characters in a string to be suitable for a filename."""
    invalid_filename.replace(" ","-")
    valid_chars = '-'.join(invalid_filename.split())
    allowed_chars = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
                      'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
                      'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                      '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_', '-')
    return ''.join(char for char in valid_chars if char in allowed_chars)





    
def load_html(inp,title,user_name="None"):
    ht=""
    if inp:
        for i,ea in enumerate(inp):
            
            blog,comm,repl=ea
            #print(f'outp:: {outp}')
            #print(f'prom:: {prom}')
            ht+=f"""<div class="div_box">"""
            if blog:
                ht+=f"""<div class="bhead"><div><h1>$btitle</h1></div><div>$user_name</div></div>"""
                ht+=f"""<pre class="bpost">{blog}</pre>"""
            if comm:
                ht+=f"""<pre class="resp1">{comm}</pre>"""
            if repl:
                ht+=f"""<pre class="resp2">{repl}</pre>"""
            ht+=f"""</div>"""                
    with open('index.html','r') as h:
        html=h.read()
        html = html.replace("$body",f"{ht}")
        #html = html.replace("$title",f"{title}")
        html = html.replace("$btitle",f"{title}")
        html = html.replace("$user_name",f"{user_name}")
    h.close()
    return html
    
def load_html_OG(inp,title):
    ht=""
    if inp:
        for i,ea in enumerate(inp):
            
            outp,prom=ea
            #print(f'outp:: {outp}')
            #print(f'prom:: {prom}')
            if i == 0:
                ht+=f"""<div class="div_box">
                <pre class="bpost">{outp}</pre>
                <pre class="resp1">{prom}</pre>
                </div>"""                
            else:
                ht+=f"""<div class="div_box">
                <pre class="resp2">{outp}</pre>
                <pre class="resp2">{prom}</pre>
                </div>"""
    with open('index.html','r') as h:
        html=h.read()
        html = html.replace("$body",f"{ht}")
        html = html.replace("$title",f"{title}")
    h.close()
    return html




def generate(prompt, history, post_check,full_conv,persona1, agent_name=agents[0], sys_prompt="", temperature=0.9, max_new_tokens=1048, top_p=0.95, repetition_penalty=1.0):
    print(persona1)
    html_out=""
    #main_point[0]=prompt
    #print(datetime.datetime.now())
    uid=uuid.uuid4()
    current_time = str(datetime.datetime.now())
    title=""
    filename=create_valid_filename(f'{current_time}---{title}')
    
    current_time=current_time.replace(":","-")
    current_time=current_time.replace(".","-")
    print (current_time)
    agent=prompts.BLOG_POSTER.format(persona=persona[persona1]['personality'])
    system_prompt=agent
    temperature = float(temperature)
    if temperature < 1e-2:
        temperature = 1e-2
    top_p = float(top_p)
    hist_out=[]
    sum_out=[]
    json_hist={}
    json_obj={}
    full_conv=[]
    post_cnt=1
    if not post_check:
        post_check={}
    if not full_conv:
        full_conv=[]

    
    seed = random.randint(1,1111111111111111)
    if not post_check:
        print("writing blog")
        generate_kwargs = dict(
            temperature=temperature,
            max_new_tokens=max_new_tokens2,
            top_p=top_p,
            repetition_penalty=repetition_penalty,
            do_sample=True,
            seed=seed,
        )    
        if prompt.startswith(' \"'):
            prompt=prompt.strip(' \"')

        formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)

        
        if len(formatted_prompt) < (40000):
            print(len(formatted_prompt))
    
            client=client_z[0]
            stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
            output = ""
            #if history:
            #    yield history
            if not prompt:
                prompt_out = None
            else: 
                prompt_out=prompt
            for response in stream:
                output += response.token.text
                yield "", [(prompt_out,output)],post_check,full_conv,summary[0],json_obj, json_hist,html_out
            
            if not title:
                for line in output.split("\n"):
                    if "title" in line.lower() and ":" in line.lower():
                        title = line.split(":")[1]
                        if title.startswith(' \"'):
                            title=title.strip(' \"')                        
                        print(f'title:: {title}')
                        filename=create_valid_filename(f'{current_time}---{title}')
                        
            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':[]}

            hist_out.append(out_json)
            #try:
            #    for ea in 
            with open(f'{uid}.json', 'w') as f:
                json_hist=json.dumps(hist_out, indent=4)
                f.write(json_hist)
            f.close()
        
            upload_file(
                path_or_fileobj =f"{uid}.json", 
                path_in_repo = f"book1/{filename}.json", 
                repo_id =f"{username}/{dataset_name}", 
                repo_type = "dataset", 
                token=token,
            )
        else:
            formatted_prompt = format_prompt(f"{prompts.COMPRESS_HISTORY_PROMPT.format(history=summary[0],focus=main_point[0])}, {summary[0]}", history)
            
            #current_time = str(datetime.datetime.now().timestamp()).split(".",1)[0]
            #filename=f'{filename}-{current_time}'
            history = []
            output = compress_history(formatted_prompt)
            summary[0]=output
            sum_json = {"summary":summary[0]}
            sum_out.append(sum_json)
            with open(f'{uid}-sum.json', 'w') as f:
                json_obj=json.dumps(sum_out, indent=4)
                f.write(json_obj)
            f.close()
            upload_file(
                path_or_fileobj =f"{uid}-sum.json", 
                path_in_repo = f"book1/{filename}-summary.json", 
                repo_id =f"{username}/{dataset_name}", 
                repo_type = "dataset", 
                token=token,
            )
    
           
            #prompt = question_generate(output, history)
        #main_point[0]=output
        full_conv.append((output,None,None))
    
    
        html_out=load_html(full_conv,title,persona[persona1]['name'])
        post_check={'filename':filename,'user':persona[persona1]['name'],'datetime':current_time,'title':title,'blog':1,'comment':0,'reply':0}
        yield prompt, history,post_check,full_conv,summary[0],json_obj,json_hist,html_out
    else:
        print("passing blog")

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):
    html_out=""
    #main_point[0]=prompt
    #print(datetime.datetime.now())
    uid=uuid.uuid4()
    current_time = str(datetime.datetime.now())
    title=""
    filename=create_valid_filename(f'{current_time}---{title}')
    
    current_time=current_time.replace(":","-")
    current_time=current_time.replace(".","-")
    print (current_time)
    agent=prompts.BLOG_POSTER
    system_prompt=agent
    temperature = float(temperature)
    if temperature < 1e-2:
        temperature = 1e-2
    top_p = float(top_p)
    hist_out=[]
    sum_out=[]
    json_hist={}
    json_obj={}
    full_conv=[]
    post_cnt=1
    while True:
        seed = random.randint(1,1111111111111111)
        if post_cnt==1:
            generate_kwargs = dict(
                temperature=temperature,
                max_new_tokens=max_new_tokens2,
                top_p=top_p,
                repetition_penalty=repetition_penalty,
                do_sample=True,
                seed=seed,
            )    
            if prompt.startswith(' \"'):
                prompt=prompt.strip(' \"')

            formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
            
            post_cnt+=1
        else:
            system_prompt=prompts.REPLY_TO_COMMENTER.format(focus=main_point[0])
    
            generate_kwargs = dict(
                temperature=temperature,
                max_new_tokens=max_new_tokens2,
                top_p=top_p,
                repetition_penalty=repetition_penalty,
                do_sample=True,
                seed=seed,
            )    
            if prompt.startswith(' \"'):
                prompt=prompt.strip(' \"')
            
            formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
            print("###############\nRUNNING REPLY TO COMMENTER\n###############\n")
            print (system_prompt)
        if len(formatted_prompt) < (40000):
            print(len(formatted_prompt))

            client=client_z[0]
            stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
            output = ""
            #if history:
            #    yield history
        
            for response in stream:
                output += response.token.text
                yield '', [(prompt,output)],summary[0],json_obj, json_hist,html_out
            
            if not title:
                for line in output.split("\n"):
                    if "title" in line.lower() and ":" in line.lower():
                        title = line.split(":")[1]
                        print(f'title:: {title}')
                        filename=create_valid_filename(f'{current_time}---{title}')
                        
            out_json = {"prompt":prompt,"output":output}
        
            prompt = question_generate(output, history)
            #output += prompt
            history.append((prompt,output))
            print ( f'Prompt:: {len(prompt)}')
            #print ( f'output:: {output}')
            print ( f'history:: {len(formatted_prompt)}')
            hist_out.append(out_json)
            #try:
            #    for ea in 
            with open(f'{uid}.json', 'w') as f:
                json_hist=json.dumps(hist_out, indent=4)
                f.write(json_hist)
            f.close()
        
            upload_file(
                path_or_fileobj =f"{uid}.json", 
                path_in_repo = f"book1/{filename}.json", 
                repo_id =f"{username}/{dataset_name}", 
                repo_type = "dataset", 
                token=token,
            )
        else:
            formatted_prompt = format_prompt(f"{prompts.COMPRESS_HISTORY_PROMPT.format(history=summary[0],focus=main_point[0])}, {summary[0]}", history)
            
            #current_time = str(datetime.datetime.now().timestamp()).split(".",1)[0]
            #filename=f'{filename}-{current_time}'
            history = []
            output = compress_history(formatted_prompt)
            summary[0]=output
            sum_json = {"summary":summary[0]}
            sum_out.append(sum_json)
            with open(f'{uid}-sum.json', 'w') as f:
                json_obj=json.dumps(sum_out, indent=4)
                f.write(json_obj)
            f.close()
            upload_file(
                path_or_fileobj =f"{uid}-sum.json", 
                path_in_repo = f"book1/{filename}-summary.json", 
                repo_id =f"{username}/{dataset_name}", 
                repo_type = "dataset", 
                token=token,
            )

           
            prompt = question_generate(output, history)
        main_point[0]=prompt
        full_conv.append((output,prompt))


        html_out=load_html(full_conv,title)
        yield prompt, history, summary[0],json_obj,json_hist,html_out
    return prompt, history, summary[0],json_obj,json_hist,html_out




with gr.Blocks() as app:
    
    chat_handler=gr.State()
    post_handler=gr.State()
    html = gr.HTML()

    chatbot=gr.Chatbot(visible=False)
    msg = gr.Textbox()
    with gr.Row():
        submit_b = gr.Button("Blog Post")
        submit_c = gr.Button("Comment")
        submit_r = gr.Button("OP Reply")
    with gr.Row():
        stop_b = gr.Button("Stop")
        clear = gr.ClearButton([msg, chatbot])
    
    with gr.Row():
        m_choice=gr.Dropdown(label="Models",type='index',choices=[c for c in models],value=models[0],interactive=True)
        tokens = gr.Slider(label="Max new tokens",value=1600,minimum=0,maximum=8000,step=64,interactive=True, visible=True,info="The maximum number of tokens")
    with gr.Row():
        #persona1=gr.Textbox(label="Bot 1 Persona",info="Your personallity can be be described as...")
        #persona2=gr.Textbox(label="Bot 2 Persona",info="Your personallity can be be described as...")
        persona1=gr.Dropdown(label="Bot 1 Persona",value=persona_names[0],type='index',choices=[p for p in persona_names])
        persona2=gr.Dropdown(label="Bot 2 Persona",value=persona_names[3],type='index',choices=[p for p in persona_names])
        
    sumbox=gr.Textbox("Summary", max_lines=100)
    with gr.Column():
        sum_out_box=gr.JSON(label="Summaries")
        hist_out_box=gr.JSON(label="History")


    m_choice.change(load_models,m_choice,[chatbot])
    #app.load(load_models,m_choice,[chatbot]).then(load_html,None,html)
    app.load(load_models,m_choice,[chatbot])
    
    sub_b = submit_b.click(generate, [msg,chatbot,post_handler,chat_handler,persona1,tokens],[msg,chatbot,post_handler,chat_handler,sumbox,sum_out_box,hist_out_box,html])
    sub_c = submit_c.click(comment_generate, [msg,chatbot,post_handler,chat_handler,persona2],[msg,chatbot,sumbox,sum_out_box,hist_out_box,html])
    sub_r = submit_r.click(reply_generate, [msg,chatbot,post_handler,chat_handler,persona1],[msg,chatbot,sumbox,sum_out_box,hist_out_box,html])
    sub_e = msg.submit(generate, [msg,chatbot,post_handler,chat_handler,chat_handler,persona1,tokens],[msg,chatbot,post_handler,chat_handler,sumbox,sum_out_box,hist_out_box,html])
    stop_b.click(None,None,None, cancels=[sub_b,sub_e,sub_c,sub_r])
    
app.queue(default_concurrency_limit=20).launch()