Spaces:
Sleeping
Sleeping
| import os | |
| import subprocess | |
| import sys | |
| # νμν ν¨ν€μ§ μλ μ€μΉ | |
| def install_packages(): | |
| packages = [ | |
| "transformers==4.45.0", | |
| "torch", | |
| "accelerate", | |
| "sentencepiece" | |
| ] | |
| for package in packages: | |
| subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", package]) | |
| print("νμν ν¨ν€μ§ μ€μΉ μ€...") | |
| install_packages() | |
| print("ν¨ν€μ§ μ€μΉ μλ£!") | |
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import torch | |
| # λͺ¨λΈ λ‘λ© | |
| print("λͺ¨λΈ λ‘λ© μ€...") | |
| model_name = "microsoft/Phi-3-mini-4k-instruct" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_name, | |
| torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, | |
| device_map="auto", | |
| trust_remote_code=True | |
| ) | |
| print("λͺ¨λΈ λ‘λ© μλ£!") | |
| def format_chat_prompt(message, chat_history): | |
| """μ±ν νμ€ν 리λ₯Ό ν둬ννΈλ‘ λ³ν""" | |
| prompt = "" | |
| for user_msg, assistant_msg in chat_history: | |
| if user_msg: | |
| prompt += f"User: {user_msg}\n" | |
| if assistant_msg: | |
| prompt += f"Assistant: {assistant_msg}\n" | |
| prompt += f"User: {message}\nAssistant:" | |
| return prompt | |
| def chat(message, history): | |
| """μ±ν μλ΅ μμ±""" | |
| # ν둬ννΈ μμ± | |
| prompt = format_chat_prompt(message, history) | |
| # ν ν¬λμ΄μ§ | |
| inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048) | |
| if torch.cuda.is_available(): | |
| inputs = inputs.to("cuda") | |
| # μλ΅ μμ± | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| inputs.input_ids, | |
| max_new_tokens=512, | |
| temperature=0.7, | |
| do_sample=True, | |
| top_p=0.9, | |
| pad_token_id=tokenizer.eos_token_id, | |
| eos_token_id=tokenizer.eos_token_id, | |
| ) | |
| # μλ΅ λμ½λ© | |
| response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True) | |
| # λΆνμν λΆλΆ μ κ±° | |
| response = response.split("User:")[0].strip() | |
| return response | |
| # Gradio μΈν°νμ΄μ€ μμ± | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown( | |
| """ | |
| # π€ AI μ±ν μλΉμ€ | |
| ### Phi-3-mini 4B λͺ¨λΈ κΈ°λ° λνν AI | |
| μμ λ‘κ² μ§λ¬Ένκ³ λνν΄λ³΄μΈμ! | |
| """ | |
| ) | |
| chatbot = gr.Chatbot( | |
| height=500, | |
| bubble_full_width=False, | |
| avatar_images=(None, "π€"), | |
| ) | |
| with gr.Row(): | |
| msg = gr.Textbox( | |
| label="λ©μμ§ μ λ ₯", | |
| placeholder="λ©μμ§λ₯Ό μ λ ₯νμΈμ...", | |
| scale=4, | |
| container=False | |
| ) | |
| submit = gr.Button("μ μ‘", scale=1, variant="primary") | |
| with gr.Row(): | |
| clear = gr.Button("λν μ΄κΈ°ν") | |
| gr.Examples( | |
| examples=[ | |
| "μλ νμΈμ! μκΈ°μκ° λΆνλλ €μ.", | |
| "PythonμΌλ‘ κ°λ¨ν κ³μ°κΈ° λ§λλ λ°©λ² μλ €μ€", | |
| "μ€λμ λͺ μΈ νλ λ€λ €μ€", | |
| "κΈ°λΆ μ’μμ§λ λλ΄ ν΄μ€", | |
| ], | |
| inputs=msg, | |
| label="μμ μ§λ¬Έ" | |
| ) | |
| # μ΄λ²€νΈ νΈλ€λ¬ | |
| def respond(message, chat_history): | |
| bot_message = chat(message, chat_history) | |
| chat_history.append((message, bot_message)) | |
| return "", chat_history | |
| msg.submit(respond, [msg, chatbot], [msg, chatbot]) | |
| submit.click(respond, [msg, chatbot], [msg, chatbot]) | |
| clear.click(lambda: None, None, chatbot, queue=False) | |
| # μ± μ€ν | |
| if __name__ == "__main__": | |
| demo.queue().launch() | |