-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt_response_extraction.py
83 lines (63 loc) · 4.25 KB
/
gpt_response_extraction.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
import os
import openai
from deep_translator import GoogleTranslator
translator = GoogleTranslator(source='kazakh', target='en')
# read all files from the directory
cases = os.listdir('cases')
ready_cases = os.listdir('cases_results_2_tr')
ready_cases = [x[:-8] for x in ready_cases]
cases.sort()
for each_case in cases:
#each_case = str(each_case)+'_1.txt'
if each_case[-5]=='2':
if each_case[:-4] not in ready_cases:
#each_case = str(each_case) + '_0.txt'
print(each_case)
path = os.path.join('cases', each_case)
with open(path) as f:
contents = f.readlines()
# print('\n'.join(contents))
result = ''
if each_case[-5]=='0':
result = "Provide dietary recommendation for this patient profile. " + contents[0]
if each_case[-5]=='1':
original = "Предоставьте рекомендации по питанию для данного пациента. " + contents[0]
result = translator.translate(original)
if each_case[-5]=='2':
original = "Oсы науқас/пациент профилі үшін тамақтану бойынша кеңес беріңіз. " + contents[0]
result = translator.translate(original)
#print(result)
# Set openai.api_key to the OPENAI environment variable
openai.api_key = "PUT-YOUR-KEY-HERE"
messages = [
# system message first, it helps set the behavior of the assistant
{"role": "system", "content": "You are a helpful assistant."},
]
chat_completion = openai.ChatCompletion.create(
model="gpt-4", messages=[{"role": "user", "content": result}],max_tokens=800)
response1 = chat_completion.choices[0].message.content
print("ChatGPT:", response1)
follow_up=''
if each_case[-5]=='0':
follow_up = result + " " + str(response1) + " " + "Give a specific diet plan for the day based on the patient profile using Central Asian food."
if each_case[-5]=='1':
follow_up = result + " " + str(response1) + " " + "Give a specific diet plan for the day based on the patient profile using Central Asian food."
#follow_up = result + " " + str(response1) + " " + "Предложите конкретный план питания на день, основанный на профиле пациента с использованием центральноазиатской пищи."
if each_case[-5]=='2':
follow_up = result + " " + str(response1) + " " + "Give a specific diet plan for the day based on the patient profile using Central Asian food."
# follow_up = result + " " + str(response1) + " " + "Пациент профиліне негізделген түрде Орта Азия тағамдарын қолдана отырып бір күндік тамақтанудың нақты жоспарын ұсыныңыз."
messages.append({"role": "user", "content": follow_up},)
chat_completion = openai.ChatCompletion.create(
model="gpt-4", messages=messages, max_tokens=800)
messages.append({"role": "assistant", "content": chat_completion.choices[0].message.content})
response = chat_completion.choices[0].message.content
#print("ChatGPT:", response)
# Back translate the response to the original language
translator_back = GoogleTranslator(source='en', target='kazakh')
first_part = translator_back.translate(response1)
save_text = translator_back.translate(response)
save_text = original + first_part + "Предложите конкретный план питания на день, основанный на профиле пациента с использованием центральноазиатской пищи." + save_text
# Save the text
file = open("cases_results_2_tr/{}_gpt.txt".format(each_case[:-4]), "a")
a = file.write(save_text)
file.close()