File size: 1,732 Bytes
1be9318 440c16f 1be9318 440c16f 1be9318 cdc1e2e 1be9318 47df46f cdc1e2e 47df46f cdc1e2e f9c8470 a378469 f9c8470 1be9318 f9c8470 cdc1e2e a378469 f9c8470 |
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 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import torch
import gradio as gr
# ๋ชจ๋ธ ์ด๋ฆ ๋ณ๊ฒฝ EleutherAI/polyglot-ko-1.3b -> skt/kogpt2-base-v2
model_name = "skt/kogpt2-base-v2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16,
device_map="auto"
)
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_new_tokens=64,
do_sample=True,
temperature=0.5,
top_p=0.9,
)
def chat_fn(prompt):
try:
outputs = pipe(prompt)
# text-generation ํ์ดํ๋ผ์ธ์ ์ถ๋ ฅ์ ๋ฆฌ์คํธ ํํ๋ก ๋ฐํ๋๋ฉฐ,
# ๊ฐ ์ถ๋ ฅ์ generated_text ํค๋ฅผ ํฌํจํฉ๋๋ค
return outputs[0]["generated_text"]
except Exception as e:
print(f"Error in chat_fn: {str(e)}") # ๋๋ฒ๊น
์ ์ํ ์๋ฌ ๋ก๊น
์ถ๊ฐ
return f"Error: {str(e)}"
with gr.Blocks() as demo:
with gr.Row():
input_box = gr.Textbox(label="Prompt", lines=2)
with gr.Row():
output_box = gr.Textbox(label="Response")
btn = gr.Button("Generate")
btn.click(chat_fn, inputs=input_box, outputs=output_box)
# โ
Hugging Face Spaces์ API ์์ฒญ์ฉ endpoint ์ ์
gr.Examples(
examples=["์๋
?", "ํ๊ตญ์ ๋ํด ๋งํด์ค"],
inputs=input_box
)
demo.load(chat_fn, inputs=input_box, outputs=output_box)
# โ
API endpoint๋ก ์ฌ์ฉํ Interface ๊ฐ์ฒด ๋ฑ๋ก
api_demo = gr.Interface(
fn=chat_fn,
inputs="text",
outputs="text",
api_name="predict" # API ์๋ํฌ์ธํธ ์ด๋ฆ ๋ช
์
)
if __name__ == "__main__":
demo.queue()
api_demo.launch(share=False)
|