Spaces:
Runtime error
Runtime error
File size: 3,164 Bytes
e48ab6b 2c77c32 8cccf6a cb92e5b 5c54d1b 669a4c0 e48ab6b 24fbd15 e48ab6b 50be456 3e48437 61e57ac 8fefd32 bfae98b 68b221f bfae98b 8fefd32 ea6b9f5 61e57ac 7b0437a 4c9e80d 7b0437a 96906d9 3e48437 5ec78b2 ea6b9f5 8fefd32 d7136d1 a2f0814 8fefd32 96906d9 7b0437a 02c9e3b 7b0437a 4c9e80d 7b0437a 96906d9 e48ab6b f0e04ff e48ab6b 24fbd15 1418034 e48ab6b |
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 |
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 npc_data
import requests, os, re, asyncio, json
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 = "daekeun-ml/Llama-2-ko-instruct-13B"
#daekeun-ml/Llama-2-ko-instruct-13B
#quantumaikr/llama-2-70b-fb16-korean
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = None
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 init():
global model
if check(model_name):
model = AutoDistributedModelForCausalLM.from_pretrained(model_name)
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
if check(model_name):
prom = f"""<s><<SYS>>
{npc_data.system_message}<</SYS>>
[INST]
{npc_data}
### ๋ช
๋ น์ด:
๋ํ๊ธฐ๋ก์ ์ฐธ๊ณ ํ์ฌ Rabbit์ด ํ ๋ง์ ์์ฐ์ค๋ฝ๊ฒ ์์ฑํด์ฃผ์ธ์. ํ ๋ฌธ์ฅ๋ง ์์ฑํ์ธ์.
์ด์ ๋ํ๊ธฐ๋ก:
'''
{history}
'''
### User:
{user_message}
### Rabbit:[/INST]
"""
inputs = tokenizer(prom, return_tensors="pt")["input_ids"]
outputs = model.generate(inputs, max_new_tokens=100)
print(tokenizer.decode(outputs[0]))
output = tokenizer.decode(outputs[0])[len(prom):-1]
print(output)
else:
output = "no model"
# add_transaction endpoint
response = requests.post("https://ldhldh-api-for-unity.hf.space/run/predict_5", json={
"data": [
id,
"inference",
"### input:\n" + prompt + "\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) |