kenton-li commited on
Commit
c84c42d
·
1 Parent(s): 5259328

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +155 -0
app.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import time
4
+ import openai
5
+ import pandas as pd
6
+
7
+ openai.api_key = "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"
8
+
9
+
10
+
11
+ model = None
12
+ tokenizer = None
13
+ generator = None
14
+ csv_name = "disease_database_mini.csv"
15
+
16
+
17
+ def csv_prompter(question,csv_name):
18
+
19
+
20
+ fulltext = "A question is provided below. Given the question, extract " + \
21
+ "keywords from the text. Focus on extracting the keywords that we can use " + \
22
+ "to best lookup answers to the question. \n" + \
23
+ "---------------------\n" + \
24
+ "{}\n".format(question) + \
25
+ "---------------------\n" + \
26
+ "Provide keywords in the following comma-separated format.\nKeywords: "
27
+
28
+ messages = [
29
+ {"role": "system", "content": ""},
30
+ ]
31
+ messages.append(
32
+ {"role": "user", "content": f"{fulltext}"}
33
+ )
34
+ rsp = openai.ChatCompletion.create(
35
+ model="gpt-3.5-turbo",
36
+ messages=messages
37
+ )
38
+ keyword_list = rsp.get("choices")[0]["message"]["content"]
39
+ keyword_list = keyword_list.split(", ")
40
+
41
+ print(keyword_list)
42
+ df = pd.read_csv(csv_name)
43
+ divided_text = []
44
+ csvdata = df.to_dict('records')
45
+ step_length = 15
46
+ for csv_item in range(0,len(csvdata),step_length):
47
+ csv_text = str(csvdata[csv_item:csv_item+step_length]).replace("}, {", "\n\n").replace("\"", "")#.replace("[", "").replace("]", "")
48
+ divided_text.append(csv_text)
49
+
50
+ answer_llm = ""
51
+
52
+ score_textlist = [0] * len(divided_text)
53
+
54
+ for i, chunk in enumerate(divided_text):
55
+ for t, keyw in enumerate(keyword_list):
56
+ if keyw.lower() in chunk.lower():
57
+ score_textlist[i] = score_textlist[i] + 1
58
+
59
+ answer_list = []
60
+ divided_text = [item for _, item in sorted(zip(score_textlist, divided_text), reverse=True)]
61
+
62
+ for i, chunk in enumerate(divided_text):
63
+
64
+ if i>5:
65
+ continue
66
+
67
+ fulltext = "{}".format(chunk) + \
68
+ "\n---------------------\n" + \
69
+ "Based on the Table above and not prior knowledge, " + \
70
+ "Select the Table Entries that will help to answer the question: {}\n Output in the format of \" Disease: <>; Symptom: <>; Medical Test: <>; Medications: <>;\". If there is no useful form entries, output: 'No Entry'".format(question)
71
+
72
+ print(fulltext)
73
+ messages = [
74
+ {"role": "system", "content": ""},
75
+ ]
76
+ messages.append(
77
+ {"role": "user", "content": f"{fulltext}"}
78
+ )
79
+ rsp = openai.ChatCompletion.create(
80
+ model="gpt-3.5-turbo",
81
+ messages=messages
82
+ )
83
+ answer_llm = rsp.get("choices")[0]["message"]["content"]
84
+
85
+ print("\nAnswer: " + answer_llm)
86
+ print()
87
+ if not "No Entry" in answer_llm:
88
+ answer_list.append(answer_llm)
89
+
90
+
91
+
92
+ fulltext = "The original question is as follows: {}\n".format(question) + \
93
+ "Based on this Table:\n" + \
94
+ "------------\n" + \
95
+ "{}\n".format(str("\n\n".join(answer_list))) + \
96
+ "------------\n" + \
97
+ "Answer: "
98
+ print(fulltext)
99
+ messages = [
100
+ {"role": "system", "content": ""},
101
+ ]
102
+ messages.append(
103
+ {"role": "user", "content": f"{fulltext}"}
104
+ )
105
+ rsp = openai.ChatCompletion.create(
106
+ model="gpt-3.5-turbo",
107
+ messages=messages
108
+ )
109
+ answer_llm = rsp.get("choices")[0]["message"]["content"]
110
+
111
+ print("\nFinal Answer: " + answer_llm)
112
+ print()
113
+
114
+ return answer_llm
115
+
116
+
117
+
118
+
119
+ with gr.Blocks() as demo:
120
+ chatbot = gr.Chatbot()
121
+ msg = gr.Textbox()
122
+ clear = gr.Button("Clear")
123
+ Initialization = gr.Button("Initialization")
124
+
125
+ def restart(history):
126
+ invitation = "ChatDoctor: "
127
+ human_invitation = "Patient: "
128
+ return [[None,invitation+" I am ChatDoctor, what medical questions do you have?"]]
129
+
130
+ def user(user_message, history):
131
+ invitation = "ChatDoctor: "
132
+ human_invitation = "Patient: "
133
+ return "", history +[[human_invitation+user_message, None]]
134
+
135
+ def bot(history):
136
+ invitation = "ChatDoctor: "
137
+ human_invitation = "Patient: "
138
+
139
+ question = history[-1][0]
140
+ response = csv_prompter(question,csv_name):
141
+
142
+ response = invitation+ response
143
+ history[-1][1] = response
144
+
145
+ return history
146
+
147
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
148
+ bot, chatbot, chatbot
149
+ )
150
+ clear.click(lambda: None, None, chatbot, queue=False).then(restart, chatbot, chatbot)
151
+ Initialization.click(lambda: None, None, chatbot, queue=False).then(restart, chatbot, chatbot)
152
+
153
+ if __name__ == "__main__":
154
+ demo.launch()
155
+