|
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, |
|
|
|
|
|
) |
|
set_seed(42) |
|
except Exception as e: |
|
print("λͺ¨λΈ λ‘λ μλ¬:", 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, |
|
pad_token_id=50256 |
|
) |
|
generated_text = outputs[0]["generated_text"] |
|
|
|
answer = generated_text[len(prompt):].strip() |
|
if not answer: |
|
answer = "λ΅λ³μ μμ±νμ§ λͺ»νμ΅λλ€. λ€μ μλν΄ μ£ΌμΈμ." |
|
return answer |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("<h1 style='text-align:center;color:#4B0082;'>Jain AI Assistant (μ κΈ°λ° μ±λ΄)</h1>") |
|
chatbot = gr.Chatbot(height=400) |
|
txt = gr.Textbox(placeholder="μ¬κΈ°μ μ§λ¬Έμ μ
λ ₯νμΈμ...", lines=3, max_lines=6) |
|
btn = gr.Button("μ μ‘") |
|
|
|
def chat_and_respond(user_message, chat_history): |
|
reply = respond_to_user(user_message) |
|
chat_history = chat_history + [(user_message, reply)] |
|
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__": |
|
|
|
demo.launch(server_name="0.0.0.0", server_port=7860, share=True) |
|
|