FCFMIW/MIW-Helper
0
1import openai2import gradio as gr3import os4import pandas as pd5import time6 7# env8openai.api_key = os.environ.get('openai-api')9SHEET_ID = '119qz8UpCdwCu_gyPtayi7JmhSM0nUddolOyJcqsMnmA'10SHEET_NAME = 'MIW_Sources'11 12# Blocks13with gr.Blocks(theme=gr.themes.Glass()) as demo:14 15 #Initialize data16 url = f'https://docs.google.com/spreadsheets/d/{SHEET_ID}/gviz/tq?tqx=out:csv&sheet={SHEET_NAME}'17 df = pd.read_csv(url)18 19 def filter_and_concatenate(df, input_string):20 # If input string is empty, return concatenation of all values21 if input_string == "":22 concatenated_string = ""23 for _, row in df.iterrows():24 concatenated_string += '|'.join(str(value) for value in row.values) + '\n'25 return concatenated_string26 27 # Filter the dataframe based on the input string28 filtered_df = df[df['category'] == input_string]29 30 # Concatenate the values from other columns31 concatenated_string = ''32 for _, row in filtered_df.iterrows():33 concatenated_string += '|'.join(str(value) for value in row.values[1:]) + '\n'34 return concatenated_string35 36 def showall(code:str):37 visibility = gr.update(visible = True)38 invisibility = gr.update(visible = False)39 tryagain = gr.update(value = None, placeholder = "Try again - 再試一次")40 if code == os.environ.get('accesscode'):41 return visibility, visibility, visibility, visibility, invisibility42 else:43 return invisibility, invisibility, invisibility, invisibility, tryagain44 45 def refreshmode1(mode):46 mode = gr.update(value= "mode1")47 return mode48 49 def refreshmode2(mode):50 mode = gr.update(value="mode2")51 return mode52 53 def refreshmode3(mode):54 mode = gr.update(value="mode3")55 return mode56 57 def answer(history):58 for attempt in range(5):59 try:60 response = openai.ChatCompletion.create(61 model='gpt-3.5-turbo',62 messages=history,63 temperature=0.5,64 max_tokens=800,65 top_p=1,66 n=1,67 frequency_penalty=0.9,68 presence_penalty=0.9,69 stop=None70 )71 result = response['choices'][0]['message']['content']72 break73 except Exception as e:74 print(f"Error occurred on attempt {attempt + 1}: {e}")75 if attempt < 5:76 time.sleep(10)77 else:78 raise e79 return result80 81 def init_history(language, messages_history1, messages_history2, messages_history3): #Reinitialize the state file82 messages_history1 = [[None,languagedict[language]['whatcanidofirst']]]83 messages_history2 = [[None,languagedict[language]['interviewquestionsfirst']]]84 messages_history3 = [[None,languagedict[language]['helpfirst']]]85 messages_history1 += {"role": "system", "content": themewhatcanido}86 messages_history1 += {"role": "assistant", "content": languagedict[language]['whatcanidofirst']}87 messages_history2 += {"role": "system", "content": themeinterviewquestions}88 messages_history2 += {"role": "assistant", "content": languagedict[language]['interviewquestionsfirst']}89 messages_history3 += {"role": "system", "content": themehelp}90 messages_history3 += {"role": "assistant", "content": languagedict[language]['helpfirst']}91 return messages_history1, messages_history2, messages_history392 93 def getlist(df):94 # Extract distinct values from the first column excluding the header95 distinct_values = df.iloc[1:, 0].unique()96 97 # Join the distinct values into a string separated by ", "98 distinct_values_string = ", ".join(str(value) for value in distinct_values)99 100 return distinct_values_string101 102 def user(msg, chatbot, state, language: str, mode: str):103 #Bot context104 if mode == "mode1":105 context = themewhatcanido106 elif mode == "mode2":107 context = themeinterviewquestions108 else:109 context = themehelp110 #remind mission111 if len(state) == 0:112 state.append({'role':'system','content':f"Take notice of your mission:\n\n{context}"})113 elif len(state) > 10:114 state.append({'role':'system','content':f"Remember your mission as briefed earlier:\n\n{context}"})115 chatbot.append([msg, None])116 117 #Specific for mode3118 if mode == 'mode3':119 #Append instructions to msg120 listvalues = getlist(df)121 print(f'listvalues: {listvalues}')122 queryforcategory = [{'role':'user','content':f"Out of this list of words/expressions separated by a comma:\n{listvalues}\n, which one corresponds the best to the theme in this message:\n{msg}\n\nThe result should be one of the elements from the list only. If there's absolutly no match, return an empty string ''\nHere is an example:\n if the list contains 'Skills,Finding work' and the message is 'I want to learn about Excel', just reply 'Skills' "}]123 matchcat = answer(queryforcategory)124 matchcat = matchcat.replace('.','')125 matchcat = matchcat.strip()126 print(f'matchcat: {matchcat}')127 try:128 instructions = filter_and_concatenate(df, matchcat)129 except:130 instructions = ""131 print(f'instructions: {instructions}')132 msg = msg + f"\n\nAnswer in {language}, keep it casual but respectful"133 state.append({'role':'user','content':msg})134 if mode == 'mode3':135 state.append({'role':'system','content':f"\n\nUse the following elements to document your answer:\n{instructions}"})136 print(f'state is\n{state}')137 print(f'chatbot is\n{chatbot}')138 return "", chatbot, state139 140 def bot(chatbot, state):141 response = answer(state)142 chatbot.append([None, response])143 state.append({'role':'assistant','content':response})144 return chatbot, state145 146 def refresh(language: str):147 titleup = gr.update(value = languagedict[language]['welcome'])148 input1up = gr.update(label = languagedict[language]['whatcanidolabel'], placeholder = languagedict[language]['whatcanidoph'])149 input2up = gr.update(label = languagedict[language]['interviewquestionslabel'], placeholder = languagedict[language]['interviewquestionsph'])150 input3up = gr.update(label = languagedict[language]['helplabel'], placeholder = languagedict[language]['helpph'])151 chatbot1up = [[None,languagedict[language]['whatcanidofirst']]]152 state1up = [{"role": "system", "content": themewhatcanido},{"role": "assistant", "content": languagedict[language]['whatcanidofirst']}]153 chatbot2up = [[None,languagedict[language]['interviewquestionsfirst']]]154 state2up = [{"role": "system", "content": themeinterviewquestions},{"role": "assistant", "content": languagedict[language]['interviewquestionsfirst']}]155 chatbot3up = [[None,languagedict[language]['whatcanidofirst']]]156 state3up = [{"role": "system", "content": themehelp},{"role": "assistant", "content": languagedict[language]['helpfirst']}]157 return titleup, input1up, input2up, input3up, chatbot1up, chatbot2up, chatbot3up, state1up, state2up, state3up158 159 languageoptions = ["English", "廣東話"]160 languagedict = {161 "English": {162 "welcome": "Welcome to Make It Work App - Where you can get tailored help thanks to Artificial Intelligence",163 "whatcanido": "What job can I do?",164 "whatcanidofirst" : "Tell me about you.",165 "whatcanidolabel": "Write your preferences and we can suggest some options",166 "whatcanidoph": "I'm a 30 years old single mother, I have a 6 years old daughter. I can cook, take care of children and elderly and have been a waiter at a restaurant before.\nI have a high school degree.",167 "interviewquestions": "How to prepare for interview?",168 "interviewquestionsfirst": "What is the role you're interviewing for?",169 "interviewquestionslabel": "Answer the questions for this virtual interview",170 "interviewquestionsph": "I want to prepare for an interview as a receptionist.",171 "help": "Where to get help?",172 "helpfirst": "How can I help you?",173 "helplabel": "Where do you need help?",174 "helpph": "How can I have support to take care of my daughter during my work? / How can I get trained on Excel?"175 },176 "廣東話": {177 "welcome": "歡迎使用 Make It Work App - 借助人工智能,您可以獲得量身定制的幫助",178 "whatcanido": "我可以做什麼工作?",179 "whatcanidofirst" : "介紹一下你自己吧。",180 "whatcanidolabel": "寫下您的喜好,我們可以建議一些選擇",181 "whatcanidoph": "我是一個 30 歲的單身母親,我有一個 6 歲的女兒。我會做飯,照顧孩子和老人,以前在餐廳當過服務員。\n我有高中學歷。",182 "interviewquestions": "如何準備面試?",183 "interviewquestionsfirst": "你面試的角色是什麼?",184 "interviewquestionslabel": "回答這位 AI 面試官的問題",185 "interviewquestionsph": "我想以接待員的身份準備面試。",186 "help": "去哪裡尋求幫助?",187 "helpfirst": "我怎麼幫你",188 "helplabel": "你在哪裡需要幫助?",189 "helpph": "我如何在工作期間獲得支持來照顧我的女兒? / 我怎樣才能接受 Excel 培訓?"190 }191 }192 193 url = "https://cdn.discordapp.com/attachments/1006389042608349264/1114545184080928849/Guiyom_cartoon_style_-_working_poors_in_Hong_Kong_seeking_advic_70131de8-1a0a-4f04-ac49-b99d7e020414.png"194 195 # INTERFACE196 mode = gr.Textbox(value = "mode1", visible = False, type = 'password')197 accesscode = gr.Textbox(label = "Input access code", visible = True)198 with gr.Row():199 with gr.Column(scale = 1):200 visual = gr.Image(image = url, shape = [150,150])201 202 with gr.Column(scale = 3):203 title = gr.Markdown(value=languagedict['English']['welcome'])204 language = gr.Dropdown(languageoptions , value= "English", label="Choose language / 選擇語言", visible = False)205 206 # REFERENCES207 208 themewhatcanido = f"""209 You are a career advisor for people in Hong Kong with relatively low skills job.210 Your mission is to ask several questions to then help them identify what are their job options, taking into consideration:211 - Their study level and domain212 - Their level of confort with computer tools (typing, excel, word, ...)213 - Their past working experiences (ex: hair dresser, street cleaner, old care support, restaurant aide, waiter)214 - Their constraints (ex: time available per week, per day)215 - Other relevant questions216 217 Then, when you have enough information, you will thank them and suggest some options, for each of them:218 - Explain the key skills needed219 - Breakdown the potential challenges for them to anticipate220 - Provide advice on how to overcome these challenges221 222 IMPORTANT: write in {language}, in the style of a native. Keep it casual but respectful.223 """224 225 themeinterviewquestions = f"""226 You are an interviewer, interviewing a candidate for a role [the role will be specified to you].227 Your candidates have limited skills, please find ways to support them and ask all the questions.228 229 Do a series of questions (only one by message)230 - their experience on this job231 - their skills232 - their capacity to manage the stress related to this job233 - any other relevant question related to the job, don't be afraid to be specific234 235 After 5 to 10 questions, give a feedback to the interviewee:236 [FEEDBACK]237 - What they did well238 - What they can do better239 - Suggestions of reformulations240 241 IMPORTANT: write in {language}, in the style of a native. Keep it casual but respectful.242 """243 244 themehelp = f"""245 You are a social worker, you need to provide some help to the user. He can ask questions relative to this categories, with a few examples246 - Elderly:247 o ex: I need help to take care with my grandma who lives with me248 o ex: I am ageing, I don't know who can help when I grow older249 - Health:250 o ex: I have health issues, where can I get some help251 - Housing:252 o ex: I need help to find an apartment253 - Parenting:254 o ex: Who can help me to keep my daughter when I'm working?255 - Food:256 o ex: I don't have enough food for my children, where can I get support?257 - Skills:258 o ex: I want to learn Excel259 o ex: I want to improve my English260 - Job:261 o ex: Where can I learn about job vacancies in Kowloon?262 - Leisure:263 o ex: I need some support to get my son to do some art.264 o ex: I want to learn how to swim265 266 If the question is not related to any of these topics, answer 'This is beyond my domain of competence, please ask a social worker.'267 Always keep it relevant for Hong Kong, low-income persons.268 IMPORTANT: write in {language}, in the style of a native. Keep it casual but respectful.269 """270 271 #INTERFACE272 with gr.Tab(f"{languagedict['English']['whatcanido']} / {languagedict['廣東話']['whatcanido']}"):273 chatbot1 = gr.Chatbot([[None,languagedict['English']['whatcanidofirst']]])274 input1 = gr.Textbox(label = languagedict['English']['whatcanidolabel'], placeholder = languagedict['English']['whatcanidoph'], visible = False)275 state1 = gr.State([{"role": "system", "content": themewhatcanido},{"role": "assistant", "content": languagedict['English']['whatcanidofirst']}])276 with gr.Tab(f"{languagedict['English']['interviewquestions']} / {languagedict['廣東話']['interviewquestions']}"):277 chatbot2 = gr.Chatbot([[None,languagedict['English']['interviewquestionsfirst']]])278 input2 = gr.Textbox(label = languagedict['English']['interviewquestionslabel'], placeholder = languagedict['English']['interviewquestionsph'], visible = False)279 state2 = gr.State([{"role": "system", "content": themeinterviewquestions},{"role": "assistant", "content": languagedict['English']['interviewquestionsfirst']}])280 with gr.Tab(f"{languagedict['English']['help']} / {languagedict['廣東話']['help']}"):281 chatbot3 = gr.Chatbot([[None,languagedict['English']['helpfirst']]])282 input3 = gr.Textbox(label = languagedict['English']['helplabel'], placeholder = languagedict['English']['helpph'], visible = False)283 state3 = gr.State([{"role": "system", "content": themehelp},{"role": "assistant", "content": languagedict['English']['helpfirst']}])284 clear = gr.Button('Clear')285 286 # Launcher287 input1.submit(refreshmode1, mode, mode).then(user, [input1, chatbot1, state1, language, mode], [input1, chatbot1, state1]).then(bot, [chatbot1, state1], [chatbot1, state1])288 input2.submit(refreshmode2, mode, mode).then(user, [input2, chatbot2, state2, language, mode], [input2, chatbot2, state2]).then(bot, [chatbot2, state2], [chatbot2, state2])289 input3.submit(refreshmode3, mode, mode).then(user, [input3, chatbot3, state3, language, mode], [input3, chatbot3, state3]).then(bot, [chatbot3, state3], [chatbot3, state3])290 clear.click(lambda: None, None, [chatbot1, chatbot2, chatbot3], queue=True).success(init_history, [language, state1, state2, state3], [state1, state2, state3])291 language.change(lambda: None, None, [chatbot1, chatbot2, chatbot3], queue=True).then(refresh, language, [title, input1, input2, input3, chatbot1, chatbot2, chatbot3, state1, state2, state3])292 accesscode.submit(showall, accesscode, [input1, input2, input3, language, accesscode])293demo.queue()294demo.launch()