chansung commited on
Commit
53796a7
·
1 Parent(s): 6cd1634

remove args

Browse files
Files changed (3) hide show
  1. .ipynb_checkpoints/app-checkpoint.py +0 -208
  2. app.py +78 -100
  3. args.py +0 -51
.ipynb_checkpoints/app-checkpoint.py DELETED
@@ -1,208 +0,0 @@
1
- from strings import TITLE, ABSTRACT, BOTTOM_LINE
2
- from strings import DEFAULT_EXAMPLES
3
- from strings import SPECIAL_STRS
4
- from styles import PARENT_BLOCK_CSS
5
-
6
- import time
7
- import gradio as gr
8
-
9
- from args import parse_args
10
- from model import load_model
11
- from gen import get_output_batch, StreamModel
12
- from utils import generate_prompt, post_processes_batch, post_process_stream, get_generation_config, common_post_process
13
-
14
- def chat_stream(
15
- context,
16
- instruction,
17
- state_chatbot,
18
- ):
19
- # print(instruction)
20
-
21
- # user input should be appropriately formatted (don't be confused by the function name)
22
- instruction_display = common_post_process(instruction)
23
- instruction_prompt = generate_prompt(instruction, state_chatbot, context)
24
- bot_response = model(
25
- instruction_prompt,
26
- max_tokens=256,
27
- temperature=1,
28
- top_p=0.9
29
- )
30
-
31
- instruction_display = None if instruction_display == SPECIAL_STRS["continue"] else instruction_display
32
- state_chatbot = state_chatbot + [(instruction_display, None)]
33
-
34
- prev_index = 0
35
- agg_tokens = ""
36
- cutoff_idx = 0
37
- for tokens in bot_response:
38
- tokens = tokens.strip()
39
- cur_token = tokens[prev_index:]
40
-
41
- if "#" in cur_token and agg_tokens == "":
42
- cutoff_idx = tokens.find("#")
43
- agg_tokens = tokens[cutoff_idx:]
44
-
45
- if agg_tokens != "":
46
- if len(agg_tokens) < len("### Instruction:") :
47
- agg_tokens = agg_tokens + cur_token
48
- elif len(agg_tokens) >= len("### Instruction:"):
49
- if tokens.find("### Instruction:") > -1:
50
- processed_response, _ = post_process_stream(tokens[:tokens.find("### Instruction:")].strip())
51
-
52
- state_chatbot[-1] = (
53
- instruction_display,
54
- processed_response
55
- )
56
- yield (state_chatbot, state_chatbot, context)
57
- break
58
- else:
59
- agg_tokens = ""
60
- cutoff_idx = 0
61
-
62
- if agg_tokens == "":
63
- processed_response, to_exit = post_process_stream(tokens)
64
- state_chatbot[-1] = (instruction_display, processed_response)
65
- yield (state_chatbot, state_chatbot, context)
66
-
67
- if to_exit:
68
- break
69
-
70
- prev_index = len(tokens)
71
-
72
- yield (
73
- state_chatbot,
74
- state_chatbot,
75
- gr.Textbox.update(value=tokens) if instruction_display == SPECIAL_STRS["summarize"] else context
76
- )
77
-
78
- def chat_batch(
79
- contexts,
80
- instructions,
81
- state_chatbots,
82
- ):
83
- state_results = []
84
- ctx_results = []
85
-
86
- instruct_prompts = [
87
- generate_prompt(instruct, histories, ctx)
88
- for ctx, instruct, histories in zip(contexts, instructions, state_chatbots)
89
- ]
90
-
91
- bot_responses = get_output_batch(
92
- model, tokenizer, instruct_prompts, generation_config
93
- )
94
- bot_responses = post_processes_batch(bot_responses)
95
-
96
- for ctx, instruction, bot_response, state_chatbot in zip(contexts, instructions, bot_responses, state_chatbots):
97
- new_state_chatbot = state_chatbot + [('' if instruction == SPECIAL_STRS["continue"] else instruction, bot_response)]
98
- ctx_results.append(gr.Textbox.update(value=bot_response) if instruction == SPECIAL_STRS["summarize"] else ctx)
99
- state_results.append(new_state_chatbot)
100
-
101
- return (state_results, state_results, ctx_results)
102
-
103
- def reset_textbox():
104
- return gr.Textbox.update(value='')
105
-
106
- def run(args):
107
- global model, tokenizer, generation_config, batch_enabled
108
-
109
- batch_enabled = True if args.batch_size > 1 else False
110
-
111
- model, tokenizer = load_model(
112
- base=args.base_url,
113
- finetuned=args.ft_ckpt_url
114
- )
115
-
116
- generation_config = get_generation_config(
117
- args.gen_config_path
118
- )
119
-
120
- if not batch_enabled:
121
- model = StreamModel(model, tokenizer)
122
- # model.generation_config = generation_config
123
-
124
- with gr.Blocks(css=PARENT_BLOCK_CSS) as demo:
125
- state_chatbot = gr.State([])
126
-
127
- with gr.Column(elem_id='col_container'):
128
- gr.Markdown(f"## {TITLE}\n\n\n{ABSTRACT}")
129
-
130
- with gr.Accordion("Context Setting", open=False):
131
- context_txtbox = gr.Textbox(placeholder="Surrounding information to AI", label="Enter Context")
132
- hidden_txtbox = gr.Textbox(placeholder="", label="Order", visible=False)
133
-
134
- chatbot = gr.Chatbot(elem_id='chatbot', label="Alpaca-LoRA")
135
- instruction_txtbox = gr.Textbox(placeholder="What do you want to say to AI?", label="Instruction")
136
- send_prompt_btn = gr.Button(value="Send Prompt")
137
-
138
- with gr.Accordion("Helper Buttons", open=False):
139
- gr.Markdown(f"`Continue` lets AI to complete the previous incomplete answers. `Summarize` lets AI to summarize the conversations so far.")
140
- continue_txtbox = gr.Textbox(value=SPECIAL_STRS["continue"], visible=False)
141
- summrize_txtbox = gr.Textbox(value=SPECIAL_STRS["summarize"], visible=False)
142
-
143
- continue_btn = gr.Button(value="Continue")
144
- summarize_btn = gr.Button(value="Summarize")
145
-
146
- gr.Markdown("#### Examples")
147
- for idx, examples in enumerate(DEFAULT_EXAMPLES):
148
- with gr.Accordion(examples["title"], open=False):
149
- gr.Examples(
150
- examples=examples["examples"],
151
- inputs=[
152
- hidden_txtbox, instruction_txtbox
153
- ],
154
- label=None
155
- )
156
-
157
- gr.Markdown(f"{BOTTOM_LINE}")
158
-
159
- send_prompt_btn.click(
160
- chat_batch if batch_enabled else chat_stream,
161
- [context_txtbox, instruction_txtbox, state_chatbot],
162
- [state_chatbot, chatbot, context_txtbox],
163
- batch=batch_enabled,
164
- max_batch_size=args.batch_size,
165
- )
166
- send_prompt_btn.click(
167
- reset_textbox,
168
- [],
169
- [instruction_txtbox],
170
- )
171
-
172
- continue_btn.click(
173
- chat_batch if batch_enabled else chat_stream,
174
- [context_txtbox, continue_txtbox, state_chatbot],
175
- [state_chatbot, chatbot, context_txtbox],
176
- batch=batch_enabled,
177
- max_batch_size=args.batch_size,
178
- )
179
- continue_btn.click(
180
- reset_textbox,
181
- [],
182
- [instruction_txtbox],
183
- )
184
-
185
- summarize_btn.click(
186
- chat_batch if batch_enabled else chat_stream,
187
- [context_txtbox, summrize_txtbox, state_chatbot],
188
- [state_chatbot, chatbot, context_txtbox],
189
- batch=batch_enabled,
190
- max_batch_size=args.batch_size,
191
- )
192
- summarize_btn.click(
193
- reset_textbox,
194
- [],
195
- [instruction_txtbox],
196
- )
197
-
198
- demo.queue(
199
- concurrency_count=2,
200
- max_size=100,
201
- ).launch(
202
- max_threads=2,
203
- server_name="0.0.0.0",
204
- )
205
-
206
- if __name__ == "__main__":
207
- args = parse_args()
208
- run(args)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py CHANGED
@@ -6,11 +6,17 @@ from styles import PARENT_BLOCK_CSS
6
  import time
