Kaengbold commited on
Commit
b35978f
·
1 Parent(s): b2e22b7

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -0
app.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import absolute_import
2
+ from __future__ import division, print_function, unicode_literals
3
+ import flet as ft
4
+ import openai
5
+ from sumy.parsers.html import HtmlParser
6
+ from sumy.parsers.plaintext import PlaintextParser
7
+ from sumy.nlp.tokenizers import Tokenizer
8
+ from sumy.summarizers.lsa import LsaSummarizer as Summarizer
9
+ from sumy.nlp.stemmers import Stemmer
10
+ from sumy.utils import get_stop_words
11
+ import gradio as gr
12
+
13
+
14
+ def greet(api_key, name):
15
+
16
+ def cut_in_half(Text):
17
+ list= Text.split('.')
18
+ x=0
19
+ for y in list:
20
+ list[x] = str(list[x])+ "."
21
+ x=x+1
22
+ lenght = round(len(list)/2)
23
+ x = 0
24
+ Basis1 = ""
25
+ Basis2 = ""
26
+ while x != lenght:
27
+ Basis1 = Basis1 + str(list[x])
28
+ x =x+1
29
+
30
+ while x != lenght*2-1 :
31
+ Basis2 = Basis2 + str(list[x])
32
+ x =x+1
33
+
34
+ Text = [Basis1, Basis2]
35
+ return Text
36
+ def study_notes(Text):
37
+ lenght = len(Text)
38
+ if lenght > 20000:
39
+ print("Over 20000 symboles. To much!")
40
+ x = 0
41
+ if lenght > 2200:
42
+ Text = cut_in_half(Text)
43
+ lenght = len(Text[0])
44
+ x=1
45
+ if lenght > 4000:
46
+ Text1 = cut_in_half(Text[0])
47
+ Text2 = cut_in_half(Text[1])
48
+ Text = Text1 + Text2
49
+ x=3
50
+ else:
51
+ Text = [Text]
52
+ return Text, x
53
+
54
+ def flashcards_maker(Text,x):
55
+ openai.api_key = api_key
56
+ response = openai.Completion.create(
57
+ model="text-davinci-003",
58
+ prompt="Please summarize the all important points from the following text and create a set of flashcards using Remnote's formatting. Each flashcard should have a question on the front and the corresponding short answer on the back. You can include additional information, such as definitions and examples, when possible. When you are finished, please provide the full set of flashcards in Remnote's format:\n\n" + Text[x] +"\n\nFlashcard \nQ: \nA: \n\n",
59
+ temperature=0.7,
60
+ max_tokens=600,
61
+ top_p=1,
62
+ frequency_penalty=0,
63
+ presence_penalty=0
64
+ )
65
+ response1 = str(response["choices"][0]["text"]).replace("\n",":>")
66
+ return response1
67
+
68
+ #response1 = response1.replace(":>:>", "\n ")
69
+ def flashcards_catorgizer(listsummery, response1,x):
70
+ response1 = response1.split(":>:>")
71
+ openai.api_key = api_key
72
+ for y in response1:
73
+ response = openai.Completion.create(
74
+ model="text-davinci-003",
75
+ prompt="Please give the main concept and the topic and the keywords to the flashcards, \"" + y + "\".\n\n",
76
+ temperature=0.7,
77
+ max_tokens=256,
78
+ top_p=1,
79
+ frequency_penalty=0,
80
+ presence_penalty=0
81
+ )
82
+
83
+ response = (str(response["choices"][0]["text"]).replace("\n\n", "\n"))
84
+ response = response.split("\n")
85
+
86
+ listsummery = listsummery + response[0] + "\n" + response[1] +"\n " + response[2]+ "\n " + response[3]+ "\n" + " " + y + "\n"
87
+
88
+ return listsummery
89
+ def summerizer(Text, detail):
90
+ # Retrieve the input from the input box
91
+ LANGUAGE = "english"
92
+ SENTENCES_COUNT = round(len(Text)/(75*detail))
93
+
94
+ if __name__ == "__main__":
95
+ parser = PlaintextParser.from_string(Text, Tokenizer(LANGUAGE))
96
+ stemmer = Stemmer(LANGUAGE)
97
+
98
+ summarizer = Summarizer(stemmer)
99
+ summarizer.stop_words = get_stop_words(LANGUAGE)
100
+ Text = ""
101
+ for sentence in summarizer(parser.document, SENTENCES_COUNT):
102
+ Text = Text + str(sentence)
103
+
104
+ return Text
105
+
106
+
107
+ Text = name
108
+ detail = 1
109
+
110
+ if detail != 1:
111
+ Text = summerizer(Text, detail)
112
+
113
+ Text, x = study_notes(Text)
114
+ catorgizer = 2
115
+
116
+ while x != -1:
117
+ response1 = flashcards_maker(Text,x)
118
+ listsummery = ""
119
+ if catorgizer == 1:
120
+ listsummery = flashcards_catorgizer(listsummery,response1,x)
121
+
122
+ else:
123
+ response1 = response1.replace(":>:>", "\n")
124
+ listsummery =response1
125
+
126
+ x = x-1
127
+
128
+ listsummery = str(listsummery).replace('\n '," \n")
129
+ out = f"{listsummery}"
130
+ return out
131
+
132
+
133
+
134
+ iface = gr.Interface(fn=greet, inputs=["text","text"], outputs="text")
135
+ iface.launch()