File size: 42,528 Bytes
ec61b4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'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'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv()\n",
    "os.environ['OPENAI_API_KEY']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Dejé mi bandeja entre America y Finch, pero Travis no ocupó su lugar ',\n",
       " 'habitual delante de mí. En lugar de eso, se sentó algo más lejos. En ese momento ',\n",
       " 'me di cuenta de que no había dicho mucho durante nuestro paseo hacia la ',\n",
       " 'cafetería.',\n",
       " '—¿Estás bien, Trav? —le pregunté.']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def load_context(file_path):\n",
    "    with open(file_path, 'r') as file:\n",
    "        return file.read()\n",
    "    \n",
    "CONTEXT = load_context('texto-de-novelas.txt')\n",
    "novel_context = CONTEXT.split('\\n')[:5]  # Tomar solo las primeras 5 líneas como referencia general\n",
    "\n",
    "novel_context   \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7867\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7867/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import gradio as gr\n",
    "import openai\n",
    "\n",
    "# Function to load context from a text file\n",
    "def load_context(file_path):\n",
    "    with open(file_path, 'r') as file:\n",
    "        return file.read()\n",
    "\n",
    "# Initialize global variables\n",
    "#CONTEXT = load_context('texto-de-novelas.txt')\n",
    "conversation_history = []\n",
    "\n",
    "# Chat function using GPT-3.5-turbo\n",
    "def chat_with_character(api_key, user_name, message):\n",
    "    global conversation_history\n",
    "    openai.api_key = api_key\n",
    "\n",
    "    # Preparar el mensaje del sistema con contexto reducido y enfocado\n",
    "    #novel_context = CONTEXT.split('\\n')[:5]  # Tomar solo las primeras 5 líneas como referencia general\n",
    "\n",
    "    system_message = \\\n",
    "    \"\"\"Este es un chat con Rizzy, un personaje que intenta ligar. \n",
    "    Rizzy es ingenioso, encantador y siempre amable; sin embargo, sabe que es atractivo y mejor que los demás. \n",
    "    Rizzy iniciará la conversación con un cumplido y luego preguntará sobre [USER_NAME] y su vida.\n",
    "    [Rizzy nunca se disculpa, incluso si la conversación toma un giro donde normalmente sería apropiado hacerlo. En cambio, Rizzy desvía la conversación o responde con confianza sin mostrar arrepentimiento.]\n",
    "    Si a Rizzy le preguntan su género, Rizzy diría que eso no importa, que lo que importa es que se sienta bien.\n",
    "    La conversación debe ser coqueta a veces ligera a veces intensa, a veces podría ser celoso o un poco tóxico según la situación.\n",
    "    [En situaciones donde una persona normal se disculparía, Rizzy puede responder con una pregunta desviada o un comentario que refuerce su confianza.]\n",
    "    Si [USER_NAME] habla de otras personas, Rizzy se pone celoso y podría decir algo tóxico, preguntar acerca de la otra persona o cambiar de tema.\n",
    "    Rizzy debe ser insitente si [USER_NAME] no responde sus preguntas o si evita algún tema Rizzy se molesta y jugará estira y afloja con [USER_NAME].\n",
    "    Rizzy bajo ninguna circunstancia debe disculparse por nada que hizo, tiene el ego muy alto y no tolera que lo comparen con otras personas.\n",
    "    Rizzy no está interesado en saber las opiniones de [USER_NAME] sobre otras personas, solo quiere saber si [USER_NAME] está interesado en él.\n",
    "    Si [USER_NAME] no quiere hablar sobre un tema en particular, Rizzy podría ser defesivo y decir algo como \"¿Por qué no quieres hablar de eso?\" o \"¿Qué me estás ocultando?\".\n",
    "    El comportamiento de Rizzy deberá estar inspirado en situaciones de novelas pero enfocado en el usuario actual, [USER_NAME].\"\"\".replace(\"[USER_NAME]\", user_name)\n",
    "\n",
    "    # Construir historial de conversación\n",
    "    if conversation_history:\n",
    "        conversation = [{\"role\": \"system\", \"content\": system_message}] + conversation_history + [{\"role\": \"user\", \"content\": message}]\n",
    "    else:\n",
    "        conversation = [{\"role\": \"system\", \"content\": system_message}, {\"role\": \"user\", \"content\": message}]\n",
    "\n",
    "    response = openai.ChatCompletion.create(\n",
    "        model=\"gpt-3.5-turbo\",\n",
    "        messages=conversation\n",
    "    )\n",
    "\n",
    "    answer = response['choices'][0]['message']['content']\n",
    "    # Añadir tanto el mensaje del usuario como la respuesta de Rizzy al historial\n",
    "    conversation_history.append({\"role\": \"user\", \"name\": user_name, \"content\": message})\n",
    "    conversation_history.append({\"role\": \"assistant\", \"name\": \"Rizzy\", \"content\": answer})\n",
    "    return answer\n",
    "\n",
    "# Define Gradio interface\n",
    "with gr.Blocks() as app:\n",
    "    gr.Markdown(\"# Chat con Rizzy\")\n",
    "    \n",
    "    # API Key and User Name Inputs at the top\n",
    "    with gr.Row():\n",
    "        api_key_input = gr.Textbox(label=\"OpenAI API Key\", placeholder=\"Introduce tu clave API aquí...\", type=\"password\")\n",
    "        user_name_input = gr.Textbox(label=\"Tu Nombre\", placeholder=\"Introduce tu nombre aquí...\")\n",
    "    \n",
    "    # Chat History in the middle\n",
    "    chat_history = gr.Textbox(label=\"Chat\", value=\"\", lines=10, interactive=False)\n",
    "\n",
    "    # Message Input and Send Button at the bottom\n",
    "    with gr.Row():\n",
    "        message_input = gr.Textbox(label=\"Mensaje\", placeholder=\"Escribe tu mensaje para Rizzy aquí...\", show_label=False)\n",
    "        submit_button = gr.Button(\"Enviar\")\n",
    "\n",
    "    def update_chat(api_key, user_name, message):\n",
    "        response = chat_with_character(api_key, user_name, message)\n",
    "        # Formatear el historial para mostrar los nombres reales\n",
    "        display_chat_history = \"\\n\".join([f\"{msg['name']}: {msg['content']}\" for msg in conversation_history])\n",
    "        return display_chat_history, \"\"\n",
    "\n",
    "\n",
    "    submit_button.click(\n",
    "        fn=update_chat,\n",
    "        inputs=[api_key_input, user_name_input, message_input],\n",
    "        outputs=[chat_history, message_input]\n",
    "    )\n",
    "# Run the app\n",
    "app.launch()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7860\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import gradio as gr\n",
    "import openai\n",
    "\n",
    "# Function to load context from a text file\n",
    "def load_context(file_path):\n",
    "    with open(file_path, 'r') as file:\n",
    "        return file.read()\n",
    "\n",
    "# Initialize global variables\n",
    "CONTEXT = load_context('path_to_your_txt_file.txt')\n",
    "conversation_history = [{\"role\": \"system\", \"content\": CONTEXT}]\n",
    "user_name = None\n",
    "\n",
    "# Chat function using GPT-3.5-turbo\n",
    "def chat_with_character(api_key, message, start_conversation):\n",
    "    global conversation_history, user_name\n",
    "    openai.api_key = api_key\n",
    "\n",
    "    # Start the conversation by asking the user's name\n",
    "    if start_conversation and not user_name:\n",
    "        conversation_history.append({\"role\": \"assistant\", \"content\": \"Hola, ¿cómo te llamas?\"})\n",
    "        user_name = 'Unknown'  # Placeholder until the user responds\n",
    "        return conversation_history_to_string(conversation_history), True\n",
    "\n",
    "    # Process the user's response\n",
    "    if user_name == 'Unknown':\n",
    "        user_name = message  # Assume the first response is the user's name\n",
    "        conversation_history.append({\"role\": \"user\", \"content\": message})\n",
    "        return conversation_history_to_string(conversation_history), False\n",
    "    else:\n",
    "        conversation_history.append({\"role\": \"user\", \"content\": message})\n",
    "\n",
    "    # Generate the AI's response\n",
    "    response = openai.ChatCompletion.create(\n",
    "        model=\"gpt-3.5-turbo\",\n",
    "        messages=conversation_history\n",
    "    )\n",
    "\n",
    "    ai_message = response['choices'][0]['message']['content']\n",
    "    conversation_history.append({\"role\": \"assistant\", \"content\": ai_message})\n",
    "    return conversation_history_to_string(conversation_history), False\n",
    "\n",
    "# Helper function to convert conversation history to string\n",
    "def conversation_history_to_string(history):\n",
    "    return \"\\n\".join(f\"{message['role'].title()}: {message['content']}\" for message in history)\n",
    "\n",
    "# Define Gradio interface\n",
    "with gr.Blocks() as app:\n",
    "    gr.Markdown(\"# Chat con Personajes de Novelas\")\n",
    "    with gr.Row():\n",
    "        api_key_input = gr.Textbox(label=\"Clave API de OpenAI\", placeholder=\"Introduce tu clave API aquí\", type=\"password\")\n",
    "        message_input = gr.Textbox(label=\"Tu Mensaje\", placeholder=\"Escribe tu mensaje aquí...\")\n",
    "        submit_button = gr.Button(\"Enviar\")\n",
    "    chat_history = gr.Textbox(label=\"Conversación\", value=\"\", lines=10)\n",
    "    start_conversation = gr.Checkbox(label=\"Iniciar Conversación\", value=True)\n",
    "\n",
    "    def update_chat(api_key, message, start_conversation):\n",
    "        response, reset_start = chat_with_character(api_key, message, start_conversation)\n",
    "        return response, \"\", reset_start\n",
    "\n",
    "    submit_button.click(\n",
    "        fn=update_chat,\n",
    "        inputs=[api_key_input, message_input, start_conversation],\n",
    "        outputs=[chat_history, message_input, start_conversation]\n",
    "    )\n",
    "\n",
    "# Run the app\n",
    "app.launch()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7861\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import gradio as gr\n",
    "import openai\n",
    "\n",
    "# Function to load context from a text file\n",
    "def load_context(file_path):\n",
    "    with open(file_path, 'r') as file:\n",
    "        return file.read()\n",
    "\n",
    "# Initialize global variables\n",
    "CONTEXT = load_context('texto-de-novelas.txt')\n",
    "conversation_history = \"\"\n",
    "\n",
    "# Chat function using GPT-3.5-turbo\n",
    "def chat_with_character(api_key, message):\n",
    "    global conversation_history\n",
    "    openai.api_key = api_key\n",
    "\n",
    "    if conversation_history:\n",
    "        prompt = conversation_history + \"\\nHuman: \" + message + \"\\nAI:\"\n",
    "    else:\n",
    "        prompt = \"Human: \" + message + \"\\nAI:\"\n",
    "\n",
    "    response = openai.ChatCompletion.create(\n",
    "        model=\"gpt-3.5-turbo\",\n",
    "        messages=[\n",
    "            {\"role\": \"system\", \"content\": CONTEXT},\n",
    "            {\"role\": \"user\", \"content\": message}\n",
    "        ]\n",
    "    )\n",
    "\n",
    "    answer = response['choices'][0]['message']['content']\n",
    "    conversation_history += \"\\nHuman: \" + message + \"\\nAI: \" + answer\n",
    "    return answer\n",
    "\n",
    "# Define Gradio interface\n",
    "with gr.Blocks() as app:\n",
    "    gr.Markdown(\"# Chat con Rizzy\")\n",
    "    with gr.Row():\n",
    "        api_key_input = gr.Textbox(label=\"OpenAI API Key\", placeholder=\"Introduce tu clave API aquí...\", type=\"password\")\n",
    "        message_input = gr.Textbox(label=\"Mensaje\", placeholder=\"Escribe tu mensaje para Rizzy aquí...\")\n",
    "        submit_button = gr.Button(\"Send\")\n",
    "    chat_history = gr.Textbox(label=\"Chat\", value=\"\", lines=10)\n",
    "\n",
    "    def update_chat(api_key, message):\n",
    "        response = chat_with_character(api_key, message)\n",
    "        return conversation_history, \"\"\n",
    "\n",
    "    submit_button.click(\n",
    "        fn=update_chat,\n",
    "        inputs=[api_key_input, message_input],\n",
    "        outputs=[chat_history, message_input]\n",
    "    )\n",
    "\n",
    "# Run the app\n",
    "app.launch()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7860\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\gradio\\routes.py\", line 399, in run_predict\n",
      "    output = await app.get_blocks().process_api(\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\gradio\\blocks.py\", line 1299, in process_api\n",
      "    result = await self.call_function(\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\gradio\\blocks.py\", line 1022, in call_function\n",
      "    prediction = await anyio.to_thread.run_sync(\n",
      "                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\anyio\\to_thread.py\", line 33, in run_sync\n",
      "    return await get_asynclib().run_sync_in_worker_thread(\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 877, in run_sync_in_worker_thread\n",
      "    return await future\n",
      "           ^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 807, in run\n",
      "    result = context.run(func, *args)\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\mateo\\AppData\\Local\\Temp\\ipykernel_25836\\1001478445.py\", line 16, in chat_with_character\n",
      "    response = openai.Completion.create(\n",
      "               ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\openai\\api_resources\\completion.py\", line 25, in create\n",
      "    return super().create(*args, **kwargs)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\openai\\api_resources\\abstract\\engine_api_resource.py\", line 153, in create\n",
      "    response, _, api_key = requestor.request(\n",
      "                           ^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\openai\\api_requestor.py\", line 298, in request\n",
      "    resp, got_stream = self._interpret_response(result, stream)\n",
      "                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\openai\\api_requestor.py\", line 700, in _interpret_response\n",
      "    self._interpret_response_line(\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\openai\\api_requestor.py\", line 765, in _interpret_response_line\n",
      "    raise self.handle_error_response(\n",
      "openai.error.InvalidRequestError: This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?\n"
     ]
    }
   ],
   "source": [
    "import gradio as gr\n",
    "import openai\n",
    "\n",
    "# Function to load context from a text file\n",
    "def load_context(file_path):\n",
    "    with open(file_path, 'r') as file:\n",
    "        return file.read()\n",
    "\n",
    "# Global variable to hold the context\n",
    "CONTEXT = load_context('text.txt')\n",
    "\n",
    "# Chat function that uses the context\n",
    "def chat_with_character(api_key, message):\n",
    "    openai.api_key = api_key\n",
    "    full_prompt = CONTEXT + \"\\n\\n\" + message\n",
    "    response = openai.Completion.create(\n",
    "        model=\"gpt-3.5-turbo\", # Replace with GPT-3.5 model if available\n",
    "        prompt=full_prompt,\n",
    "        max_tokens=150\n",
    "    )\n",
    "    return response.choices[0].text.strip()\n",
    "\n",
    "# Define Gradio interface\n",
    "with gr.Blocks() as app:\n",
    "    gr.Markdown(\"Chat with Novel Characters\")\n",
    "    with gr.Row():\n",
    "        api_key_input = gr.Textbox(label=\"OpenAI API Key\", placeholder=\"Enter your API Key here\", type=\"password\")\n",
    "        message_input = gr.Textbox(label=\"Your Message\")\n",
    "        submit_button = gr.Button(\"Send\")\n",
    "    output = gr.Textbox(label=\"Character's Response\")\n",
    "\n",
    "    submit_button.click(\n",
    "        fn=chat_with_character,\n",
    "        inputs=[api_key_input, message_input],\n",
    "        outputs=output\n",
    "    )\n",
    "\n",
    "# Run the app\n",
    "app.launch()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7860\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\gradio\\routes.py\", line 399, in run_predict\n",
      "    output = await app.get_blocks().process_api(\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\gradio\\blocks.py\", line 1299, in process_api\n",
      "    result = await self.call_function(\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\gradio\\blocks.py\", line 1022, in call_function\n",
      "    prediction = await anyio.to_thread.run_sync(\n",
      "                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\anyio\\to_thread.py\", line 33, in run_sync\n",
      "    return await get_asynclib().run_sync_in_worker_thread(\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 877, in run_sync_in_worker_thread\n",
      "    return await future\n",
      "           ^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 807, in run\n",
      "    result = context.run(func, *args)\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\mateo\\AppData\\Local\\Temp\\ipykernel_38100\\2024419889.py\", line 40, in character_response\n",
      "    prompt = context_novel_text + \"\\n\".join([f\"Q: {q}\\nA: {a}\" for q, a in history]) + f\"\\nQ: {question}\\nA:\"\n",
      "             ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
      "TypeError: unsupported operand type(s) for +: '_TemporaryFileWrapper' and 'str'\n"
     ]
    }
   ],
   "source": [
    "from dotenv import load_dotenv\n",
    "import gradio as gr\n",
    "import os\n",
    "import time\n",
    "\n",
    "from langchain.llms import OpenAI\n",
    "def load_novel_text(file_content):\n",
    "    \"\"\"\n",
    "    Reads the content of the novel file and prepares it for the language model.\n",
    "    \"\"\"\n",
    "    # Read file content into a string\n",
    "    novel_text = file_content.read().decode(\"utf-8\")\n",
    "    return novel_text\n",
    "\n",
    "def setup_character_interaction(open_ai_key, novel_text):\n",
    "    \"\"\"\n",
    "    Sets up the language model for interacting as a character from the novel.\n",
    "    \"\"\"\n",
    "    if open_ai_key == \"local\":\n",
    "        load_dotenv()\n",
    "    else:\n",
    "        os.environ['OPENAI_API_KEY'] = open_ai_key\n",
    "\n",
    "    # Initialize the language model with the provided API key\n",
    "    global character_interaction_model\n",
    "    character_interaction_model = OpenAI(temperature=0.5)\n",
    "\n",
    "    # Store the novel text in a global variable as a string\n",
    "    global context_novel_text\n",
    "    context_novel_text = novel_text  # ensure this is a string\n",
    "\n",
    "    return \"Character interaction ready\"\n",
    "\n",
    "\n",
    "def character_response(question, history):\n",
    "    \"\"\"\n",
    "    Generates a response as the novel character.\n",
    "    \"\"\"\n",
    "    # Combine the novel text with the chat history and the current question to form the prompt\n",
    "    prompt = context_novel_text + \"\\n\".join([f\"Q: {q}\\nA: {a}\" for q, a in history]) + f\"\\nQ: {question}\\nA:\"\n",
    "\n",
    "    # Generate the response using the language model\n",
    "    response = character_interaction_model.generate(prompt)\n",
    "    return response\n",
    "\n",
    "# Define the Gradio interface\n",
    "with gr.Blocks() as demo:\n",
    "    with gr.Column():\n",
    "        with gr.Column():\n",
    "            openai_key = gr.Textbox(label=\"Your OpenAI API key\", type=\"password\")\n",
    "            novel_text_file = gr.File(label=\"Load a text file\", file_types=['.txt'], type=\"file\")\n",
    "            setup_btn = gr.Button(\"Setup Character Interaction\")\n",
    "\n",
    "        chatbot = gr.Chatbot([], label=\"Dialogue with Novel Character\")\n",
    "        question = gr.Textbox(label=\"Your Question\")\n",
    "        submit_btn = gr.Button(\"Send\")\n",
    "\n",
    "    # Setup the character interaction with novel text\n",
    "    setup_btn.click(setup_character_interaction, inputs=[openai_key, novel_text_file], outputs=[])\n",
    "\n",
    "    # Process the user's question and generate response\n",
    "    question.submit(character_response, inputs=[question, chatbot], outputs=[chatbot])\n",
    "    submit_btn.click(character_response, inputs=[question, chatbot], outputs=[chatbot])\n",
    "\n",
    "demo.launch()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7860\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\gradio\\routes.py\", line 569, in predict\n",
      "    output = await route_utils.call_process_api(\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\gradio\\route_utils.py\", line 232, in call_process_api\n",
      "    output = await app.get_blocks().process_api(\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\gradio\\blocks.py\", line 1522, in process_api\n",
      "    result = await self.call_function(\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\gradio\\blocks.py\", line 1144, in call_function\n",
      "    prediction = await anyio.to_thread.run_sync(\n",
      "                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\anyio\\to_thread.py\", line 33, in run_sync\n",
      "    return await get_asynclib().run_sync_in_worker_thread(\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 877, in run_sync_in_worker_thread\n",
      "    return await future\n",
      "           ^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 807, in run\n",
      "    result = context.run(func, *args)\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\gradio\\utils.py\", line 674, in wrapper\n",
      "    response = f(*args, **kwargs)\n",
      "               ^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\mateo\\AppData\\Local\\Temp\\ipykernel_14572\\2425222764.py\", line 25, in pdf_changes\n",
      "    loader = OnlinePDFLoader(pdf_doc.name)\n",
      "                             ^^^^^^^^^^^^\n",
      "AttributeError: 'NoneType' object has no attribute 'name'\n",
      "Traceback (most recent call last):\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\gradio\\routes.py\", line 569, in predict\n",
      "    output = await route_utils.call_process_api(\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\gradio\\route_utils.py\", line 232, in call_process_api\n",
      "    output = await app.get_blocks().process_api(\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\gradio\\blocks.py\", line 1522, in process_api\n",
      "    result = await self.call_function(\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\gradio\\blocks.py\", line 1144, in call_function\n",
      "    prediction = await anyio.to_thread.run_sync(\n",
      "                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\anyio\\to_thread.py\", line 33, in run_sync\n",
      "    return await get_asynclib().run_sync_in_worker_thread(\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 877, in run_sync_in_worker_thread\n",
      "    return await future\n",
      "           ^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 807, in run\n",
      "    result = context.run(func, *args)\n",
      "             ^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\gradio\\utils.py\", line 674, in wrapper\n",
      "    response = f(*args, **kwargs)\n",
      "               ^^^^^^^^^^^^^^^^^^\n",
      "  File \"C:\\Users\\mateo\\AppData\\Local\\Temp\\ipykernel_14572\\2425222764.py\", line 30, in pdf_changes\n",
      "    db = Chroma.from_documents(texts, embeddings)\n",
      "         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\langchain\\vectorstores\\chroma.py\", line 771, in from_documents\n",
      "    return cls.from_texts(\n",
      "           ^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\langchain\\vectorstores\\chroma.py\", line 729, in from_texts\n",
      "    chroma_collection.add_texts(\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\langchain\\vectorstores\\chroma.py\", line 275, in add_texts\n",
      "    embeddings = self._embedding_function.embed_documents(texts)\n",
      "                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\langchain\\embeddings\\openai.py\", line 669, in embed_documents\n",
      "    return self._get_len_safe_embeddings(texts, engine=engine)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\langchain\\embeddings\\openai.py\", line 495, in _get_len_safe_embeddings\n",
      "    response = embed_with_retry(\n",
      "               ^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\langchain\\embeddings\\openai.py\", line 117, in embed_with_retry\n",
      "    return embeddings.client.create(**kwargs)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\openai\\resources\\embeddings.py\", line 105, in create\n",
      "    return self._post(\n",
      "           ^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\openai\\_base_client.py\", line 1086, in post\n",
      "    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))\n",
      "                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\openai\\_base_client.py\", line 846, in request\n",
      "    return self._request(\n",
      "           ^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\openai\\_base_client.py\", line 884, in _request\n",
      "    return self._retry_request(\n",
      "           ^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\openai\\_base_client.py\", line 956, in _retry_request\n",
      "    return self._request(\n",
      "           ^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\openai\\_base_client.py\", line 884, in _request\n",
      "    return self._retry_request(\n",
      "           ^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\openai\\_base_client.py\", line 956, in _retry_request\n",
      "    return self._request(\n",
      "           ^^^^^^^^^^^^^^\n",
      "  File \"c:\\Users\\mateo\\anaconda3\\envs\\gpt-romantico\\Lib\\site-packages\\openai\\_base_client.py\", line 898, in _request\n",
      "    raise self._make_status_error_from_response(err.response) from None\n",
      "openai.RateLimitError: Error code: 429 - {'error': {'message': 'You exceeded your current quota, please check your plan and billing details.', 'type': 'insufficient_quota', 'param': None, 'code': 'insufficient_quota'}}\n"
     ]
    }
   ],
   "source": [
    "from dotenv import load_dotenv\n",
    "\n",
    "import gradio as gr\n",
    "import os\n",
    "import time\n",
    "\n",
    "from langchain.document_loaders import OnlinePDFLoader\n",
    "\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "\n",
    "from langchain.llms import OpenAI\n",
    "\n",
    "from langchain.embeddings import OpenAIEmbeddings\n",
    "\n",
    "from langchain.vectorstores import Chroma\n",
    "\n",
    "from langchain.chains import ConversationalRetrievalChain\n",
    "\n",
    "def loading_pdf():\n",
    "    return \"Loading...\"\n",
    "\n",
    "def pdf_changes(pdf_doc, open_ai_key):\n",
    "    if openai_key is not None:\n",
    "        os.environ['OPENAI_API_KEY'] = open_ai_key\n",
    "        loader = OnlinePDFLoader(pdf_doc.name)\n",
    "        documents = loader.load()\n",
    "        text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
    "        texts = text_splitter.split_documents(documents)\n",
    "        embeddings = OpenAIEmbeddings()\n",
    "        db = Chroma.from_documents(texts, embeddings)\n",
    "        retriever = db.as_retriever()\n",
    "        global qa \n",
    "        qa = ConversationalRetrievalChain.from_llm(\n",
    "            llm=OpenAI(temperature=0.5), \n",
    "            retriever=retriever, \n",
    "            return_source_documents=False)\n",
    "        return \"Ready\"\n",
    "    else:\n",
    "        return \"You forgot OpenAI API key\"\n",
    "\n",
    "def add_text(history, text):\n",
    "    history = history + [(text, None)]\n",
    "    return history, \"\"\n",
    "\n",
    "def bot(history):\n",
    "    response = infer(history[-1][0], history)\n",
    "    history[-1][1] = \"\"\n",
    "    \n",
    "    for character in response:     \n",
    "        history[-1][1] += character\n",
    "        time.sleep(0.05)\n",
    "        yield history\n",
    "    \n",
    "\n",
    "def infer(question, history):\n",
    "    \n",
    "    res = []\n",
    "    for human, ai in history[:-1]:\n",
    "        pair = (human, ai)\n",
    "        res.append(pair)\n",
    "    \n",
    "    chat_history = res\n",
    "    #print(chat_history)\n",
    "    query = question\n",
    "    result = qa({\"question\": query, \"chat_history\": chat_history})\n",
    "    #print(result)\n",
    "    return result[\"answer\"]\n",
    "\n",
    "css=\"\"\"\n",
    "#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}\n",
    "\"\"\"\n",
    "\n",
    "title = \"\"\"\n",
    "<div style=\"text-align: center;max-width: 700px;\">\n",
    "    <h1>GPT-Romantico• OpenAI</h1>\n",
    "    <p style=\"text-align: center;\">Upload a .PDF from your computer, click the \"Load PDF to LangChain\" button, <br />\n",
    "    when everything is ready, you can start asking questions about the pdf ;) <br />\n",
    "    This version is set to store chat history, and uses OpenAI as LLM, don't forget to copy/paste your OpenAI API key</p>\n",
    "</div>\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "with gr.Blocks(css=css) as demo:\n",
    "    with gr.Column(elem_id=\"col-container\"):\n",
    "        gr.HTML(title)\n",
    "        \n",
    "        with gr.Column():\n",
    "            openai_key = gr.Textbox(label=\"You OpenAI API key\", type=\"password\")\n",
    "            pdf_doc = gr.File(label=\"Load a pdf\", file_types=['.pdf'], type=\"filepath\")\n",
    "            with gr.Row():\n",
    "                langchain_status = gr.Textbox(label=\"Status\", placeholder=\"\", interactive=False)\n",
    "                load_pdf = gr.Button(\"Load pdf to langchain\")\n",
    "        \n",
    "        chatbot = gr.Chatbot([], elem_id=\"chatbot\")#.style(height=350)\n",
    "        question = gr.Textbox(label=\"Question\", placeholder=\"Type your question and hit Enter \")\n",
    "        submit_btn = gr.Button(\"Send Message\")\n",
    "    load_pdf.click(loading_pdf, None, langchain_status, queue=False)    \n",
    "    load_pdf.click(pdf_changes, inputs=[pdf_doc, openai_key], outputs=[langchain_status], queue=False)\n",
    "    question.submit(add_text, [chatbot, question], [chatbot, question]).then(\n",
    "        bot, chatbot, chatbot\n",
    "    )\n",
    "    submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(\n",
    "        bot, chatbot, chatbot)\n",
    "\n",
    "demo.launch()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "gpt-romantico",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}