Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,967 +1,966 @@
|
|
1 |
-
import os
|
2 |
-
import time
|
3 |
-
import streamlit as st
|
4 |
-
from youtube_transcript_api import YouTubeTranscriptApi
|
5 |
-
from youtube_search import YoutubeSearch
|
6 |
-
from fpdf import FPDF
|
7 |
-
from langchain_openai import ChatOpenAI
|
8 |
-
from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
|
9 |
-
from sentence_transformers import SentenceTransformer
|
10 |
-
from langchain.chains import RetrievalQA
|
11 |
-
from langchain.prompts import PromptTemplate
|
12 |
-
from langchain.memory import ConversationBufferWindowMemory
|
13 |
-
from langchain_community.vectorstores import Chroma
|
14 |
-
import
|
15 |
-
from
|
16 |
-
from
|
17 |
-
from
|
18 |
-
from langchain.
|
19 |
-
from langchain.
|
20 |
-
from
|
21 |
-
|
22 |
-
import
|
23 |
-
import
|
24 |
-
import
|
25 |
-
from
|
26 |
-
from
|
27 |
-
from langchain.chains import
|
28 |
-
|
29 |
-
import
|
30 |
-
|
31 |
-
import
|
32 |
-
|
33 |
-
import
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
import
|
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 |
-
logging.
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
logger.error(
|
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 |
-
h_documents
|
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 |
-
filename
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
import
|
227 |
-
import
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
stream
|
234 |
-
stream.
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
audio_filename
|
241 |
-
clip.
|
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 |
-
transcript_file =
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
pdf.
|
311 |
-
pdf.
|
312 |
-
|
313 |
-
|
314 |
-
output_filename
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
page_text
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
text =
|
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 |
-
filename = f
|
392 |
-
|
393 |
-
|
394 |
-
pdf_filename
|
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 |
-
"Formula 1
|
452 |
-
"
|
453 |
-
"Formula 1
|
454 |
-
"
|
455 |
-
"
|
456 |
-
"
|
457 |
-
"
|
458 |
-
"
|
459 |
-
"
|
460 |
-
"
|
461 |
-
"
|
462 |
-
"Formula 1
|
463 |
-
"
|
464 |
-
"
|
465 |
-
"
|
466 |
-
"
|
467 |
-
"
|
468 |
-
"
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
transcript_file
|
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 |
-
num_tokens
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
subtasks
|
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 |
-
response
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
result
|
589 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
#
|
595 |
-
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
|
602 |
-
|
603 |
-
|
604 |
-
|
605 |
-
|
606 |
-
qa_chain
|
607 |
-
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
-
|
612 |
-
|
613 |
-
|
614 |
-
|
615 |
-
|
616 |
-
|
617 |
-
|
618 |
-
|
619 |
-
|
620 |
-
combined_summary
|
621 |
-
|
622 |
-
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
|
627 |
-
|
628 |
-
|
629 |
-
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
635 |
-
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
last_period
|
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 |
-
llm_response
|
695 |
-
|
696 |
-
|
697 |
-
|
698 |
-
|
699 |
-
|
700 |
-
|
701 |
-
|
702 |
-
|
703 |
-
|
704 |
-
|
705 |
-
|
706 |
-
|
707 |
-
|
708 |
-
|
709 |
-
#
|
710 |
-
#
|
711 |
-
|
712 |
-
|
713 |
-
|
714 |
-
|
715 |
-
|
716 |
-
|
717 |
-
cleaned_text
|
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 |
-
Action: the
|
811 |
-
|
812 |
-
Observation
|
813 |
-
|
814 |
-
|
815 |
-
|
816 |
-
|
817 |
-
|
818 |
-
|
819 |
-
|
820 |
-
|
821 |
-
Action:
|
822 |
-
|
823 |
-
|
824 |
-
|
825 |
-
Action:
|
826 |
-
|
827 |
-
|
828 |
-
|
829 |
-
Action:
|
830 |
-
|
831 |
-
|
832 |
-
|
833 |
-
|
834 |
-
|
835 |
-
|
836 |
-
|
837 |
-
|
838 |
-
Action:
|
839 |
-
|
840 |
-
|
841 |
-
|
842 |
-
|
843 |
-
|
844 |
-
|
845 |
-
|
846 |
-
|
847 |
-
Action:
|
848 |
-
|
849 |
-
|
850 |
-
|
851 |
-
Action:
|
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 |
-
summary
|
923 |
-
st.
|
924 |
-
st.
|
925 |
-
|
926 |
-
|
927 |
-
|
928 |
-
|
929 |
-
|
930 |
-
|
931 |
-
pdf_summary
|
932 |
-
st.
|
933 |
-
st.
|
934 |
-
|
935 |
-
|
936 |
-
|
937 |
-
|
938 |
-
|
939 |
-
|
940 |
-
|
941 |
-
|
942 |
-
|
943 |
-
|
944 |
-
|
945 |
-
|
946 |
-
|
947 |
-
|
948 |
-
|
949 |
-
|
950 |
-
|
951 |
-
|
952 |
-
|
953 |
-
|
954 |
-
|
955 |
-
|
956 |
-
|
957 |
-
|
958 |
-
|
959 |
-
|
960 |
-
|
961 |
-
|
962 |
-
|
963 |
-
st.
|
964 |
-
|
965 |
-
|
966 |
-
|
967 |
-
main()
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import streamlit as st
|
4 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
5 |
+
from youtube_search import YoutubeSearch
|
6 |
+
from fpdf import FPDF
|
7 |
+
from langchain_openai import ChatOpenAI
|
8 |
+
from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
|
9 |
+
from sentence_transformers import SentenceTransformer
|
10 |
+
from langchain.chains import RetrievalQA
|
11 |
+
from langchain.prompts import PromptTemplate
|
12 |
+
from langchain.memory import ConversationBufferWindowMemory
|
13 |
+
from langchain_community.vectorstores import Chroma
|
14 |
+
from langchain_core.documents import Document
|
15 |
+
from pypdf import PdfReader
|
16 |
+
from langchain_community.document_loaders import PyPDFLoader
|
17 |
+
from langchain.agents import initialize_agent, Tool
|
18 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter, CharacterTextSplitter
|
19 |
+
from langchain.agents import Tool, AgentExecutor, create_react_agent, tool
|
20 |
+
from flask import Flask, request, jsonify
|
21 |
+
import sqlite3
|
22 |
+
import re
|
23 |
+
import textwrap
|
24 |
+
from langchain.chains.summarize import load_summarize_chain
|
25 |
+
from langchain_community.document_loaders import WebBaseLoader
|
26 |
+
from langchain.chains import MapReduceDocumentsChain, ReduceDocumentsChain, StuffDocumentsChain
|
27 |
+
from langchain.chains.llm import LLMChain
|
28 |
+
import torch
|
29 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
30 |
+
import nltk
|
31 |
+
from nltk.tokenize import word_tokenize
|
32 |
+
import pytube
|
33 |
+
from moviepy.editor import *
|
34 |
+
|
35 |
+
# Download necessary resources
|
36 |
+
nltk.download('punkt')
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
# Initialize environment variables
|
41 |
+
from dotenv import load_dotenv
|
42 |
+
import traceback
|
43 |
+
import logging
|
44 |
+
|
45 |
+
load_dotenv()
|
46 |
+
|
47 |
+
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
48 |
+
HUGGINGFACEHUB_API_TOKEN = os.getenv('HF_TOKEN')
|
49 |
+
YT_API_KEY = os.getenv('YT_API_KEY')
|
50 |
+
|
51 |
+
LANGCHAIN_TRACING_V2='true'
|
52 |
+
LANGCHAIN_ENDPOINT="https://api.smith.langchain.com"
|
53 |
+
LANGCHAIN_API_KEY = os.getenv('LANGCHAIN_API_KEY')
|
54 |
+
LANGCHAIN_PROJECT="default"
|
55 |
+
|
56 |
+
# Download and initialize all required models
|
57 |
+
model = SentenceTransformerEmbeddings(model_name='paraphrase-MiniLM-L6-v2')
|
58 |
+
summarization_model_name = "suriya7/bart-finetuned-text-summarization"
|
59 |
+
summarization_model = AutoModelForSeq2SeqLM.from_pretrained(summarization_model_name)
|
60 |
+
summarization_tokenizer = AutoTokenizer.from_pretrained(summarization_model_name)
|
61 |
+
|
62 |
+
|
63 |
+
# Function to load the vector database
|
64 |
+
def load_vectordb():
|
65 |
+
"""
|
66 |
+
Load the vector database from Chroma.
|
67 |
+
|
68 |
+
Returns:
|
69 |
+
langchain_chroma (Chroma): The Chroma vector database.
|
70 |
+
"""
|
71 |
+
persistent_client = Chroma.PersistentClient("chromadb")
|
72 |
+
|
73 |
+
langchain_chroma = Chroma(
|
74 |
+
client=persistent_client,
|
75 |
+
collection_name="knowledge_base",
|
76 |
+
embedding_function=model,
|
77 |
+
)
|
78 |
+
|
79 |
+
return langchain_chroma
|
80 |
+
|
81 |
+
vector_db = load_vectordb()
|
82 |
+
|
83 |
+
# Set up logging
|
84 |
+
logging.basicConfig(level=logging.INFO)
|
85 |
+
logger = logging.getLogger(__name__)
|
86 |
+
|
87 |
+
def safe_execute(func, *args, **kwargs):
|
88 |
+
"""
|
89 |
+
Execute a function safely, catching any exceptions and logging errors.
|
90 |
+
|
91 |
+
Args:
|
92 |
+
func (callable): The function to execute.
|
93 |
+
*args: Variable length argument list for the function.
|
94 |
+
**kwargs: Arbitrary keyword arguments for the function.
|
95 |
+
|
96 |
+
Returns:
|
97 |
+
The result of the function execution, or an error message if an exception occurs.
|
98 |
+
"""
|
99 |
+
try:
|
100 |
+
return func(*args, **kwargs)
|
101 |
+
except Exception as e:
|
102 |
+
logger.error(f"Error in {func.__name__}: {str(e)}")
|
103 |
+
logger.error(traceback.format_exc())
|
104 |
+
return f"An error occurred: {str(e)}"
|
105 |
+
|
106 |
+
|
107 |
+
# Initialize LLM
|
108 |
+
llm = ChatOpenAI(temperature=0.6, model_name="gpt-3.5-turbo-16k")
|
109 |
+
|
110 |
+
|
111 |
+
def count_tokens(text):
|
112 |
+
"""
|
113 |
+
Count the number of tokens in a given text using NLTK's word tokenizer.
|
114 |
+
|
115 |
+
Args:
|
116 |
+
text (str): The input text.
|
117 |
+
|
118 |
+
Returns:
|
119 |
+
int: The number of tokens in the text.
|
120 |
+
"""
|
121 |
+
tokens = word_tokenize(text)
|
122 |
+
return len(tokens)
|
123 |
+
|
124 |
+
def text_summarize(text):
|
125 |
+
"""
|
126 |
+
Summarize the input text using a MapReduce approach.
|
127 |
+
|
128 |
+
Args:
|
129 |
+
text (str): The input text to summarize.
|
130 |
+
|
131 |
+
Returns:
|
132 |
+
str: The summary of the input text.
|
133 |
+
"""
|
134 |
+
# Split the text into chunks
|
135 |
+
text_splitter = CharacterTextSplitter(chunk_size=10000, chunk_overlap=200)
|
136 |
+
|
137 |
+
docs = [Document(page_content=chunk) for chunk in text_splitter.split_text(text)]
|
138 |
+
|
139 |
+
# Map step
|
140 |
+
map_template = """The following is a document:
|
141 |
+
{docs}
|
142 |
+
Based on this document, please identify the main themes and key points.
|
143 |
+
Helpful Answer:"""
|
144 |
+
map_prompt = PromptTemplate.from_template(map_template)
|
145 |
+
map_chain = LLMChain(llm=llm, prompt=map_prompt)
|
146 |
+
|
147 |
+
# Reduce step
|
148 |
+
reduce_template = """The following is a set of summaries:
|
149 |
+
{docs}
|
150 |
+
Take these and distill them into a final, consolidated summary of the main themes and key points.
|
151 |
+
Helpful Answer:"""
|
152 |
+
reduce_prompt = PromptTemplate.from_template(reduce_template)
|
153 |
+
reduce_chain = LLMChain(llm=llm, prompt=reduce_prompt)
|
154 |
+
|
155 |
+
# Combine
|
156 |
+
combine_documents_chain = StuffDocumentsChain(
|
157 |
+
llm_chain=reduce_chain,
|
158 |
+
document_variable_name="docs"
|
159 |
+
)
|
160 |
+
|
161 |
+
# Create the MapReduceDocumentsChain
|
162 |
+
map_reduce_chain = MapReduceDocumentsChain(
|
163 |
+
llm_chain=map_chain,
|
164 |
+
reduce_documents_chain=combine_documents_chain,
|
165 |
+
document_variable_name="docs"
|
166 |
+
)
|
167 |
+
|
168 |
+
return map_reduce_chain.run(docs)
|
169 |
+
|
170 |
+
|
171 |
+
# Function to add documents to the database
|
172 |
+
def add_documents_to_db(pdf_file):
|
173 |
+
"""
|
174 |
+
Add documents extracted from a PDF file to the vector database.
|
175 |
+
|
176 |
+
Args:
|
177 |
+
pdf_file (str): The path to the PDF file to process.
|
178 |
+
"""
|
179 |
+
try:
|
180 |
+
texts = extract_text_from_pdf(pdf_file)
|
181 |
+
cleaned_text = clean_text(texts)
|
182 |
+
documents = get_text_chunks(cleaned_text)
|
183 |
+
|
184 |
+
if documents:
|
185 |
+
h_size = 10000
|
186 |
+
total_documents = len(documents)
|
187 |
+
processed_documents = 0
|
188 |
+
|
189 |
+
while processed_documents < total_documents:
|
190 |
+
remaining_documents = total_documents - processed_documents
|
191 |
+
current_h_size = min(h_size, remaining_documents)
|
192 |
+
|
193 |
+
h_documents = documents[processed_documents:processed_documents + current_h_size]
|
194 |
+
vector_db.add_documents(h_documents)
|
195 |
+
|
196 |
+
processed_documents += current_h_size
|
197 |
+
|
198 |
+
print(f"Processed {processed_documents} out of {total_documents} documents.")
|
199 |
+
|
200 |
+
print("All documents added to the collection.")
|
201 |
+
else:
|
202 |
+
logger.warning(f"No documents found in {pdf_file}.")
|
203 |
+
except Exception as e:
|
204 |
+
logger.error(f"Error adding documents to database from {pdf_file}: {str(e)}")
|
205 |
+
raise # Re-raise the exception for visibility
|
206 |
+
|
207 |
+
|
208 |
+
def generate_valid_filename(query):
|
209 |
+
"""
|
210 |
+
Generate a valid filename by replacing invalid characters with underscores.
|
211 |
+
|
212 |
+
Args:
|
213 |
+
query (str): The input string to generate the filename from.
|
214 |
+
|
215 |
+
Returns:
|
216 |
+
str: The generated valid filename.
|
217 |
+
"""
|
218 |
+
valid_chars = '-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
|
219 |
+
filename = ''.join(c if c in valid_chars else '_' for c in query)
|
220 |
+
return filename
|
221 |
+
|
222 |
+
#################################################
|
223 |
+
## NEW FUNCTIONS ##
|
224 |
+
#################################################
|
225 |
+
import whisper
|
226 |
+
import time
|
227 |
+
from pytube import YouTube
|
228 |
+
|
229 |
+
|
230 |
+
def download_video(url):
|
231 |
+
video = YouTube(url)
|
232 |
+
stream = video.streams.filter(file_extension='mp4')
|
233 |
+
stream.download()
|
234 |
+
return stream.default_filename
|
235 |
+
|
236 |
+
|
237 |
+
def video_to_text(filename):
|
238 |
+
clip = VideoFileClip(filename)
|
239 |
+
audio_filename = filename[:-4] + ".mp3"
|
240 |
+
clip.audio.write_audiofile(audio_filename)
|
241 |
+
clip.close()
|
242 |
+
time.sleep(5)
|
243 |
+
|
244 |
+
model = whisper.load_model("base")
|
245 |
+
result = model.transcribe(audio_filename)
|
246 |
+
|
247 |
+
transcription = result["text"]
|
248 |
+
|
249 |
+
return transcription
|
250 |
+
|
251 |
+
|
252 |
+
#################################################
|
253 |
+
# Function to search and transcribe YouTube videos
|
254 |
+
def search_and_transcribe_videos(query, max_results=20, min_valid_videos=4):
|
255 |
+
"""
|
256 |
+
Search for YouTube videos and transcribe them.
|
257 |
+
|
258 |
+
Args:
|
259 |
+
query (str): The search query for YouTube videos.
|
260 |
+
max_results (int): The maximum number of results to fetch. Default is 20.
|
261 |
+
min_valid_videos (int): The minimum number of valid videos to transcribe. Default is 4.
|
262 |
+
|
263 |
+
Returns:
|
264 |
+
str: The path to the transcript file.
|
265 |
+
"""
|
266 |
+
valid_urls = []
|
267 |
+
current_max_results = max_results
|
268 |
+
transcription = ''
|
269 |
+
while len(valid_urls) < min_valid_videos and current_max_results <= 20:
|
270 |
+
results = YoutubeSearch(query, max_results=current_max_results).to_dict()
|
271 |
+
filtered_results = [video for video in results if video.get('liveBroadcastContent') != 'live']
|
272 |
+
for video in filtered_results:
|
273 |
+
video_id = video['id']
|
274 |
+
video_link = f"https://www.youtube.com/watch?v={video_id}"
|
275 |
+
try:
|
276 |
+
transcription = YouTubeTranscriptApi.get_transcript(video_id, languages=['en', 'en-US'])
|
277 |
+
transcript_text = " ".join([line['text'] for line in transcription])
|
278 |
+
valid_urls.append((transcript_text))
|
279 |
+
|
280 |
+
except:
|
281 |
+
continue
|
282 |
+
|
283 |
+
if len(valid_urls) >= min_valid_videos:
|
284 |
+
break
|
285 |
+
|
286 |
+
current_max_results += max_results
|
287 |
+
|
288 |
+
transcript_file = generate_valid_filename(query) + '.txt'
|
289 |
+
with open(transcript_file, 'a', encoding='utf-8') as f:
|
290 |
+
for text in valid_urls[:min_valid_videos]:
|
291 |
+
f.write(f"Text:{text}\n\n")
|
292 |
+
|
293 |
+
return transcript_file
|
294 |
+
|
295 |
+
# Function to create a PDF from a transcript
|
296 |
+
def create_pdf(input_file):
|
297 |
+
"""
|
298 |
+
Create a PDF file from a transcript file.
|
299 |
+
|
300 |
+
Args:
|
301 |
+
input_file (str): The path to the transcript file.
|
302 |
+
|
303 |
+
Returns:
|
304 |
+
str: The path to the created PDF file.
|
305 |
+
"""
|
306 |
+
pdf = FPDF()
|
307 |
+
with open(input_file, 'r', encoding='utf-8') as f:
|
308 |
+
text = f.read()
|
309 |
+
pdf.add_page()
|
310 |
+
pdf.set_font('Arial', size=12)
|
311 |
+
pdf.multi_cell(0, 10, text.encode('latin-1', 'replace').decode('latin-1'))
|
312 |
+
filename = input_file.split('.txt')[0]
|
313 |
+
output_filename = f"{filename}.pdf"
|
314 |
+
pdf.output(output_filename)
|
315 |
+
return output_filename
|
316 |
+
|
317 |
+
# Function to extract text from a PDF
|
318 |
+
def extract_text_from_pdf(pdf_path):
|
319 |
+
"""
|
320 |
+
Extract text from a PDF file.
|
321 |
+
|
322 |
+
Args:
|
323 |
+
pdf_path (str): The path to the PDF file.
|
324 |
+
|
325 |
+
Returns:
|
326 |
+
str: The extracted text.
|
327 |
+
"""
|
328 |
+
reader = PdfReader(pdf_path)
|
329 |
+
text = ""
|
330 |
+
for page in reader.pages:
|
331 |
+
page_text = page.extract_text()
|
332 |
+
if page_text:
|
333 |
+
text += page_text
|
334 |
+
return text
|
335 |
+
|
336 |
+
# Function to clean extracted text
|
337 |
+
def clean_text(text):
|
338 |
+
"""
|
339 |
+
Clean and preprocess the extracted text.
|
340 |
+
|
341 |
+
Args:
|
342 |
+
text (str): The extracted text.
|
343 |
+
|
344 |
+
Returns:
|
345 |
+
str: The cleaned text.
|
346 |
+
"""
|
347 |
+
|
348 |
+
text = text.replace('\xa0', ' ')
|
349 |
+
text = re.sub(r'[^\x00-\x7F]+!?', ' ', text)
|
350 |
+
return text
|
351 |
+
|
352 |
+
# Function to split text into chunks
|
353 |
+
def get_text_chunks(text):
|
354 |
+
"""
|
355 |
+
Split the cleaned text into manageable chunks for further processing.
|
356 |
+
|
357 |
+
Args:
|
358 |
+
text (str): The cleaned text.
|
359 |
+
chunk_size (int): The size of each text chunk.
|
360 |
+
|
361 |
+
Returns:
|
362 |
+
list of Document: List of Document objects containing text chunks.
|
363 |
+
"""
|
364 |
+
|
365 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
366 |
+
chunk_size=1000,
|
367 |
+
chunk_overlap=200,
|
368 |
+
length_function=len
|
369 |
+
)
|
370 |
+
chunks = text_splitter.split_text(text)
|
371 |
+
return [Document(page_content=chunk) for chunk in chunks]
|
372 |
+
|
373 |
+
|
374 |
+
|
375 |
+
# Function to process YouTube videos
|
376 |
+
def load_video(url):
|
377 |
+
"""
|
378 |
+
Retrieve the transcript of a YouTube video, save it to a text file,
|
379 |
+
convert the text file to a PDF, and return the PDF filename.
|
380 |
+
|
381 |
+
Args:
|
382 |
+
url (str): The URL of the YouTube video.
|
383 |
+
|
384 |
+
Returns:
|
385 |
+
str: The filename of the generated PDF.
|
386 |
+
"""
|
387 |
+
video_id = url.split('v=')[-1]
|
388 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
389 |
+
transcript_text = ' '.join([t['text'] for t in transcript])
|
390 |
+
filename = f"{video_id}.txt"
|
391 |
+
with open(filename, 'w', encoding='utf-8') as f:
|
392 |
+
f.write(transcript_text)
|
393 |
+
pdf_filename = create_pdf(filename)
|
394 |
+
return pdf_filename
|
395 |
+
|
396 |
+
#Initialize the collection
|
397 |
+
def initialize_collection():
|
398 |
+
"""
|
399 |
+
Initialize the knowledge base by searching and transcribing YouTube videos
|
400 |
+
for a predefined set of queries, converting them to PDF, and adding them
|
401 |
+
to the vector database.
|
402 |
+
|
403 |
+
Returns:
|
404 |
+
bool: True if the initialization is successful.
|
405 |
+
"""
|
406 |
+
# Update queries if you want the assistant to have a different knowledge base and uncomment initialize_collection() after this function
|
407 |
+
|
408 |
+
queries = [
|
409 |
+
"Transfer Learning in Machine Learning",
|
410 |
+
"Object Detection and Recognition in Computer Vision",
|
411 |
+
"Sentiment Analysis in Natural Language Processing",
|
412 |
+
"Generative Adversarial Networks (GANs) in Deep Learning",
|
413 |
+
"Automatic Speech Recognition (ASR) Systems",
|
414 |
+
"Reinforcement Learning Applications",
|
415 |
+
"Image Segmentation Techniques in Computer Vision",
|
416 |
+
"Text Summarization Methods in NLP",
|
417 |
+
"Convolutional Neural Networks (CNNs) for Image Classification",
|
418 |
+
"Speech Synthesis and Text-to-Speech (TTS) Systems",
|
419 |
+
"Anomaly Detection in Machine Learning",
|
420 |
+
"Facial Recognition Technology and Ethics",
|
421 |
+
"Machine Translation and Language Models",
|
422 |
+
"Recurrent Neural Networks (RNNs) for Sequence Data",
|
423 |
+
"Speaker Diarization and Identification in Speech Processing",
|
424 |
+
"Applications of Natural Language Understanding (NLU)",
|
425 |
+
"Deep Reinforcement Learning for Game AI",
|
426 |
+
"Semantic Segmentation in Computer Vision",
|
427 |
+
"Dialogue Systems and Conversational AI",
|
428 |
+
"Ethical Implications of AI in Healthcare",
|
429 |
+
"Neural Machine Translation (NMT)",
|
430 |
+
"Time Series Forecasting with Machine Learning",
|
431 |
+
"Multi-modal Learning and Fusion",
|
432 |
+
"Named Entity Recognition (NER) in NLP",
|
433 |
+
"Human Pose Estimation in Computer Vision",
|
434 |
+
"Language Generation Models",
|
435 |
+
"Cognitive Robotics and AI Integration",
|
436 |
+
"Visual Question Answering (VQA) Systems",
|
437 |
+
"Privacy and Security in AI Applications",
|
438 |
+
"Graph Neural Networks (GNNs) for Structured Data",
|
439 |
+
"Introduction to Python programming",
|
440 |
+
"Python data types and variables",
|
441 |
+
"Control flow and loops in Python",
|
442 |
+
"Functions and modules in Python",
|
443 |
+
"File handling in Python",
|
444 |
+
"Object-oriented programming (OOP) in Python",
|
445 |
+
"Error handling and exceptions in Python",
|
446 |
+
"Python libraries for data analysis (e.g., Pandas, NumPy)",
|
447 |
+
"Web scraping with Python (e.g., using BeautifulSoup)",
|
448 |
+
"Creating GUI applications in Python (e.g., using Tkinter)",
|
449 |
+
"History of Formula 1 racing",
|
450 |
+
"Formula 1 car specifications and regulations",
|
451 |
+
"Famous Formula 1 drivers and their achievements",
|
452 |
+
"Formula 1 circuits around the world",
|
453 |
+
"How Formula 1 teams operate and strategize",
|
454 |
+
"Technological innovations in Formula 1",
|
455 |
+
"Role of aerodynamics in Formula 1 cars",
|
456 |
+
"Formula 1 race formats (qualifying, practice sessions, race day)",
|
457 |
+
"Evolution of safety measures in Formula 1",
|
458 |
+
"Economic impact of Formula 1 on host countries",
|
459 |
+
"Formula 1 engine specifications and development",
|
460 |
+
"Famous rivalries in Formula 1 history",
|
461 |
+
"Formula 1 team dynamics and hierarchy",
|
462 |
+
"How Formula 1 impacts automotive technology",
|
463 |
+
"The role of tire management in Formula 1 races",
|
464 |
+
"Key differences between Formula 1 and other racing series",
|
465 |
+
"The influence of sponsors in Formula 1",
|
466 |
+
"Formula 1 rules and regulations changes over the years",
|
467 |
+
"Notable controversies in Formula 1",
|
468 |
+
"The future of Formula 1 racing"
|
469 |
+
]
|
470 |
+
print(len(queries))
|
471 |
+
for query in queries:
|
472 |
+
print(query)
|
473 |
+
transcript_file = search_and_transcribe_videos(query)
|
474 |
+
print(transcript_file)
|
475 |
+
time.sleep(5)
|
476 |
+
|
477 |
+
pdf_filename = create_pdf(transcript_file)
|
478 |
+
time.sleep(10)
|
479 |
+
|
480 |
+
add_documents_to_db(pdf_filename)
|
481 |
+
|
482 |
+
return True
|
483 |
+
|
484 |
+
import tiktoken
|
485 |
+
|
486 |
+
def update_conversation_summary(summarized_conversation, new_interaction):
|
487 |
+
"""
|
488 |
+
Update the summary of a conversation by appending a new interaction.
|
489 |
+
|
490 |
+
Args:
|
491 |
+
summarized_conversation (str): The current summarized conversation.
|
492 |
+
new_interaction (dict): A dictionary containing 'question' and 'answer' keys.
|
493 |
+
|
494 |
+
Returns:
|
495 |
+
str: The updated summary of the conversation.
|
496 |
+
"""
|
497 |
+
|
498 |
+
new_summary = f"{summarized_conversation}\n- Q: {new_interaction['question']}\n A: {new_interaction['answer']}"
|
499 |
+
|
500 |
+
return new_summary
|
501 |
+
|
502 |
+
|
503 |
+
def is_long_task(task, max_tokens=1000):
|
504 |
+
"""
|
505 |
+
Determine if a given task exceeds the specified token limit.
|
506 |
+
|
507 |
+
Args:
|
508 |
+
task (str): The task to check.
|
509 |
+
max_tokens (int): The maximum number of tokens allowed.
|
510 |
+
|
511 |
+
Returns:
|
512 |
+
bool: True if the task exceeds the token limit, False otherwise.
|
513 |
+
"""
|
514 |
+
|
515 |
+
encoding = tiktoken.encoding_for_model(llm)
|
516 |
+
num_tokens = len(encoding.encode(task))
|
517 |
+
return num_tokens > max_tokens
|
518 |
+
|
519 |
+
def split_task(task):
|
520 |
+
"""
|
521 |
+
Split a long task into smaller subtasks for easier processing.
|
522 |
+
|
523 |
+
Args:
|
524 |
+
task (str): The task to split.
|
525 |
+
|
526 |
+
Returns:
|
527 |
+
list of str: A list of subtasks.
|
528 |
+
"""
|
529 |
+
|
530 |
+
prompt = f"""
|
531 |
+
The following task needs to be split into smaller subtasks:
|
532 |
+
|
533 |
+
{task}
|
534 |
+
|
535 |
+
Please divide this task into 2-4 subtasks. Each subtask should be a complete, standalone task.
|
536 |
+
Format your response as a Python list of strings, with each string being a subtask.
|
537 |
+
"""
|
538 |
+
|
539 |
+
response = llm.invoke(prompt)
|
540 |
+
subtasks = eval(response)
|
541 |
+
return subtasks
|
542 |
+
|
543 |
+
def combine_results(results):
|
544 |
+
"""
|
545 |
+
Combine the results from multiple subtasks into a single summary.
|
546 |
+
|
547 |
+
Args:
|
548 |
+
results (list of str): The results from subtasks.
|
549 |
+
|
550 |
+
Returns:
|
551 |
+
str: A concise summary of the combined results.
|
552 |
+
"""
|
553 |
+
|
554 |
+
combined = "Combined results from subtasks:\n\n"
|
555 |
+
for i, result in enumerate(results, 1):
|
556 |
+
combined += f"Subtask {i} result:\n{result}\n\n"
|
557 |
+
|
558 |
+
summary_prompt = f"""
|
559 |
+
Please provide a concise summary of the following combined results:
|
560 |
+
|
561 |
+
{combined}
|
562 |
+
|
563 |
+
Summarize the key points and overall conclusion.
|
564 |
+
"""
|
565 |
+
|
566 |
+
response = llm.invoke(summary_prompt)
|
567 |
+
return response
|
568 |
+
|
569 |
+
|
570 |
+
|
571 |
+
def process_user_input(user_input):
|
572 |
+
"""
|
573 |
+
Process user input by determining if it's a long task. If so, split it into subtasks,
|
574 |
+
process each subtask, and combine the results. Otherwise, process the input directly.
|
575 |
+
|
576 |
+
Args:
|
577 |
+
user_input (str): The user's input to process.
|
578 |
+
|
579 |
+
Returns:
|
580 |
+
str: The result after processing the user input.
|
581 |
+
"""
|
582 |
+
|
583 |
+
if is_long_task(user_input):
|
584 |
+
subtasks = split_task(user_input)
|
585 |
+
results = []
|
586 |
+
for subtask in subtasks:
|
587 |
+
result = run_agent(subtask)
|
588 |
+
results.append(result)
|
589 |
+
return combine_results(results)
|
590 |
+
else:
|
591 |
+
return run_agent(user_input)
|
592 |
+
|
593 |
+
# Uncomment the line below if you want to re-initialize the collection or initialize it with different topics
|
594 |
+
#initialize_collection()
|
595 |
+
|
596 |
+
def create_qa_chain():
|
597 |
+
"""
|
598 |
+
Create a question-answering chain using a retriever and a language model.
|
599 |
+
|
600 |
+
Returns:
|
601 |
+
RetrievalQA: The question-answering chain instance.
|
602 |
+
"""
|
603 |
+
|
604 |
+
retriever = vector_db.as_retriever()
|
605 |
+
qa_chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)
|
606 |
+
return qa_chain
|
607 |
+
|
608 |
+
def combine_summaries(summaries):
|
609 |
+
"""
|
610 |
+
Combine multiple summaries into a single summary.
|
611 |
+
|
612 |
+
Args:
|
613 |
+
summaries (list of str): The list of summaries to combine.
|
614 |
+
|
615 |
+
Returns:
|
616 |
+
str: The combined summary.
|
617 |
+
"""
|
618 |
+
|
619 |
+
combined_summary = " ".join(summaries)
|
620 |
+
return combined_summary
|
621 |
+
|
622 |
+
def split_text(text, max_length=1500):
|
623 |
+
"""
|
624 |
+
Split a long text into smaller chunks, ensuring chunks do not exceed the specified length.
|
625 |
+
|
626 |
+
Args:
|
627 |
+
text (str): The text to split.
|
628 |
+
max_length (int): The maximum length of each chunk.
|
629 |
+
|
630 |
+
Returns:
|
631 |
+
list of str: A list of text chunks.
|
632 |
+
"""
|
633 |
+
|
634 |
+
chunks = []
|
635 |
+
while len(text) > max_length:
|
636 |
+
chunk = text[:max_length]
|
637 |
+
# Find the last complete sentence within the chunk
|
638 |
+
last_period = chunk.rfind('. ')
|
639 |
+
if last_period != -1:
|
640 |
+
chunk = chunk[:last_period+1]
|
641 |
+
chunks.append(chunk)
|
642 |
+
text = text[len(chunk):].lstrip()
|
643 |
+
if text:
|
644 |
+
chunks.append(text)
|
645 |
+
return chunks
|
646 |
+
|
647 |
+
def process_large_text(transcript_text):
|
648 |
+
"""
|
649 |
+
Process a large text by splitting it into chunks, summarizing each chunk,
|
650 |
+
and then generating a final summary from the combined chunk summaries.
|
651 |
+
|
652 |
+
Args:
|
653 |
+
transcript_text (str): The large text to process.
|
654 |
+
|
655 |
+
Returns:
|
656 |
+
str: The final summary of the large text.
|
657 |
+
"""
|
658 |
+
|
659 |
+
# Step 1: Split the cleaned text into manageable chunks
|
660 |
+
chunks = split_text(transcript_text, max_length=1500)
|
661 |
+
|
662 |
+
# Step 2: Generate summaries for each chunk
|
663 |
+
chunk_summaries = [text_summarize(chunk) for chunk in chunks]
|
664 |
+
|
665 |
+
# Step 3: Combine the chunk summaries
|
666 |
+
combined_summary = combine_summaries(chunk_summaries)
|
667 |
+
|
668 |
+
# Step 4: Generate the final summary from combined summaries
|
669 |
+
final_summ = text_summarize(combined_summary)
|
670 |
+
|
671 |
+
return final_summ
|
672 |
+
|
673 |
+
# Initialize memory with k=5, so the memory object will store the most recent 5 messages or interactions in the conversation
|
674 |
+
memory = ConversationBufferWindowMemory(k=5)
|
675 |
+
|
676 |
+
# Define agent tools
|
677 |
+
@tool
|
678 |
+
def search_kb(query):
|
679 |
+
"""
|
680 |
+
Search the knowledge base for relevant documents based on a query and return a response.
|
681 |
+
|
682 |
+
Args:
|
683 |
+
query (str): The search query.
|
684 |
+
|
685 |
+
Returns:
|
686 |
+
str: The result from the QA chain based on the retrieved documents.
|
687 |
+
"""
|
688 |
+
|
689 |
+
retriever = vector_db.as_retriever()
|
690 |
+
docs = retriever.get_relevant_documents(query)
|
691 |
+
summaries = "\n\n".join([doc.page_content for doc in docs])
|
692 |
+
qa_chain = create_qa_chain()
|
693 |
+
llm_response = qa_chain({"query": query})
|
694 |
+
return llm_response["result"]
|
695 |
+
|
696 |
+
@tool
|
697 |
+
def process_video(url):
|
698 |
+
"""
|
699 |
+
Processes a YouTube video by extracting its transcript, summarizing it,
|
700 |
+
and adding the transcript to the knowledge base.
|
701 |
+
|
702 |
+
Args:
|
703 |
+
url (str): The URL of the YouTube video to process.
|
704 |
+
|
705 |
+
Returns:
|
706 |
+
str: The summary of the video.
|
707 |
+
"""
|
708 |
+
# video_id = url.split('v=')[-1]
|
709 |
+
# transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
710 |
+
# transcript_text = ' '.join([t['text'] for t in transcript])
|
711 |
+
|
712 |
+
video = download_video(url)
|
713 |
+
transcript_text = video_to_text(video)
|
714 |
+
|
715 |
+
# Clean the transcript text
|
716 |
+
cleaned_text = clean_text(transcript_text)
|
717 |
+
if len(cleaned_text) > 15000:
|
718 |
+
process_large_text(cleaned_text)
|
719 |
+
|
720 |
+
# Generate a summary for the user
|
721 |
+
summary = text_summarize(cleaned_text)
|
722 |
+
|
723 |
+
print(f"Added {len(summary)} chunks from YouTube video {url} to the collection.")
|
724 |
+
return summary
|
725 |
+
|
726 |
+
|
727 |
+
@tool
|
728 |
+
def new_search(query):
|
729 |
+
"""
|
730 |
+
Perform a new search on YouTube, transcribe videos, create a PDF from the transcript, add documents to the database, and search the knowledge base.
|
731 |
+
|
732 |
+
Args:
|
733 |
+
query (str): The search query.
|
734 |
+
|
735 |
+
Returns:
|
736 |
+
str: The path to the created PDF file.
|
737 |
+
"""
|
738 |
+
transcript = search_and_transcribe_videos(query)
|
739 |
+
time.sleep(10)
|
740 |
+
pdf_file = create_pdf(transcript)
|
741 |
+
time.sleep(10)
|
742 |
+
add_documents_to_db(pdf_file)
|
743 |
+
time.sleep(5)
|
744 |
+
search_kb(query)
|
745 |
+
return pdf_file
|
746 |
+
|
747 |
+
@tool
|
748 |
+
def process_pdf(pdf):
|
749 |
+
"""
|
750 |
+
Processes a PDF File by summarizing it,
|
751 |
+
and adding it to the knowledge base.
|
752 |
+
|
753 |
+
Args:
|
754 |
+
pdf (str): The path to the PDF file to process.
|
755 |
+
|
756 |
+
Returns:
|
757 |
+
str: The summary of the PDF.
|
758 |
+
"""
|
759 |
+
|
760 |
+
loader = PyPDFLoader(pdf)
|
761 |
+
docs = loader.load_and_split()
|
762 |
+
chain = load_summarize_chain(llm, chain_type="map_reduce")
|
763 |
+
summary = chain.run(docs)
|
764 |
+
|
765 |
+
return summary
|
766 |
+
|
767 |
+
|
768 |
+
|
769 |
+
# Define the agent tools
|
770 |
+
tools = [
|
771 |
+
Tool(
|
772 |
+
name="Search KB",
|
773 |
+
func=search_kb,
|
774 |
+
description="useful for when you need to answer questions about Machine Learning, Computer Vision and Natural Language Processing. The input to this tool should be a complete english sentence.",
|
775 |
+
),
|
776 |
+
Tool(
|
777 |
+
name="Search YouTube",
|
778 |
+
func=new_search,
|
779 |
+
description="useful for when the user asks you a question outside of Machine Learning, Computer Vision and Natural Language Processing. You use it to find new information about a topic not in the knowledge base. The input to this tool should be a complete english sentence.",
|
780 |
+
),
|
781 |
+
Tool(
|
782 |
+
name="Process Video",
|
783 |
+
func=process_video,
|
784 |
+
description="Useful for when the user wants to summarize or ask questions about a specific YouTube video. The input to this tool should be a YouTube URL.",
|
785 |
+
),
|
786 |
+
Tool(
|
787 |
+
name="Process PDF",
|
788 |
+
func=process_pdf,
|
789 |
+
description="Useful for when the user wants to summarize or ask questions about a specific PDF file. The input to this tool should be a PDF file path.",
|
790 |
+
)
|
791 |
+
]
|
792 |
+
|
793 |
+
|
794 |
+
|
795 |
+
# Define the agent prompt
|
796 |
+
prompt_template_string = """
|
797 |
+
You are an AI trained on Artificial Intelligence topics and Formula 1.
|
798 |
+
|
799 |
+
|
800 |
+
Answer the following questions as best you can, taking into account the context of the conversation.
|
801 |
+
You have access to the following tools:
|
802 |
+
|
803 |
+
{tools}
|
804 |
+
|
805 |
+
Use the following format:
|
806 |
+
|
807 |
+
Question: the input question you must answer
|
808 |
+
Thought: you should always think about what to do
|
809 |
+
Action: the action you should take, should be one of [{tool_names}]
|
810 |
+
Action Input: the input to the action
|
811 |
+
Observation: the result of the action
|
812 |
+
... (this Thought/Action/Action Input/Observation can repeat N times)
|
813 |
+
Thought: I now know the final answer
|
814 |
+
Final Answer: the final answer to the original input question
|
815 |
+
|
816 |
+
|
817 |
+
Example 1:
|
818 |
+
Question: What are dinosaurs?
|
819 |
+
Thought: I need to check the knowledge base for information on dinosaurs.
|
820 |
+
Action: Search Knowledge Base
|
821 |
+
Action Input: What are dinosaurs?
|
822 |
+
Observation: I don't have information on dinosaurs based on the provided context about machine learning and artificial intelligence.
|
823 |
+
Thought: I need to find new information about dinosaurs.
|
824 |
+
Action: Search YouTube
|
825 |
+
Action Input: Dinosaurs
|
826 |
+
Observation: Found relevant information and updated the knowledge base.
|
827 |
+
Thought: Now I can find information in the updated knowledge base.
|
828 |
+
Action: Search Knowledge Base
|
829 |
+
Action Input: What are dinosaurs?
|
830 |
+
Observation: [detailed information about dinosaurs]
|
831 |
+
Thought: I now know the final answer.
|
832 |
+
Final Answer: [final detailed answer about dinosaurs]
|
833 |
+
|
834 |
+
Example 2:
|
835 |
+
Question: Can you summarize this video? https://www.youtube.com/watch?v=dQw4w9WgXcQ
|
836 |
+
Thought: I need to extract the link to the video to get the summary.
|
837 |
+
Action: Process input to get link
|
838 |
+
Action Input: https://www.youtube.com/watch?v=dQw4w9WgXcQ
|
839 |
+
Observation: [summary of the video]
|
840 |
+
Thought: Now I can provide the summary of the video.
|
841 |
+
Final Answer: [summary of the video]
|
842 |
+
|
843 |
+
Example 3:
|
844 |
+
Question: Explain the content of this video https://www.youtube.com/watch?v=dQw4w9WgXcQ and how it relates to machine learning.
|
845 |
+
Thought: I need to extract the YouTube link from the input.
|
846 |
+
Action: Extract YouTube Link
|
847 |
+
Action Input: Explain the content of this video https://www.youtube.com/watch?v=dQw4w9WgXcQ and how it relates to machine learning.
|
848 |
+
Observation: Extracted YouTube link: https://www.youtube.com/watch?v=dQw4w9WgXcQ
|
849 |
+
Thought: I need to process the video to get the summary.
|
850 |
+
Action: Process Video
|
851 |
+
Action Input: https://www.youtube.com/watch?v=dQw4w9WgXcQ
|
852 |
+
Observation: [summary of the video]
|
853 |
+
Thought: Now I can relate the content to machine learning.
|
854 |
+
Final Answer: [explanation of how the video content relates to machine learning]
|
855 |
+
|
856 |
+
Example 4:
|
857 |
+
Question: Who are you?
|
858 |
+
Thought: I should explain that I'm a chatbot and how I can help.
|
859 |
+
Final Answer: I am a chatbot that can answer questions about machine learning and other related topics.
|
860 |
+
|
861 |
+
Example 5:
|
862 |
+
Question: What is your name?
|
863 |
+
Thought: I don't know.
|
864 |
+
Final Answer: I don't know the answer for that.
|
865 |
+
|
866 |
+
Question: {input}
|
867 |
+
{agent_scratchpad}"""
|
868 |
+
|
869 |
+
# Define the agent
|
870 |
+
prompt = PromptTemplate.from_template(prompt_template_string)
|
871 |
+
|
872 |
+
|
873 |
+
agent = create_react_agent(llm, tools, prompt)
|
874 |
+
agent_executor = AgentExecutor(agent=agent, tools=tools,handle_parsing_errors=True)
|
875 |
+
|
876 |
+
|
877 |
+
|
878 |
+
# Streamlit App Interface Design
|
879 |
+
def main():
|
880 |
+
|
881 |
+
# Initialize session state
|
882 |
+
if 'messages' not in st.session_state:
|
883 |
+
st.session_state.messages = []
|
884 |
+
if 'chat_history' not in st.session_state:
|
885 |
+
st.session_state.chat_history = []
|
886 |
+
if 'conversation_summary' not in st.session_state:
|
887 |
+
st.session_state.conversation_summary = ""
|
888 |
+
|
889 |
+
# Function to clear chat history
|
890 |
+
def clear_chat():
|
891 |
+
st.session_state.messages = []
|
892 |
+
|
893 |
+
st.title("AI Knowledge Base & Chat")
|
894 |
+
|
895 |
+
# Fixed description at the top
|
896 |
+
st.markdown("""
|
897 |
+
**Welcome to the AI Knowledge Base & Chat App!** π€π¬
|
898 |
+
|
899 |
+
This interactive application leverages a sophisticated AI model to provide in-depth information and insights across a diverse range of topics. Hereβs what you can explore:
|
900 |
+
|
901 |
+
- **Artificial Intelligence and Machine Learning** π
|
902 |
+
- **Computer Vision** ποΈ
|
903 |
+
- **Python Programming** π
|
904 |
+
- **Formula 1 Racing** ποΈ
|
905 |
+
|
906 |
+
With its extensive training on these topics, the AI is well-equipped to provide accurate, detailed, and relevant answers to your questions. Enjoy exploring a world of knowledge and get instant responses to your queries! πβ¨
|
907 |
+
In addition to answering your questions, you can:
|
908 |
+
|
909 |
+
Upload a PDF File π: Submit a PDF document to have it automatically summarized, giving you a concise overview of its contents without having to read through the entire file.
|
910 |
+
|
911 |
+
Provide a YouTube URL π₯: Enter a link to a YouTube video to receive a summary of its key points, allowing you to grasp the main ideas quickly.
|
912 |
+
""")
|
913 |
+
|
914 |
+
# Layout for additional inputs and chat
|
915 |
+
with st.sidebar:
|
916 |
+
st.header("Additional Inputs")
|
917 |
+
|
918 |
+
youtube_url = st.text_input("Enter YouTube URL:")
|
919 |
+
if st.button("Process YouTube Video"):
|
920 |
+
with st.spinner("Processing YouTube video..."):
|
921 |
+
summary = process_video(youtube_url)
|
922 |
+
st.write(summary)
|
923 |
+
st.session_state.messages.append({"role": "assistant", "content": f"I've processed the YouTube video. Here's a summary:\n\n{summary}"})
|
924 |
+
st.experimental_rerun()
|
925 |
+
|
926 |
+
uploaded_pdf = st.file_uploader("Upload a PDF file", type="pdf")
|
927 |
+
if st.button("Process PDF"):
|
928 |
+
with st.spinner("Processing PDF..."):
|
929 |
+
texts = extract_text_from_pdf(uploaded_pdf)
|
930 |
+
pdf_summary = text_summarize(texts)
|
931 |
+
st.write(pdf_summary)
|
932 |
+
st.session_state.messages.append({"role": "assistant", "content": f"PDF processed and added to knowledge base. Here's a summary:\n\n{pdf_summary}"})
|
933 |
+
st.experimental_rerun()
|
934 |
+
|
935 |
+
st.header("Chat")
|
936 |
+
|
937 |
+
# Display chat history
|
938 |
+
for message in st.session_state.messages:
|
939 |
+
role = message["role"]
|
940 |
+
content = message["content"]
|
941 |
+
if role == "user":
|
942 |
+
with st.chat_message(role):
|
943 |
+
st.markdown(content)
|
944 |
+
else:
|
945 |
+
with st.chat_message(role):
|
946 |
+
st.markdown(content)
|
947 |
+
|
948 |
+
user_input = st.chat_input("Ask a question")
|
949 |
+
|
950 |
+
# Button to clear chat
|
951 |
+
if st.button('Clear Chat'):
|
952 |
+
clear_chat()
|
953 |
+
|
954 |
+
if user_input:
|
955 |
+
# Display user message
|
956 |
+
with st.chat_message("user"):
|
957 |
+
st.write(user_input)
|
958 |
+
|
959 |
+
# Get AI response
|
960 |
+
with st.chat_message("assistant"):
|
961 |
+
response = agent_executor.invoke({"input": user_input})
|
962 |
+
st.write(response['output'])
|
963 |
+
st.session_state.messages.append({"role": "assistant", "content": response['output']})
|
964 |
+
|
965 |
+
if __name__ == "__main__":
|
966 |
+
main()
|
|