Update app.py
Browse files
app.py
CHANGED
|
@@ -2,94 +2,29 @@ import torch
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
#
|
| 6 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
print(f"Model loading failed: {e}")
|
| 21 |
-
raise
|
| 22 |
-
|
| 23 |
-
# 2. μ±ν
μμ± ν¨μ
|
| 24 |
-
def generate_response(chat_history, user_input):
|
| 25 |
-
# λν κΈ°λ‘ μ
λ°μ΄νΈ
|
| 26 |
-
chat_history.append({"role": "user", "content": user_input})
|
| 27 |
-
|
| 28 |
-
# ν
νλ¦Ώ μ μ©
|
| 29 |
-
inputs = tokenizer.apply_chat_template(
|
| 30 |
-
chat_history,
|
| 31 |
-
add_generation_prompt=True,
|
| 32 |
-
return_tensors="pt"
|
| 33 |
-
).to(device)
|
| 34 |
-
|
| 35 |
-
# μλ΅ μμ±
|
| 36 |
-
output_ids = model.generate(
|
| 37 |
-
inputs,
|
| 38 |
-
max_length=1024,
|
| 39 |
-
temperature=0.7,
|
| 40 |
-
top_p=0.9,
|
| 41 |
-
do_sample=True,
|
| 42 |
-
eos_token_id=tokenizer.eos_token_id
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
# μλ΅ λμ½λ©
|
| 46 |
-
response = tokenizer.decode(
|
| 47 |
-
output_ids[0][inputs.shape[1]:],
|
| 48 |
-
skip_special_tokens=True
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
chat_history.append({"role": "assistant", "content": response})
|
| 52 |
-
return response
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
]
|
| 61 |
-
else:
|
| 62 |
-
chat_history = []
|
| 63 |
-
for msg in chat_history_ui:
|
| 64 |
-
chat_history.extend([
|
| 65 |
-
{"role": "user", "content": msg[0]},
|
| 66 |
-
{"role": "assistant", "content": msg[1]}
|
| 67 |
-
])
|
| 68 |
-
|
| 69 |
-
# μλ΅ μμ±
|
| 70 |
-
bot_response = generate_response(chat_history, user_input)
|
| 71 |
-
chat_history_ui.append((user_input, bot_response))
|
| 72 |
-
|
| 73 |
-
return "", chat_history_ui
|
| 74 |
-
|
| 75 |
-
# 4. μ± μ€ν
|
| 76 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 77 |
-
gr.Markdown("## π§π» HyperCLOVAX-SEED μ±λ΄")
|
| 78 |
-
|
| 79 |
-
chatbot = gr.Chatbot(height=500)
|
| 80 |
-
msg = gr.Textbox(label="λ©μμ§ μ
λ ₯")
|
| 81 |
-
clear = gr.Button("μ΄κΈ°ν")
|
| 82 |
-
|
| 83 |
-
msg.submit(
|
| 84 |
-
chat_interface,
|
| 85 |
-
[msg, chatbot],
|
| 86 |
-
[msg, chatbot]
|
| 87 |
-
)
|
| 88 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
-
demo.launch(
|
| 92 |
-
server_name="0.0.0.0",
|
| 93 |
-
server_port=7860,
|
| 94 |
-
share=False
|
| 95 |
-
)
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
# λͺ¨λΈ μ΄κΈ°ν (κ°μν λ²μ )
|
| 6 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained("naver-hyperclovax/HyperCLOVAX-SEED-Text-Instruct-0.5B").to(device)
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained("naver-hyperclovax/HyperCLOVAX-SEED-Text-Instruct-0.5B")
|
| 9 |
|
| 10 |
+
def respond(message, history):
|
| 11 |
+
# λν κΈ°λ‘μ λͺ¨λΈ μ
λ ₯ νμμΌλ‘ λ³ν
|
| 12 |
+
chat = [
|
| 13 |
+
{"role": "system", "content": "λΉμ μ λ€μ΄λ²μ CLOVA X AIμ
λλ€."},
|
| 14 |
+
*[{"role": "user" if h[0] == message else "assistant", "content": h[1]} for h in history],
|
| 15 |
+
{"role": "user", "content": message}
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
inputs = tokenizer.apply_chat_template(chat, return_tensors="pt").to(device)
|
| 19 |
+
outputs = model.generate(inputs, max_length=1024, temperature=0.7)
|
| 20 |
+
return tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Gradio μΈν°νμ΄μ€ (μ΅μν)
|
| 23 |
+
demo = gr.ChatInterface(
|
| 24 |
+
respond,
|
| 25 |
+
title="CLOVA X μ±λ΄",
|
| 26 |
+
description="λ€μ΄λ² HyperCLOVAX-SEED κΈ°λ° μ±λ΄"
|
| 27 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
if __name__ == "__main__":
|
| 30 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|