-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTextConverter.py
138 lines (114 loc) · 6.12 KB
/
TextConverter.py
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
135
136
137
138
import os
import openai
from dotenv import load_dotenv, find_dotenv
from llama_index import VectorStoreIndex, SimpleDirectoryReader
import pyperclip
import config
load_dotenv(find_dotenv())
openai.api_key = os.environ['OPENAI_API_KEY']
MODEL = "gpt-4"
def waitAndReturnNewText():
clipboard = pyperclip.waitForNewPaste()
return clipboard
def translateText(text, language):
response = openai.ChatCompletion.create(
model=MODEL,
messages=[
{"role": "assistant", "content": f"You are a translator for someone only know {language} (try to translate and keep the tone and the meaning closest)."},
{"role": "user", "content": f"Translate the following text into {language} and recognize the language: " + text},
],
temperature=0
)
return response['choices'][0]['message']['content']
def getTitleFromText(text, language):
response = openai.ChatCompletion.create(
model=MODEL,
# decide which system, assistant to use.
# input from user data, yo uask to summarize, it will put assistant as "you are a summarizer..."
messages=[
{"role": "system", "content": f"You generate one very short title in {language} less than 7 words from given texts with a in the middle of the title"},
{"role": "assistant", "content": f"You are someone that generate one title in {language} (the title should be about the text and creative)."},
{"role": "user", "content": "Given the following text, generate one title: " + text},
],
temperature=0
)
return response['choices'][0]['message']['content']
def generateSummaryFromText(text, minimumWords, maximumWords, language):
print(language)
response = openai.ChatCompletion.create(
model=MODEL,
# decide which system, assistant to use.
# input from user data, yo uask to summarize, it will put assistant as "you are a summarizer..."
messages=[
{"role": "system", "content": f"You are a summary writer in {language} for a very busy business man so you need to be short, condense, and quick in form of bullet points."},
{"role": "assistant", "content": f"You are someone that summarizes information in {language} on a given topic that user want to know about, make it short and condese."},
{"role": "user", "content": "Summarize the following information in " + str(minimumWords) + " to " + str(maximumWords) + " words: " + text},
],
temperature=0
)
return response['choices'][0]['message']['content']
def generateQuizFromText(text, numOfQuestions, language):
response = openai.ChatCompletion.create(
model=MODEL,
#decide which system, assistant to use.
messages=[
{"role": "assistant", "content": f"You are someone that creates questions in {language} on a given topic for test user's knowledge about a given text. Question must be about the text, ask about main topic or key parts or ideas of the text"},
{"role": "user", "content": "Create " + str(numOfQuestions) + " questions based off of the following text: " + text},
],
temperature=0
)
return response['choices'][0]['message']['content']
def getMultipleChoiceQuiz(prompt, language, num = 5):
response = openai.ChatCompletion.create(
model=MODEL,
messages=[
{"role": "system", "content": f"You are a very helpful quiz maker in {language} with this exact prompt: each line less than 100 characters of a question with 4 alternatives (1 right, 3 wrong) about {str(prompt)} formatted like this: first line: question, next four lines: alternatives. correct marked with '*' at the end of line. label alternatives 'a.'-'d.' and question '<num>.', try to make a quiz that truely test user's knowledge on the given text"},
{"role": "assistant", "content": f"generate {str(num)} questions in {language} with 4 alternatives (1 right, 3 wrong) about {str(prompt)} formatted like this: first line: question, next four lines: alternatives. correct marked with '*' at the end of line. label alternatives 'a.'-'d.' and question '<num>.'"},
{"role": "user", "content": "Make a" + str(num) + " question quiz about " + prompt},
],
temperature=0.2
)
return(response['choices'][0]['message']['content'])
def generateResponseFromFile(file, query):
# Load data from a file
documents = SimpleDirectoryReader(input_files=[file]).load_data()
# Create an index from the loaded documents
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
res = query_engine.query(query)
return res
def getResponseLengthFromText(text):
length = len(text)
if length < 50:
return length
if length < 1000:
return length // 5
return 200;
def translateAudio(audioFile, language):
audio_file = open(audioFile, "rb")
transcript = openai.Audio.translate("whisper-1", audio_file)
return transcript.text
def sendGptRequest(prompt, context, language, memory = None):
if not memory:
response = openai.ChatCompletion.create(
model=MODEL,
messages=[
{"role": "system", "content": "You are a an assistant that helps user requests based on a given context. Decide what is the user's struggle or request and try to help as much as you can"},
{"role": "assistant", "content": "You are given the following context:" + context},
{"role": "user", "content": prompt},
],
temperature=0.2,
# memory = memory
)
else:
response = openai.ChatCompletion.create(
model=MODEL,
messages=[
{"role": "system", "content": "You are a an assistant that helps user requests based on a given context. Decide what is the user's struggle or request and try to help as much as you can"},
{"role": "assistant", "content": "You are given the following context:" + context},
{"role": "user", "content": prompt},
],
temperature=0.2,
memory = memory
)
return(response['choices'][0]['message']['content'])