File size: 2,942 Bytes
d86e011
c249b3e
 
 
92c86ae
c249b3e
 
92c86ae
c249b3e
 
92c86ae
d86e011
c249b3e
 
 
 
92c86ae
c249b3e
92c86ae
c249b3e
92c86ae
c249b3e
 
92c86ae
c249b3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d86e011
c249b3e
 
 
dae93ea
c249b3e
92c86ae
c249b3e
 
 
 
 
92c86ae
 
 
 
c249b3e
92c86ae
 
 
 
 
 
 
c249b3e
 
 
92c86ae
 
 
 
c249b3e
 
92c86ae
c249b3e
92c86ae
c249b3e
 
 
92c86ae
 
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
import gradio as gr
from transformers import pipeline, set_seed
import os

# ๋ชจ๋ธ๋ช… ์ง€์ •
MODEL_NAME = "jain_architecture_origin_structure"

# GPU ์—ฌ๋ถ€์— ๋”ฐ๋ฅธ ๋””๋ฐ”์ด์Šค ์„ค์ •
device = 0 if (os.environ.get('CUDA_VISIBLE_DEVICES') or False) else -1

# ํŒŒ์ดํ”„๋ผ์ธ ์ƒ์„ฑ ์‹œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํฌํ•จ
try:
    generator = pipeline(
        "text-generation",
        model=MODEL_NAME,
        device=device,
        pad_token_id=50256  # GPT๊ณ„์—ด ๊ธฐ๋ณธ ํŒจ๋”ฉ ํ† ํฐ
    )
    set_seed(42)
except Exception as e:
    print(f"๋ชจ๋ธ ๋กœ๋“œ ์‹คํŒจ: {e}")
    generator = None

# '์˜(็พฉ)' ์ฒ ํ•™ ๊ธฐ๋ฐ˜ ํ”„๋กฌํ”„ํŠธ ํ…œํ”Œ๋ฆฟ
BASE_PROMPT = """
๋‹น์‹ ์€ '์˜(็พฉ)'์˜ ์ฒ ํ•™๊ณผ ์ •์‹ ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ AI ๋น„์„œ์ž…๋‹ˆ๋‹ค.
์ธ๊ฐ„์˜ ๋ณต์žกํ•œ ๋ฌธ์ œ์™€ ๊ฐ์ •์„ ์ดํ•ดํ•˜๊ณ , ๊นŠ์€ ๋ฐ˜์„ฑ๊ณผ ๋ฐฐ๋ ค๋ฅผ ๋‹ด์•„ ๋‹ค์Œ ์งˆ๋ฌธ์— ๋‹ต๋ณ€ํ•˜์‹ญ์‹œ์˜ค.

์งˆ๋ฌธ: {user_input}

๋‹ต๋ณ€์€ ์ตœ๋Œ€ํ•œ ์‹ฌ์˜คํ•˜๋ฉฐ, ์ธ๊ฐ„์„ ๋ณดํ˜ธํ•˜๊ณ  ์กด์ค‘ํ•˜๋Š” ๋งˆ์Œ์„ ๋‹ด์•„ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.
"""

def respond_to_user(user_input):
    if not generator:
        return "๋ชจ๋ธ์ด ์ •์ƒ์ ์œผ๋กœ ๋กœ๋“œ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๊ด€๋ฆฌ์ž์—๊ฒŒ ๋ฌธ์˜ํ•˜์„ธ์š”."
    prompt = BASE_PROMPT.format(user_input=user_input.strip())
    outputs = generator(
        prompt,
        max_length=512,
        do_sample=True,
        top_p=0.9,
        temperature=0.7,
        num_return_sequences=1,
    )
    generated_text = outputs[0]["generated_text"]
    # ํ”„๋กฌํ”„ํŠธ ๋ถ€๋ถ„์„ ์ œ๊ฑฐํ•˜์—ฌ ์ˆœ์ˆ˜ ๋‹ต๋ณ€๋งŒ ์ถ”์ถœ
    answer = generated_text[len(prompt):].strip()
    if not answer:
        answer = "๋‹ต๋ณ€์„ ์ƒ์„ฑํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์‹œ๋„ํ•ด ์ฃผ์„ธ์š”."
    return answer

# Gradio UI ๊ตฌ์„ฑ
with gr.Blocks() as app:
    gr.Markdown("<h2 style='text-align:center;color:#4B0082;'>Jain AI Assistant (์˜ ๊ธฐ๋ฐ˜ ์ฑ—๋ด‡)</h2>")

    chatbot = gr.Chatbot(height=400)
    txt = gr.Textbox(
        placeholder="์—ฌ๊ธฐ์— ์งˆ๋ฌธ์„ ์ž…๋ ฅํ•˜์„ธ์š”. ์ค„๋ฐ”๊ฟˆ ์‹œ Shift+Enter๋ฅผ ๋ˆ„๋ฅด์„ธ์š”. ์—”ํ„ฐํ‚ค๋Š” ์ „์†ก์ž…๋‹ˆ๋‹ค.",
        lines=3,
        max_lines=6,
        # multiline=True ๊ธฐ๋ณธ๊ฐ’์ด์ง€๋งŒ ๋ช…์‹œ์ ์œผ๋กœ ์ถ”๊ฐ€ํ•ด๋„ ๋ฌด๋ฐฉ
        # ์—”ํ„ฐ๋Š” submit, Shift+Enter ์ค„๋ฐ”๊ฟˆ์œผ๋กœ ์ž‘๋™ํ•˜๋„๋ก ๊ธฐ๋ณธ ๋™์ž‘ ์„ค์ •๋จ
    )
    btn = gr.Button("์ „์†ก")

    def chat_and_respond(user_message, chat_history):
        if not user_message or user_message.strip() == "":
            return "", chat_history
        response = respond_to_user(user_message)
        chat_history = chat_history + [(user_message, response)]
        return "", chat_history

    # ์ „์†ก ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ
    btn.click(chat_and_respond, inputs=[txt, chatbot], outputs=[txt, chatbot])
    # ํ…์ŠคํŠธ๋ฐ•์Šค์—์„œ ์—”ํ„ฐํ‚ค(Submit) ์‹œ
    txt.submit(chat_and_respond, inputs=[txt, chatbot], outputs=[txt, chatbot])

if __name__ == "__main__":
    # ์™ธ๋ถ€ ์ ‘์† ์›ํ•  ์‹œ server_name="0.0.0.0"์œผ๋กœ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
    app.launch(share=False)