Spaces:
Build error
Build error
| import gradio as gr | |
| import torch | |
| from transformers import ( | |
| AutoModelForCausalLM, | |
| AutoTokenizer, | |
| TextIteratorStreamer, | |
| LlamaTokenizer, | |
| ) | |
| import os | |
| from threading import Thread | |
| import spaces | |
| import subprocess | |
| # flash-attn ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น. CUDA ๋น๋๋ ๊ฑด๋๋. | |
| subprocess.run( | |
| "pip install flash-attn --no-build-isolation", | |
| env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"}, | |
| shell=True, | |
| ) | |
| # Hugging Face ํ ํฐ ๊ฐ์ ธ์ค๊ธฐ | |
| token = os.environ["HF_TOKEN"] | |
| # apple/OpenELM-270M ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ก๋ | |
| # ํ ํฌ๋์ด์ ๊ฐ ์ค๋ฅ๋๋ ๋ฌธ์ ๊ฐ ์์ด์ NousResearch/Llama-2-7b-hf๋ฅผ ์ | |
| # ํ๊ตญ์ด ๋ชจ๋ธ ํ ํฌ๋์ด์ ๋ก ๋ฐ๊ฟ๋ด beomi/llama-2-ko-7b | |
| # apple/OpenELM-1.1B ํ ํฌ๋์ด์ ๋ง ํฌ๊ฒ ํด๋ด <- ์๋จ | |
| # apple/OpenELM-270M-Instruct๋ก ๋๋ค ๋ณ๊ฒฝ ํด๋ด <- ์๋จ | |
| model = AutoModelForCausalLM.from_pretrained( | |
| "apple/OpenELM-270M-Instruct", | |
| token=token, | |
| trust_remote_code=True, | |
| ) | |
| tok = AutoTokenizer.from_pretrained( | |
| "NousResearch/Llama-2-7b-hf", | |
| token=token, | |
| trust_remote_code=True, | |
| tokenizer_class=LlamaTokenizer, | |
| ) | |
| # ์ข ๋ฃ ํ ํฐ ID ์ค์ | |
| terminators = [ | |
| tok.eos_token_id, | |
| ] | |
| # GPU๊ฐ ์ฌ์ฉ ๊ฐ๋ฅํ ๊ฒฝ์ฐ GPU๋ก, ์๋๋ฉด CPU๋ก ๋ชจ๋ธ ๋ก๋ | |
| if torch.cuda.is_available(): | |
| device = torch.device("cuda") | |
| print(f"Using GPU: {torch.cuda.get_device_name(device)}") | |
| else: | |
| device = torch.device("cpu") | |
| print("Using CPU") | |
| model = model.to(device) | |
| # Spaces์ GPU ์์์ ์ฌ์ฉํ์ฌ chat ํจ์ ์คํ. ์ต๋ 60์ด ๋์ GPU ์์ ์ฌ์ฉ ๊ฐ๋ฅ. | |
| def chat(message, history, temperature, do_sample, max_tokens): | |
| # ์ฑํ ๊ธฐ๋ก์ ์ ์ ํ ํ์์ผ๋ก ๋ณํ | |
| chat = [] | |
| for item in history: | |
| chat.append({"role": "user", "content": item[0]}) | |
| if item[1] is not None: | |
| chat.append({"role": "assistant", "content": item[1]}) | |
| chat.append({"role": "user", "content": message}) | |
| # ํ ํฌ๋์ด์ ๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋ ฅ ์ฒ๋ฆฌ | |
| messages = tok.apply_chat_template(chat, tokenize=False, add_generation_prompt=True) | |
| model_inputs = tok([messages], return_tensors="pt").to(device) | |
| # TextIteratorStreamer๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ ์ถ๋ ฅ ์คํธ๋ฆฌ๋ฐ | |
| streamer = TextIteratorStreamer( | |
| tok, timeout=20.0, skip_prompt=True, skip_special_tokens=True | |
| ) | |
| # ์์ฑ ๊ด๋ จ ๋งค๊ฐ๋ณ์ ์ค์ | |
| generate_kwargs = dict( | |
| model_inputs, | |
| streamer=streamer, | |
| max_new_tokens=max_tokens, # ์์ฑํ ์ต๋ ์ ํ ํฐ ์ | |
| do_sample=True, # ์ํ๋ง ์ฌ๋ถ | |
| temperature=temperature, # ์จ๋ ๋งค๊ฐ๋ณ์. ๋์์๋ก ๋ค์์ฑ ์ฆ๊ฐ | |
| eos_token_id=terminators, # ์ข ๋ฃ ํ ํฐ ID | |
| ) | |
| # ์จ๋๊ฐ 0์ด๋ฉด ์ํ๋งํ์ง ์์ | |
| if temperature == 0: | |
| generate_kwargs["do_sample"] = False | |
| # ๋ณ๋ ์ค๋ ๋์์ ๋ชจ๋ธ ์์ฑ ์์ | |
| t = Thread(target=model.generate, kwargs=generate_kwargs) | |
| t.start() | |
| # ์์ฑ๋ ํ ์คํธ๋ฅผ ๋ฐ๋ณต์ ์ผ๋ก yield | |
| partial_text = "" | |
| for new_text in streamer: | |
| partial_text += new_text | |
| yield partial_text | |
| yield partial_text | |
| # Gradio์ ChatInterface๋ฅผ ์ฌ์ฉํ์ฌ ๋ํํ ์ธํฐํ์ด์ค ์์ฑ | |
| demo = gr.ChatInterface( | |
| fn=chat, | |
| examples=[["let's talk about korea"]], | |
| additional_inputs_accordion=gr.Accordion( | |
| label="โ๏ธ Parameters", open=False, render=False | |
| ), | |
| additional_inputs=[ | |
| gr.Slider( | |
| minimum=0, maximum=1, step=0.1, value=0.7, label="Temperature", render=False | |
| ), | |
| gr.Checkbox(label="Sampling", value=True), | |
| gr.Slider( | |
| minimum=128, | |
| maximum=4096, | |
| step=1, | |
| value=512, | |
| label="Max new tokens", | |
| render=False, | |
| ), | |
| ], | |
| stop_btn="Stop Generation", | |
| title="Chat With LLMs", | |
| description="Now Running [apple/OpenELM-270M](https://huggingface.co/apple/OpenELM-270M)", | |
| ) | |
| # Gradio ์ธํฐํ์ด์ค ์คํ | |
| demo.launch() |