groloch commited on
Commit
21a5563
1 Parent(s): bb8305c
Files changed (2) hide show
  1. app.py +138 -58
  2. app_gpu.py +146 -0
app.py CHANGED
@@ -1,62 +1,142 @@
 
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
 
 
1
+ import torch
2
  import gradio as gr
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+
5
+
6
+ choices_base_models = {
7
+ 'groloch/Llama-3.2-3B-Instruct-PromptEnhancing': 'meta-llama/Llama-3.2-3B-Instruct',
8
+ 'groloch/gemma-2-2b-it-PromptEnhancing': 'google/gemma-2-2b-it',
9
+ 'groloch/Qwen2.5-3B-Instruct-PromptEnhancing': 'Qwen/Qwen2.5-3B-Instruct',
10
+ 'groloch/Ministral-3b-instruct-PromptEnhancing': 'ministral/Ministral-3b-instruct'
11
+ }
12
+
13
+ choices_gen_token = {
14
+ 'groloch/Llama-3.2-3B-Instruct-PromptEnhancing': 'assistant',
15
+ 'groloch/gemma-2-2b-it-PromptEnhancing': 'model',
16
+ 'groloch/Qwen2.5-3B-Instruct-PromptEnhancing': 'assistant',
17
+ 'groloch/Ministral-3b-instruct-PromptEnhancing': 'ministral/Ministral-3b-instruct'
18
+ }
19
+
20
+ previous_choice = ''
21
+
22
+ model = None
23
+ tokenizer = None
24
+
25
+
26
+ def load_model(adapter_repo_id: str):
27
+ global model, tokenizer
28
+ base_repo_id = choices_base_models[adapter_repo_id]
29
+
30
+ tokenizer = AutoTokenizer.from_pretrained(base_repo_id)
31
+ model = AutoModelForCausalLM.from_pretrained(base_repo_id, torch_dtype=torch.bfloat16)
32
+
33
+ model.load_adapter(adapter_repo_id)
34
+
35
+ def generate(prompt_to_enhance: str,
36
+ choice: str,
37
+ max_tokens: float,
38
+ temperature: float,
39
+ top_p: float,
40
+ repetition_penalty: float
41
+ ):
42
+ if prompt_to_enhance is None or prompt_to_enhance == '':
43
+ raise gr.Error('Please enter a prompt')
44
+ global previous_choice
45
+
46
+ if choice != previous_choice:
47
+ previous_choice = choice
48
+ load_model(choice)
49
+
50
+ chat = [
51
+ {'role' : 'user', 'content': prompt_to_enhance}
52
+ ]
53
+
54
+ prompt = tokenizer.apply_chat_template(chat,
55
+ tokenize=False,
56
+ add_generation_prompt=True,
57
+ return_tensors='pt')
58
+
59
+ encoding = tokenizer(prompt, return_tensors="pt")
60
+
61
+ generation_config = model.generation_config
62
+ generation_config.do_sample = True
63
+ generation_config.max_new_tokens = int(max_tokens)
64
+ generation_config.temperature = float(temperature)
65
+ generation_config.top_p = float(top_p)
66
+ generation_config.num_return_sequences = 1
67
+ generation_config.pad_token_id = tokenizer.eos_token_id
68
+ generation_config.eos_token_id = tokenizer.eos_token_id
69
+ generation_config.repetition_penalty = float(repetition_penalty)
70
+
71
+ with torch.inference_mode():
72
+ outputs = model.generate(
73
+ input_ids=encoding.input_ids,
74
+ attention_mask=encoding.attention_mask,
75
+ generation_config=generation_config
76
+ )
77
+
78
+ return tokenizer.decode(outputs[0], skip_special_tokens=True).split(choices_gen_token[choice])[-1]
79
+
80
+
81
+ #
82
+ # Inputs
83
+ #
84
+ model_choice = gr.Dropdown(
85
+ label='Model choice',
86
+ choices=['groloch/Llama-3.2-3B-Instruct-PromptEnhancing',
87
+ 'groloch/gemma-2-2b-it-PromptEnhancing',
88
+ 'groloch/Qwen2.5-3B-Instruct-PromptEnhancing',
89
+ 'groloch/Ministral-3b-instruct-PromptEnhancing'
90
+ ],
91
+ value='groloch/Llama-3.2-3B-Instruct-PromptEnhancing'
92
+ )
93
+ input_prompt = gr.Text(
94
+ label='Prompt to enhance'
95
+ )
96
+
97
+ #
98
+ # Additional inputs
99
+ #
100
+ input_max_tokens = gr.Number(
101
+ label='Max generated tokens',
102
+ value=64,
103
+ minimum=16,
104
+ maximum=128
105
+ )
106
+ input_temperature = gr.Number(
107
+ label='Temperature',
108
+ value=0.3,
109
+ minimum=0.0,
110
+ maximum=1.5,
111
+ step=0.05
112
+ )
113
+ input_top_p = gr.Number(
114
+ label='Top p',
115
+ value=0.9,
116
+ minimum=0.0,
117
+ maximum=1.0,
118
+ step=0.05
119
+ )
120
+ input_repetition_penalty = gr.Number(
121
+ label='Repetition penalty',
122
+ value=2.0,
123
+ minimum=0.0,
124
+ maximum=5.0,
125
+ step=0.1
126
+ )
127
+
128
+ demo = gr.Interface(
129
+ generate,
130
+ title='Prompt Enhancing Playground',
131
+ description='This space is a tool to compare the different prompt enhancing model I have finetuned. \
132
+ Feel free to experiment as you want !',
133
+ inputs=[input_prompt, model_choice],
134
+ additional_inputs=[input_max_tokens,
135
+ input_temperature,
136
+ input_top_p,
137
+ input_repetition_penalty
138
+ ],
139
+ outputs=['text']
140
  )
