|
import gradio as gr |
|
from transformers import pipeline, set_seed |
|
import os |
|
|
|
|
|
MODEL_NAME = "jain_architecture_origin_structure" |
|
|
|
|
|
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 |
|
) |
|
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 |
|
|
|
|
|
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, |
|
|
|
|
|
) |
|
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]) |
|
|
|
txt.submit(chat_and_respond, inputs=[txt, chatbot], outputs=[txt, chatbot]) |
|
|
|
if __name__ == "__main__": |
|
|
|
app.launch(share=False) |
|
|