File size: 4,549 Bytes
b35978f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 greet(api_key, name): 
    
    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
        
    
    Text = name
    detail = 1
    
    if  detail != 1:
        Text = summerizer(Text, detail)
    
    Text, x = study_notes(Text)
    catorgizer = 2

    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}"
    return out
    


iface = gr.Interface(fn=greet, inputs=["text","text"], outputs="text")
iface.launch()