Kaengbold's picture
Update app.py
2a9e82a
raw
history blame
4.55 kB
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()