Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -626,92 +626,6 @@ def get_backend_health() -> str:
|
|
| 626 |
# ============================================================================
|
| 627 |
# GRADIO INTERFACE
|
| 628 |
# ============================================================================
|
| 629 |
-
|
| 630 |
-
with gr.Blocks(title="ZeroEngine-Backend") as demo:
|
| 631 |
-
# Apply theme after Blocks creation for Gradio 6.5.0 compatibility
|
| 632 |
-
if hasattr(demo, 'theme'):
|
| 633 |
-
demo.theme = gr.themes.Monochrome()
|
| 634 |
-
gr.HTML("""
|
| 635 |
-
<div style='text-align: center; padding: 20px;'>
|
| 636 |
-
<h1>π§ ZeroEngine-Backend</h1>
|
| 637 |
-
<p style='color: #888;'>Background Processing Service for ZeroEngine</p>
|
| 638 |
-
</div>
|
| 639 |
-
""")
|
| 640 |
-
|
| 641 |
-
with gr.Tab("π’ Tokenize"):
|
| 642 |
-
gr.Markdown("### Fast Tokenization Pre-Processing")
|
| 643 |
-
with gr.Row():
|
| 644 |
-
with gr.Column():
|
| 645 |
-
tokenize_input = gr.Textbox(
|
| 646 |
-
label="Text to Tokenize",
|
| 647 |
-
placeholder="Enter text here...",
|
| 648 |
-
lines=5
|
| 649 |
-
)
|
| 650 |
-
tokenize_btn = gr.Button("Tokenize", variant="primary")
|
| 651 |
-
with gr.Column():
|
| 652 |
-
tokenize_output = gr.Code(label="Result (JSON)", language="json")
|
| 653 |
-
|
| 654 |
-
tokenize_btn.click(tokenize_text, [tokenize_input], [tokenize_output])
|
| 655 |
-
|
| 656 |
-
with gr.Tab("πΎ Prompt Cache"):
|
| 657 |
-
gr.Markdown("### Store and Retrieve Prompts")
|
| 658 |
-
with gr.Row():
|
| 659 |
-
with gr.Column():
|
| 660 |
-
cache_key_input = gr.Textbox(label="Cache Key")
|
| 661 |
-
cache_value_input = gr.Textbox(label="Value to Cache", lines=3)
|
| 662 |
-
cache_store_btn = gr.Button("Store", variant="primary")
|
| 663 |
-
cache_store_output = gr.Code(label="Result", language="json")
|
| 664 |
-
|
| 665 |
-
with gr.Column():
|
| 666 |
-
cache_get_input = gr.Textbox(label="Key to Retrieve")
|
| 667 |
-
cache_get_btn = gr.Button("Retrieve", variant="secondary")
|
| 668 |
-
cache_get_output = gr.Code(label="Result", language="json")
|
| 669 |
-
|
| 670 |
-
cache_store_btn.click(cache_prompt, [cache_key_input, cache_value_input], [cache_store_output])
|
| 671 |
-
cache_get_btn.click(get_cached_prompt, [cache_get_input], [cache_get_output])
|
| 672 |
-
|
| 673 |
-
with gr.Tab("β‘ Response Cache"):
|
| 674 |
-
gr.Markdown("### Cache Complete Responses")
|
| 675 |
-
with gr.Row():
|
| 676 |
-
with gr.Column():
|
| 677 |
-
resp_hash_input = gr.Textbox(label="Prompt Hash")
|
| 678 |
-
resp_value_input = gr.Textbox(label="Response to Cache", lines=5)
|
| 679 |
-
resp_cache_btn = gr.Button("Cache Response", variant="primary")
|
| 680 |
-
resp_cache_output = gr.Code(label="Result", language="json")
|
| 681 |
-
|
| 682 |
-
with gr.Column():
|
| 683 |
-
resp_get_input = gr.Textbox(label="Hash to Retrieve")
|
| 684 |
-
resp_get_btn = gr.Button("Get Response", variant="secondary")
|
| 685 |
-
resp_get_output = gr.Code(label="Result", language="json")
|
| 686 |
-
|
| 687 |
-
resp_cache_btn.click(cache_response, [resp_hash_input, resp_value_input], [resp_cache_output])
|
| 688 |
-
resp_get_btn.click(get_cached_response, [resp_get_input], [resp_get_output])
|
| 689 |
-
|
| 690 |
-
with gr.Tab("π° Token Accounting"):
|
| 691 |
-
gr.Markdown("### Calculate Token Costs")
|
| 692 |
-
with gr.Row():
|
| 693 |
-
username_input = gr.Textbox(label="Username", value="turtle170")
|
| 694 |
-
duration_input = gr.Number(label="Duration (ms)", value=5000)
|
| 695 |
-
|
| 696 |
-
calc_btn = gr.Button("Calculate Cost", variant="primary")
|
| 697 |
-
calc_output = gr.Code(label="Result (JSON)", language="json")
|
| 698 |
-
|
| 699 |
-
calc_btn.click(calculate_token_cost, [username_input, duration_input], [calc_output])
|
| 700 |
-
|
| 701 |
-
with gr.Tab("π Stats"):
|
| 702 |
-
gr.Markdown("### Cache Statistics")
|
| 703 |
-
stats_btn = gr.Button("Get Stats", variant="primary")
|
| 704 |
-
stats_output = gr.Code(label="Statistics (JSON)", language="json")
|
| 705 |
-
|
| 706 |
-
stats_btn.click(get_cache_stats, None, [stats_output])
|
| 707 |
-
|
| 708 |
-
with gr.Tab("π₯ Health"):
|
| 709 |
-
gr.Markdown("### Backend Health Status")
|
| 710 |
-
health_btn = gr.Button("Check Health", variant="primary")
|
| 711 |
-
health_output = gr.Code(label="Health Status (JSON)", language="json")
|
| 712 |
-
|
| 713 |
-
health_btn.click(get_backend_health, None, [health_output])
|
| 714 |
-
|
| 715 |
if __name__ == "__main__":
|
| 716 |
import atexit
|
| 717 |
import signal
|
|
@@ -754,4 +668,124 @@ if __name__ == "__main__":
|
|
| 754 |
logger.info(f"[INIT] Server will launch on port 7861")
|
| 755 |
logger.info(f"[INIT] ===== BACKEND APPLICATION STARTUP END =====")
|
| 756 |
|
| 757 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 626 |
# ============================================================================
|
| 627 |
# GRADIO INTERFACE
|
| 628 |
# ============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 629 |
if __name__ == "__main__":
|
| 630 |
import atexit
|
| 631 |
import signal
|
|
|
|
| 668 |
logger.info(f"[INIT] Server will launch on port 7861")
|
| 669 |
logger.info(f"[INIT] ===== BACKEND APPLICATION STARTUP END =====")
|
| 670 |
|
| 671 |
+
logger.info("[INIT] Creating Gradio interface...")
|
| 672 |
+
try:
|
| 673 |
+
with gr.Blocks(title="ZeroEngine-Backend") as demo:
|
| 674 |
+
logger.info("[INIT] Gradio Blocks created successfully")
|
| 675 |
+
# Apply theme after Blocks creation for Gradio 6.5.0 compatibility
|
| 676 |
+
if hasattr(demo, 'theme'):
|
| 677 |
+
logger.info("[INIT] Applying theme...")
|
| 678 |
+
demo.theme = gr.themes.Monochrome()
|
| 679 |
+
logger.info("[INIT] Theme applied successfully")
|
| 680 |
+
else:
|
| 681 |
+
logger.warning("[INIT] Theme attribute not found, skipping theme application")
|
| 682 |
+
|
| 683 |
+
logger.info("[INIT] Creating HTML header...")
|
| 684 |
+
gr.HTML("""
|
| 685 |
+
<div style='text-align: center; padding: 20px;'>
|
| 686 |
+
<h1>π§ ZeroEngine-Backend</h1>
|
| 687 |
+
<p style='color: #888;'>Background Processing Service for ZeroEngine</p>
|
| 688 |
+
</div>
|
| 689 |
+
""")
|
| 690 |
+
logger.info("[INIT] HTML header created")
|
| 691 |
+
|
| 692 |
+
logger.info("[INIT] Creating tabs...")
|
| 693 |
+
with gr.Tab("π’ Tokenize"):
|
| 694 |
+
logger.info("[INIT] Tokenize tab created")
|
| 695 |
+
gr.Markdown("### Fast Tokenization Pre-Processing")
|
| 696 |
+
with gr.Row():
|
| 697 |
+
with gr.Column():
|
| 698 |
+
tokenize_input = gr.Textbox(
|
| 699 |
+
label="Text to Tokenize",
|
| 700 |
+
placeholder="Enter text here...",
|
| 701 |
+
lines=5
|
| 702 |
+
)
|
| 703 |
+
tokenize_btn = gr.Button("Tokenize", variant="primary")
|
| 704 |
+
with gr.Column():
|
| 705 |
+
tokenize_output = gr.Code(label="Result (JSON)", language="json")
|
| 706 |
+
|
| 707 |
+
tokenize_btn.click(tokenize_text, [tokenize_input], [tokenize_output])
|
| 708 |
+
logger.info("[INIT] Tokenize tab components configured")
|
| 709 |
+
|
| 710 |
+
with gr.Tab("πΎ Prompt Cache"):
|
| 711 |
+
logger.info("[INIT] Prompt Cache tab created")
|
| 712 |
+
gr.Markdown("### Store and Retrieve Prompts")
|
| 713 |
+
with gr.Row():
|
| 714 |
+
with gr.Column():
|
| 715 |
+
cache_key_input = gr.Textbox(label="Cache Key")
|
| 716 |
+
cache_value_input = gr.Textbox(label="Value to Cache", lines=3)
|
| 717 |
+
cache_store_btn = gr.Button("Store", variant="primary")
|
| 718 |
+
cache_store_output = gr.Code(label="Result", language="json")
|
| 719 |
+
|
| 720 |
+
with gr.Column():
|
| 721 |
+
cache_get_input = gr.Textbox(label="Key to Retrieve")
|
| 722 |
+
cache_get_btn = gr.Button("Retrieve", variant="secondary")
|
| 723 |
+
cache_get_output = gr.Code(label="Result", language="json")
|
| 724 |
+
|
| 725 |
+
cache_store_btn.click(cache_prompt, [cache_key_input, cache_value_input], [cache_store_output])
|
| 726 |
+
cache_get_btn.click(get_cached_prompt, [cache_get_input], [cache_get_output])
|
| 727 |
+
logger.info("[INIT] Prompt Cache tab components configured")
|
| 728 |
+
|
| 729 |
+
with gr.Tab("β‘ Response Cache"):
|
| 730 |
+
logger.info("[INIT] Response Cache tab created")
|
| 731 |
+
gr.Markdown("### Cache Complete Responses")
|
| 732 |
+
with gr.Row():
|
| 733 |
+
with gr.Column():
|
| 734 |
+
resp_hash_input = gr.Textbox(label="Prompt Hash")
|
| 735 |
+
resp_value_input = gr.Textbox(label="Response to Cache", lines=5)
|
| 736 |
+
resp_cache_btn = gr.Button("Cache Response", variant="primary")
|
| 737 |
+
resp_cache_output = gr.Code(label="Result", language="json")
|
| 738 |
+
|
| 739 |
+
with gr.Column():
|
| 740 |
+
resp_get_input = gr.Textbox(label="Hash to Retrieve")
|
| 741 |
+
resp_get_btn = gr.Button("Get Response", variant="secondary")
|
| 742 |
+
resp_get_output = gr.Code(label="Result", language="json")
|
| 743 |
+
|
| 744 |
+
resp_cache_btn.click(cache_response, [resp_hash_input, resp_value_input], [resp_cache_output])
|
| 745 |
+
resp_get_btn.click(get_cached_response, [resp_get_input], [resp_get_output])
|
| 746 |
+
logger.info("[INIT] Response Cache tab components configured")
|
| 747 |
+
|
| 748 |
+
with gr.Tab("π° Token Accounting"):
|
| 749 |
+
logger.info("[INIT] Token Accounting tab created")
|
| 750 |
+
gr.Markdown("### Calculate Token Costs")
|
| 751 |
+
with gr.Row():
|
| 752 |
+
username_input = gr.Textbox(label="Username", value="turtle170")
|
| 753 |
+
duration_input = gr.Number(label="Duration (ms)", value=5000)
|
| 754 |
+
|
| 755 |
+
calc_btn = gr.Button("Calculate Cost", variant="primary")
|
| 756 |
+
calc_output = gr.Code(label="Result (JSON)", language="json")
|
| 757 |
+
|
| 758 |
+
calc_btn.click(calculate_token_cost, [username_input, duration_input], [calc_output])
|
| 759 |
+
logger.info("[INIT] Token Accounting tab components configured")
|
| 760 |
+
|
| 761 |
+
with gr.Tab("π Stats"):
|
| 762 |
+
logger.info("[INIT] Stats tab created")
|
| 763 |
+
gr.Markdown("### Cache Statistics")
|
| 764 |
+
stats_btn = gr.Button("Get Stats", variant="primary")
|
| 765 |
+
stats_output = gr.Code(label="Statistics (JSON)", language="json")
|
| 766 |
+
|
| 767 |
+
stats_btn.click(get_cache_stats, None, [stats_output])
|
| 768 |
+
logger.info("[INIT] Stats tab components configured")
|
| 769 |
+
|
| 770 |
+
with gr.Tab("π₯ Health"):
|
| 771 |
+
logger.info("[INIT] Health tab created")
|
| 772 |
+
gr.Markdown("### Backend Health Status")
|
| 773 |
+
health_btn = gr.Button("Check Health", variant="primary")
|
| 774 |
+
health_output = gr.Code(label="Health Status (JSON)", language="json")
|
| 775 |
+
|
| 776 |
+
health_btn.click(get_backend_health, None, [health_output])
|
| 777 |
+
logger.info("[INIT] Health tab components configured")
|
| 778 |
+
|
| 779 |
+
logger.info("[INIT] All tabs created successfully")
|
| 780 |
+
|
| 781 |
+
logger.info("[INIT] Launching Gradio demo...")
|
| 782 |
+
demo.launch(server_name="0.0.0.0", server_port=7861, ssr_mode=False)
|
| 783 |
+
logger.info("[INIT] Gradio demo launched successfully")
|
| 784 |
+
|
| 785 |
+
except Exception as e:
|
| 786 |
+
logger.error(f"[INIT] Failed to create Gradio interface: {e}")
|
| 787 |
+
logger.error(f"[INIT] Error type: {type(e).__name__}")
|
| 788 |
+
logger.error(f"[INIT] Error details: {str(e)}")
|
| 789 |
+
import traceback
|
| 790 |
+
logger.error(f"[INIT] Traceback: {traceback.format_exc()}")
|
| 791 |
+
raise
|