chobob311 commited on
Commit
470180a
ยท
verified ยท
1 Parent(s): ff9c801

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -29
app.py CHANGED
@@ -1,49 +1,58 @@
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
  def respond(
10
  message,
11
- history: list[tuple[str, str]],
12
  system_message,
13
  max_tokens,
14
  temperature,
15
  top_p,
16
  ):
17
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
18
 
19
- for val in history:
20
- if val[0]:
21
- messages.append({"role": "user", "content": val[0]})
22
- if val[1]:
23
- messages.append({"role": "assistant", "content": val[1]})
24
 
25
- messages.append({"role": "user", "content": message})
26
-
27
- response = ""
28
-
29
- for message in client.chat_completion(
30
- messages,
31
- max_tokens=max_tokens,
32
- stream=True,
33
  temperature=temperature,
34
  top_p=top_p,
35
- ):
36
- token = message.choices[0].delta.content
 
 
37
 
38
- response += token
39
- yield response
40
 
 
 
 
41
 
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
  demo = gr.ChatInterface(
46
- respond,
47
  additional_inputs=[
48
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
@@ -58,6 +67,5 @@ demo = gr.ChatInterface(
58
  ],
59
  )
60
 
61
-
62
  if __name__ == "__main__":
63
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import LlamaTokenizer, LlamaForCausalLM
3
+ import torch
4
 
5
+ model_repo_id = "Bllossom/llama-3-Korean-Bllossom-70B"
6
+
7
+ # ํ† ํฌ๋‚˜์ด์ € ๋กœ๋“œ
8
+ tokenizer = LlamaTokenizer.from_pretrained(
9
+ model_repo_id,
10
+ use_auth_token='your_hf_access_token' # ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์•ก์„ธ์Šค ํ† ํฐ ์ถ”๊ฐ€
11
+ )
12
+
13
+ # ๋ชจ๋ธ ๋กœ๋“œ
14
+ model = LlamaForCausalLM.from_pretrained(
15
+ model_repo_id,
16
+ torch_dtype=torch.float16, # ๋˜๋Š” torch.bfloat16
17
+ device_map="auto", # ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ GPU์— ์ž๋™ ํ• ๋‹น
18
+ use_auth_token='your_hf_access_token' # ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์•ก์„ธ์Šค ํ† ํฐ ์ถ”๊ฐ€
19
+ )
20
 
21
  def respond(
22
  message,
23
+ history,
24
  system_message,
25
  max_tokens,
26
  temperature,
27
  top_p,
28
  ):
29
+ # ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ
30
+ prompt = system_message + "\n"
31
+ for user_msg, bot_msg in history:
32
+ prompt += f"User: {user_msg}\nAssistant: {bot_msg}\n"
33
+ prompt += f"User: {message}\nAssistant:"
34
 
35
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
 
 
 
 
36
 
37
+ outputs = model.generate(
38
+ **inputs,
39
+ max_new_tokens=max_tokens,
 
 
 
 
 
40
  temperature=temperature,
41
  top_p=top_p,
42
+ do_sample=True,
43
+ eos_token_id=tokenizer.eos_token_id,
44
+ pad_token_id=tokenizer.eos_token_id,
45
+ )
46
 
47
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
48
+ response = response[len(prompt):].strip()
49
 
50
+ history.append((message, response))
51
+
52
+ return history
53
 
 
 
 
54
  demo = gr.ChatInterface(
55
+ fn=respond,
56
  additional_inputs=[
57
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
58
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
 
67
  ],
68
  )
69
 
 
70
  if __name__ == "__main__":
71
  demo.launch()