Kaengbold's picture
Update app.py
6a9ed56
raw
history blame contribute delete
No virus
7.36 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 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()