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)
|