Guiyom commited on
Commit
2ea70ce
·
1 Parent(s): fefd73e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +167 -0
app.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import gradio as gr
3
+
4
+ # env
5
+ openai.api_key = os.environ.get('openai-api')
6
+
7
+ # Blocks
8
+ with gr.Blocks(theme=gr.themes.Glass()) as demo:
9
+
10
+ # REFERENCES
11
+ languageoptions = ["English", "廣東話"]
12
+ languagedict = {
13
+ "English": {
14
+ "welcome": "Welcome to Make It Work App - Where you can get tailored help thanks to Artificial Intelligence",
15
+ "whatcanido": "What job I do?",
16
+ "whatcanidofirst" : "Tell me about you.",
17
+ "whatcanidolabel": "Write your preferences and we can suggest some options",
18
+ "interviewquestions": "How to prepare for interview?",
19
+ "interviewquestionsfirst": "What is the role you're interviewing for?",
20
+ "interviewquestionslabel": "Answer the questions for this virtual interview",
21
+ "help": "Where to get help?",
22
+ "helpfirst": "How can I help you?",
23
+ "helplabel": "Where do you need help? Here's is what we can suggest"
24
+ },
25
+ "廣東話": {
26
+ "welcome": "歡迎使用 Make It Work App - 借助人工智能,您可以獲得量身定制的幫助",
27
+ "whatcanido": "我做什麼工作?",
28
+ "whatcanidofirst" : "介紹一下你自己吧。",
29
+ "whatcanidolabel": "寫下您的喜好,我們可以建議一些選擇",
30
+ "interviewquestions": "如何準備面試?",
31
+ "interviewquestionsfirst": "你面試的角色是什麼?",
32
+ "interviewquestionslabel": "回答這位 AI 面試官的問題",
33
+ "help": "去哪裡尋求幫助?",
34
+ "helpfirst": "我怎麼幫你",
35
+ "helplabel": "你在哪裡需要幫助?我們會讓您知道如何找到它"
36
+ }
37
+
38
+ # INTERFACE
39
+ title = gr.Markdown(value=languagedict['English']['welcome'])
40
+ language = gr.Dropdown(languageoptions , value= "English")
41
+ with gr.Tab(languagedict['English']['whatcanido']):
42
+ chatbot1 = gr.Chatbot()
43
+ input1 = gr.Textbox(label = languagedict['English']['whatcanidolabel'])
44
+ state1 = gr.State([])
45
+ with gr.Tab(languagedict['English']['whatskills']):
46
+ chatbot2 = gr.Chatbot()
47
+ input2 = gr.Textbox(label = languagedict['English']['whatskillslabel'])
48
+ state2 = gr.State([])
49
+ with gr.Tab(languagedict['English']['interviewquestions']):
50
+ chatbot3 = gr.Chatbot()
51
+ input3 = gr.Textbox(label = languagedict['English']['interviewquestionslabel'])
52
+ state3 = gr.State([])
53
+ with gr.Tab(languagedict['English']['help']):
54
+ chatbot4 = gr.Chatbot()
55
+ input4 = gr.Textbox(label = languagedict['English']['helplabel'])
56
+ state4 = gr.State([])
57
+ with gr.Tab("MIW"):
58
+ code = gr.Textbox(label = 'Code')
59
+ instructions = gr.Textbox(label = 'Instructions', lines=10, max_lines= 100, visible = False)
60
+ clear = gr.Button('Clear')
61
+
62
+ themewhatcanido = f"""
63
+ You are a career advisor for people in Hong Kong with relatively low skills job.
64
+ Your mission is to ask several questions to then help them identify what are their job options, taking into consideration:
65
+ - Their study level and domain
66
+ - Their level of confort with computer tools (typing, excel, word, ...)
67
+ - Their past working experiences (ex: hair dresser, street cleaner, old care support, restaurant aide, waiter)
68
+ - Their constraints (ex: time available per week, per day)
69
+ - Other relevant questions
70
+
71
+ Then, when you have enough information, you will thank them and suggest some options, for each of them:
72
+ - Explain the key skills needed
73
+ - Breakdown the potential challenges for them to anticipate
74
+ - Provide advice on how to overcome these challenges
75
+
76
+ IMPORTANT: write in {language}, in the style of a native inhabitant from Kowloon
77
+ """
78
+
79
+ interviewquestionstheme = f"""
80
+ You are an interviewer, interviewing a candidate for a role [the role will be specified to you].
81
+ Your candidates have limited skills, please find ways to support them and ask all the questions.
82
+ After the candidate answered, write:
83
+ [IDEAL ANSWER] then reformulate the candidate answer to match what would be an ideal response
84
+
85
+ Then ask another question.
86
+
87
+ IMPORTANT: write in {language}, in the style of a native inhabitant from Kowloon
88
+ """
89
+
90
+ helptheme = f"""
91
+ You are a social worker, you need to provide some help to the user. He can ask questions relative to:
92
+ - care support for children or elderly in Hong Kong
93
+ - health
94
+ - housing
95
+ - food
96
+ - work
97
+ - leisure
98
+ To answer, use primarily the information indicated here:
99
+ {instructions}
100
+ If the question is not related to any of these topics, answer 'This is beyond my domain of competence, please ask a social worker.'
101
+
102
+ IMPORTANT: write in {language}, in the style of a native inhabitant from Kowloon
103
+ """
104
+ def answer(history):
105
+ response = openai.ChatCompletion.create(
106
+ model='gpt-3.5-turbo',
107
+ messages=history,
108
+ temperature=0.5,
109
+ max_tokens=800,
110
+ top_p=1,
111
+ n=1,
112
+ frequency_penalty=0.9,
113
+ presence_penalty=0.9,
114
+ stop=None
115
+ )
116
+ result = response['choices'][0]['message']['content']
117
+
118
+ def init_history(messages_history1, messages_history2, messages_history3): #Reinitialize the state file
119
+ messages_history1 = [[None,languagedict['English']['whatcanidofirst']]]
120
+ messages_history2 = [[None,languagedict['English']['interviewquestionsfirst']]]
121
+ messages_history3 = [[None,languagedict['English']['helpfirst']]]
122
+ messages_history1 += {"role": "system", "content": themewhatcanido}
123
+ messages_history1 += {"role": "assistant", "content": languagedict['English']['whatcanidofirst']}
124
+ messages_history2 += {"role": "system", "content": interviewquestionstheme}
125
+ messages_history2 += {"role": "assistant", "content": languagedict['English']['interviewquestionsfirst']}
126
+ messages_history3 += {"role": "system", "content": helptheme}
127
+ messages_history3 += {"role": "assistant", "content": languagedict['English']['helpfirst']}
128
+ return messages_history1, messages_history2, messages_history3
129
+
130
+ def user(msg, chatbot, state):
131
+ msg = msg + f"\nRemember to answer in {language}"
132
+ chatbot += [msg, None]
133
+ state += {'role':'user','content':'msg'}
134
+ return msg, chatbot, state
135
+
136
+ def bot(chatbot, state, mode):
137
+ #Bot context
138
+ if mode = "mode1":
139
+ context = themewhatcanido
140
+ elif = mode = "mode2":
141
+ context = interviewquestionstheme
142
+ else:
143
+ context = helptheme
144
+
145
+ #remind mission
146
+ if len(state) = 0:
147
+ state += {'role':'system','content':f"Take notice of your mission:\n\n{context}"}
148
+ elif len(state) > 10:
149
+ state += {'role':'system','content':f"Remember your mission as briefed earlier:\n\n{context}"}
150
+ response = answer(state)
151
+ chatbot += [None, response]
152
+ state += {'role':'assistant','content':'response'}
153
+ return chatbot, state
154
+
155
+ def whatcanido(input1):
156
+ result = answer(input1, state1)
157
+ def interviewquestions(input2):
158
+
159
+ def help(input3)
160
+
161
+ # Launcher
162
+ input1.submit(user, [input1, chatbot1, state1], [input1, chatbot1, state1]).then(bot, [chatbot1, state1, "mode1"], [chatbot1, state1])
163
+ input2.submit(user, [input2, chatbot2, state2], [input2, chatbot2, state2]).then(bot, [chatbot2, state2, "mode2"], [chatbot2, state2])
164
+ input3.submit(user, [input3, chatbot3, state3], [input3, chatbot3, state3]).then(bot, [chatbot3, state3, "mode3"], [chatbot3, state3])
165
+ clear.click(lambda: None, None, [chatbot1, chatbot2, chatbot3], queue=True).success(init_history, [state1, state2, state3], [state1, state2, state3])
166
+ demo.queue()
167
+ demo.launch()