Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -778,7 +778,41 @@ def generate_response_stream(prompt, temperature=0.7, backend=None, max_tokens=2
|
|
| 778 |
final_tokens_per_sec = tokens_generated / elapsed if elapsed > 0 else 0
|
| 779 |
yield "", False, final_tokens_per_sec, final_tokens_per_sec, False
|
| 780 |
|
| 781 |
-
# PART 3 - Continue from Part 2 -
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 782 |
|
| 783 |
if __name__ == "__main__":
|
| 784 |
import gradio as gr
|
|
@@ -804,6 +838,8 @@ if __name__ == "__main__":
|
|
| 804 |
.send-btn { background: linear-gradient(135deg, #10a37f 0%, #0d8c6c 100%) !important; border: none !important; }
|
| 805 |
.stop-btn { background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%) !important; border: none !important; }
|
| 806 |
.announcement-banner { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px 28px; border-radius: 12px; margin-bottom: 20px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); text-align: center; font-size: 16px; font-weight: 500; line-height: 1.6; }
|
|
|
|
|
|
|
| 807 |
"""
|
| 808 |
|
| 809 |
def format_message_html(role, content, show_thinking=True):
|
|
@@ -845,10 +881,10 @@ if __name__ == "__main__":
|
|
| 845 |
html += format_message_html(msg["role"], msg["content"], show_thinking)
|
| 846 |
return html
|
| 847 |
|
| 848 |
-
def render_limits_panel(
|
| 849 |
-
if not
|
| 850 |
return ""
|
| 851 |
-
info = get_user_limits_info(
|
| 852 |
if not info:
|
| 853 |
return ""
|
| 854 |
plan_badge_class = f"plan-{info['plan']}"
|
|
@@ -870,31 +906,34 @@ if __name__ == "__main__":
|
|
| 870 |
return html
|
| 871 |
|
| 872 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="slate")) as demo:
|
| 873 |
-
# Session state
|
| 874 |
-
user_session = gr.State({})
|
| 875 |
|
| 876 |
-
gr.HTML('<div class="announcement-banner">🔐 <strong>SAM-X-1 V3.0 -
|
| 877 |
|
| 878 |
with gr.Tabs() as main_tabs:
|
| 879 |
with gr.Tab("🔐 Sign In"):
|
| 880 |
with gr.Column():
|
| 881 |
login_username = gr.Textbox(label="Username", placeholder="Enter username")
|
| 882 |
login_password = gr.Textbox(label="Password", type="password", placeholder="Enter password")
|
| 883 |
-
login_btn = gr.Button("Sign In", variant="primary")
|
| 884 |
login_msg = gr.Markdown("")
|
|
|
|
| 885 |
|
| 886 |
with gr.Tab("📝 Sign Up"):
|
| 887 |
with gr.Column():
|
| 888 |
signup_username = gr.Textbox(label="Username", placeholder="Choose a username")
|
| 889 |
signup_email = gr.Textbox(label="Email (optional)", placeholder="your@email.com")
|
| 890 |
signup_password = gr.Textbox(label="Password", type="password", placeholder="Choose a password")
|
| 891 |
-
signup_btn = gr.Button("Create Account", variant="primary")
|
| 892 |
signup_msg = gr.Markdown("")
|
| 893 |
|
| 894 |
with gr.Tab("💬 Chat") as chat_tab:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 895 |
with gr.Row():
|
| 896 |
with gr.Column(scale=4):
|
| 897 |
-
user_info = gr.Markdown("
|
| 898 |
with gr.Column(scale=1):
|
| 899 |
logout_btn = gr.Button("🚪 Logout", size="sm")
|
| 900 |
|
|
@@ -912,10 +951,10 @@ if __name__ == "__main__":
|
|
| 912 |
chat_html = gr.HTML(value="", elem_classes=["chat-container"])
|
| 913 |
|
| 914 |
with gr.Row():
|
| 915 |
-
msg_input = gr.Textbox(placeholder="
|
| 916 |
with gr.Column(scale=1, min_width=120):
|
| 917 |
with gr.Row():
|
| 918 |
-
send_btn = gr.Button("▶", variant="primary", elem_classes=["circular-btn", "send-btn"])
|
| 919 |
stop_btn = gr.Button("⏹", variant="stop", elem_classes=["circular-btn", "stop-btn"], interactive=False)
|
| 920 |
|
| 921 |
with gr.Row():
|
|
@@ -946,24 +985,27 @@ if __name__ == "__main__":
|
|
| 946 |
request_msg = gr.Markdown("")
|
| 947 |
|
| 948 |
with gr.Accordion("⭐ Request Plan Upgrade", visible=False, open=False) as upgrade_panel:
|
|
|
|
| 949 |
upgrade_plan_choice = gr.Radio(choices=["plus", "pro"], label="Select Plan", value="plus")
|
| 950 |
upgrade_reason = gr.Textbox(label="Reason for Upgrade", placeholder="Why do you need this upgrade?", lines=3)
|
| 951 |
submit_upgrade_btn = gr.Button("Submit Request", variant="primary")
|
| 952 |
upgrade_msg = gr.Markdown("")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 953 |
|
| 954 |
# Event handlers
|
| 955 |
def handle_login(username, password):
|
| 956 |
success, user_data = authenticate_user(username, password)
|
| 957 |
if success:
|
| 958 |
-
|
| 959 |
-
|
| 960 |
-
|
| 961 |
-
|
| 962 |
-
|
| 963 |
-
gr.update(selected=2) # Switch to Chat tab (index 2)
|
| 964 |
-
)
|
| 965 |
-
return {}, "❌ Invalid credentials!", "", gr.update()
|
| 966 |
-
|
| 967 |
def handle_signup(username, email, password):
|
| 968 |
if len(username) < 3:
|
| 969 |
return "❌ Username must be at least 3 characters!"
|
|
@@ -971,36 +1013,24 @@ if __name__ == "__main__":
|
|
| 971 |
return "❌ Password must be at least 6 characters!"
|
| 972 |
success, message = create_user(username, password, email)
|
| 973 |
if success:
|
| 974 |
-
return f"✅ {message} Now sign in
|
| 975 |
return f"❌ {message}"
|
| 976 |
|
| 977 |
-
def
|
| 978 |
-
|
| 979 |
-
|
| 980 |
|
| 981 |
-
|
| 982 |
-
|
| 983 |
-
return (
|
| 984 |
-
"⚠️ Please sign in to use chat",
|
| 985 |
-
"",
|
| 986 |
-
gr.update(visible=False),
|
| 987 |
-
gr.update(choices=["🤖 Auto (Recommended)"], value="🤖 Auto (Recommended)"),
|
| 988 |
-
gr.update()
|
| 989 |
-
)
|
| 990 |
|
| 991 |
-
info = get_user_limits_info(
|
| 992 |
if not info:
|
| 993 |
-
return (
|
| 994 |
-
"⚠️ Could not load user info",
|
| 995 |
-
"",
|
| 996 |
-
gr.update(visible=False),
|
| 997 |
-
gr.update(choices=["🤖 Auto (Recommended)"], value="🤖 Auto (Recommended)"),
|
| 998 |
-
gr.update()
|
| 999 |
-
)
|
| 1000 |
|
| 1001 |
plan_badge = f'<span class="plan-badge plan-{info["plan"]}">{info["plan"].upper()}</span>'
|
| 1002 |
-
user_info_text = f"
|
| 1003 |
-
limits_html = render_limits_panel(
|
| 1004 |
|
| 1005 |
if info['can_choose_model']:
|
| 1006 |
available_model_names = list(available_models.keys())
|
|
@@ -1008,33 +1038,37 @@ if __name__ == "__main__":
|
|
| 1008 |
else:
|
| 1009 |
choices = ["🤖 Auto (Recommended)"]
|
| 1010 |
|
| 1011 |
-
|
| 1012 |
-
is_admin = session.get('is_admin', False)
|
| 1013 |
|
| 1014 |
return (
|
| 1015 |
user_info_text,
|
| 1016 |
limits_html,
|
| 1017 |
gr.update(visible=is_admin),
|
| 1018 |
gr.update(choices=choices, value="🤖 Auto (Recommended)"),
|
| 1019 |
-
gr.update(maximum=info['max_tokens'], value=min(256, info['max_tokens']))
|
|
|
|
|
|
|
| 1020 |
)
|
| 1021 |
-
|
| 1022 |
|
| 1023 |
-
def send_message_handler(message, show_thinking, temperature, model_choice, max_tokens,
|
| 1024 |
global stop_generation
|
| 1025 |
stop_generation.clear()
|
| 1026 |
|
| 1027 |
-
if not
|
| 1028 |
-
return "", "", "❌
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1029 |
|
| 1030 |
if not message.strip():
|
| 1031 |
return "", "", "⚡ Ready", gr.update(interactive=True), gr.update(interactive=False)
|
| 1032 |
|
| 1033 |
-
info = get_user_limits_info(
|
| 1034 |
|
| 1035 |
# Auto or manual model selection
|
| 1036 |
if model_choice == "🤖 Auto (Recommended)" or not info['can_choose_model']:
|
| 1037 |
-
user_available = get_available_models_for_user(
|
| 1038 |
if not user_available:
|
| 1039 |
return "", "", "❌ No models available (limits reached)", gr.update(interactive=True), gr.update(interactive=False)
|
| 1040 |
backend = select_model_auto(message, available_models, user_available)
|
|
@@ -1043,18 +1077,18 @@ if __name__ == "__main__":
|
|
| 1043 |
model_name = backend.get_name()
|
| 1044 |
else:
|
| 1045 |
model_name = model_choice
|
| 1046 |
-
can_use, msg = can_use_model(
|
| 1047 |
if not can_use:
|
| 1048 |
return "", "", f"❌ {msg}", gr.update(interactive=True), gr.update(interactive=False)
|
| 1049 |
backend = available_models[model_name]
|
| 1050 |
|
| 1051 |
# Final check
|
| 1052 |
-
can_use, msg = can_use_model(
|
| 1053 |
if not can_use:
|
| 1054 |
return "", "", f"❌ {msg}", gr.update(interactive=True), gr.update(interactive=False)
|
| 1055 |
|
| 1056 |
# Increment usage
|
| 1057 |
-
increment_model_usage(
|
| 1058 |
|
| 1059 |
yield "", "", f"⚡ Using {model_name}...", gr.update(interactive=False), gr.update(interactive=True)
|
| 1060 |
|
|
@@ -1097,14 +1131,52 @@ if __name__ == "__main__":
|
|
| 1097 |
def show_upgrade_panel():
|
| 1098 |
return gr.update(visible=True, open=True)
|
| 1099 |
|
| 1100 |
-
def submit_upgrade_request(plan, reason
|
| 1101 |
-
if not
|
| 1102 |
-
return "❌
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1103 |
if not reason.strip():
|
| 1104 |
return "❌ Please provide a reason"
|
| 1105 |
-
|
|
|
|
| 1106 |
return f"{'✅' if success else '❌'} {msg}"
|
| 1107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1108 |
def load_all_users():
|
| 1109 |
users = get_all_users()
|
| 1110 |
formatted = []
|
|
@@ -1137,27 +1209,31 @@ if __name__ == "__main__":
|
|
| 1137 |
success, msg = deny_request(int(request_id))
|
| 1138 |
return f"{'✅' if success else '❌'} {msg}"
|
| 1139 |
|
| 1140 |
-
def handle_logout():
|
| 1141 |
-
return {}, "❌ Logged out - Please sign in", ""
|
| 1142 |
-
|
| 1143 |
# Wire up events
|
| 1144 |
-
login_btn.click(handle_login, [login_username, login_password], [
|
| 1145 |
signup_btn.click(handle_signup, [signup_username, signup_email, signup_password], [signup_msg])
|
| 1146 |
|
| 1147 |
-
#
|
| 1148 |
-
|
|
|
|
| 1149 |
|
|
|
|
| 1150 |
send_outputs = [msg_input, chat_html, speed_display, send_btn, stop_btn]
|
| 1151 |
-
send_btn.click(send_message_handler, [msg_input, show_thinking_checkbox, temperature_slider, model_selector, max_tokens_slider,
|
| 1152 |
-
msg_input.submit(send_message_handler, [msg_input, show_thinking_checkbox, temperature_slider, model_selector, max_tokens_slider,
|
| 1153 |
|
| 1154 |
stop_btn.click(stop_generation_handler, outputs=[speed_display, send_btn, stop_btn])
|
| 1155 |
clear_btn.click(clear_chat, outputs=[chat_html, speed_display, send_btn, stop_btn])
|
| 1156 |
upgrade_btn.click(show_upgrade_panel, outputs=[upgrade_panel])
|
| 1157 |
-
submit_upgrade_btn.click(submit_upgrade_request, [upgrade_plan_choice, upgrade_reason
|
| 1158 |
-
|
|
|
|
|
|
|
| 1159 |
|
| 1160 |
# Admin panel events
|
|
|
|
|
|
|
|
|
|
| 1161 |
refresh_users_btn.click(load_all_users, outputs=[users_table])
|
| 1162 |
refresh_requests_btn.click(load_pending_requests, outputs=[requests_table])
|
| 1163 |
update_plan_btn.click(admin_update_plan, [admin_username, admin_new_plan], [admin_msg])
|
|
@@ -1166,6 +1242,13 @@ if __name__ == "__main__":
|
|
| 1166 |
|
| 1167 |
gr.Markdown("""
|
| 1168 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1169 |
### 📊 Plan Comparison
|
| 1170 |
|
| 1171 |
| Feature | FREE | PLUS ⭐ | PRO 💎 |
|
|
@@ -1178,7 +1261,7 @@ if __name__ == "__main__":
|
|
| 1178 |
| **Max Tokens** | 256 | 384 | 512 |
|
| 1179 |
|
| 1180 |
### 🆓 Sign up for FREE account - Nano & Mini unlimited!
|
| 1181 |
-
### 👨💼 Admins:
|
| 1182 |
""")
|
| 1183 |
|
| 1184 |
demo.launch(debug=True, share=False, server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 778 |
final_tokens_per_sec = tokens_generated / elapsed if elapsed > 0 else 0
|
| 779 |
yield "", False, final_tokens_per_sec, final_tokens_per_sec, False
|
| 780 |
|
| 781 |
+
# PART 3 - Continue from Part 2 - SESSION CODE VERSION
|
| 782 |
+
|
| 783 |
+
import secrets
|
| 784 |
+
|
| 785 |
+
# Global session codes storage
|
| 786 |
+
active_sessions = {} # {session_code: user_data}
|
| 787 |
+
session_lock = threading.Lock()
|
| 788 |
+
|
| 789 |
+
def generate_session_code():
|
| 790 |
+
"""Generate a unique 4-digit session code."""
|
| 791 |
+
with session_lock:
|
| 792 |
+
while True:
|
| 793 |
+
code = ''.join([str(secrets.randbelow(10)) for _ in range(4)])
|
| 794 |
+
if code not in active_sessions:
|
| 795 |
+
return code
|
| 796 |
+
|
| 797 |
+
def create_session(user_data):
|
| 798 |
+
"""Create a new session and return the code."""
|
| 799 |
+
code = generate_session_code()
|
| 800 |
+
with session_lock:
|
| 801 |
+
active_sessions[code] = user_data
|
| 802 |
+
return code
|
| 803 |
+
|
| 804 |
+
def validate_session(code):
|
| 805 |
+
"""Validate a session code and return user data."""
|
| 806 |
+
with session_lock:
|
| 807 |
+
return active_sessions.get(code, None)
|
| 808 |
+
|
| 809 |
+
def invalidate_session(code):
|
| 810 |
+
"""Remove a session code."""
|
| 811 |
+
with session_lock:
|
| 812 |
+
if code in active_sessions:
|
| 813 |
+
del active_sessions[code]
|
| 814 |
+
return True
|
| 815 |
+
return False
|
| 816 |
|
| 817 |
if __name__ == "__main__":
|
| 818 |
import gradio as gr
|
|
|
|
| 838 |
.send-btn { background: linear-gradient(135deg, #10a37f 0%, #0d8c6c 100%) !important; border: none !important; }
|
| 839 |
.stop-btn { background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%) !important; border: none !important; }
|
| 840 |
.announcement-banner { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px 28px; border-radius: 12px; margin-bottom: 20px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); text-align: center; font-size: 16px; font-weight: 500; line-height: 1.6; }
|
| 841 |
+
.session-code-box { background: linear-gradient(135deg, #10a37f 0%, #0d8c6c 100%); color: white; padding: 20px; border-radius: 12px; text-align: center; margin: 20px 0; box-shadow: 0 4px 12px rgba(0,0,0,0.2); }
|
| 842 |
+
.session-code-display { font-size: 32px; font-weight: 700; letter-spacing: 8px; margin: 10px 0; font-family: monospace; }
|
| 843 |
"""
|
| 844 |
|
| 845 |
def format_message_html(role, content, show_thinking=True):
|
|
|
|
| 881 |
html += format_message_html(msg["role"], msg["content"], show_thinking)
|
| 882 |
return html
|
| 883 |
|
| 884 |
+
def render_limits_panel(user_data):
|
| 885 |
+
if not user_data or 'user_id' not in user_data:
|
| 886 |
return ""
|
| 887 |
+
info = get_user_limits_info(user_data['user_id'])
|
| 888 |
if not info:
|
| 889 |
return ""
|
| 890 |
plan_badge_class = f"plan-{info['plan']}"
|
|
|
|
| 906 |
return html
|
| 907 |
|
| 908 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="slate")) as demo:
|
|
|
|
|
|
|
| 909 |
|
| 910 |
+
gr.HTML('<div class="announcement-banner">🔐 <strong>SAM-X-1 V3.0 - SESSION CODE ACCESS</strong> 🔐<br>✨ Sign in to get your 4-digit session code!<br>🆓 FREE: Nano & Mini unlimited, Fast 10/3h, Large 8/3h<br>⭐ PLUS: Nano/Mini/Fast unlimited, Large 20/3h<br>💎 PRO: Everything unlimited!</div>')
|
| 911 |
|
| 912 |
with gr.Tabs() as main_tabs:
|
| 913 |
with gr.Tab("🔐 Sign In"):
|
| 914 |
with gr.Column():
|
| 915 |
login_username = gr.Textbox(label="Username", placeholder="Enter username")
|
| 916 |
login_password = gr.Textbox(label="Password", type="password", placeholder="Enter password")
|
| 917 |
+
login_btn = gr.Button("Sign In", variant="primary", size="lg")
|
| 918 |
login_msg = gr.Markdown("")
|
| 919 |
+
session_code_display = gr.HTML("")
|
| 920 |
|
| 921 |
with gr.Tab("📝 Sign Up"):
|
| 922 |
with gr.Column():
|
| 923 |
signup_username = gr.Textbox(label="Username", placeholder="Choose a username")
|
| 924 |
signup_email = gr.Textbox(label="Email (optional)", placeholder="your@email.com")
|
| 925 |
signup_password = gr.Textbox(label="Password", type="password", placeholder="Choose a password")
|
| 926 |
+
signup_btn = gr.Button("Create Account", variant="primary", size="lg")
|
| 927 |
signup_msg = gr.Markdown("")
|
| 928 |
|
| 929 |
with gr.Tab("💬 Chat") as chat_tab:
|
| 930 |
+
with gr.Row():
|
| 931 |
+
chat_session_code = gr.Textbox(label="🔑 Enter Your 4-Digit Session Code", placeholder="0000", max_lines=1, scale=3)
|
| 932 |
+
verify_session_btn = gr.Button("✅ Verify", variant="primary", scale=1)
|
| 933 |
+
|
| 934 |
with gr.Row():
|
| 935 |
with gr.Column(scale=4):
|
| 936 |
+
user_info = gr.Markdown("❌ Not authenticated - Enter your session code above")
|
| 937 |
with gr.Column(scale=1):
|
| 938 |
logout_btn = gr.Button("🚪 Logout", size="sm")
|
| 939 |
|
|
|
|
| 951 |
chat_html = gr.HTML(value="", elem_classes=["chat-container"])
|
| 952 |
|
| 953 |
with gr.Row():
|
| 954 |
+
msg_input = gr.Textbox(placeholder="Enter session code first to chat...", show_label=False, scale=8, interactive=False)
|
| 955 |
with gr.Column(scale=1, min_width=120):
|
| 956 |
with gr.Row():
|
| 957 |
+
send_btn = gr.Button("▶", variant="primary", elem_classes=["circular-btn", "send-btn"], interactive=False)
|
| 958 |
stop_btn = gr.Button("⏹", variant="stop", elem_classes=["circular-btn", "stop-btn"], interactive=False)
|
| 959 |
|
| 960 |
with gr.Row():
|
|
|
|
| 985 |
request_msg = gr.Markdown("")
|
| 986 |
|
| 987 |
with gr.Accordion("⭐ Request Plan Upgrade", visible=False, open=False) as upgrade_panel:
|
| 988 |
+
upgrade_session_code = gr.Textbox(label="Your Session Code", placeholder="0000")
|
| 989 |
upgrade_plan_choice = gr.Radio(choices=["plus", "pro"], label="Select Plan", value="plus")
|
| 990 |
upgrade_reason = gr.Textbox(label="Reason for Upgrade", placeholder="Why do you need this upgrade?", lines=3)
|
| 991 |
submit_upgrade_btn = gr.Button("Submit Request", variant="primary")
|
| 992 |
upgrade_msg = gr.Markdown("")
|
| 993 |
+
|
| 994 |
+
with gr.Tab("👨💼 Admin Access") as admin_tab:
|
| 995 |
+
admin_session_code = gr.Textbox(label="🔑 Enter Your Admin Session Code", placeholder="0000", max_lines=1)
|
| 996 |
+
verify_admin_btn = gr.Button("✅ Verify Admin", variant="primary", size="lg")
|
| 997 |
+
admin_verify_msg = gr.Markdown("")
|
| 998 |
+
admin_logout_btn = gr.Button("🚪 Logout", size="sm")
|
| 999 |
|
| 1000 |
# Event handlers
|
| 1001 |
def handle_login(username, password):
|
| 1002 |
success, user_data = authenticate_user(username, password)
|
| 1003 |
if success:
|
| 1004 |
+
session_code = create_session(user_data)
|
| 1005 |
+
code_html = f'<div class="session-code-box"><div style="font-size: 18px; margin-bottom: 10px;">✅ Login Successful!</div><div style="font-size: 16px; margin-bottom: 5px;">Your Session Code:</div><div class="session-code-display">{session_code}</div><div style="font-size: 14px; margin-top: 10px;">💡 Use this code in the Chat or Admin tab</div><div style="font-size: 13px; margin-top: 5px; opacity: 0.9;">⚠️ Keep this code private!</div></div>'
|
| 1006 |
+
return f"✅ Welcome back, **{username}**! Use your session code above to access chat.", code_html
|
| 1007 |
+
return "❌ Invalid credentials!", ""
|
| 1008 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1009 |
def handle_signup(username, email, password):
|
| 1010 |
if len(username) < 3:
|
| 1011 |
return "❌ Username must be at least 3 characters!"
|
|
|
|
| 1013 |
return "❌ Password must be at least 6 characters!"
|
| 1014 |
success, message = create_user(username, password, email)
|
| 1015 |
if success:
|
| 1016 |
+
return f"✅ {message} Now sign in to get your session code!"
|
| 1017 |
return f"❌ {message}"
|
| 1018 |
|
| 1019 |
+
def verify_session_code(code):
|
| 1020 |
+
if not code or len(code) != 4 or not code.isdigit():
|
| 1021 |
+
return "❌ Invalid code format", "", gr.update(visible=False), gr.update(), gr.update(), gr.update(interactive=False, placeholder="Enter valid session code first..."), gr.update(interactive=False)
|
| 1022 |
|
| 1023 |
+
user_data = validate_session(code)
|
| 1024 |
+
if not user_data:
|
| 1025 |
+
return "❌ Invalid or expired session code", "", gr.update(visible=False), gr.update(), gr.update(), gr.update(interactive=False, placeholder="Enter valid session code first..."), gr.update(interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1026 |
|
| 1027 |
+
info = get_user_limits_info(user_data['user_id'])
|
| 1028 |
if not info:
|
| 1029 |
+
return "❌ Could not load user info", "", gr.update(visible=False), gr.update(), gr.update(), gr.update(interactive=False), gr.update(interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1030 |
|
| 1031 |
plan_badge = f'<span class="plan-badge plan-{info["plan"]}">{info["plan"].upper()}</span>'
|
| 1032 |
+
user_info_text = f"✅ **Authenticated as: {user_data['username']}** {plan_badge}"
|
| 1033 |
+
limits_html = render_limits_panel(user_data)
|
| 1034 |
|
| 1035 |
if info['can_choose_model']:
|
| 1036 |
available_model_names = list(available_models.keys())
|
|
|
|
| 1038 |
else:
|
| 1039 |
choices = ["🤖 Auto (Recommended)"]
|
| 1040 |
|
| 1041 |
+
is_admin = user_data.get('is_admin', False)
|
|
|
|
| 1042 |
|
| 1043 |
return (
|
| 1044 |
user_info_text,
|
| 1045 |
limits_html,
|
| 1046 |
gr.update(visible=is_admin),
|
| 1047 |
gr.update(choices=choices, value="🤖 Auto (Recommended)"),
|
| 1048 |
+
gr.update(maximum=info['max_tokens'], value=min(256, info['max_tokens'])),
|
| 1049 |
+
gr.update(interactive=True, placeholder="Ask me anything..."),
|
| 1050 |
+
gr.update(interactive=True)
|
| 1051 |
)
|
|
|
|
| 1052 |
|
| 1053 |
+
def send_message_handler(message, show_thinking, temperature, model_choice, max_tokens, session_code):
|
| 1054 |
global stop_generation
|
| 1055 |
stop_generation.clear()
|
| 1056 |
|
| 1057 |
+
if not session_code or len(session_code) != 4:
|
| 1058 |
+
return "", "", "❌ Invalid session code", gr.update(), gr.update()
|
| 1059 |
+
|
| 1060 |
+
user_data = validate_session(session_code)
|
| 1061 |
+
if not user_data:
|
| 1062 |
+
return "", "", "❌ Session expired - please re-enter your code", gr.update(), gr.update()
|
| 1063 |
|
| 1064 |
if not message.strip():
|
| 1065 |
return "", "", "⚡ Ready", gr.update(interactive=True), gr.update(interactive=False)
|
| 1066 |
|
| 1067 |
+
info = get_user_limits_info(user_data['user_id'])
|
| 1068 |
|
| 1069 |
# Auto or manual model selection
|
| 1070 |
if model_choice == "🤖 Auto (Recommended)" or not info['can_choose_model']:
|
| 1071 |
+
user_available = get_available_models_for_user(user_data['user_id'])
|
| 1072 |
if not user_available:
|
| 1073 |
return "", "", "❌ No models available (limits reached)", gr.update(interactive=True), gr.update(interactive=False)
|
| 1074 |
backend = select_model_auto(message, available_models, user_available)
|
|
|
|
| 1077 |
model_name = backend.get_name()
|
| 1078 |
else:
|
| 1079 |
model_name = model_choice
|
| 1080 |
+
can_use, msg = can_use_model(user_data['user_id'], model_name)
|
| 1081 |
if not can_use:
|
| 1082 |
return "", "", f"❌ {msg}", gr.update(interactive=True), gr.update(interactive=False)
|
| 1083 |
backend = available_models[model_name]
|
| 1084 |
|
| 1085 |
# Final check
|
| 1086 |
+
can_use, msg = can_use_model(user_data['user_id'], model_name)
|
| 1087 |
if not can_use:
|
| 1088 |
return "", "", f"❌ {msg}", gr.update(interactive=True), gr.update(interactive=False)
|
| 1089 |
|
| 1090 |
# Increment usage
|
| 1091 |
+
increment_model_usage(user_data['user_id'], model_name)
|
| 1092 |
|
| 1093 |
yield "", "", f"⚡ Using {model_name}...", gr.update(interactive=False), gr.update(interactive=True)
|
| 1094 |
|
|
|
|
| 1131 |
def show_upgrade_panel():
|
| 1132 |
return gr.update(visible=True, open=True)
|
| 1133 |
|
| 1134 |
+
def submit_upgrade_request(session_code, plan, reason):
|
| 1135 |
+
if not session_code or len(session_code) != 4:
|
| 1136 |
+
return "❌ Invalid session code"
|
| 1137 |
+
|
| 1138 |
+
user_data = validate_session(session_code)
|
| 1139 |
+
if not user_data:
|
| 1140 |
+
return "❌ Session expired"
|
| 1141 |
+
|
| 1142 |
if not reason.strip():
|
| 1143 |
return "❌ Please provide a reason"
|
| 1144 |
+
|
| 1145 |
+
success, msg = request_upgrade(user_data['user_id'], plan, reason)
|
| 1146 |
return f"{'✅' if success else '❌'} {msg}"
|
| 1147 |
|
| 1148 |
+
def handle_logout(session_code):
|
| 1149 |
+
if session_code and len(session_code) == 4:
|
| 1150 |
+
invalidate_session(session_code)
|
| 1151 |
+
return (
|
| 1152 |
+
"",
|
| 1153 |
+
"❌ Logged out - Session code invalidated",
|
| 1154 |
+
"",
|
| 1155 |
+
gr.update(visible=False),
|
| 1156 |
+
gr.update(choices=["🤖 Auto (Recommended)"], value="🤖 Auto (Recommended)"),
|
| 1157 |
+
gr.update(value=256),
|
| 1158 |
+
gr.update(interactive=False, placeholder="Enter session code first..."),
|
| 1159 |
+
gr.update(interactive=False)
|
| 1160 |
+
)
|
| 1161 |
+
|
| 1162 |
+
def verify_admin_session(code):
|
| 1163 |
+
if not code or len(code) != 4 or not code.isdigit():
|
| 1164 |
+
return "❌ Invalid code format", gr.update(visible=False)
|
| 1165 |
+
|
| 1166 |
+
user_data = validate_session(code)
|
| 1167 |
+
if not user_data:
|
| 1168 |
+
return "❌ Invalid or expired session code", gr.update(visible=False)
|
| 1169 |
+
|
| 1170 |
+
if not user_data.get('is_admin', False):
|
| 1171 |
+
return "❌ Access denied - Admin privileges required", gr.update(visible=False)
|
| 1172 |
+
|
| 1173 |
+
return f"✅ Admin access granted for **{user_data['username']}**", gr.update(visible=True, open=True)
|
| 1174 |
+
|
| 1175 |
+
def admin_logout_handler(code):
|
| 1176 |
+
if code and len(code) == 4:
|
| 1177 |
+
invalidate_session(code)
|
| 1178 |
+
return "", "❌ Logged out", gr.update(visible=False)
|
| 1179 |
+
|
| 1180 |
def load_all_users():
|
| 1181 |
users = get_all_users()
|
| 1182 |
formatted = []
|
|
|
|
| 1209 |
success, msg = deny_request(int(request_id))
|
| 1210 |
return f"{'✅' if success else '❌'} {msg}"
|
| 1211 |
|
|
|
|
|
|
|
|
|
|
| 1212 |
# Wire up events
|
| 1213 |
+
login_btn.click(handle_login, [login_username, login_password], [login_msg, session_code_display])
|
| 1214 |
signup_btn.click(handle_signup, [signup_username, signup_email, signup_password], [signup_msg])
|
| 1215 |
|
| 1216 |
+
# Session verification
|
| 1217 |
+
verify_outputs = [user_info, limits_display, admin_panel, model_selector, max_tokens_slider, msg_input, send_btn]
|
| 1218 |
+
verify_session_btn.click(verify_session_code, [chat_session_code], verify_outputs)
|
| 1219 |
|
| 1220 |
+
# Chat functionality
|
| 1221 |
send_outputs = [msg_input, chat_html, speed_display, send_btn, stop_btn]
|
| 1222 |
+
send_btn.click(send_message_handler, [msg_input, show_thinking_checkbox, temperature_slider, model_selector, max_tokens_slider, chat_session_code], send_outputs)
|
| 1223 |
+
msg_input.submit(send_message_handler, [msg_input, show_thinking_checkbox, temperature_slider, model_selector, max_tokens_slider, chat_session_code], send_outputs)
|
| 1224 |
|
| 1225 |
stop_btn.click(stop_generation_handler, outputs=[speed_display, send_btn, stop_btn])
|
| 1226 |
clear_btn.click(clear_chat, outputs=[chat_html, speed_display, send_btn, stop_btn])
|
| 1227 |
upgrade_btn.click(show_upgrade_panel, outputs=[upgrade_panel])
|
| 1228 |
+
submit_upgrade_btn.click(submit_upgrade_request, [upgrade_session_code, upgrade_plan_choice, upgrade_reason], [upgrade_msg])
|
| 1229 |
+
|
| 1230 |
+
logout_outputs = [chat_session_code, user_info, limits_display, admin_panel, model_selector, max_tokens_slider, msg_input, send_btn]
|
| 1231 |
+
logout_btn.click(handle_logout, [chat_session_code], logout_outputs)
|
| 1232 |
|
| 1233 |
# Admin panel events
|
| 1234 |
+
verify_admin_btn.click(verify_admin_session, [admin_session_code], [admin_verify_msg, admin_panel])
|
| 1235 |
+
admin_logout_btn.click(admin_logout_handler, [admin_session_code], [admin_session_code, admin_verify_msg, admin_panel])
|
| 1236 |
+
|
| 1237 |
refresh_users_btn.click(load_all_users, outputs=[users_table])
|
| 1238 |
refresh_requests_btn.click(load_pending_requests, outputs=[requests_table])
|
| 1239 |
update_plan_btn.click(admin_update_plan, [admin_username, admin_new_plan], [admin_msg])
|
|
|
|
| 1242 |
|
| 1243 |
gr.Markdown("""
|
| 1244 |
---
|
| 1245 |
+
### 🔑 How Session Codes Work
|
| 1246 |
+
|
| 1247 |
+
1. **Sign In** on the "Sign In" tab to get your unique 4-digit code
|
| 1248 |
+
2. **Copy** your session code (displayed after login)
|
| 1249 |
+
3. **Enter** the code in the Chat or Admin tab to access features
|
| 1250 |
+
4. **Logout** invalidates your code (you'll need to sign in again)
|
| 1251 |
+
|
| 1252 |
### 📊 Plan Comparison
|
| 1253 |
|
| 1254 |
| Feature | FREE | PLUS ⭐ | PRO 💎 |
|
|
|
|
| 1261 |
| **Max Tokens** | 256 | 384 | 512 |
|
| 1262 |
|
| 1263 |
### 🆓 Sign up for FREE account - Nano & Mini unlimited!
|
| 1264 |
+
### 👨💼 Admins: Use your session code in the Admin Access tab
|
| 1265 |
""")
|
| 1266 |
|
| 1267 |
demo.launch(debug=True, share=False, server_name="0.0.0.0", server_port=7860)
|