kimhyunwoo commited on
Commit
9d9cc80
ยท
verified ยท
1 Parent(s): bcba60b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -51
app.py CHANGED
@@ -1,64 +1,92 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
27
 
28
- response = ""
 
 
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
 
 
 
 
 
 
 
 
41
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
 
 
 
62
 
63
- if __name__ == "__main__":
64
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ import os
5
 
6
+ # --- ๋ชจ๋ธ ๋กœ๋“œ ---
7
+ # ๋ชจ๋ธ ๊ฒฝ๋กœ ์„ค์ • (Hugging Face ๋ชจ๋ธ ID)
8
+ model_id = "microsoft/bitnet-b1.58-2B-4T"
 
9
 
10
+ # ๋ชจ๋ธ ๋กœ๋“œ ์‹œ ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ์ตœ์†Œํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๋กœ๊น… ๋ ˆ๋ฒจ ์„ค์ •
11
+ os.environ["TRANSFORMERS_VERBOSITY"] = "error"
12
 
13
+ # AutoModelForCausalLM๊ณผ AutoTokenizer๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.
14
+ # BitNet ๋ชจ๋ธ์€ trust_remote_code=True๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
15
+ # bf16์€ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์ด๊ณ  ์†๋„๋ฅผ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค (GPU ์ง€์› ์‹œ).
16
+ # CPU๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ torch_dtype์„ ์ƒ๋žตํ•˜๊ฑฐ๋‚˜ torch.float32๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
17
+ try:
18
+ print(f"๋ชจ๋ธ ๋กœ๋”ฉ ์ค‘: {model_id}...")
19
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
20
+ # GPU๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋ฉด bf16 ์‚ฌ์šฉ
21
+ if torch.cuda.is_available():
22
+ model = AutoModelForCausalLM.from_pretrained(
23
+ model_id,
24
+ torch_dtype=torch.bfloat16,
25
+ trust_remote_code=True
26
+ ).to("cuda") # GPU๋กœ ๋ชจ๋ธ ์ด๋™
27
+ print("GPU๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ ๋กœ๋“œ ์™„๋ฃŒ.")
28
+ else:
29
+ model = AutoModelForCausalLM.from_pretrained(
30
+ model_id,
31
+ trust_remote_code=True
32
+ )
33
+ print("CPU๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ ๋กœ๋“œ ์™„๋ฃŒ. ์„ฑ๋Šฅ์ด ๋А๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.")
34
 
35
+ except Exception as e:
36
+ print(f"๋ชจ๋ธ ๋กœ๋“œ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {e}")
37
+ tokenizer = None
38
+ model = None
39
+ print("๋ชจ๋ธ ๋กœ๋“œ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.")
40
 
 
41
 
42
+ # --- ํ…์ŠคํŠธ ์ƒ์„ฑ ํ•จ์ˆ˜ ---
43
+ def generate_text(prompt, max_length=100, temperature=0.7):
44
+ if model is None or tokenizer is None:
45
+ return "๋ชจ๋ธ ๋กœ๋“œ์— ์‹คํŒจํ•˜์—ฌ ํ…์ŠคํŠธ ์ƒ์„ฑ์„ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
46
 
47
+ try:
48
+ # ํ”„๋กฌํ”„ํŠธ ํ† ํฐํ™”
49
+ inputs = tokenizer(prompt, return_tensors="pt")
50
+ # GPU ์‚ฌ์šฉ ๊ฐ€๋Šฅ ์‹œ GPU๋กœ ์ž…๋ ฅ ์ด๋™
51
+ if torch.cuda.is_available():
52
+ inputs = {k: v.to("cuda") for k, v in inputs.items()}
 
 
53
 
54
+ # ํ…์ŠคํŠธ ์ƒ์„ฑ
55
+ # LLaMA 3 ํ† ํฌ๋‚˜์ด์ €๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ chat template ์ ์šฉ ๊ฐ€๋Šฅ (์„ ํƒ ์‚ฌํ•ญ)
56
+ # ๋ฉ”์‹œ์ง€ ํ˜•์‹์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ง์ ‘ ํ”„๋กฌํ”„ํŠธ ์ž…๋ ฅ ์‹œ ์•„๋ž˜ ์ฝ”๋“œ ์‚ฌ์šฉ
57
+ outputs = model.generate(
58
+ **inputs,
59
+ max_new_tokens=max_length,
60
+ temperature=temperature,
61
+ do_sample=True, # ์ƒ˜ํ”Œ๋ง ํ™œ์„ฑํ™”
62
+ pad_token_id=tokenizer.eos_token_id # ํŒจ๋”ฉ ํ† ํฐ ID ์„ค์ • (ํ•„์š”์‹œ)
63
+ )
64
 
65
+ # ์ƒ์„ฑ๋œ ํ…์ŠคํŠธ ๋””์ฝ”๋”ฉ
66
+ # ์ž…๋ ฅ ํ”„๋กฌํ”„ํŠธ ๋ถ€๋ถ„์„ ์ œ์™ธํ•˜๊ณ  ์ƒ์„ฑ๋œ ๋ถ€๋ถ„๋งŒ ๋””์ฝ”๋”ฉ
67
+ generated_text = tokenizer.decode(outputs[0][inputs['input_ids'].shape[-1]:], skip_special_tokens=True)
68
 
69
+ return generated_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
+ except Exception as e:
72
+ return f"ํ…์ŠคํŠธ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {e}"
73
 
74
+ # --- Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ • ---
75
+ if model is not None and tokenizer is not None:
76
+ interface = gr.Interface(
77
+ fn=generate_text,
78
+ inputs=[
79
+ gr.Textbox(lines=2, placeholder="ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”...", label="์ž…๋ ฅ ํ”„๋กฌํ”„ํŠธ"),
80
+ gr.Slider(minimum=10, maximum=500, value=100, label="์ตœ๋Œ€ ์ƒ๏ฟฝ๏ฟฝ๏ฟฝ ๊ธธ์ด"),
81
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.7, label="Temperature (์ฐฝ์˜์„ฑ)")
82
+ ],
83
+ outputs=gr.Textbox(label="์ƒ์„ฑ๋œ ํ…์ŠคํŠธ"),
84
+ title="BitNet b1.58-2B-4T ํ…์ŠคํŠธ ์ƒ์„ฑ ๋ฐ๋ชจ",
85
+ description="BitNet b1.58-2B-4T ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค."
86
+ )
87
+
88
+ # Gradio ์•ฑ ์‹คํ–‰
89
+ # share=True๋ฅผ ํ•˜๋ฉด ์ž„์‹œ ๊ณต๊ฐœ ๋งํฌ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.
90
+ interface.launch(share=False)
91
+ else:
92
+ print("๋ชจ๋ธ ๋กœ๋“œ ์‹คํŒจ๋กœ ์ธํ•ด Gradio ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")