Spaces:
Sleeping
Sleeping
Commit ·
ebdfacb
1
Parent(s): 8b5f4b6
feat: add NTU LLaMA model
Browse filesFiles changed (4)
14 unmodified lines15161718192021236 unmodified lines25825926026126226324 unmodified lines28828929029129229312 unmodified lines306307308309310311148 unmodified lines46046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949022 unmodified lines5135145155165175185195205215225235243 unmodified lines52852953053153253346 unmodified lines58058158258358458558658758858959059159259359459559659759859960060160214 unmodified lines)from utils.completion_reward import CompletionRewardfrom utils.completion_reward_utils import (get_llm_response,set_player_name,set_player_selected_character,create_certificate,236 unmodified linesmtk_description = gr.Markdown("# 蔚藍", elem_id="mtk_description", visible=False)with gr.Row():openai_img = gr.Image(24 unmodified linesinteractive=False,show_download_button=False,)with gr.Row():start_generate_story = gr.Button(12 unmodified linesbot2 = gr.Chatbot(visible=False, elem_id="bot2")bot3 = gr.Chatbot(visible=False, elem_id="bot3")bot4 = gr.Chatbot(visible=False, elem_id="bot4")with gr.Row():select_story = gr.Radio(148 unmodified lines],queue=False,).then(lambda: create_visibility_updates(True, 11),None,[openai_img,aws_img,google_img,mtk_img,story_title,story_description,openai_description,aws_description,google_description,mtk_description,start_generate_story,],queue=False,)get_llm_response_args = dict(fn=get_llm_response,inputs=[completion_reward, player_logs],outputs=[bot1, bot2, bot3, bot4],queue=False,)get_first_llm_response_args = dict(fn=get_llm_response_once,inputs=[completion_reward, player_logs],22 unmodified linesqueue=False,)start_generate_story.click(lambda: gr.update(visible=False), None, start_generate_story, queue=False).then(lambda: create_visibility_updates(True, 5),None,[bot1, bot2, bot3, bot4, weaving],queue=False,).then(**get_first_llm_response_args3 unmodified lines**get_third_llm_response_args).then(**get_fourth_llm_response_args).then(lambda: gr.update(visible=True), None, [select_story], queue=False).then(46 unmodified lines)start_generate_certificate.click(lambda: create_visibility_updates(False, 18),None,[openai_img,aws_img,google_img,mtk_img,story_title,story_description,openai_description,aws_description,google_description,mtk_description,bot1,bot2,bot3,bot4,select_story,processing,cancel_story,14 unmodified lines151617181920236 unmodified lines25725825926026126226326426526624 unmodified lines29129229329429529629729829930030130230312 unmodified lines316317318319320321322148 unmodified lines47147247347447547647747847948048148248348448548648748848949049149249349449549622 unmodified lines5195205215225235245255265275285295305315325335345355365373 unmodified lines54154254354454554654754846 unmodified lines59559659759859960060160260360460560660760860961061161261361461561661761861962014 unmodified lines)from utils.completion_reward import CompletionRewardfrom utils.completion_reward_utils import (set_player_name,set_player_selected_character,create_certificate,236 unmodified linesmtk_description = gr.Markdown("# 蔚藍", elem_id="mtk_description", visible=False)ntu_description = gr.Markdown("# 紅寶石", elem_id="ntu_description", visible=False)with gr.Row():openai_img = gr.Image(24 unmodified linesinteractive=False,show_download_button=False,)ntu_img = gr.Image("medias/ntu.png",visible=False,elem_id="ntu_img",interactive=False,show_download_button=False,)with gr.Row():start_generate_story = gr.Button(12 unmodified linesbot2 = gr.Chatbot(visible=False, elem_id="bot2")bot3 = gr.Chatbot(visible=False, elem_id="bot3")bot4 = gr.Chatbot(visible=False, elem_id="bot4")bot5 = gr.Chatbot(visible=False, elem_id="bot5")with gr.Row():select_story = gr.Radio(148 unmodified lines],queue=False,).then(lambda: create_visibility_updates(True, 13),None,[openai_img,aws_img,google_img,mtk_img,ntu_img,story_title,story_description,openai_description,aws_description,google_description,mtk_description,ntu_description,start_generate_story,],queue=False,)get_first_llm_response_args = dict(fn=get_llm_response_once,inputs=[completion_reward, player_logs],22 unmodified linesqueue=False,)get_fifth_llm_response_args = dict(fn=get_llm_response_once,inputs=[completion_reward, player_logs],outputs=bot5,queue=False,)start_generate_story.click(lambda: gr.update(visible=False), None, start_generate_story, queue=False).then(lambda: create_visibility_updates(True, 6),None,[bot1, bot2, bot3, bot4, bot5, weaving],queue=False,).then(**get_first_llm_response_args3 unmodified lines**get_third_llm_response_args).then(**get_fourth_llm_response_args).then(**get_fifth_llm_response_args).then(lambda: gr.update(visible=True), None, [select_story], queue=False).then(46 unmodified lines)start_generate_certificate.click(lambda: create_visibility_updates(False, 21),None,[openai_img,aws_img,google_img,mtk_img,ntu_img,story_title,story_description,openai_description,aws_description,google_description,mtk_description,ntu_description,bot1,bot2,bot3,bot4,bot5,select_story,processing,cancel_story,
373 unmodified lines37437537637737837954 unmodified lines434435436437373 unmodified linesborder: None !important;}#processing {margin: 20vh 10vw;height: 30vh;54 unmodified lines[data-testid="蔚藍-radio-label"] {background: rgba(66, 130, 227, 0.4) !important;}373 unmodified lines37437537637737837938038138238338454 unmodified lines439440441442443444445446373 unmodified linesborder: None !important;}#bot4 .message {background: rgba(227, 66, 104, 0.4) !important;border: None !important;}#processing {margin: 20vh 10vw;height: 30vh;54 unmodified lines[data-testid="蔚藍-radio-label"] {background: rgba(66, 130, 227, 0.4) !important;}[data-testid="紅寶石-radio-label"] {background: rgba(227, 66, 104, 0.4) !important;}
47 unmodified lines48495051525354555657585960616263642 unmodified lines6768697071727374757677787980818283848586878889909192939495969798991001011021031045 unmodified lines1101111121131141151161 unmodified line11811912012112212313 unmodified lines137138139140141142147 unmodified lines29029129229329429559 unmodified lines35535635735835936051 unmodified lines41241341441541641767 unmodified lines48548648748848949049147 unmodified linesself.paragraph_aws = Noneself.paragraph_google = Noneself.paragraph_mtk = Noneself.player_certificate_url = Noneself.openai_agent = OpenAIAgent()self.aws_agent = AWSAgent()self.google_agent = GoogleAgent()self.mtk_agent = MTKAgent()self.agents_responses = {}self.agent_list = [self.openai_agent,self.aws_agent,self.google_agent,self.mtk_agent,]self.shuffled_response_order = {}self.pop_response_order = []2 unmodified lines"aws": self.paragraph_aws,"google": self.paragraph_google,"mtk": self.paragraph_mtk,}def get_llm_response(self, player_logs):openai_story = self.openai_agent.get_story(player_logs)aws_story = self.aws_agent.get_story(player_logs)google_story = self.google_agent.get_story(player_logs)mtk_story = self.mtk_agent.get_story(player_logs)agents_responses = {"openai": openai_story,"aws": aws_story,"google": google_story,"mtk": mtk_story,}self.paragraph_openai = agents_responses["openai"]self.paragraph_aws = agents_responses["aws"]self.paragraph_google = agents_responses["google"]self.paragraph_mtk = agents_responses["mtk"]response_items = list(agents_responses.items())random.shuffle(response_items)self.shuffled_response_order = {str(index): agent for index, (agent, _) in enumerate(response_items)}shuffled_responses = tuple(response for _, response in response_items)return ([(None, shuffled_responses[0])],[(None, shuffled_responses[1])],[(None, shuffled_responses[2])],[(None, shuffled_responses[3])],)def get_llm_response_once(self, player_logs):if self.agent_list:# Randomly select and remove an agent from the list5 unmodified linesself.agents_responses[agent.name] = storyself.pop_response_order.append(agent.name)if len(self.pop_response_order) == 4:self.shuffled_response_order = {str(index): agent for index, agent in enumerate(self.pop_response_order)}1 unmodified lineself.paragraph_aws = self.agents_responses["aws"]self.paragraph_google = self.agents_responses["google"]self.paragraph_mtk = self.agents_responses["mtk"]return [(None, story)]13 unmodified lines"索拉拉": "1","薇丹特": "2","蔚藍": "3",}self.player_selected_character = player_selected_characterself.player_selected_model = self.shuffled_response_order[147 unmodified lineslogging.error(f"OpenAI Attempt {retry_attempts}: {e}")time.sleep(1 * retry_attempts)def get_background(self):client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))image_url = None59 unmodified lineslogging.error(f"AWS Attempt {retry_attempts}: {e}")time.sleep(1 * retry_attempts)class GoogleAgent:from google.cloud import aiplatform51 unmodified lineslogging.error(f"Google Attempt {retry_attempts}: {e}")time.sleep(1 * retry_attempts)class MTKAgent:def __init__(self):67 unmodified linesretry_attempts += 1logging.error(f"MTK Attempt {retry_attempts}: {e}")time.sleep(1 * retry_attempts)class ImageProcessor:@staticmethod47 unmodified lines48495051525354555657585960616263646566672 unmodified lines7071727374757677785 unmodified lines848586878889901 unmodified line9293949596979813 unmodified lines112113114115116117118147 unmodified lines26626726826927027127227359 unmodified lines33333433533633733833934051 unmodified lines39239339439539639739839967 unmodified lines46746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154247 unmodified linesself.paragraph_aws = Noneself.paragraph_google = Noneself.paragraph_mtk = Noneself.paragraph_ntu = Noneself.player_certificate_url = Noneself.openai_agent = OpenAIAgent()self.aws_agent = AWSAgent()self.google_agent = GoogleAgent()self.mtk_agent = MTKAgent()self.ntu_agent = NTUAgent()self.agents_responses = {}self.agent_list = [self.openai_agent,self.aws_agent,self.google_agent,self.mtk_agent,self.ntu_agent,]self.shuffled_response_order = {}self.pop_response_order = []2 unmodified lines"aws": self.paragraph_aws,"google": self.paragraph_google,"mtk": self.paragraph_mtk,"ntu": self.paragraph_ntu,}def get_llm_response_once(self, player_logs):if self.agent_list:# Randomly select and remove an agent from the list5 unmodified linesself.agents_responses[agent.name] = storyself.pop_response_order.append(agent.name)if len(self.pop_response_order) == 5:self.shuffled_response_order = {str(index): agent for index, agent in enumerate(self.pop_response_order)}1 unmodified lineself.paragraph_aws = self.agents_responses["aws"]self.paragraph_google = self.agents_responses["google"]self.paragraph_mtk = self.agents_responses["mtk"]self.paragraph_ntu = self.agents_responses["ntu"]return [(None, story)]13 unmodified lines"索拉拉": "1","薇丹特": "2","蔚藍": "3","紅寶石": "4",}self.player_selected_character = player_selected_characterself.player_selected_model = self.shuffled_response_order[147 unmodified lineslogging.error(f"OpenAI Attempt {retry_attempts}: {e}")time.sleep(1 * retry_attempts)return '星際夥伴短時間內寫了太多故事,需要休息一下,請稍後再試,或是選擇其他星際夥伴的故事。'def get_background(self):client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))image_url = None59 unmodified lineslogging.error(f"AWS Attempt {retry_attempts}: {e}")time.sleep(1 * retry_attempts)return '星際夥伴短時間內寫了太多故事,需要休息一下,請稍後再試,或是選擇其他星際夥伴的故事。'class GoogleAgent:from google.cloud import aiplatform51 unmodified lineslogging.error(f"Google Attempt {retry_attempts}: {e}")time.sleep(1 * retry_attempts)return '星際夥伴短時間內寫了太多故事,需要休息一下,請稍後再試,或是選擇其他星際夥伴的故事。'class MTKAgent:def __init__(self):67 unmodified linesretry_attempts += 1logging.error(f"MTK Attempt {retry_attempts}: {e}")time.sleep(1 * retry_attempts)return '星際夥伴短時間內寫了太多故事,需要休息一下,請稍後再試,或是選擇其他星際夥伴的故事。'class NTUAgent:def __init__(self):self.name = "ntu"def get_story(self, user_log):system_prompt = """我正在舉辦一個學習型的活動,我為學生設計了一個獨特的故事機制,每天每個學生都會收到屬於自己獨特的冒險紀錄,現在我需要你協助我將這些冒險紀錄,製作成一段冒險故事,請- 以「你」稱呼學生- 可以裁減內容以將內容限制在 1024 個 token 內- 試著合併故事記錄成一段連貫、有吸引力的故事- 請使用 zh_TW- 請直接回覆故事內容,不需要回覆任何訊息"""user_log = f"""```{user_log}```"""url = 'http://api.twllm.com:20002/v1/chat/completions'data = {"model": "yentinglin/Taiwan-LLM-13B-v2.0-chat","messages": f"{system_prompt}, 以下是我的冒險故事 ```{user_log}```","temperature": 0.7,"top_p": 1,"n": 1,"max_tokens": 2048,"stop": ["string"],"stream": False,"presence_penalty": 0,"frequency_penalty": 0,"user": "string","best_of": 1,"top_k": -1,"ignore_eos": False,"use_beam_search": False,"stop_token_ids": [0],"skip_special_tokens": True,"spaces_between_special_tokens": True,"add_generation_prompt": True,"echo": False,"repetition_penalty": 1,"min_p": 0}headers = {'accept': 'application/json','Content-Type': 'application/json'}retry_attempts = 0while retry_attempts < 5:try:response = requests.post(url, headers=headers, data=json.dumps(data)).json()response_text = response["choices"][0]["message"]["content"]chinese_converter = OpenCC("s2tw")return chinese_converter.convert(response_text)except Exception as e:retry_attempts += 1logging.error(f"NTU Attempt {retry_attempts}: {e}")time.sleep(1 * retry_attempts)return '星際夥伴短時間內寫了太多故事,需要休息一下,請稍後再試,或是選擇其他星際夥伴的故事。'class ImageProcessor:@staticmethod
1 unmodified line234567891011121 unmodified lineimport gradio as grdef get_llm_response(completion_reward, *args):return completion_reward.get_llm_response(*args)def get_llm_response_once(completion_reward, *args):return completion_reward.get_llm_response_once(*args)1 unmodified line2345671 unmodified lineimport gradio as grdef get_llm_response_once(completion_reward, *args):return completion_reward.get_llm_response_once(*args)
app.py+30-12
css/style.css+9
utils/completion_reward.py+82-31
utils/completion_reward_utils.py-5