Pal-Lab commited on
Commit
ef45e01
·
1 Parent(s): 5d3f79d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +283 -0
app.py ADDED
@@ -0,0 +1,283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+ import openai
4
+
5
+ req1 = "可能性のある仮説や因果モデルを提案して下さい"
6
+ req2 = "疑問を明らかにするために最適な研究デザインを簡潔に提案して下さい"
7
+ req3 = f'''研究計画書の草案を作成して下さい。構成は
8
+ 概要:
9
+ 研究の目的:
10
+ 研究の背景:
11
+ 研究デザイン:
12
+ 参加者選択基準:
13
+ 参加者登録方法:
14
+ 治療計画:
15
+ 評価項目、評価スケジュール:
16
+ データ収集:
17
+ エンドポイント:
18
+ 統計解析:
19
+ 倫理的事項:
20
+ 研究結果の発表:
21
+ の順番で作成して下さい。'''
22
+
23
+ # openai.api_key = openai_api_key
24
+
25
+
26
+ def generate_clinical_query_solution(text0, text1,text2, text3, text4):
27
+
28
+ if text3 != "": #自由記載の相談があればそちらを優先する
29
+ text2 = text3
30
+ elif text2 == "仮説形成":
31
+ text2 = req1
32
+ elif text2 == "研究デザイン":
33
+ text2 = req2
34
+ elif text2 == "研究計画書":
35
+ text2 = req3
36
+
37
+ query = f'''あなたは臨床研究の専門家です。以下の臨床疑問や記述について、{text2}。必ず{text4}でお答えください。Please think step by step.;
38
+ 臨床疑問:{text1}'''
39
+
40
+ openai.api_key = text0
41
+
42
+ response = openai.ChatCompletion.create(
43
+ model="gpt-3.5-turbo",
44
+ temperature=0.7,
45
+ max_tokens=1024,
46
+ messages=[
47
+ {"role": "user", "content": query},
48
+ ],
49
+ )
50
+ return response.choices[0]["message"]["content"].strip()
51
+
52
+
53
+ def generate_intro_draft(text0, free_text, text6,text7, text8, text9, text4): #clinical_question, req, ori_req, lang
54
+
55
+ if free_text != "":
56
+ query = f'''あなたは臨床研究の専門家です。以下に示すこの研究の背景を踏まえて、学術論文のintroduction sectionを600文字程度で作成して下さい。必ず{text4}の文章を作成してください。Please think step by step.;
57
+ {free_text}'''
58
+ else:
59
+ query = f'''あなたは臨床研究の専門家です。以下に示すこの研究の背景を踏まえて、学術論文のintroduction sectionを600文字程度で作成して下さい。必ず{text4}の文章を作成してください。Please think step by step.;
60
+ What is the problem?: {text6}
61
+ What do we know?: {text7}
62
+ What do we need to know?: {text8}
63
+ What is this study aiming to?: {text9}'''
64
+
65
+ openai.api_key = text0
66
+
67
+ response = openai.ChatCompletion.create(
68
+ model="gpt-3.5-turbo",
69
+ temperature=0.7,
70
+ max_tokens=1024,
71
+ messages=[
72
+ {"role": "user", "content": query},
73
+ ],
74
+ )
75
+ return response.choices[0]["message"]["content"].strip()
76
+
77
+
78
+ def generate_method_draft(text0, text11, text12, text13, text14, text15, text16, text17, text18, text19, text20, text21, text4):
79
+
80
+ query = f'''あなたは臨床研究の専門家です。以下に示すこの研究方法を踏まえて、学術論文のMethod sectionを800文字以内で作成して下さい。必ず{text4}の文章を作成してください。Please think step by step.;
81
+ Sudy design: {text11}
82
+ Study setting: {text12}
83
+ Number of participants: {text13}
84
+ Inclusion criteria: {text14}
85
+ Exclusion criteria: {text15}
86
+ Intervention: {text16}
87
+ Outcomes: {text17}
88
+ Measurements: {text18}
89
+ review board approval: {text19}
90
+ Statistical method: {text20}
91
+ others: {text21}'''
92
+
93
+ openai.api_key = text0
94
+
95
+ response = openai.ChatCompletion.create(
96
+ model="gpt-3.5-turbo",
97
+ temperature=0.7,
98
+ max_tokens=1024,
99
+ messages=[
100
+ {"role": "user", "content": query},
101
+ ],
102
+ )
103
+
104
+ return response.choices[0]["message"]["content"].strip()
105
+
106
+
107
+ def generate_results_draft(text0, text23_1, text23_2, text23_3, text23_4, text4):
108
+
109
+ query = f'''あなたは臨床研究の専門家です。以下に示すこの研究の結果について考察を加えずに学術論文のresults sectionを600文字以内で作成して下さい。必ず{text4}の文章を作成してください。Please think step by step.;
110
+ {text23_1}, {text23_2}, {text23_3}, {text23_4}'''
111
+
112
+ openai.api_key = text0
113
+
114
+ response = openai.ChatCompletion.create(
115
+ model="gpt-3.5-turbo",
116
+ temperature=0.7,
117
+ max_tokens=1024,
118
+ messages=[
119
+ {"role": "user", "content": query},
120
+ ],
121
+ )
122
+
123
+ return response.choices[0]["message"]["content"].strip()
124
+
125
+
126
+ def generate_discussions_draft(text0, text25, text4):
127
+
128
+ query = f'''あなたは臨床研究の専門家です。以下に示すこの研究の概要を踏まえて学術論文のdiscussions sectionを1000文字以内で作成して下さい。必ず{text4}の文章を作成してください。Please think step by step.;
129
+ {text25}'''
130
+
131
+ openai.api_key = text0
132
+
133
+ response = openai.ChatCompletion.create(
134
+ model="gpt-3.5-turbo",
135
+ temperature=0.7,
136
+ max_tokens=1024,
137
+ messages=[
138
+ {"role": "user", "content": query},
139
+ ],
140
+ )
141
+
142
+ return response.choices[0]["message"]["content"].strip()
143
+
144
+ def generate_discussions_draft_morita_style(text0, text25, text4):
145
+
146
+ query = f'''あなたは臨床研究の専門家です。以下に示すこの研究の概要を踏まえて学術論文のdiscussions sectionを1000文字以内で作成して下さい。必ず{text4}で文章を作成してください。
147
+ 文章のスタイルは以下の形式に従ってください。
148
+ "This study is, to our knowledge, the first study to clarify,,,
149
+ The most impoけant finding is that,,,
150
+ The second important finding is that,,,
151
+ Of note (interest) was that,,,
152
+ Strength of this study is,,,
153
+ Limitation include,,,
154
+ In conclusion, ,,, "
155
+ {text25}'''
156
+
157
+ openai.api_key = text0
158
+
159
+ response = openai.ChatCompletion.create(
160
+ model="gpt-3.5-turbo",
161
+ temperature=0.7,
162
+ max_tokens=1024,
163
+ messages=[
164
+ {"role": "user", "content": query},
165
+ ],
166
+ )
167
+
168
+ return response.choices[0]["message"]["content"].strip()
169
+
170
+
171
+ def generate_abstract(text0, text27, text28, text4):
172
+
173
+ query = f'''あなたは臨床研究の専門家です。以下に示すこの研究論文を元にabstractを{text27}文字以内で作成して下さい。必ず{text4}で文章を作成してください。
174
+ 論文の内容: {text28}
175
+ abstractのスタイルは以下の形式に従ってください。
176
+ Background:
177
+ Method:
178
+ Results:
179
+ Conlusion:
180
+ '''
181
+ openai.api_key = text0
182
+
183
+ response = openai.ChatCompletion.create(
184
+ model="gpt-3.5-turbo",
185
+ temperature=0.7,
186
+ max_tokens=1024,
187
+ messages=[
188
+ {"role": "user", "content": query},
189
+ ],
190
+ )
191
+
192
+ return response.choices[0]["message"]["content"].strip()
193
+
194
+ title = ("""
195
+ <h2 align="center">GPT-Doc</h2>
196
+ <h5 align="center">This is a support tool for writing papers using OpenAI-API<br>OpenAI-APIを利用した論文作成支援ツールです</h5>
197
+ <h2 align="center"> </h2>
198
+
199
+
200
+ ###### Potential users would be clinical research learners who use English as a second language. 利用者は英語が母国語ではない臨床研究初学者を想定しています。
201
+
202
+ ###### At first, please input your OpenAI-API-Key in Setteing tab. まずセッティングタブにOpenAI-API-Keyを入力して下さい。
203
+
204
+ ###### Please select the tabs with OpenAI-API-Key. 使用するツールを選んでください
205
+ """)
206
+
207
+
208
+ with gr.Blocks() as demo:
209
+ gr.Markdown(title)
210
+ with gr.Tab("Settings"):
211
+ with gr.Column(scale=1):
212
+ text0 = gr.Textbox(label="OPENAI-API-KEY", placeholder="sk-", type='password')
213
+ text4 = gr.Radio(["Japanese 日本語", "English 英語"], label="Output language 出力言語", value="Japanese 日本語")
214
+ with gr.Tab("Clinical Qustion Generation"):
215
+ with gr.Column(scale=1):
216
+ text1 = gr.Textbox(lines=2, label="Clinical question 臨床疑問", placeholder="Both formulated messages and vague tweets are acceptable. 定式化されたものでも漠然としたつぶやきでもOKです")
217
+ text2 = gr.Radio(["Hypothesis generation 仮説形成", "Study design 研究デザイン", "Protocol draft 研究計画書"], label="Please select your request 相談内容", value="Hypothesis generation 仮説形成")
218
+ text3 = gr.Textbox(lines=2, label="Create original query 相談内容を記載", placeholder="Please ask any questions. If not, please leave blank here. 上の3項目の他に質問があれば記載して下さい。なければ空欄のままにしてください")
219
+ cq_button = gr.Button("Generate!")
220
+ text5 = gr.Textbox(lines=2, label="Answer")
221
+
222
+ with gr.Tab("Introduction"):
223
+ with gr.Column(scale=1):
224
+ text6 = gr.Textbox(lines=2, label="1. What is the problem? そのテーマを研究することの重要性")
225
+ text7 = gr.Textbox(lines=2, label="2. What do we know? そのテーマについてこれまで研究されてきたこと")
226
+ text8 = gr.Textbox(lines=2, label="3. What do we need to know? これまでの研究で課題となっていること")
227
+ text9 = gr.Textbox(lines=2, label="4. What is this study aiming to? この研究の目的")
228
+ free_text = gr.Textbox(lines=2, label="If you want to enter information regardless of the above structure, enter it here. ※上記構造にこだわらずに情報を入力する場合はこちら", placeholder="If you are entering 1-4 above, leave this field blank. 上の1-4に入力する場合ここは空欄のままにしてください。")
229
+ intro_button = gr.Button("Generate!")
230
+ text10 = gr.Textbox(lines=2, label="Answer")
231
+
232
+ with gr.Tab("Methods"):
233
+ with gr.Column(scale=1):
234
+ text11 = gr.Radio(["Observational study 観察研究", "Randomized controlled study 無作為化比較試験"], label="Study design 研究デザイン", value="Observational study 観察研究")
235
+ text12 = gr.Textbox(lines=2, label="Setting セッティング", placeholder="Institutions, participants etc 研究に参加した施設や対象者など")
236
+ text13 = gr.Textbox(lines=2, label="Number of participants 参加人数", placeholder="number of screening, included number, number of analyzed case etc スクリーニング人数、除外人数、完遂人数など")
237
+ text14 = gr.Textbox(lines=2, label="Inclusion criteria 適格基準")
238
+ text15 = gr.Textbox(lines=2, label="Exclusion criteria 除外基準")
239
+ text16 = gr.Textbox(lines=2, label="Intervention 介入方法")
240
+ text17 = gr.Textbox(lines=2, label="Main endpoint Secondary endpoint 主要評価項目、副次評価項目")
241
+ text18 = gr.Textbox(lines=2, label="Measurement 測定方法")
242
+ text19 = gr.Radio(["Approved 承認済", "Not approved 審査未"], label="Institutional Review Boards 倫理委員会の審査", value="Approved 承認済")
243
+ text20 = gr.Textbox(lines=2, label="Statistical analysis 解析方法")
244
+ text21 = gr.Textbox(lines=2, label="Other information その他必要な情報", placeholder="Please add other important information 追加したい情報があれば記載して下さい")
245
+ meth_button = gr.Button("Generate!")
246
+ text22 = gr.Textbox(lines=2, label="Answer")
247
+
248
+ with gr.Tab("Results"):
249
+ with gr.Column(scale=1):
250
+ gr.Markdown("Please input findings here. 結果を重要と思われるものから順番に入力して下さい")
251
+ text23_1 = gr.Textbox(lines=2, label="Please input finding 1 here. 結果1を入力して下さい")
252
+ text23_2 = gr.Textbox(lines=2, label="Please input finding 2 here. 結果2を入力して下さい")
253
+ text23_3 = gr.Textbox(lines=2, label="Please input finding 3 here. 結果3を入力して下さい")
254
+ text23_4 = gr.Textbox(lines=2, label="Please input other findings here. その他の結果を入力して下さい")
255
+ results_button = gr.Button("Generate!")
256
+ text24 = gr.Textbox(lines=2, label="Answer")
257
+
258
+ with gr.Tab("Discussions"):
259
+ with gr.Column(scale=1):
260
+ text25 = gr.Textbox(lines=2, label="Introduction, Methods, Resultsを入力して下さい")
261
+ with gr.Row():
262
+ disc_button = gr.Button("Generate!")
263
+ disc2_button = gr.Button("Generate in Morita style")
264
+ with gr.Row():
265
+ with gr.Column(scale=1):
266
+ text26 = gr.Textbox(lines=2, label="Answer")
267
+
268
+ with gr.Tab("Abstract"):
269
+ with gr.Column(scale=1):
270
+ text27 = gr.Number(label="Number of words 文字数")
271
+ text28 = gr.Textbox(lines=2, label="Please input information such as a manuscript draft 論文本文など必要な情報を記載して下さい")
272
+ abst_button = gr.Button("Generate!")
273
+ text29 = gr.Textbox(lines=2, label="Answer")
274
+
275
+ cq_button.click(generate_clinical_query_solution, inputs=[text0, text1, text2, text3, text4], outputs=text5)
276
+ intro_button.click(generate_intro_draft, inputs=[text0, free_text, text6,text7, text8, text9, text4], outputs=text10)
277
+ meth_button.click(generate_method_draft, inputs=[text0, text11, text12, text13, text14, text15, text16, text17, text18, text19, text20, text21, text4], outputs=text22)
278
+ results_button.click(generate_results_draft, inputs=[text0, text23_1, text23_2, text23_3, text23_4, text4], outputs=text24)
279
+ disc_button.click(generate_discussions_draft, inputs=[text0, text25, text4], outputs=text26)
280
+ disc2_button.click(generate_discussions_draft_morita_style, inputs=[text0, text25, text4], outputs=text26)
281
+ abst_button.click(generate_abstract, inputs=[text0, text27, text28, text4], outputs=text29)
282
+
283
+ demo.launch()