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)