stevengrove commited on
Commit
0164d1b
β€’
1 Parent(s): dd1fecf

add example cache and resize input image

Browse files
Files changed (1) hide show
  1. app.py +52 -20
app.py CHANGED
@@ -39,6 +39,28 @@ def bot_request(method, *args, **kwargs):
39
  return response
40
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  if __name__ == '__main__':
43
  parser = argparse.ArgumentParser()
44
  parser.add_argument('--temperature', type=float, default=0.0, help='temperature for the llm model')
@@ -61,17 +83,20 @@ if __name__ == '__main__':
61
  os.makedirs('image')
62
 
63
  with gr.Blocks() as demo:
 
 
 
64
  with gr.Row():
65
  with gr.Column(scale=0.3):
66
  with gr.Row():
67
- image = gr.Image(type="pil", label="input image")
68
  with gr.Row():
69
- txt = gr.Textbox(lines=7, show_label=False, elem_id="textbox",
70
- placeholder="Enter text and press submit, or upload an image").style(container=False)
71
  with gr.Row():
72
- submit = gr.Button("Submit")
73
  with gr.Row():
74
- clear = gr.Button("Clear")
75
  with gr.Row():
76
  keep_last_n_paragraphs = gr.Slider(
77
  minimum=0,
@@ -79,44 +104,51 @@ if __name__ == '__main__':
79
  value=args.keep_last_n_paragraphs,
80
  step=1,
81
  interactive=True,
82
- label="Remember Last N Paragraphs")
83
  max_new_token = gr.Slider(
84
  minimum=64,
85
  maximum=512,
86
  value=args.max_new_tokens,
87
  step=1,
88
  interactive=True,
89
- label="Max New Tokens")
90
  temperature = gr.Slider(
91
  minimum=0.0,
92
  maximum=1.0,
93
  value=args.temperature,
94
  step=0.1,
95
  interactive=True,
96
- label="Temperature")
97
  top_p = gr.Slider(
98
  minimum=0.0,
99
  maximum=1.0,
100
  value=args.top_p,
101
  step=0.1,
102
  interactive=True,
103
- label="Top P")
104
  with gr.Column(scale=0.7):
105
- chatbot = gr.Chatbot(elem_id="chatbot", label="πŸ¦™ GPT4Tools").style(height=690)
106
- state = gr.State([])
107
-
108
- txt.submit(partial(bot_request, 'run_text'), [txt, state], [chatbot, state])
109
- txt.submit(lambda: "", None, txt)
110
- image.upload(lambda: "", None, txt)
111
- submit.click(partial(bot_request, 'run_image'), [image, state, txt], [chatbot, state, txt]).then(
112
- partial(bot_request, 'run_text'), [txt, state, temperature, top_p, max_new_token, keep_last_n_paragraphs], [chatbot, state, txt]).then(
 
113
  lambda: None, None, image)
114
  clear.click(partial(bot_request, 'clear'))
115
- clear.click(lambda: [], None, chatbot)
116
- clear.click(lambda: [], None, state)
117
  with gr.Row():
118
  gr.Examples(
119
  examples=examples,
 
 
120
  inputs=[image, txt],
 
 
121
  )
122
- demo.launch(enable_queue=True)
 
 
 
39
  return response
40
 
41
 
42
+ def run_image(image, *args, **kwargs):
43
+ if image is not None:
44
+ width, height = image.size
45
+ ratio = min(512 / width, 512 / height)
46
+ width_new, height_new = (round(width * ratio), round(height * ratio))
47
+ width_new = int(np.round(width_new / 64.0)) * 64
48
+ height_new = int(np.round(height_new / 64.0)) * 64
49
+ image = image.resize((width_new, height_new))
50
+ image = image.convert('RGB')
51
+ return bot_request('run_image', image, *args, **kwargs)
52
+
53
+
54
+ def predict_example(temperature, top_p, max_new_token, keep_last_n_paragraphs, image, text):
55
+ state = []
56
+ buffer = ''
57
+ chatbot, state, text, buffer = run_image(image, state, text, buffer)
58
+ chatbot, state, text, buffer = bot_request(
59
+ 'run_text', text, state, temperature, top_p,
60
+ max_new_token, keep_last_n_paragraphs, buffer)
61
+ return chatbot, state, text, None, buffer
62
+
63
+
64
  if __name__ == '__main__':
65
  parser = argparse.ArgumentParser()
66
  parser.add_argument('--temperature', type=float, default=0.0, help='temperature for the llm model')
 
83
  os.makedirs('image')
84
 
85
  with gr.Blocks() as demo:
86
+ state = gr.Chatbot([], visible=False)
87
+ buffer = gr.Textbox('', visible=False)
88
+
89
  with gr.Row():
90
  with gr.Column(scale=0.3):
91
  with gr.Row():
92
+ image = gr.Image(type='pil', label='input image')
93
  with gr.Row():
94
+ txt = gr.Textbox(lines=7, show_label=False, elem_id='textbox',
95
+ placeholder='Enter text and press submit, or upload an image').style(container=False)
96
  with gr.Row():
97
+ submit = gr.Button('Submit')
98
  with gr.Row():
99
+ clear = gr.Button('Clear')
100
  with gr.Row():
101
  keep_last_n_paragraphs = gr.Slider(
102
  minimum=0,
 
104
  value=args.keep_last_n_paragraphs,
105
  step=1,
106
  interactive=True,
107
+ label='Remember Last N Paragraphs')
108
  max_new_token = gr.Slider(
109
  minimum=64,
110
  maximum=512,
111
  value=args.max_new_tokens,
112
  step=1,
113
  interactive=True,
114
+ label='Max New Tokens')
115
  temperature = gr.Slider(
116
  minimum=0.0,
117
  maximum=1.0,
118
  value=args.temperature,
119
  step=0.1,
120
  interactive=True,
121
+ label='Temperature')
122
  top_p = gr.Slider(
123
  minimum=0.0,
124
  maximum=1.0,
125
  value=args.top_p,
126
  step=0.1,
127
  interactive=True,
128
+ label='Top P')
129
  with gr.Column(scale=0.7):
130
+ chatbot = gr.Chatbot(elem_id='chatbot', label='πŸ¦™ GPT4Tools').style(height=690)
131
+
132
+ image.upload(lambda: '', None, txt)
133
+ submit.click(run_image,
134
+ [image, state, txt, buffer],
135
+ [chatbot, state, txt, buffer]).then(
136
+ partial(bot_request, 'run_text'),
137
+ [txt, state, temperature, top_p, max_new_token, keep_last_n_paragraphs, buffer],
138
+ [chatbot, state, txt, buffer]).then(
139
  lambda: None, None, image)
140
  clear.click(partial(bot_request, 'clear'))
141
+ clear.click(lambda: [[], [], '', ''], None, [chatbot, state, txt, buffer])
142
+
143
  with gr.Row():
144
  gr.Examples(
145
  examples=examples,
146
+ fn=partial(predict_example, args.temperature, args.top_p,
147
+ args.max_new_tokens, args.keep_last_n_paragraphs),
148
  inputs=[image, txt],
149
+ outputs=[chatbot, state, txt, image, buffer],
150
+ cache_examples=True,
151
  )
152
+
153
+ demo.queue(concurrency_count=6)
154
+ demo.launch()