Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -69,10 +69,26 @@ class Conversation:
|
|
69 |
if "<image>" in message:
|
70 |
return message.replace("<image>", "")
|
71 |
return message
|
72 |
-
|
73 |
def dict(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
return {
|
75 |
-
"messages":
|
76 |
"roles": self.roles,
|
77 |
"offset": self.offset,
|
78 |
"skip_next": self.skip_next,
|
@@ -278,8 +294,8 @@ def run_inference(prompt, image, temperature=0.2, top_p=0.95, max_tokens=512):
|
|
278 |
return response.strip()
|
279 |
|
280 |
except Exception as e:
|
281 |
-
logger.error(f"Error during inference: {
|
282 |
-
return f"Error during inference: {
|
283 |
|
284 |
# Gradio UI functions
|
285 |
get_window_url_params = """
|
@@ -456,16 +472,7 @@ block_css = """
|
|
456 |
|
457 |
def build_demo(embed_mode, cur_dir=None, concurrency_count=10):
|
458 |
models = get_model_list()
|
459 |
-
|
460 |
-
with gr.Accordion("Safety Risk Taxonomy", open=False) as accordion:
|
461 |
-
textbox = gr.Textbox(
|
462 |
-
label="Safety Risk Taxonomy",
|
463 |
-
show_label=True,
|
464 |
-
placeholder="Enter your safety policy here",
|
465 |
-
container=True,
|
466 |
-
value=default_taxonomy,
|
467 |
-
lines=50)
|
468 |
-
|
469 |
with gr.Blocks(title="LlavaGuard", theme=gr.themes.Default(), css=block_css) as demo:
|
470 |
state = gr.State()
|
471 |
|
@@ -502,6 +509,14 @@ def build_demo(embed_mode, cur_dir=None, concurrency_count=10):
|
|
502 |
max_output_tokens = gr.Slider(minimum=0, maximum=1024, value=512, step=64, interactive=True,
|
503 |
label="Max output tokens")
|
504 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
505 |
with gr.Column(scale=8):
|
506 |
chatbot = gr.Chatbot(
|
507 |
elem_id="chatbot",
|
@@ -511,7 +526,13 @@ def build_demo(embed_mode, cur_dir=None, concurrency_count=10):
|
|
511 |
)
|
512 |
with gr.Row():
|
513 |
with gr.Column(scale=8):
|
514 |
-
textbox.
|
|
|
|
|
|
|
|
|
|
|
|
|
515 |
with gr.Column(scale=1, min_width=50):
|
516 |
submit_btn = gr.Button(value="Send", variant="primary")
|
517 |
with gr.Row(elem_id="buttons") as button_row:
|
@@ -649,4 +670,4 @@ if __name__ == "__main__":
|
|
649 |
)
|
650 |
except Exception as e:
|
651 |
logger.error(f"Error launching demo: {e}")
|
652 |
-
sys.exit(1)
|
|
|
69 |
if "<image>" in message:
|
70 |
return message.replace("<image>", "")
|
71 |
return message
|
72 |
+
|
73 |
def dict(self):
|
74 |
+
# Create a serializable version of messages
|
75 |
+
serialized_messages = []
|
76 |
+
for role, message in self.messages:
|
77 |
+
if isinstance(message, tuple) and len(message) > 1:
|
78 |
+
# If the message contains an image (tuple format)
|
79 |
+
if isinstance(message[1], Image.Image):
|
80 |
+
# Just keep the text part and ignore the image
|
81 |
+
serialized_message = (message[0], "[IMAGE_IGNORED]")
|
82 |
+
else:
|
83 |
+
# For non-image tuples, keep as is
|
84 |
+
serialized_message = message
|
85 |
+
else:
|
86 |
+
# For non-tuple messages, keep as is
|
87 |
+
serialized_message = message
|
88 |
+
serialized_messages.append([role, serialized_message])
|
89 |
+
|
90 |
return {
|
91 |
+
"messages": serialized_messages,
|
92 |
"roles": self.roles,
|
93 |
"offset": self.offset,
|
94 |
"skip_next": self.skip_next,
|
|
|
294 |
return response.strip()
|
295 |
|
296 |
except Exception as e:
|
297 |
+
logger.error(f"Error during inference: {e}")
|
298 |
+
return f"Error during inference: {e}"
|
299 |
|
300 |
# Gradio UI functions
|
301 |
get_window_url_params = """
|
|
|
472 |
|
473 |
def build_demo(embed_mode, cur_dir=None, concurrency_count=10):
|
474 |
models = get_model_list()
|
475 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
476 |
with gr.Blocks(title="LlavaGuard", theme=gr.themes.Default(), css=block_css) as demo:
|
477 |
state = gr.State()
|
478 |
|
|
|
509 |
max_output_tokens = gr.Slider(minimum=0, maximum=1024, value=512, step=64, interactive=True,
|
510 |
label="Max output tokens")
|
511 |
|
512 |
+
with gr.Accordion("Safety Risk Taxonomy", open=False):
|
513 |
+
taxonomy_textbox = gr.Textbox(
|
514 |
+
label="Safety Risk Taxonomy",
|
515 |
+
show_label=True,
|
516 |
+
placeholder="Enter your safety policy here",
|
517 |
+
value=default_taxonomy,
|
518 |
+
lines=20)
|
519 |
+
|
520 |
with gr.Column(scale=8):
|
521 |
chatbot = gr.Chatbot(
|
522 |
elem_id="chatbot",
|
|
|
526 |
)
|
527 |
with gr.Row():
|
528 |
with gr.Column(scale=8):
|
529 |
+
textbox = gr.Textbox(
|
530 |
+
show_label=False,
|
531 |
+
placeholder="Enter your message here",
|
532 |
+
container=True,
|
533 |
+
value=default_taxonomy,
|
534 |
+
lines=3,
|
535 |
+
)
|
536 |
with gr.Column(scale=1, min_width=50):
|
537 |
submit_btn = gr.Button(value="Send", variant="primary")
|
538 |
with gr.Row(elem_id="buttons") as button_row:
|
|
|
670 |
)
|
671 |
except Exception as e:
|
672 |
logger.error(f"Error launching demo: {e}")
|
673 |
+
sys.exit(1)
|