Spaces:
Runtime error
Runtime error
Upload app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135from __future__ import absolute_importfrom __future__ import division, print_function, unicode_literalsimport flet as ftimport openaifrom sumy.parsers.html import HtmlParserfrom sumy.parsers.plaintext import PlaintextParserfrom sumy.nlp.tokenizers import Tokenizerfrom sumy.summarizers.lsa import LsaSummarizer as Summarizerfrom sumy.nlp.stemmers import Stemmerfrom sumy.utils import get_stop_wordsimport 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()app.pyAdded+135