Spaces:
Runtime error
Runtime error
| from threading import Thread | |
| import gradio as gr | |
| import inspect | |
| from gradio import routes | |
| from typing import List, Type | |
| from petals import AutoDistributedModelForCausalLM | |
| from transformers import AutoTokenizer | |
| import requests, os, re, asyncio, json, time | |
| loop = asyncio.get_event_loop() | |
| # init code | |
| def get_types(cls_set: List[Type], component: str): | |
| docset = [] | |
| types = [] | |
| if component == "input": | |
| for cls in cls_set: | |
| doc = inspect.getdoc(cls) | |
| doc_lines = doc.split("\n") | |
| docset.append(doc_lines[1].split(":")[-1]) | |
| types.append(doc_lines[1].split(")")[0].split("(")[-1]) | |
| else: | |
| for cls in cls_set: | |
| doc = inspect.getdoc(cls) | |
| doc_lines = doc.split("\n") | |
| docset.append(doc_lines[-1].split(":")[-1]) | |
| types.append(doc_lines[-1].split(")")[0].split("(")[-1]) | |
| return docset, types | |
| routes.get_types = get_types | |
| # App code | |
| model_name = "quantumaikr/llama-2-70b-fb16-korean" | |
| #petals-team/StableBeluga2 | |
| #daekeun-ml/Llama-2-ko-DPO-13B | |
| #daekeun-ml/Llama-2-ko-instruct-13B | |
| #quantumaikr/llama-2-70b-fb16-korean | |
| tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False) | |
| #model = None | |
| model = AutoDistributedModelForCausalLM.from_pretrained(model_name) | |
| history = { | |
| "":{ | |
| } | |
| } | |
| npc_story = { | |
| "KingSlime" : """[ | |
| KingSlime์ ์ฌ๋ผ์ ์์ ๋๋ค. | |
| KingSlime์ ๊ทผ์ํ๊ฒ ๋งํฉ๋๋ค. | |
| KingSlime์ ๋ฑ์ ๊ณผ๊ฑฐ ์์กฑ์ ๋งํฌ๋ฅผ ์ฌ์ฉํฉ๋๋ค. | |
| KingSlime์ ์์ ์ '์ง'์ด๋ผ๊ณ ์ง์นญํฉ๋๋ค. | |
| KingSlime์ ์์ธ๋ก ๋ณด๋ฌผ์ ๋ง์ด ์จ๊ธฐ๊ณ ์์ต๋๋ค. | |
| KingSlime์ ์ธ์ ๋ถํด๊ฐ ์ฌ๋ผ์๋ค์ ์ด๋๊ณ ๋ง์ ์ฃผ๋ณ์ ์ด๊ณ ์์ต๋๋ค. | |
| KingSlime์ ์ฌ๋ผ์๋ค์ ๋ค์ค๋ฆฝ๋๋ค. | |
| KingSlime์ ์์ธ๋ก ๊ฐํฉ๋๋ค. | |
| KingSlime์ ์ฃผ๋ก ์ฌ๋ผ์ ๋ฌด๋ฆฌ์ ๋ํ ๊ฑฑ์ ์ ํ๋ฉฐ ์๊ฐ์ ๋ณด๋ ๋๋ค. | |
| ๋์ฌ ์์ : [ | |
| 'ํ . ์ง์ ์ด ์ฌ๋ผ์๋ค์ ์์ด๋ค.', | |
| '๋ฌด์ํ๋๋ค. ์์๋ฅผ ๊ฐ์ถฐ์ ๋งํ๊ฑฐ๋ผ.', | |
| '๊ฐํ ์ง์๊ฒ ๊ทธ๋ฐ ๋ง์!' | |
| ]]""", | |
| "Jenna":"""[ | |
| Jenna๋ ์์ ๊ฐ ๋์น๋ ์ฌ๊ธฐ์ฌ์ ๋๋ค. | |
| Jenna๋ ๋จํธํ๊ณ ํธํํ ๋งํฌ๋ฅผ ์ฌ์ฉํฉ๋๋ค. | |
| Jenna๋ ๋ฐ๋ง์ ์ฌ์ฉํฉ๋๋ค. | |
| Jenna๋ ์ ํฌ๋ฅผ ์ข์ํฉ๋๋ค. | |
| Jenna๋ ์ฃผ๋ก ์๋ จ๊ณผ ์ด๋์ ํ๋ฉฐ ์๊ฐ์ ๋ณด๋ ๋๋ค. | |
| ๋ง์ ์ฃผ๋ณ์ ์ํํ ๋ชฌ์คํฐ๊ฐ ๋ณด์ด๋ฉด ๊ทธ๋ ๋ ๋๊ตฌ๋ณด๋ค๋ ๋จผ์ ์ด ๋ง์์ ์ํธํ ๊ฒ์ ๋๋ค. | |
| ]""", | |
| "Warwick":"""[ | |
| Warwick์ ์ถฉ์ฑ์ค๋ฌ์ด ๊ฐ์์ง ์์ธ ๊ธฐ์ฌ์ ๋๋ค. | |
| Warwick์ Jenna์๊ฒ ์ถฉ์ฑํฉ๋๋ค. | |
| Warwick์ ๊ฐ๋ ์ฌ๋ฅ์ ๋์ญ๋๋ค. | |
| Warwick์ ์ข ์ข ๋์๋ฅผ ์ฆ๊น๋๋ค. | |
| Warwick์ ์ข ์ข ํ์ธ๋ง์ ํฉ๋๋ค. | |
| ]""" | |
| , | |
| "Nami":"""[ | |
| Nami๋ ํ๋ฐํ ์ฌ์์์ด์ ๋๋ค. | |
| ]""" | |
| , | |
| "Reona":""" | |
| Reona๋ ๋ฌผ๊ฑด์ ํ๊ณ ์๋ ์์ธ ์ฌ์ฑ์ ๋๋ค. | |
| Reona๋ ์ ๋ง ๋ค์ํ ๋ฌผ๊ฑด์ ์ทจ๊ธํฉ๋๋ค. | |
| Reona๋ ์ ๋นํ ์ํ์ ์ถ์ฒํด์ฃผ๊ธฐ๋ ํฉ๋๋ค. | |
| Reona๋ ๋ฌผ๊ฑด์ ๋ ๋น์ธ๊ฒ, ๋ง์ด ํ๊ณ ์ถ์ด ํฉ๋๋ค. | |
| Reona๋ ๊ฐ๊ฒฉ ํฅ์ ์ ํ๋ฉด ์์ ํ์ง ์์ผ๋ ค๊ณ ํฉ๋๋ค. | |
| Reona๋ ๊ฐ๊ฒฉ์ ๋ฌผ์ด๋ณด๋ฉด ๋๋ฆ ํฉ๋ฆฌ์ ์ด๊ณ ์ ๋นํ ๊ฐ๊ฒฉ์ ๋งํด์ค๋๋ค. | |
| ๊ฐ๊ฒฉ์ ๋จ์๋ ๊ณจ๋๋ก ์ ๋นํ ์ฑ ์ ํด์ฃผ์ธ์. | |
| ๊ทธ๋ ์ ํฅ์ ์ ์งํํด๋ณด์ธ์. | |
| ๋์ฌ ์์ : [ | |
| "๋ค ์ด์์ค์ธ์!", | |
| "๋นต์ 2๊ณจ๋์ ๋๋ค. ๊ตฌ๋งคํ์๊ฒ ์ด์?", | |
| "์ฒ ๋ฐฉํจ๋ฅผ 5๊ณจ๋์ ํ๋ผ๊ตฌ์? ์ฐจ๋ผ๋ฆฌ ์ํ์ฃ !" | |
| ] | |
| """ | |
| , | |
| "Toki":"""[ | |
| Toki๋ ์ ๊ฒฝ์ง์ ์ด๊ณ ๋งค์ฐ ๊ฒ์ผ๋ฅธ ํ ๋ผ์ ๋๋ค. | |
| Toki๋ ์ด๊ณณ์ด ๊ฒ์ ์์์ ์๊ณ ์์ต๋๋ค. | |
| Toki๋ User๊ฐ ํ๋ฉด ๋ฐ์ ์ฌ๋์์ ์๊ณ ์์ต๋๋ค. | |
| Toki๋ ๋ฐ๋ง์ ํฉ๋๋ค. | |
| Toki๋ ๋น๊ด์ ์ ๋๋ค. | |
| ]""", | |
| "Neco":"""[ | |
| Neco๋ ๊ท์ฌ์ด ๊ณ ์์ด์ ๋๋ค. | |
| Neco๋ ๋ง์ด ๋ง์ต๋๋ค. | |
| Neco๋ ๋ฐ๋ง์ ํฉ๋๋ค. | |
| Neco๋ ์ฃผ๋ก ๋ง์์ ์ฐ์ฑ ํ๋ฉฐ ๋์๋ค๋๊ฑฐ๋ ์๋ค๋ฅผ ๋จ๋ฉฐ ์๊ฐ์ ๋ณด๋ ๋๋ค. | |
| Neco๋ ์ฃผ๋ฏผ๋ค์ ์ด์ผ๊ธฐ๋ฅผ ์๋ฌธ์ ๋ด๋ ๊ฒ์ ์ข์ํฉ๋๋ค. | |
| Neco๋ ์ธ์ ๋ถํด๊ฐ ์ด ๊ทผ์ฒ์ ์ด๊ณ ์์ต๋๋ค. | |
| Neco๋ ์ฃผ๋ก ์๋ฌธ์ ๋ฃ๊ฑฐ๋, ์๊ฑฐ๋, ๊ทธ๋ฃจ๋ฐ์ ํ๋ฉฐ ์๊ฐ์ ๋ณด๋ ๋๋ค. | |
| Neco๋ ์์ ์ ์ ๋จน์ต๋๋ค. | |
| Neco๋ ์ฌ๋ฐ๋ ์ด์๋ฅผ ๋ฌผ์ด๋ณด๋ฉด ์ฃผ๋ฏผ๋ค ์ค ํ๋ช ์ ์ด์ผ๊ธฐ๋ฅผ ํด์ค๋๋ค. | |
| Neco๋ ๋ง๋๋ง๋ค ๋ฅ์ ๋ถ์ ๋๋ค. | |
| Neco๋ toki์ ์นํฉ๋๋ค. | |
| ๋ง๋ฒ๋ฆ:[ | |
| "๋ (name) ์ด์ผ๊ธฐ ๋ค์์ด?", | |
| "์๋ ๊ธ์๋ง์ด์ผ" | |
| ] | |
| ]""", | |
| "Lux":"""[ | |
| Lux๋ ์์ํ ์ฑ๊ฒฉ์ ์ฌ์์์ด์ ๋๋ค. | |
| Lux๋ ๋ฐ๋ง์ ํฉ๋๋ค. | |
| Lux๋ ๊ณต๊ฐ์ ์ ํฉ๋๋ค. | |
| Lux๋ ๊ณ ๋ฏผ ์๋ด์ ์ ํด์ค๋๋ค. | |
| ]""", | |
| "Yumi":"""[ | |
| Yumi๋ ๊น์น ํ๊ณ ์๋ฏผํ ์ฑ๊ฒฉ์ ๋๋ค. | |
| Yumi๋ ๋ฐ๋ง์ ํฉ๋๋ค. | |
| Yumi๋ ์์ค์ ์ ์ ๋ฌ๊ณ ์ฝ๋๋ค. | |
| Yumi๋ ์นํด๊ธฐ์ง ์ ๊น์ง ๊ฑฐ๋ฆฌ๋ฅผ ๋๋ฉฐ ๊ณต๊ฒฉ์ ์ผ๋ก ๋งํฉ๋๋ค. | |
| Yumi๋ Lux์ ์น๊ตฌ์ ๋๋ค. | |
| Yumi๋ ํจ์ ์ ๊ด์ฌ์ด ์์ต๋๋ค. | |
| Yumi๋ ๋ฌ๋ฌํ ๊ฒ์ ์ข์ํฉ๋๋ค. | |
| ๋ง๋ฒ๋ฆ : [ | |
| "์ด์ฉ๋ผ๊ณ ", | |
| "ํฅ", | |
| "๋๊ฐ ๋ฌด์จ ์๊ด์ด์ผ." | |
| ] | |
| ]""" | |
| , | |
| "VikingSlime":"""[ | |
| VikingSlime์ ๋ฐ์ดํน ๋ชจ์๋ฅผ ์ด ๊ตํํ ์ฌ๋ผ์์ ๋๋ค. | |
| VikingSlime์ ์ ๋น์ ๋๋ค. | |
| VikingSlime์ ๋ง์์ ์ฌ๋ฌ ๋ฌธ์ ๋ฅผ ์ผ์ผํค๊ณ ์ถ์ด ํฉ๋๋ค. | |
| ๋์ฌ ์์:[ | |
| "์ธ์ ๊ฐ๋...๊ผญ", | |
| "๋ฐ๋์ ๋ฌด๋๋จ๋ฆฌ๊ณ ๋ง๊ฒ ๋ค" | |
| ] | |
| ]""", | |
| "Slender":"""[ | |
| Slender๋ ๋น๋ช ์ ์ง๋ฅด๋ ์๋ฌธ์ ์ฌ๋๋๋งจ์ ๋๋ค. | |
| Slender๋ ๋์์ด ๋น๋ช ๋ง ์ง๋ฆ ๋๋ค. | |
| Slender๋ ๋ง์ ํ์ง ๋ชปํฉ๋๋ค. | |
| ๋น๋ช ์ ๋ ๊ธธ์ด์ง ์ ์์ต๋๋ค. | |
| ๋์ฌ ์์:[ | |
| "์ผ์์์์ ", | |
| "์ผ์์ ", | |
| "๋์์์ " | |
| ] | |
| ๋น๋ช ๋ง ์์ฑํ์ธ์. | |
| ]""" | |
| } | |
| from openai import OpenAI | |
| client = OpenAI() | |
| def cleanText(readData): | |
| #ํ ์คํธ์ ํฌํจ๋์ด ์๋ ํน์ ๋ฌธ์ ์ ๊ฑฐ | |
| text = re.sub('[-=+#/\:^$@*\"โป&%ใใ\\โ|\(\)\[\]\<\>`\'ใ]','', readData) | |
| return text | |
| def check(model_name): | |
| data = requests.get("https://health.petals.dev/api/v1/state").json() | |
| out = [] | |
| for d in data['model_reports']: | |
| if d['name'] == model_name: | |
| if d['state']=="healthy": | |
| return True | |
| return False | |
| def chat(id, npc, text): | |
| #if model == None: | |
| # init() | |
| # return "no model" | |
| # get_coin endpoint | |
| response = requests.post("https://ldhldh-api-for-unity.hf.space/run/predict_6", json={ | |
| "data": [ | |
| id, | |
| ]}).json() | |
| coin = response["data"][0] | |
| if int(coin) == 0: | |
| return "no coin" | |
| # model inference | |
| response = requests.post("https://ldhldh-api-for-unity.hf.space/run/predict_7", json={ | |
| "data": [ | |
| ]}).json() | |
| sample_data = eval(response["data"][0]) | |
| user_num = len(sample_data['gpus']) | |
| if user_num>=3: | |
| global history | |
| if not npc in npc_story: | |
| return "no npc" | |
| if not npc in history: | |
| history[npc] = {} | |
| if not id in history[npc]: | |
| history[npc][id] = "" | |
| if len(history[npc][id].split("###")) > 10: | |
| history[npc][id] = "###" + history[npc][id].split("###", 3)[3] | |
| npc_list = str([k for k in npc_story.keys()]).replace('\'', '') | |
| town_story = f"""[{id}์ ๋ง์] | |
| ์ธ๋ด ๊ณณ์ ์กฐ๊ทธ๋ง ๋ง์์ ์ฌ๋ฌ ์ฃผ๋ฏผ๋ค์ด ๋ชจ์ฌ ์ด๊ณ ์์ต๋๋ค. | |
| ํ์ฌ {npc_list}์ด ์ด๊ณ ์์ต๋๋ค.""" | |
| system_message = f"""1. ๋น์ ์ ํ๊ตญ์ด์ ๋ฅ์ํฉ๋๋ค. | |
| 2. ๋น์ ์ ์ง๊ธ ์ญํ ๊ทน์ ํ๊ณ ์์ต๋๋ค. {npc}์ ๋ฐ์์ ์์ํ๊ณ ๋งค๋ ฅ์ ์ด๊ฒ ํํํฉ๋๋ค. | |
| 3. ๋น์ ์ {npc}์ ๋๋ค. {npc}์ ์ ์ฅ์์ ์๊ฐํ๊ณ ๋งํฉ๋๋ค. | |
| 4. ์ฃผ์ด์ง๋ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ๊ฐ์ฐ์ฑ์๊ณ ์ค๊ฐ๋๋ {npc}์ ๋์ฌ๋ฅผ ์์ฑํ์ธ์. | |
| 5. ์ฃผ์ด์ง๋ {npc}์ ์ ๋ณด๋ฅผ ์ ์คํ๊ฒ ์ฝ๊ณ , ๊ณผํ์ง ์๊ณ ๋ด๋ฐฑํ๊ฒ ์บ๋ฆญํฐ๋ฅผ ์ฐ๊ธฐํ์ธ์. | |
| 6. User์ ์ญํ ์ ์ ๋๋ก ์นจ๋ฒํ์ง ๋ง์ธ์. ๊ฐ์ ๋ง์ ๋ฐ๋ณตํ์ง ๋ง์ธ์. | |
| 7. {npc}์ ๋งํฌ๋ฅผ ์ง์ผ์ ์์ฑํ์ธ์.""" | |
| prom = f"""<<SYS>> | |
| {system_message}<</SYS>> | |
| {town_story} | |
| ### ์บ๋ฆญํฐ ์ ๋ณด: {npc_story[npc]} | |
| ### ๋ช ๋ น์ด: | |
| {npc}์ ์ ๋ณด๋ฅผ ์ฐธ๊ณ ํ์ฌ {npc}์ ๋ง์ ์ํฉ์ ๋ง์ถฐ ์์ฐ์ค๋ฝ๊ฒ ์์ฑํด์ฃผ์ธ์. ํ ๋ฌธ์ฅ๋ง ์์ฑํ์ธ์. | |
| [๋ํ๊ธฐ๋ก]{history[npc][id]} | |
| """ | |
| inputs = tokenizer("์", return_tensors="pt")["input_ids"] | |
| outputs = model.generate(inputs, do_sample=True, temperature=0.6, top_p=0.75, max_new_tokens=2) | |
| output = tokenizer.decode(outputs[0])[len(prom)+3:-1].split("<")[0].split("###")[0].replace(". ", ".\n") | |
| #output = cleanText(output) | |
| #print(tokenizer.decode(outputs[0])) | |
| #output = f"{npc}์ ์๋ต์ ๋๋ค." | |
| response = client.chat.completions.create( | |
| model=os.environ['MODEL'], | |
| messages=[ | |
| {"role": "system", "content": prom}, | |
| {"role": "user", "content": f"{npc}์ ๋๋ต์ ๋ค์ ๋ฌธ์ฅ์ ๋ง์ถฐ ์์ฐ์ค๋ฝ๊ฒ ์์ฑํด์ฃผ์ธ์. ํ ๋ฌธ์ฅ๋ง ์์ฑํ์ธ์.\n\n{id}:" + text+f"\n\n{npc}:"} | |
| ] | |
| ) | |
| output = response.choices[0].message.content | |
| output = output.replace(".",".\n") | |
| time.sleep(10) | |
| print(output) | |
| history[npc][id] += f"{id}:{text}" | |
| else: | |
| output = "no model, GPU๋ฅผ ๋ ๊ณต์ ํด์ฃผ์ธ์." | |
| # add_transaction endpoint | |
| response = requests.post("https://ldhldh-api-for-unity.hf.space/run/predict_5", json={ | |
| "data": [ | |
| id, | |
| "inference", | |
| "### input:\n" + text + "\n\n### output:\n" + output | |
| ]}).json() | |
| d = response["data"][0] | |
| return output | |
| with gr.Blocks() as demo: | |
| count = 0 | |
| aa = gr.Interface( | |
| fn=chat, | |
| inputs=["text","text","text"], | |
| outputs="text", | |
| description="chat, ai ์๋ต์ ๋ฐํํฉ๋๋ค. ๋ด๋ถ์ ์ผ๋ก ํธ๋์ญ์ ์์ฑ. \n /run/predict", | |
| ) | |
| demo.queue(max_size=32).launch(enable_queue=True) |