Team Ai
Apppublic

Kaengbold/Study_Notes-Flashcard_maker

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
app.py197 linesDownload Raw Back to root
1from __future__ import absolute_import2from __future__ import division, print_function, unicode_literals3import openai4from sumy.parsers.html import HtmlParser5from sumy.parsers.plaintext import PlaintextParser6from sumy.nlp.tokenizers import Tokenizer7from sumy.summarizers.lsa import LsaSummarizer as Summarizer8from sumy.nlp.stemmers import Stemmer9from sumy.utils import get_stop_words10import gradio as gr11 12 13 14def maker(api_key, mode, input_text,detail,Spend_input_data_API_key_is_not_safed): 15    16    def cut_in_half(Text):17        list= Text.split('.')18        x=019        for y in list:20            list[x] = str(list[x])+ "."21            x=x+122        lenght = round(len(list)/2)23        x = 024        Basis1 = ""25        Basis2 = ""26        while x != lenght:27            Basis1 = Basis1 + str(list[x])28            x =x+129        30        while x  != lenght*2-1 :31            Basis2 = Basis2 + str(list[x])32            x =x+133            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 = 041        if lenght > 2200:42            Text = cut_in_half(Text)43            lenght = len(Text[0])44            x=145            if lenght > 4000:46                Text1 = cut_in_half(Text[0])47                Text2 = cut_in_half(Text[1])48                Text = Text1 + Text2 49                x=350        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=064        )65        response1 = str(response["choices"][0]["text"]).replace("\n",":>")66        return response167 68        #response1 = response1.replace(":>:>", "\n ")69    def flashcards_catorgizer(listsummery, response1,x):70        response1 = response1.split(":>:>")71        openai.api_key = api_key72        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=081            )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 listsummery89    def summerizer(Text, detail):90        # Retrieve the input from the input box91        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 Text105    def concept_card_maker(api_key, input_text,detail,Spend_input_data_API_key_is_not_safed):106        concept_name =input_text107        openai.api_key = api_key108        response = openai.Completion.create(109        model="text-davinci-003",110        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:",111        temperature=0.7,112        max_tokens=800,113        top_p=1,114        frequency_penalty=0,115        presence_penalty=0116        )117        118        response = str(response["choices"][0]["text"])119        response = (response).replace("Formula (if existed): N/A\n\n", "")120        response = (response).replace("Formula (if existed): N/A\n", "")121        response = (response).replace("Formula: N/A\n", "")122        response = (response).replace("\n\n", "#\t\t")123        response = (response).replace("\n-", ";\t\t-")124        response = (response).replace("\n", "#\t\t")125        response = (response).replace(";", "\n")126        response = (response).replace("#", "\n\n")127 128        return concept_name + "\tConcept"+"\n\t\tName:" +response    129    130    Text = input_text131    if detail == "No summersiation":132        detail = 1133    elif detail == "low summersation":134        detail = 2135    elif detail == "medium summersation":136        detail = 6137    elif detail == "high summersation":138        detail = 12139 140    if mode == "flashcards with topics":141        catorgizer = 1142    if mode == "flashcards":143        catorgizer = 0144    if mode == "concepts cards":145        out = concept_card_maker(api_key, input_text,detail,Spend_input_data_API_key_is_not_safed)146            147        if Spend_input_data_API_key_is_not_safed == True:148            with open('\concept_data.txt', 'w', newline='') as f: 149                f.write([input_text,out,"concept"])150            151        return out152 153    if  detail != 1:154        Text = summerizer(Text, detail)155    156    Text, x = study_notes(Text)157    158 159    while x != -1:160        response1 = flashcards_maker(Text,x)161        listsummery = ""162        if catorgizer == 1:163            listsummery = flashcards_catorgizer(listsummery,response1,x)164            165        else:166            response1 = response1.replace(":>:>", "\n")167            listsummery =response1168            169        x = x-1170 171    listsummery = str(listsummery).replace('\n '," \n")172    out = f"{listsummery}"173    if Spend_input_data_API_key_is_not_safed == True:174        with open('\\flashcard_data.csv', 'w', newline='') as f:175            f.write([input_text,out])176    177    return out178  179 180 181 182 183iface =gr.Interface(fn = maker, allow_flagging= "never", inputs=184    [185    gr.Textbox(),186    gr.Dropdown(["flashcards", "concepts cards", "flashcards with topics"]),187    gr.Textbox( label = "Input Text", lines=10, placeholder="Enter your text here..."),188    gr.Dropdown(["No summersiation", "low summersation", "medium summersation", "high summersation"]),189    gr.Checkbox(value = True)190    191    ],192    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/ "193)194 195 196if __name__ == "__main__":197    iface.launch()