-
Notifications
You must be signed in to change notification settings - Fork 8
/
achademio.py
183 lines (143 loc) · 7.18 KB
/
achademio.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import streamlit as st
import openai
from redlines import Redlines
#openai.api_key = open("API_key", "r").read()
openai.api_key = st.secrets["API_KEY"]
def send_click():
if st.session_state.user != '':
prompt = st.session_state.user
# Define the bot role:
bot_role = "You are AChatdemio, a bot that helps young researchers to write better research papers. You respond in a concise and academic style. Write short sentences, so that they are clear."
# Define the ChatGPT model
output = openai.ChatCompletion.create(
# Define the model
model="gpt-4o-2024-08-06",
# Define the prompt - add the context
messages = [
{'role':'system', 'content':"{}".format(bot_role)},
{'role':'user', 'content':"Rewrite the following: {}".format(prompt)}
],
# Set the temperature to 0 to get the most stable results
temperature = 0,
)
response = output['choices'][0].message.content
st.write(response)
def academic():
if st.session_state.user != '':
prompt = st.session_state.user
# Define the bot role:
bot_role = "You are AChatdemio, a bot that helps young researchers to write better research papers. You respond in a clear, concise and academic style."
# Define the ChatGPT model
output = openai.ChatCompletion.create(
# Define the model
model="gpt-4o-2024-08-06",
# Define the prompt - add the context
messages = [
{'role':'system', 'content':"{}".format(bot_role)},
{'role':'user', 'content':"Rewrite the following text, delimited by triple backticks, in academic style: ```{}```".format(prompt)}
],
# Set the temperature to 0 to get the most stable results
temperature = 0,
)
response = output['choices'][0].message.content
st.subheader("Text with corrections")
diff = Redlines(prompt,response)
st.markdown(diff.output_markdown, unsafe_allow_html=True)
st.subheader("Rewritten text")
st.write(response)
def bullet():
if st.session_state.user != '':
prompt = st.session_state.user
# Define the bot role:
bot_role = "You are AChatdemio, a bot that helps young researchers to write better research papers. You respond in a clear, concise and academic style."
# Define the ChatGPT model
output = openai.ChatCompletion.create(
# Define the model
model="gpt-4o-2024-08-06",
# Define the prompt - add the context
messages = [
{'role':'system', 'content':"{}".format(bot_role)},
{'role':'user', 'content':"Provide one paragraph of text from the following bullet point list, delimited by triple backticks: ```{}```".format(prompt)}
],
# Set the temperature to 0 to get the most stable results
temperature = 0,
)
response = output['choices'][0].message.content
st.write(response)
def slides():
if st.session_state.user != '':
prompt = st.session_state.user
# Define the bot role:
bot_role = "You are AChatdemio, a bot that helps young researchers to write better research papers. You respond in a clear, concise and academic style."
# Define the ChatGPT model
output = openai.ChatCompletion.create(
# Define the model
model="gpt-4o-2024-08-06",
# Define the prompt - add the context
messages = [
{'role':'system', 'content':"{}".format(bot_role)},
{'role':'user', 'content':"Prepare text for powerpoint slides in form of bullet points from the following text, delimited by triple backticks. The bullet points should summarize the provided texts in a few sentences. The sentences should be short and summarization of the provided text. Avoid using full sentences when possible. End each sentence with a dot. The text: ```{}```".format(prompt)}
],
# Set the temperature to 0 to get the most stable results
temperature = 0,
)
response = output['choices'][0].message.content
st.write(response)
def proofreading():
if st.session_state.user != '':
prompt = st.session_state.user
# Define the bot role:
bot_role = "You are AChatdemio, a bot that helps young researchers to write better research papers. You respond in a clear, concise and academic style."
# Define the ChatGPT model
output = openai.ChatCompletion.create(
# Define the model
model="gpt-4o-2024-08-06",
# Define the prompt - add the context
messages = [
{'role':'system', 'content':"{}".format(bot_role)},
{'role':'user', 'content':"Find errors in the following text, delimited by triple backticks: ```{}```. Only ouput the errors and suggest how they should be corrected.".format(prompt)}
],
# Set the temperature to 0 to get the most stable results
temperature = 0,
)
response = output['choices'][0].message.content
st.write(response)
def own_prompt():
if st.session_state.user != '':
prompt = st.session_state.user
user_defined_prompt = st.session_state.user_prompt
# Define the bot role:
bot_role = "You are AChatdemio, a bot that helps young researchers to write better research papers."
# Define the ChatGPT model
output = openai.ChatCompletion.create(
# Define the model
model="gpt-4o-2024-08-06",
# Define the prompt - add the context
messages = [
{'role':'system', 'content':"{}".format(bot_role)},
{'role':'user', 'content':"{}: ```{}```".format(user_defined_prompt,prompt)}
],
# Set the temperature to 0 to get the most stable results
temperature = 0,
)
response = output['choices'][0].message.content
st.write(response)
st.title(":blue[Achademio] ☕")
st.write("# Improve the style of your research papers with AI")
# Create another two Streamlit widgets st.text_input() and st.button() to facilitate the query interface to users.
st.text_area("Provide your text:", key="user")
option = st.selectbox("Select what would you like the Achademio bot to do:", ('Rewrite in academic style', 'Write a paragraph from my bullet point list', 'Summarize text in bullet points for slides', 'Proofread my text and output errors', 'Define your own prompt'))
if 'Rewrite in academic style' in option:
academic()
elif 'Write a paragraph from my bullet point list' in option:
bullet()
elif 'Summarize text in bullet points for slides' in option:
slides()
elif 'Proofread my text and output errors' in option:
proofreading()
elif 'Define your own prompt' in option:
if st.session_state.get("user_prompt", None) != None:
user_defined_prompt = st.session_state.user_prompt
st.write(f"Previous prompt: {user_defined_prompt}")
st.text_area("Provide your prompt:", key="user_prompt")
own_prompt()