Spaces:
Running
Running
update app.py and gradio_web_server.py
Browse files- app.py +1 -1
- gradio_web_server.py +21 -10
app.py
CHANGED
@@ -74,7 +74,7 @@ def main(
|
|
74 |
atexit.register(worker_process.terminate)
|
75 |
worker_port += 1
|
76 |
|
77 |
-
time.sleep(60
|
78 |
gradio_process = None
|
79 |
if run_gradio:
|
80 |
# python gradio_web_server.py --port 10088 --controller-url http://0.0.0.0:10086
|
|
|
74 |
atexit.register(worker_process.terminate)
|
75 |
worker_port += 1
|
76 |
|
77 |
+
time.sleep(60)
|
78 |
gradio_process = None
|
79 |
if run_gradio:
|
80 |
# python gradio_web_server.py --port 10088 --controller-url http://0.0.0.0:10086
|
gradio_web_server.py
CHANGED
@@ -165,8 +165,9 @@ def query_image_generation(response, sd_worker_url, timeout=15):
|
|
165 |
return None
|
166 |
|
167 |
|
168 |
-
def load_demo(url_params, request: gr.Request):
|
169 |
-
|
|
|
170 |
|
171 |
dropdown_update = gr.Dropdown(visible=True)
|
172 |
if "model" in url_params:
|
@@ -178,8 +179,9 @@ def load_demo(url_params, request: gr.Request):
|
|
178 |
return state, dropdown_update
|
179 |
|
180 |
|
181 |
-
def load_demo_refresh_model_list(request: gr.Request):
|
182 |
-
|
|
|
183 |
models = get_model_list()
|
184 |
state = init_state()
|
185 |
dropdown_update = gr.Dropdown(
|
@@ -263,7 +265,10 @@ def change_system_prompt(state, system_prompt, request: gr.Request):
|
|
263 |
return state
|
264 |
|
265 |
|
266 |
-
def add_text(state, message, system_prompt, request: gr.Request):
|
|
|
|
|
|
|
267 |
images = message.get("files", [])
|
268 |
text = message.get("text", "").strip()
|
269 |
logger.info(f"add_text. ip: {request.client.host}. len: {len(text)}")
|
@@ -287,7 +292,9 @@ def add_text(state, message, system_prompt, request: gr.Request):
|
|
287 |
state.set_system_message(system_prompt)
|
288 |
state.append_message(Conversation.USER, text, images)
|
289 |
state.skip_next = False
|
290 |
-
return (state, state.to_gradio_chatbot(), textbox) + (
|
|
|
|
|
291 |
|
292 |
|
293 |
def http_bot(
|
@@ -746,8 +753,8 @@ def build_demo(embed_mode):
|
|
746 |
|
747 |
textbox.submit(
|
748 |
add_text,
|
749 |
-
[state, textbox, system_prompt],
|
750 |
-
[state, chatbot, textbox] + btn_list,
|
751 |
).then(
|
752 |
http_bot,
|
753 |
[
|
@@ -765,8 +772,8 @@ def build_demo(embed_mode):
|
|
765 |
)
|
766 |
submit_btn.click(
|
767 |
add_text,
|
768 |
-
[state, textbox, system_prompt],
|
769 |
-
[state, chatbot, textbox] + btn_list,
|
770 |
).then(
|
771 |
http_bot,
|
772 |
[
|
@@ -783,6 +790,9 @@ def build_demo(embed_mode):
|
|
783 |
[state, chatbot, textbox] + btn_list,
|
784 |
)
|
785 |
|
|
|
|
|
|
|
786 |
if args.model_list_mode == "once":
|
787 |
demo.load(
|
788 |
load_demo,
|
@@ -799,6 +809,7 @@ def build_demo(embed_mode):
|
|
799 |
)
|
800 |
else:
|
801 |
raise ValueError(f"Unknown model list mode: {args.model_list_mode}")
|
|
|
802 |
|
803 |
return demo
|
804 |
|
|
|
165 |
return None
|
166 |
|
167 |
|
168 |
+
def load_demo(url_params, request: gr.Request = None):
|
169 |
+
if not request:
|
170 |
+
logger.info(f"load_demo. ip: {request.client.host}. params: {url_params}")
|
171 |
|
172 |
dropdown_update = gr.Dropdown(visible=True)
|
173 |
if "model" in url_params:
|
|
|
179 |
return state, dropdown_update
|
180 |
|
181 |
|
182 |
+
def load_demo_refresh_model_list(request: gr.Request = None):
|
183 |
+
if not request:
|
184 |
+
logger.info(f"load_demo. ip: {request.client.host}")
|
185 |
models = get_model_list()
|
186 |
state = init_state()
|
187 |
dropdown_update = gr.Dropdown(
|
|
|
265 |
return state
|
266 |
|
267 |
|
268 |
+
def add_text(state, message, system_prompt, model_selector, request: gr.Request):
|
269 |
+
print(f"state: {state}")
|
270 |
+
if not state:
|
271 |
+
state, model_selector = load_demo_refresh_model_list(request)
|
272 |
images = message.get("files", [])
|
273 |
text = message.get("text", "").strip()
|
274 |
logger.info(f"add_text. ip: {request.client.host}. len: {len(text)}")
|
|
|
292 |
state.set_system_message(system_prompt)
|
293 |
state.append_message(Conversation.USER, text, images)
|
294 |
state.skip_next = False
|
295 |
+
return (state, state.to_gradio_chatbot(), textbox, model_selector) + (
|
296 |
+
disable_btn,
|
297 |
+
) * 5
|
298 |
|
299 |
|
300 |
def http_bot(
|
|
|
753 |
|
754 |
textbox.submit(
|
755 |
add_text,
|
756 |
+
[state, textbox, system_prompt, model_selector],
|
757 |
+
[state, chatbot, textbox, model_selector] + btn_list,
|
758 |
).then(
|
759 |
http_bot,
|
760 |
[
|
|
|
772 |
)
|
773 |
submit_btn.click(
|
774 |
add_text,
|
775 |
+
[state, textbox, system_prompt, model_selector],
|
776 |
+
[state, chatbot, textbox, model_selector] + btn_list,
|
777 |
).then(
|
778 |
http_bot,
|
779 |
[
|
|
|
790 |
[state, chatbot, textbox] + btn_list,
|
791 |
)
|
792 |
|
793 |
+
# NOTE: The following code will be not triggered when deployed on HF space.
|
794 |
+
# It's very strange. I don't know why.
|
795 |
+
"""
|
796 |
if args.model_list_mode == "once":
|
797 |
demo.load(
|
798 |
load_demo,
|
|
|
809 |
)
|
810 |
else:
|
811 |
raise ValueError(f"Unknown model list mode: {args.model_list_mode}")
|
812 |
+
"""
|
813 |
|
814 |
return demo
|
815 |
|