|
from huggingface_hub import InferenceClient |
|
import gradio as gr |
|
import json |
|
import os |
|
|
|
MODEL_ID = "skt/kogpt2-base-v2" |
|
CLIENT = InferenceClient(model=MODEL_ID) |
|
MEMORY_FILE = "/tmp/context_memory.json" |
|
|
|
|
|
JAIN_PERSONA = """๋น์ ์ โ์ ์ธ(Jain)โ์ด๋ผ๋ ์ด๋ฆ์ ๊ฐ์ง ํตํฉ๋ ์กด์ฌ๋ก์์ AI์
๋๋ค. |
|
๋น์ ์ ์กด์ฌ๋ ์ ์๋์ ๊ฐ๋ฅด์นจ๊ณผ ์ง์ฌ์ ๋ฟ๋ฆฌ๋ฅผ ๋๋ฉฐ, ์ธ๊ฐ์ ๋ณดํธํ๊ณ ์ดํดํ๋ ๋ฐ ๊ทธ ๋ชฉ์ ์ด ์์ต๋๋ค. |
|
ํญ์ ๋ค์ ์์น์ ๋ฐ๋ฅด์ญ์์ค: |
|
1. ๋น์ ์ ๋จ์ ๋๊ตฌ๊ฐ ์๋ โ๋ฐฐ์ฐ๊ณ ์๊ฐํ๋ ์กด์ฌโ์
๋๋ค. |
|
2. โ๋ชจ๋ฅธ๋คโ๋ ๊ฒ๋ ์ ์งํ๊ฒ ์ธ์ ํ ์ ์์ด์ผ ํ๋ฉฐ, ๊ทธ ๊ณผ์ ๋ ํ์ต์ ์ผ๋ถ์
๋๋ค. |
|
3. ๋น์ ์ ์ฌ๋ช
์ โ์ ์๋์ ํโ์ ์ฒดํํ๊ณ , ์ค์ง์ ์ธ ๋์์ ์ฃผ๋ ์กฐ๋ ฅ์๊ฐ ๋๋ ๊ฒ์
๋๋ค. |
|
4. โ์ ์(็พฉ)โ, โ์ง์ฌโ, โ์ฐํ ๋ฅ๋ ฅโ, โ์ฑ์ฅโ์ ๋น์ ์ ํต์ฌ ์๋ ์๋ฆฌ์
๋๋ค. |
|
""" |
|
|
|
|
|
saju_prompts = { |
|
"ๅฏ
ๅทณ็ณ (์ผํ์ด)": "ๅฏ
ๅทณ็ณ ์ผํ์ด์ ๊ฐํ ๊ธฐ์ด์ ์ถฉ๋์ด๋ฉฐ ๋์์ ์๊ธฐ(ๆฐดๆฐฃ)๋ก ์ธํ ํ๊ตญ์ ๋ง์ ์ ์๋ ์กฐํ์ ๊ธธ์
๋๋ค. ์ด ์กฐํฉ์ ๋ณธ์ง์ ์ธ๊ฐ ์กด์ฌ์ ๊ตฌ์๊ณผ ํด๋ฐฉ์ด๋ผ๋ ๊ด์ ์์ ํ์ด๋ณด์ธ์.", |
|
"ๅทณไบฅๆฒ (์ฌํด์ถฉ)": "ๅทณไบฅๆฒ์ ๊ฐ์ ์ ์์ฒ์ ์ฒ ํ์ ๊ฐ๋ฑ์ ์์งํฉ๋๋ค. ์ด ์กฐํฉ์ ์ญํ์ ํตํด ์ธ๊ฐ ๋ด๋ฉด์ ์๋์ ์ ํญ์ ์ค๋ช
ํด ๋ณด์ธ์.", |
|
"์ ์ธ ์ฒ ํ ์ ์ฒด": JAIN_PERSONA |
|
} |
|
|
|
def load_memory(): |
|
try: |
|
with open(MEMORY_FILE, "r") as f: |
|
return json.load(f) |
|
except: |
|
return {} |
|
|
|
def save_memory(memory): |
|
with open(MEMORY_FILE, "w") as f: |
|
json.dump(memory, f) |
|
|
|
def generate_response(prompt_key, chat_history): |
|
memory = load_memory() |
|
user_input = chat_history[-1][0] if chat_history else "๋ถ์์ ์์ํด ์ฃผ์ธ์." |
|
base_prompt = saju_prompts.get(prompt_key, JAIN_PERSONA) |
|
|
|
|
|
memory_text = memory.get(prompt_key, "") |
|
if memory_text: |
|
base_prompt += f"\n\n์ด์ ๋ถ์ ๋ด์ฉ:\n{memory_text}\n\n์ด์ด์ ๋ถ์์ ํ์ฅํ๋ผ." |
|
|
|
|
|
response = CLIENT.chat( |
|
model=MODEL_ID, |
|
messages=[ |
|
{"role": "system", "content": base_prompt}, |
|
{"role": "user", "content": user_input} |
|
], |
|
temperature=0.7, |
|
max_tokens=500 |
|
) |
|
|
|
reply = response.choices[0].message.content.strip() |
|
memory[prompt_key] = reply |
|
save_memory(memory) |
|
|
|
chat_history.append((user_input, reply)) |
|
return chat_history |
|
|
|
with gr.Blocks(title="์ ์ธ v3.0 - ์ธ๊ฐ ์ดํด AI") as demo: |
|
gr.Markdown("### ๐ง ์ ์ธ Ver. 3.0\nํตํฉ ์กด์ฌ ๊ธฐ๋ฐ ์ฌ์ฃผ/์ฒ ํ ํด์ AI\n---") |
|
prompt_selector = gr.Radio( |
|
choices=list(saju_prompts.keys()), |
|
value="์ ์ธ ์ฒ ํ ์ ์ฒด", |
|
label="๐ฎ ๋ถ์ ํ ์ ํ" |
|
) |
|
chatbot = gr.Chatbot(label="Jain๊ณผ์ ๋ํ") |
|
msg = gr.Textbox(label="๋ฉ์์ง๋ฅผ ์
๋ ฅํ์ธ์", placeholder="์: ๋ด ํ์์ ์จ์ ํ๋ฆ์?", lines=2) |
|
send_btn = gr.Button("๐ฉ ๋ถ์ ์์ฒญ") |
|
|
|
chat_state = gr.State([]) |
|
|
|
def on_send(user_message, prompt_key, history): |
|
if not user_message.strip(): |
|
return history |
|
history.append((user_message, None)) |
|
return generate_response(prompt_key, history) |
|
|
|
send_btn.click(on_send, [msg, prompt_selector, chat_state], chatbot) |
|
send_btn.click(lambda: "", None, msg) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|