MAGAer13 commited on
Commit
045894b
โ€ข
1 Parent(s): ee8f457
Files changed (5) hide show
  1. .gitattributes +1 -0
  2. app.py +225 -0
  3. model_optim_rng.pt +3 -0
  4. requirements.txt +11 -0
  5. tokenizer.model +3 -0
.gitattributes CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
35
+ .pt filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import wget
3
+ # apex_file = wget.download(os.getenv('apex'))
4
+ # os.system('tar zxvf {}'.format(apex_file))
5
+
6
+ # os.system('ls -l; cd apex;'
7
+ # 'pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" .; cd ..')
8
+
9
+ # os.system('pip install gradio --upgrade')
10
+
11
+ resources = os.getenv('resources')
12
+ resources_tokenizer = os.getenv('resources_tokenizer')
13
+
14
+ _ = wget.download(resources_tokenizer)
15
+ resources_filename = wget.download(resources)
16
+
17
+ os.system('tar zxvf {}'.format(resources_filename))
18
+
19
+ if not os.path.exists('./model_optim_rng.pth'):
20
+ resources_weight = os.getenv('resources_weight')
21
+ _ = wget.download(resources_weight)
22
+
23
+ os.system('ls -l')
24
+
25
+
26
+ import time
27
+ import argparse
28
+ import datetime
29
+ import json
30
+ import re
31
+ import os
32
+ import gradio as gr
33
+ import requests
34
+ from utils import (
35
+ mplug_owl, load_demo_refresh_model_list, vote_last_response,
36
+ upvote_last_response, downvote_last_response, flag_last_response, regenerate,
37
+ add_text, after_process_image, get_inputs, init,
38
+ headers, no_change_btn, enable_btn, disable_btn, get_window_url_params
39
+ )
40
+ from gradio_css import code_highlight_css
41
+ from gradio_patch import Chatbot as grChatbot
42
+ from conversation import default_conversation
43
+
44
+ SHARED_UI_WARNING = f'''### [NOTE]
45
+ You can duplicate and use it with a paid private GPU.
46
+ <a class="duplicate-button" style="display:inline-block" target="_blank" href="https://huggingface.co/spaces/MAGAer13/mPLUG-Owl?duplicate=true"><img style="margin-top:0;margin-bottom:0" src="https://huggingface.co/datasets/huggingface/badges/raw/main/duplicate-this-space-xl-dark.svg" alt="Duplicate Space"></a>
47
+ Alternatively, you can also use the Colab demo on our project page.
48
+ <a style="display:inline-block" href="https://https://github.com/X-PLUG/mPLUG-Owl"><img style="margin-top:0;margin-bottom:0" src="https://img.shields.io/badge/Project%20Page-online-brightgreen"></a>
49
+ '''
50
+
51
+ io = None
52
+ init()
53
+ model = mplug_owl(device="cuda")
54
+ log_dir = ""
55
+
56
+ def load_demo(url_params, request: gr.Request):
57
+
58
+ dropdown_update = gr.Dropdown.update(visible=True)
59
+ if "model" in url_params:
60
+ model = url_params["model"]
61
+ if model in models:
62
+ dropdown_update = gr.Dropdown.update(
63
+ value=model, visible=True)
64
+
65
+ state = default_conversation.copy()
66
+
67
+ return (state,
68
+ dropdown_update,
69
+ gr.Chatbot.update(visible=True),
70
+ gr.Textbox.update(visible=True),
71
+ gr.Button.update(visible=True),
72
+ gr.Row.update(visible=True),
73
+ gr.Accordion.update(visible=True))
74
+
75
+ def contains_chinese(string):
76
+ pattern = re.compile(r'[ไธ€-้พฅ]')
77
+ match = pattern.search(string)
78
+ return match is not None
79
+
80
+ def clear_history(request: gr.Request):
81
+ state = default_conversation.copy()
82
+
83
+ return (state, state.to_gradio_chatbot(), "", None) + (disable_btn,) * 5
84
+
85
+ def http_bot(state, topk, max_new_tokens, random_seed, request: gr.Request):
86
+ prompt = after_process_image(state.get_prompt())
87
+ images = state.get_images()
88
+ state.messages[-1][-1] = "โ–Œ"
89
+ yield (state, state.to_gradio_chatbot()) + (disable_btn,) * 5
90
+
91
+ if contains_chinese(prompt):
92
+ state.messages[-1][-1] = "**CURRENTLY WE ONLY SUPPORT ENGLISH. PLEASE REFRESH THIS PAGE TO RESTART.**"
93
+ yield (state, state.to_gradio_chatbot()) + (disable_btn, disable_btn, disable_btn, enable_btn, enable_btn)
94
+ return
95
+
96
+ try:
97
+ data = get_inputs(prompt, images, topk, max_new_tokens, random_seed)
98
+ output = model.prediction(data, log_dir)
99
+ print(output)
100
+ # output = output.replace("```", "")
101
+
102
+ state.messages[-1][-1] = output + "โ–Œ"
103
+ yield (state, state.to_gradio_chatbot()) + (disable_btn,) * 5
104
+ time.sleep(0.03)
105
+
106
+ except requests.exceptions.RequestException as e:
107
+ state.messages[-1][-1] = "**NETWORK ERROR DUE TO HIGH TRAFFIC. PLEASE REGENERATE OR REFRESH THIS PAGE.**"
108
+ yield (state, state.to_gradio_chatbot()) + (disable_btn, disable_btn, disable_btn, enable_btn, enable_btn)
109
+ return
110
+
111
+ state.messages[-1][-1] = state.messages[-1][-1][:-1]
112
+ yield (state, state.to_gradio_chatbot()) + (enable_btn,) * 5
113
+
114
+ title_markdown = ("""
115
+ # mPLUG-Owl๐Ÿฆ‰ (GitHub Repo: https://github.com/X-PLUG/mPLUG-Owl)
116
+ """)
117
+
118
+ tos_markdown = ("""
119
+ ### Terms of use
120
+ By using this service, users are required to agree to the following terms:
121
+ The service is a research preview intended for non-commercial use only. It only provides limited safety measures and may generate offensive content. It must not be used for any illegal, harmful, violent, racist, or sexual purposes. The service may collect user dialogue data for future research.
122
+ Please click the "Flag" button if you get any inappropriate answer! We will collect those to keep improving our moderator.
123
+ For an optimal experience, please use desktop computers for this demo, as mobile devices may compromise its quality.
124
+
125
+ Copyright 2023 Alibaba DAMO Academy.
126
+ """)
127
+
128
+ learn_more_markdown = ("""
129
+ ### License
130
+ The service is a research preview intended for non-commercial use only, subject to the model [License](https://github.com/facebookresearch/llama/blob/main/MODEL_CARD.md) of LLaMA, [Terms of Use](https://openai.com/policies/terms-of-use) of the data generated by OpenAI, and [Privacy Practices](https://chrome.google.com/webstore/detail/sharegpt-share-your-chatg/daiacboceoaocpibfodeljbdfacokfjb) of ShareGPT. Please contact us if you find any potential violation.
131
+ """)
132
+
133
+ css = code_highlight_css + """
134
+ version 1.0
135
+ """
136
+
137
+ def build_demo():
138
+ #with gr.Blocks(title="mPLUG-Owl๐Ÿฆ‰", theme=gr.themes.Base(), css=css) as demo:
139
+ with gr.Blocks(title="mPLUG-Owl๐Ÿฆ‰") as demo:
140
+ state = gr.State()
141
+ with gr.Box():
142
+ gr.Markdown(SHARED_UI_WARNING)
143
+
144
+ gr.Markdown(title_markdown)
145
+
146
+ with gr.Row():
147
+ with gr.Column(scale=3):
148
+
149
+ imagebox = gr.Image(type="pil")
150
+
151
+ with gr.Accordion("Parameters", open=True, visible=False) as parameter_row:
152
+ topk = gr.Slider(minimum=1, maximum=5, value=5, step=1, interactive=True, label="Top K",)
153
+ max_output_tokens = gr.Slider(minimum=0, maximum=1024, value=512, step=64, interactive=True, label="Max output tokens",)
154
+ temperature = gr.Slider(minimum=0, maximum=10, value=1, step=0.1, interactive=True, label="Temperature",)
155
+ gr.Markdown(tos_markdown)
156
+
157
+ with gr.Column(scale=6):
158
+ chatbot = grChatbot(elem_id="chatbot", visible=False).style(height=550)
159
+ with gr.Row():
160
+ with gr.Column(scale=8):
161
+ textbox = gr.Textbox(show_label=False,
162
+ placeholder="Enter text and press ENTER", visible=False).style(container=False)
163
+ with gr.Column(scale=1, min_width=60):
164
+ submit_btn = gr.Button(value="Submit", visible=False)
165
+ with gr.Row(visible=False) as button_row:
166
+ upvote_btn = gr.Button(value="๐Ÿ‘ Upvote", interactive=False)
167
+ downvote_btn = gr.Button(value="๐Ÿ‘Ž Downvote", interactive=False)
168
+ flag_btn = gr.Button(value="โš ๏ธ Flag", interactive=False)
169
+ regenerate_btn = gr.Button(value="๐Ÿ”„ Regenerate", interactive=False)
170
+ clear_btn = gr.Button(value="๐Ÿ—‘๏ธ Clear history", interactive=False)
171
+
172
+ gr.Examples(examples=[
173
+ [f"examples/monday.jpg", "Explain why this meme is funny."],
174
+ [f'examples/rap.jpeg', 'Can you write me a master rap song that rhymes very well based on this image?'],
175
+ [f'examples/titanic.jpeg', 'What happened at the end of this movie?'],
176
+ [f'examples/vga.jpeg', 'What is funny about this image? Describe it panel by panel.'],
177
+ [f'examples/mug_ad.jpeg', 'We design new mugs shown in the image. Can you help us write an advertisement?'],
178
+ [f'examples/laundry.jpeg', 'Why this happens and how to fix it?'],
179
+ [f'examples/ca.jpeg', "What do you think about the person's behavior?"],
180
+ [f'examples/monalisa-fun.jpg', 'Do you know who drew this painting?โ€‹'],
181
+ [f"examples/Yao_Ming.jpeg", "What is the name of the man on the right?"],
182
+ ], inputs=[imagebox, textbox])
183
+
184
+ gr.Markdown(learn_more_markdown)
185
+ url_params = gr.JSON(visible=False)
186
+
187
+ btn_list = [upvote_btn, downvote_btn, flag_btn, regenerate_btn, clear_btn]
188
+ upvote_btn.click(upvote_last_response,
189
+ [state], [textbox, upvote_btn, downvote_btn, flag_btn])
190
+ downvote_btn.click(downvote_last_response,
191
+ [state], [textbox, upvote_btn, downvote_btn, flag_btn])
192
+ flag_btn.click(flag_last_response,
193
+ [state], [textbox, upvote_btn, downvote_btn, flag_btn])
194
+ regenerate_btn.click(regenerate, state,
195
+ [state, chatbot, textbox, imagebox] + btn_list).then(
196
+ http_bot, [state, topk, max_output_tokens, temperature],
197
+ [state, chatbot] + btn_list)
198
+ clear_btn.click(clear_history, None, [state, chatbot, textbox, imagebox] + btn_list)
199
+
200
+ textbox.submit(add_text, [state, textbox, imagebox], [state, chatbot, textbox, imagebox] + btn_list
201
+ ).then(http_bot, [state, topk, max_output_tokens, temperature],
202
+ [state, chatbot] + btn_list)
203
+ submit_btn.click(add_text, [state, textbox, imagebox], [state, chatbot, textbox, imagebox] + btn_list
204
+ ).then(http_bot, [state, topk, max_output_tokens, temperature],
205
+ [state, chatbot] + btn_list)
206
+
207
+ demo.load(load_demo, [url_params], [state,
208
+ chatbot, textbox, submit_btn, button_row, parameter_row],
209
+ _js=get_window_url_params)
210
+
211
+ return demo
212
+
213
+ if __name__ == "__main__":
214
+ cur_dir = os.getenv("cur_dir")
215
+ log_dir = cur_dir + "log"
216
+
217
+ parser = argparse.ArgumentParser()
218
+ parser.add_argument("--host", type=str, default="0.0.0.0")
219
+ parser.add_argument("--debug", action="store_true", help="using debug mode")
220
+ parser.add_argument("--port", type=int)
221
+ parser.add_argument("--concurrency-count", type=int, default=100)
222
+ args = parser.parse_args()
223
+ demo = build_demo()
224
+ demo.queue(concurrency_count=args.concurrency_count, status_update_rate=10, api_open=False).launch(server_name=args.host, debug=args.debug, server_port=args.port, share=False)
225
+
model_optim_rng.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:af244914525f592ad44647084b05b02e49c12e6d42e91232bbe6cd7aeff0ed17
3
+ size 14305364484
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ icecream
2
+ transformers==4.28.1
3
+ tqdm
4
+ decord==0.6.0
5
+ timm==0.6.7
6
+ oss2
7
+ markdown2
8
+ hjson
9
+ einops
10
+ wget
11
+ gradio==3.27.0
tokenizer.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9e556afd44213b6bd1be2b850ebbbd98f5481437a8021afaf58ee7fb1818d347
3
+ size 499723