7
  import gradio as gr
8
 
9
- from args import parse_args
10
  from model import load_model
11
  from gen import get_output_batch, StreamModel
12
  from utils import generate_prompt, post_processes_batch, post_process_stream, get_generation_config, common_post_process
13
 
 
 
 
 
 
 
 
14
  def chat_stream(
15
  context,
16
  instruction,
@@ -103,106 +109,78 @@ def chat_batch(
103
  def reset_textbox():
104
  return gr.Textbox.update(value='')
105
 
106
- def run(args):
107
- global model, tokenizer, generation_config, batch_enabled
108
-
109
- batch_enabled = True if args.batch_size > 1 else False
110
-
111
- model, tokenizer = load_model(
112
- base=args.base_url,
113
- finetuned=args.ft_ckpt_url
114
- )
115
-
116
- generation_config = get_generation_config(
117
- args.gen_config_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  )
119
-
120
- if not batch_enabled:
121
- model = StreamModel(model, tokenizer)
122
- # model.generation_config = generation_config
123
-
124
- with gr.Blocks(css=PARENT_BLOCK_CSS) as demo:
125
- state_chatbot = gr.State([])
126
-
127
- with gr.Column(elem_id='col_container'):
128
- gr.Markdown(f"## {TITLE}\n\n\n{ABSTRACT}")
129
-
130
- with gr.Accordion("Context Setting", open=False):
131
- context_txtbox = gr.Textbox(placeholder="Surrounding information to AI", label="Enter Context")
132
- hidden_txtbox = gr.Textbox(placeholder="", label="Order", visible=False)
133
-
134
- chatbot = gr.Chatbot(elem_id='chatbot', label="Alpaca-LoRA")
135
- instruction_txtbox = gr.Textbox(placeholder="What do you want to say to AI?", label="Instruction")
136
- send_prompt_btn = gr.Button(value="Send Prompt")
137
-
138
- with gr.Accordion("Helper Buttons", open=False):
139
- gr.Markdown(f"`Continue` lets AI to complete the previous incomplete answers. `Summarize` lets AI to summarize the conversations so far.")
140
- continue_txtbox = gr.Textbox(value=SPECIAL_STRS["continue"], visible=False)
141
- summrize_txtbox = gr.Textbox(value=SPECIAL_STRS["summarize"], visible=False)
142
-
143
- continue_btn = gr.Button(value="Continue")
144
- summarize_btn = gr.Button(value="Summarize")
145
-
146
- gr.Markdown("#### Examples")
147
- for idx, examples in enumerate(DEFAULT_EXAMPLES):
148
- with gr.Accordion(examples["title"], open=False):
149
- gr.Examples(
150
- examples=examples["examples"],
151
- inputs=[
152
- hidden_txtbox, instruction_txtbox
153
- ],
154
- label=None
155
- )
156
 
157
- gr.Markdown(f"{BOTTOM_LINE}")
158
-
159
- send_prompt_btn.click(
160
- chat_batch if batch_enabled else chat_stream,
161
- [context_txtbox, instruction_txtbox, state_chatbot],
162
- [state_chatbot, chatbot, context_txtbox],
163
- batch=batch_enabled,
164
- max_batch_size=args.batch_size,
165
- )
166
- send_prompt_btn.click(
167
- reset_textbox,
168
- [],
169
- [instruction_txtbox],
170
- )
171
-
172
- continue_btn.click(
173
- chat_batch if batch_enabled else chat_stream,
174
- [context_txtbox, continue_txtbox, state_chatbot],
175
- [state_chatbot, chatbot, context_txtbox],
176
- batch=batch_enabled,
177
- max_batch_size=args.batch_size,
178
- )
179
- continue_btn.click(
180
- reset_textbox,
181
- [],
182
- [instruction_txtbox],
183
- )
184
-
185
- summarize_btn.click(
186
- chat_batch if batch_enabled else chat_stream,
187
- [context_txtbox, summrize_txtbox, state_chatbot],
188
- [state_chatbot, chatbot, context_txtbox],
189
- batch=batch_enabled,
190
- max_batch_size=args.batch_size,
191
- )
192
- summarize_btn.click(
193
- reset_textbox,
194
- [],
195
- [instruction_txtbox],
196
- )
197
-
198
- demo.queue(
199
- concurrency_count=2,
200
- max_size=100,
201
- ).launch(
202
- max_threads=2,
203
- server_name="0.0.0.0",
204
  )
205
 
206
- if __name__ == "__main__":
207
- args = parse_args()
208
- run(args)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  import time
7
  import gradio as gr
8
 
 
9
  from model import load_model
10
  from gen import get_output_batch, StreamModel
11
  from utils import generate_prompt, post_processes_batch, post_process_stream, get_generation_config, common_post_process
12
 
13
+ model, tokenizer = load_model(
14
+ base="decapoda-research/llama-7b-hf",
15
+ finetuned="tloen/alpaca-lora-7b"
16
+ )
17
+
18
+ model = StreamModel(model, tokenizer)
19
+
20
  def chat_stream(
21
  context,
22
  instruction,
 
109
  def reset_textbox():
110
  return gr.Textbox.update(value='')
111
 
112
+ with gr.Blocks(css=PARENT_BLOCK_CSS) as demo:
113
+ state_chatbot = gr.State([])
114
+
115
+ with gr.Column(elem_id='col_container'):
116
+ gr.Markdown(f"## {TITLE}\n\n\n{ABSTRACT}")
117
+
118
+ with gr.Accordion("Context Setting", open=False):
119
+ context_txtbox = gr.Textbox(placeholder="Surrounding information to AI", label="Enter Context")
120
+ hidden_txtbox = gr.Textbox(placeholder="", label="Order", visible=False)
121
+
122
+ chatbot = gr.Chatbot(elem_id='chatbot', label="Alpaca-LoRA")
123
+ instruction_txtbox = gr.Textbox(placeholder="What do you want to say to AI?", label="Instruction")
124
+ send_prompt_btn = gr.Button(value="Send Prompt")
125
+
126
+ with gr.Accordion("Helper Buttons", open=False):
127
+ gr.Markdown(f"`Continue` lets AI to complete the previous incomplete answers. `Summarize` lets AI to summarize the conversations so far.")
128
+ continue_txtbox = gr.Textbox(value=SPECIAL_STRS["continue"], visible=False)
129
+ summrize_txtbox = gr.Textbox(value=SPECIAL_STRS["summarize"], visible=False)
130
+
131
+ continue_btn = gr.Button(value="Continue")
132
+ summarize_btn = gr.Button(value="Summarize")
133
+
134
+ gr.Markdown("#### Examples")
135
+ for idx, examples in enumerate(DEFAULT_EXAMPLES):
136
+ with gr.Accordion(examples["title"], open=False):
137
+ gr.Examples(
138
+ examples=examples["examples"],
139
+ inputs=[
140
+ hidden_txtbox, instruction_txtbox
141
+ ],
142
+ label=None
143
+ )
144
+
145
+ gr.Markdown(f"{BOTTOM_LINE}")
146
+
147
+ send_prompt_btn.click(
148
+ chat_batch if batch_enabled else chat_stream,
149
+ [context_txtbox, instruction_txtbox, state_chatbot],
150
+ [state_chatbot, chatbot, context_txtbox],
151
+ )
152
+ send_prompt_btn.click(
153
+ reset_textbox,
154
+ [],
155
+ [instruction_txtbox],
156
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
 
158
+ continue_btn.click(
159
+ chat_batch if batch_enabled else chat_stream,
160
+ [context_txtbox, continue_txtbox, state_chatbot],
161
+ [state_chatbot, chatbot, context_txtbox],
162
+ )
163
+ continue_btn.click(
164
+ reset_textbox,
165
+ [],
166
+ [instruction_txtbox],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
  )
168
 
169
+ summarize_btn.click(
170
+ chat_batch if batch_enabled else chat_stream,
171
+ [context_txtbox, summrize_txtbox, state_chatbot],
172
+ [state_chatbot, chatbot, context_txtbox],
173
+ )
174
+ summarize_btn.click(
175
+ reset_textbox,
176
+ [],
177
+ [instruction_txtbox],
178
+ )
179
+
180
+ demo.queue(
181
+ concurrency_count=2,
182
+ max_size=100,
183
+ ).launch(
184
+ max_threads=2,
185
+ server_name="0.0.0.0",
186
+ )
args.py DELETED
@@ -1,51 +0,0 @@
1
- import argparse
2
-
3
- def parse_args():
4
- parser = argparse.ArgumentParser(
5
- description="Gradio Application for Alpaca-LoRA as a chatbot service"
6
- )
7
- # Dataset related.
8
- parser.add_argument(
9
- "--base_url",
10
- help="Hugging Face Hub URL",
11
- default="decapoda-research/llama-7b-hf",
12
- type=str,
13
- )
14
- parser.add_argument(
15
- "--ft_ckpt_url",
16
- help="Hugging Face Hub URL",
17
- default="tloen/alpaca-lora-7b",
18
- type=str,
19
- )
20
- parser.add_argument(
21
- "--port",
22
- help="PORT number where the app is served",
23
- default=6006,
24
- type=int,
25
- )
26
- parser.add_argument(
27
- "--batch_size",
28
- help="how many requests to handle at the same time",
29
- default=1,
30
- type=int
31
- )
32
- parser.add_argument(
33
- "--api_open",
34
- help="do you want to open as API",
35
- default="no",
36
- type=str,
37
- )
38
- parser.add_argument(
39
- "--share",
40
- help="do you want to share temporarily (useful in Colab env)",
41
- default="no",
42
- type=str
43
- )
44
- parser.add_argument(
45
- "--gen_config_path",
46
- help="which config to use for GenerationConfig",
47
- default="generation_config_default.yaml",
48
- type=str
49
- )
50
-
51
- return parser.parse_args()