-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
165 lines (143 loc) · 6.97 KB
/
main.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
import os
import ast
from dotenv import load_dotenv
import openai
from flask_cors import CORS, cross_origin
from flask import Flask, request, jsonify, send_file
import json
import requests
app = Flask(__name__)
CORS(app)
load_dotenv()
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
openai.api_key = os.getenv("OPENAI_API_KEY")
def get_content(response):
json_object = response["choices"][0]
answer = json_object["message"]["content"]
return answer
def suggest_visa(content):
need_visa_question = content + " Do I need a visa? Give me a one-letter yes (Y) or no (N) answer"
messages = [
{"role": "system", "content": need_visa_question},
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=0,
)
need_visa = get_content(response)
if need_visa == "Y":
visa_explain_question = content + ". Tell me the name of the visa I need without any explaination"
else:
visa_explain_question = content + ". Explain why I don't need a visa"
messages = [
{"role": "system", "content": visa_explain_question},
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=0,
)
visa_explain = get_content(response)
# print(need_visa_question, visa_explain_question)
# print([True, visa_explain])
if need_visa == "Y":
return {
'need_visa' : True,
'explain': visa_explain
}
else:
return {
'need_visa' : False,
'explain': visa_explain
}
# p1 = "My country of origin is USA. My country of destination is Canada. My reason of travel is to visit a friend. "
# p2 = "My country of origin is Cameroon. My country of destination is Canada. My reason of travel is to visit a friend. "
# print(suggest_visa(p1))
# print(suggest_visa(p2))
@app.route("/init", methods=["POST"])
@cross_origin()
def init():
data = request.json
initial_context = "You are a kind helpful assistant chatbot. Your job is to assist people applying for visa to travel abroad."
context = initial_context + data['info']
res = suggest_visa(context)
return jsonify(res)
@app.route("/questions", methods=["POST"])
@cross_origin()
def questions():
context = request.json.get("context")
context += " What personal information do you need from me to be able to assist me? Provide a list of questions. If I have already provided an answer to a question (e.g purpose of my visit), do not ask again. Do not ask any identifying questions like my name, date of birth or passport number. Do not ask about criminal or medical history. Think about the requirements for the specific visa and common pitfalls that cause visas to be rejected when generating the questions. Be specific as possible; for example, instead of asking if I have enough funds, ask how much money my bank statement has, or instead of asking if I have documents to prove my ties, ask me questions that can be used to determine my ties (e.g marital status, number of kids, property, employment status, whether I'm travelling alone or not). Research about the supporting documents and immigration regulations for this specific visa type and use as a guide for the questions, making sure to ask questions only relevant to this visa type. If a question isn't relevant, do not ask it. No questions about application formalities like application forms, feed, passport photos. Response should be a list of questions in Python array format"
messages = [
{
"role": "system",
"content": context,
},
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=0,
)
questions = get_content(response)
questions = ast.literal_eval(questions)
first_text = "You have provided some information. I'll ask a few questions to help you. Type anything to get started."
response = {"first": first_text, "answer": questions}
return jsonify(response)
@app.route("/suggestions", methods=["POST"])
@cross_origin()
def suggestions():
data = request.json
questions = data["questions"]
answers = data["answers"]
context = "Here is a Python list of question-answer pairs in the form 'question: answer', which provide context on my personal background:\n"
length = min(len(questions), len(answers))
for i in range(length):
context += questions[i] + ": " + answers[i] + " \n"
context += "Research about the requirements and official immigrations regulations/rules (of the country I'm applying for) for the specific visa I'm applying for. List out these immigration rules, and alongside each rule, either tell me how my background satisfies this rule or suggest supporting documents that might make my application stronger. If my personal background shows that my visa is likely to be rejected due to not fufilling an immigration rule, let me know. Make suggestions of supporting documents based on my background provided in the question-answer pairs"
messages = [
{
"role": "system",
"content": context,
},
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=0,
)
response = get_content(response)
response = {"answer": response}
return jsonify(response)
@app.route("/cover", methods=["POST"])
@cross_origin()
def cover():
data = request.json
questions = data["questions"]
answers = data["answers"]
context = "Here is a Python list of question-answer pairs in the form 'question: answer', which provide context on my personal background:\n"
length = min(len(questions), len(answers))
for i in range(length):
context += questions[i] + ": " + answers[i] + " \n"
context += "Research about the requirements and official immigrations regulations/rules (of the country I'm applying for) for the specific visa I'm applying for. Use this information and my personal background information to generate a visa cover letter for me that specifies the purpose of my visit and specifies how I satisfy the immigration rules. This letter should convince the visa offer that I am genuine and reduce the chance of a visa rejection. Make sure it is returned in HTML format"
messages = [
{
"role": "system",
"content": context,
},
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
temperature=0,
)
response = get_content(response)
import base64
import pdf_gen
data = {"file": base64.b64encode(pdf_gen.get_pdf(response)).decode()}
r = requests.post("https://chpris.org:5000/pdf/generate", json=data)
r.raise_for_status()
response += f"""<br><form formtarget="_blank" action="{r.text}"><input type="submit" value="Download"/></form>"""
response = {"answer": response}
return jsonify(response)
app.run(debug=True)