switiz87 commited on
Commit
c141e5a
β€’
1 Parent(s): 4f2c85c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -39
app.py CHANGED
@@ -1,63 +1,141 @@
 
 
 
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
- 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"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
 
 
 
 
 
 
 
 
 
51
  gr.Slider(
 
52
  minimum=0.1,
 
 
 
 
 
 
 
53
  maximum=1.0,
54
- value=0.95,
55
  step=0.05,
56
- label="Top-p (nucleus sampling)",
 
 
 
 
 
 
 
57
  ),
 
 
 
 
 
 
 
 
 
 
 
 
58
  ],
 
59
  )
60
 
 
 
 
61
 
62
  if __name__ == "__main__":
63
- demo.launch()
 
1
+ import os
2
+ from threading import Thread
3
+ from typing import Iterator
4
  import gradio as gr
5
+ import torch
6
+ import spaces
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
8
 
9
+ DESCRIPTION = """ EXAONE-3.0-7.8B-Instruct Official Demo \
10
  """
 
 
 
11
 
12
+ MAX_MAX_NEW_TOKENS = 4096
13
+ DEFAULT_MAX_NEW_TOKENS = 256
14
+ MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "3840"))
15
+
16
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
17
+
18
+ model_id = "LGAI-EXAONE/EXAONE-3.0-7.8B-Instruct"
19
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
20
+ model = AutoModelForCausalLM.from_pretrained(
21
+ model_id,
22
+ torch_dtype=torch.bfloat16,
23
+ trust_remote_code=True,
24
+ device_map="auto",
25
+ )
26
 
27
+ model.eval()
 
 
 
 
 
 
 
 
28
 
 
 
 
 
 
29
 
30
+ #@spaces.GPU(duration=90)
31
+ def generate(
32
+ message: str,
33
+ chat_history: list[tuple[str, str]],
34
+ system_prompt: str,
35
+ max_new_tokens: int = 256,
36
+ temperature: float = 0.6,
37
+ top_p: float = 0.9,
38
+ top_k: int = 50,
39
+ repetition_penalty: float = 1.2,
40
+ ) -> Iterator[str]:
41
+ messages = [{"role":"system","content": system_prompt}]
42
+ print(f'message: {message}')
43
+ print(f'history: {history}')
44
+ for user, assistant in chat_history:
45
+ messages.extend(
46
+ [
47
+ {"role": "user", "content": user},
48
+ {"role": "assistant", "content": assistant},
49
+ ]
50
+ )
51
  messages.append({"role": "user", "content": message})
52
 
53
+ input_ids = tokenizer.apply_chat_template(
54
+ messages,
55
+ add_generation_prompt=True,
56
+ return_tensors="pt"
57
+ )
58
+ if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
59
+ input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
60
+ gr.Warning(f"Trimmed input from messages as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
61
+ input_ids = input_ids.to(model.device)
62
 
63
+ streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
64
+ generate_kwargs = dict(
65
+ {"input_ids": input_ids},
66
+ streamer=streamer,
67
+ max_new_tokens=max_new_tokens,
68
+ do_sample=True,
69
  top_p=top_p,
70
+ top_k=top_k,
71
+ temperature=temperature,
72
+ num_beams=1,
73
+ repetition_penalty=repetition_penalty,
74
+ )
75
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
76
+ t.start()
77
 
78
+ outputs = []
79
+ for text in streamer:
80
+ outputs.append(text)
81
+ yield "".join(outputs)
82
 
83
+
84
+ chat_interface = gr.ChatInterface(
85
+ fn=generate,
 
 
86
  additional_inputs=[
87
+ gr.Textbox(
88
+ value="You are EXAONE model from LG AI Research, a helpful assistant.",
89
+ label="System Prompt",
90
+ render=False,
91
+ ),
92
+ gr.Slider(
93
+ label="Max new tokens",
94
+ minimum=1,
95
+ maximum=MAX_MAX_NEW_TOKENS,
96
+ step=1,
97
+ value=DEFAULT_MAX_NEW_TOKENS,
98
+ ),
99
  gr.Slider(
100
+ label="Temperature",
101
  minimum=0.1,
102
+ maximum=4.0,
103
+ step=0.1,
104
+ value=0.6,
105
+ ),
106
+ gr.Slider(
107
+ label="Top-p (nucleus sampling)",
108
+ minimum=0.05,
109
  maximum=1.0,
 
110
  step=0.05,
111
+ value=0.9,
112
+ ),
113
+ gr.Slider(
114
+ label="Top-k",
115
+ minimum=1,
116
+ maximum=1000,
117
+ step=1,
118
+ value=50,
119
  ),
120
+ gr.Slider(
121
+ label="Repetition penalty",
122
+ minimum=1.0,
123
+ maximum=2.0,
124
+ step=0.05,
125
+ value=1.2,
126
+ ),
127
+ ],
128
+ stop_btn=None,
129
+ examples=[
130
+ ["Explain who you are"],
131
+ ["λ„ˆμ˜ μ†Œμ›μ„ 말해봐"],
132
  ],
133
+ cache_examples=False,
134
  )
135
 
136
+ with gr.Blocks(css="style.css", fill_height=True) as demo:
137
+ gr.Markdown(DESCRIPTION)
138
+ chat_interface.render()
139
 
140
  if __name__ == "__main__":
141
+ demo.queue(max_size=20).launch()