141
 
142
 
app_gpu.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import gradio as gr
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+
5
+
6
+ choices_base_models = {
7
+ 'groloch/Llama-3.2-3B-Instruct-PromptEnhancing': 'meta-llama/Llama-3.2-3B-Instruct',
8
+ 'groloch/gemma-2-2b-it-PromptEnhancing': 'google/gemma-2-2b-it',
9
+ 'groloch/Qwen2.5-3B-Instruct-PromptEnhancing': 'Qwen/Qwen2.5-3B-Instruct',
10
+ 'groloch/Ministral-3b-instruct-PromptEnhancing': 'ministral/Ministral-3b-instruct'
11
+ }
12
+
13
+ choices_gen_token = {
14
+ 'groloch/Llama-3.2-3B-Instruct-PromptEnhancing': 'assistant',
15
+ 'groloch/gemma-2-2b-it-PromptEnhancing': 'model',
16
+ 'groloch/Qwen2.5-3B-Instruct-PromptEnhancing': 'assistant',
17
+ 'groloch/Ministral-3b-instruct-PromptEnhancing': 'ministral/Ministral-3b-instruct'
18
+ }
19
+
20
+ previous_choice = ''
21
+
22
+ model = None
23
+ tokenizer = None
24
+
25
+
26
+ def load_model(adapter_repo_id: str):
27
+ global model, tokenizer
28
+ base_repo_id = choices_base_models[adapter_repo_id]
29
+
30
+ tokenizer = AutoTokenizer.from_pretrained(base_repo_id)
31
+ model = AutoModelForCausalLM.from_pretrained(base_repo_id, torch_dtype=torch.bfloat16)
32
+
33
+ model.load_adapter(adapter_repo_id)
34
+
35
+ model.to('cuda')
36
+
37
+ def generate(prompt_to_enhance: str,
38
+ choice: str,
39
+ max_tokens: float,
40
+ temperature: float,
41
+ top_p: float,
42
+ repetition_penalty: float
43
+ ):
44
+ if prompt_to_enhance is None or prompt_to_enhance == '':
45
+ raise gr.Error('Please enter a prompt')
46
+ global previous_choice
47
+
48
+ if choice != previous_choice:
49
+ previous_choice = choice
50
+ load_model(choice)
51
+
52
+ chat = [
53
+ {'role' : 'user', 'content': prompt_to_enhance}
54
+ ]
55
+
56
+ prompt = tokenizer.apply_chat_template(chat,
57
+ tokenize=False,
58
+ add_generation_prompt=True,
59
+ return_tensors='pt')
60
+
61
+ encoding = tokenizer(prompt, return_tensors="pt").to('cuda')
62
+
63
+ generation_config = model.generation_config
64
+ generation_config.do_sample = True
65
+ generation_config.max_new_tokens = int(max_tokens)
66
+ generation_config.temperature = float(temperature)
67
+ generation_config.top_p = float(top_p)
68
+ generation_config.num_return_sequences = 1
69
+ generation_config.pad_token_id = tokenizer.eos_token_id
70
+ generation_config.eos_token_id = tokenizer.eos_token_id
71
+ generation_config.repetition_penalty = float(repetition_penalty)
72
+
73
+ with torch.inference_mode():
74
+ outputs = model.generate(
75
+ input_ids=encoding.input_ids,
76
+ attention_mask=encoding.attention_mask,
77
+ generation_config=generation_config
78
+ )
79
+
80
+ return tokenizer.decode(outputs[0], skip_special_tokens=True).split(choices_gen_token[choice])[-1]
81
+
82
+
83
+ #
84
+ # Inputs
85
+ #
86
+ model_choice = gr.Dropdown(
87
+ label='Model choice',
88
+ choices=['groloch/Llama-3.2-3B-Instruct-PromptEnhancing',
89
+ 'groloch/gemma-2-2b-it-PromptEnhancing',
90
+ 'groloch/Qwen2.5-3B-Instruct-PromptEnhancing',
91
+ 'groloch/Ministral-3b-instruct-PromptEnhancing'
92
+ ],
93
+ value='groloch/Llama-3.2-3B-Instruct-PromptEnhancing'
94
+ )
95
+ input_prompt = gr.Text(
96
+ label='Prompt to enhance'
97
+ )
98
+
99
+ #
100
+ # Additional inputs
101
+ #
102
+ input_max_tokens = gr.Number(
103
+ label='Max generated tokens',
104
+ value=64,
105
+ minimum=16,
106
+ maximum=128
107
+ )
108
+ input_temperature = gr.Number(
109
+ label='Temperature',
110
+ value=0.3,
111
+ minimum=0.0,
112
+ maximum=1.5,
113
+ step=0.05
114
+ )
115
+ input_top_p = gr.Number(
116
+ label='Top p',
117
+ value=0.9,
118
+ minimum=0.0,
119
+ maximum=1.0,
120
+ step=0.05
121
+ )
122
+ input_repetition_penalty = gr.Number(
123
+ label='Repetition penalty',
124
+ value=2.0,
125
+ minimum=0.0,
126
+ maximum=5.0,
127
+ step=0.1
128
+ )
129
+
130
+ demo = gr.Interface(
131
+ generate,
132
+ title='Prompt Enhancing Playground',
133
+ description='This space is a tool to compare the different prompt enhancing model I have finetuned. \
134
+ Feel free to experiment as you want !',
135
+ inputs=[input_prompt, model_choice],
136
+ additional_inputs=[input_max_tokens,
137
+ input_temperature,
138
+ input_top_p,
139
+ input_repetition_penalty
140
+ ],
141
+ outputs=['text']
142
+ )
143
+
144
+
145
+ if __name__ == "__main__":
146
+ demo.launch()