Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,87 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
return history + [[message, "📡 TeraBites™ Core: Received."]]
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
with gr.Row():
|
| 23 |
-
#
|
| 24 |
-
with gr.Column(scale=1,
|
| 25 |
-
gr.Markdown("###
|
| 26 |
-
gr.LoginButton()
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
profile_name = gr.Textbox(value="Authenticating...", interactive=False, show_label=False)
|
| 32 |
-
|
| 33 |
-
# RAW HTML DIVIDER (Indestructible)
|
| 34 |
-
gr.HTML("<hr style='border-top: 1px solid #333; margin: 10px 0;'>")
|
| 35 |
-
|
| 36 |
-
gr.Markdown("### 📡 FREQUENCIES")
|
| 37 |
-
group_nav = gr.Radio(["🌐 main", "📢 news"], value="🌐 main", label=None)
|
| 38 |
-
|
| 39 |
-
with gr.Accordion("👥 OTHER GROUPS", open=False):
|
| 40 |
-
gr.Radio(["🛠️ build-squad"], label=None)
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
with gr.Column(scale=
|
| 44 |
-
#
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
# THE PROMPT TYPING (Per PDF: Center Bottom)
|
| 48 |
with gr.Row():
|
| 49 |
-
|
| 50 |
-
placeholder="
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
container=False
|
| 54 |
)
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
# --- ACTION BINDING ---
|
| 58 |
-
def sync(profile: gr.OAuthProfile | None):
|
| 59 |
-
if profile: return profile.name
|
| 60 |
-
return "Guest Warrior"
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
demo.launch(
|
| 69 |
-
theme=gr.themes.Monochrome(),
|
| 70 |
-
css="""
|
| 71 |
-
.gradio-container { background-color: #050505 !important; }
|
| 72 |
-
footer { display: none !important; }
|
| 73 |
-
.message-row { justify-content: flex-start !important; }
|
| 74 |
-
"""
|
| 75 |
-
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
|
| 4 |
+
# GLOBAL STORAGE: Managed within the 7GB RAM Fortress
|
| 5 |
+
# Stores messages as: {"user": str, "avatar": str, "text": str}
|
| 6 |
+
team_messages = []
|
|
|
|
| 7 |
|
| 8 |
+
def get_user_data(request: gr.Request):
|
| 9 |
+
"""Retrieves OAuth data from Hugging Face."""
|
| 10 |
+
if request and request.user_info:
|
| 11 |
+
return request.user_info.preferred_username, request.user_info.picture
|
| 12 |
+
return None, None
|
| 13 |
|
| 14 |
+
def send_to_fleet(message, request: gr.Request):
|
| 15 |
+
"""Processes message and adds to the global stream."""
|
| 16 |
+
if not message:
|
| 17 |
+
return ""
|
| 18 |
+
|
| 19 |
+
username, avatar = get_user_data(request)
|
| 20 |
+
if not username:
|
| 21 |
+
return "⚠️ ACCESS DENIED: Please Login with HF first."
|
| 22 |
+
|
| 23 |
+
# Prevent Bot Overload: Keep last 50 messages in RAM
|
| 24 |
+
team_messages.append({"user": username, "avatar": avatar, "text": message})
|
| 25 |
+
if len(team_messages) > 50:
|
| 26 |
+
team_messages.pop(0)
|
| 27 |
+
return ""
|
| 28 |
|
| 29 |
+
def sync_labyrinth():
|
| 30 |
+
"""Updates the UI with the global message stream."""
|
| 31 |
+
formatted = []
|
| 32 |
+
for m in team_messages:
|
| 33 |
+
formatted.append({
|
| 34 |
+
"role": "user",
|
| 35 |
+
"content": m["text"],
|
| 36 |
+
"metadata": {"title": f" {m['user']}"}
|
| 37 |
+
})
|
| 38 |
+
return formatted
|
| 39 |
+
|
| 40 |
+
# --- UI ARCHITECTURE (Onyx Color Rules) ---
|
| 41 |
+
css = """
|
| 42 |
+
.gradio-container { background-color: #050505 !important; color: #00FF00 !important; }
|
| 43 |
+
.message-user { border-left: 3px solid #FFD700 !important; background: #0a0a0a !important; }
|
| 44 |
+
#status-panel { border: 1px solid #333; padding: 15px; border-radius: 8px; }
|
| 45 |
+
footer { visibility: hidden; }
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
with gr.Blocks(theme=gr.themes.Monochrome(), css=css) as demo:
|
| 49 |
+
gr.HTML("<h1 style='color: #00FF00; text-align: center;'>🟢 TERABITES AI TEAM</h1>")
|
| 50 |
+
|
| 51 |
with gr.Row():
|
| 52 |
+
# SECURITY PANEL
|
| 53 |
+
with gr.Column(scale=1, elem_id="status-panel"):
|
| 54 |
+
gr.Markdown("### 🛡️ SYSTEM STATUS")
|
| 55 |
+
gr.LoginButton(size="lg")
|
| 56 |
+
gr.Label(value="VM: ACTIVE", color="green")
|
| 57 |
+
gr.Label(value="RAM: 7GB STABLE", color="green")
|
| 58 |
+
gr.Markdown("---")
|
| 59 |
+
gr.Markdown("Welcome, Team. Use the **Gold-Bordered** stream for secure comms.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# CHAT INTERFACE
|
| 62 |
+
with gr.Column(scale=3):
|
| 63 |
+
# type="messages" is required for Gradio 5.x
|
| 64 |
+
chat_stream = gr.Chatbot(
|
| 65 |
+
label="TeraBites Global Feed",
|
| 66 |
+
height=550,
|
| 67 |
+
type="messages",
|
| 68 |
+
avatar_images=(None, "https://huggingface.co/front/assets/huggingface_logo.svg")
|
| 69 |
+
)
|
| 70 |
|
|
|
|
| 71 |
with gr.Row():
|
| 72 |
+
txt_input = gr.Textbox(
|
| 73 |
+
placeholder="Input command to the fleet...",
|
| 74 |
+
show_label=False,
|
| 75 |
+
scale=4
|
|
|
|
| 76 |
)
|
| 77 |
+
submit_btn = gr.Button("🚀 SEND", variant="primary", scale=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
# REFRESH LOGIC: Syncs every 2 seconds
|
| 80 |
+
demo.load(sync_labyrinth, None, chat_stream, every=2)
|
| 81 |
+
|
| 82 |
+
# INTERACTION LOGIC
|
| 83 |
+
txt_input.submit(send_to_fleet, [txt_input], [txt_input]).then(sync_labyrinth, None, chat_stream)
|
| 84 |
+
submit_btn.click(send_to_fleet, [txt_input], [txt_input]).then(sync_labyrinth, None, chat_stream)
|
| 85 |
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|