File size: 7,364 Bytes
b35978f
 
 
 
 
 
 
 
 
 
6f8b104
b35978f
 
43e70c6
b35978f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43e70c6
 
 
 
 
 
 
 
 
 
 
 
b35978f
43e70c6
 
 
 
 
 
 
 
 
 
 
b35978f
43e70c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e71080
 
 
6f8b104
 
43e70c6
 
 
b35978f
 
 
 
43e70c6
b35978f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43e70c6
6f8b104
 
b35978f
43e70c6
 
 
 
 
b35978f
43e70c6
 
 
 
 
 
 
2e71080
43e70c6
6a9ed56
43e70c6
b35978f
2e71080
43e70c6
 
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
from __future__ import absolute_import
from __future__ import division, print_function, unicode_literals
import openai
from sumy.parsers.html import HtmlParser
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lsa import LsaSummarizer as Summarizer
from sumy.nlp.stemmers import Stemmer
from sumy.utils import get_stop_words
import gradio as gr



def maker(api_key, mode, input_text,detail,Spend_input_data_API_key_is_not_safed): 
    
    def cut_in_half(Text):
        list= Text.split('.')
        x=0
        for y in list:
            list[x] = str(list[x])+ "."
            x=x+1
        lenght = round(len(list)/2)
        x = 0
        Basis1 = ""
        Basis2 = ""
        while x != lenght:
            Basis1 = Basis1 + str(list[x])
            x =x+1
        
        while x  != lenght*2-1 :
            Basis2 = Basis2 + str(list[x])
            x =x+1
            
        Text = [Basis1, Basis2]
        return Text 
    def study_notes(Text):
        lenght = len(Text)
        if lenght > 20000:
            print("Over 20000 symboles. To much!")
        x = 0
        if lenght > 2200:
            Text = cut_in_half(Text)
            lenght = len(Text[0])
            x=1
            if lenght > 4000:
                Text1 = cut_in_half(Text[0])
                Text2 = cut_in_half(Text[1])
                Text = Text1 + Text2 
                x=3
        else:
            Text = [Text]
        return Text, x 

    def flashcards_maker(Text,x):  
        openai.api_key = api_key     
        response = openai.Completion.create(
        model="text-davinci-003",
        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",
        temperature=0.7,
        max_tokens=600,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
        )
        response1 = str(response["choices"][0]["text"]).replace("\n",":>")
        return response1

        #response1 = response1.replace(":>:>", "\n ")
    def flashcards_catorgizer(listsummery, response1,x):
        response1 = response1.split(":>:>")
        openai.api_key = api_key
        for y in response1:
            response = openai.Completion.create(
                model="text-davinci-003",
                prompt="Please give the main concept and the topic and the keywords to the flashcards, \"" + y + "\".\n\n",
                temperature=0.7,
                max_tokens=256,
                top_p=1,
                frequency_penalty=0,
                presence_penalty=0
            )
            
            response = (str(response["choices"][0]["text"]).replace("\n\n", "\n"))
            response = response.split("\n")
            
            listsummery = listsummery + response[0] + "\n" + response[1] +"\n   " + response[2]+ "\n      " + response[3]+ "\n" + "         " + y + "\n"
        
        return listsummery
    def summerizer(Text, detail):
        # Retrieve the input from the input box
        LANGUAGE = "english"
        SENTENCES_COUNT = round(len(Text)/(75*detail))
        
        if __name__ == "__main__":
            parser = PlaintextParser.from_string(Text, Tokenizer(LANGUAGE))
            stemmer = Stemmer(LANGUAGE)

            summarizer = Summarizer(stemmer)
            summarizer.stop_words = get_stop_words(LANGUAGE)
            Text = ""
            for sentence in summarizer(parser.document, SENTENCES_COUNT):
                Text = Text + str(sentence)
            
            return Text
    def concept_card_maker(api_key, input_text,detail,Spend_input_data_API_key_is_not_safed):
        concept_name =input_text
        openai.api_key = api_key
        response = openai.Completion.create(
        model="text-davinci-003",
        prompt="Describe \""+ concept_name + "\" as a concept with the point: name, concepts under it(listed in a list split by a comma), which are the bigger topics above it(listed in a list split by a comma), explanation, formula with a variable explanation if existed, use case, example.\n\nName:",
        temperature=0.7,
        max_tokens=800,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
        )
        
        response = str(response["choices"][0]["text"])
        response = (response).replace("Formula (if existed): N/A\n\n", "")
        response = (response).replace("Formula (if existed): N/A\n", "")
        response = (response).replace("Formula: N/A\n", "")
        response = (response).replace("\n\n", "#\t\t")
        response = (response).replace("\n-", ";\t\t-")
        response = (response).replace("\n", "#\t\t")
        response = (response).replace(";", "\n")
        response = (response).replace("#", "\n\n")

        return concept_name + "\tConcept"+"\n\t\tName:" +response    
    
    Text = input_text
    if detail == "No summersiation":
        detail = 1
    elif detail == "low summersation":
        detail = 2
    elif detail == "medium summersation":
        detail = 6
    elif detail == "high summersation":
        detail = 12

    if mode == "flashcards with topics":
        catorgizer = 1
    if mode == "flashcards":
        catorgizer = 0
    if mode == "concepts cards":
        out = concept_card_maker(api_key, input_text,detail,Spend_input_data_API_key_is_not_safed)
            
        if Spend_input_data_API_key_is_not_safed == True:
            with open('\concept_data.txt', 'w', newline='') as f: 
                f.write([input_text,out,"concept"])
            
        return out

    if  detail != 1:
        Text = summerizer(Text, detail)
    
    Text, x = study_notes(Text)
    

    while x != -1:
        response1 = flashcards_maker(Text,x)
        listsummery = ""
        if catorgizer == 1:
            listsummery = flashcards_catorgizer(listsummery,response1,x)
            
        else:
            response1 = response1.replace(":>:>", "\n")
            listsummery =response1
            
        x = x-1

    listsummery = str(listsummery).replace('\n '," \n")
    out = f"{listsummery}"
    if Spend_input_data_API_key_is_not_safed == True:
        with open('\\flashcard_data.csv', 'w', newline='') as f:
            f.write([input_text,out])
    
    return out
  




iface =gr.Interface(fn = maker, allow_flagging= "never", inputs=
    [
    gr.Textbox(),
    gr.Dropdown(["flashcards", "concepts cards", "flashcards with topics"]),
    gr.Textbox( label = "Input Text", lines=10, placeholder="Enter your text here..."),
    gr.Dropdown(["No summersiation", "low summersation", "medium summersation", "high summersation"]),
    gr.Checkbox(value = True)
    
    ],
    outputs = "text", title = "Study cards maker", description= "This tool uses openAI GPT-3 to make study cards for you. This include flashcards, super usefull for Remnotes(reday to copy past). Please use yoru Openai api key. Which you get by sining up at https://openai.com/api/ "
)


if __name__ == "__main__":
    iface.launch()