Spaces:
Runtime error
Runtime error
File size: 13,368 Bytes
d8732c6 dcb9934 0d34d33 dcb9934 d8732c6 dcb9934 62922dd dcb9934 0d34d33 dcb9934 9253124 dcb9934 04217b8 dcb9934 f4d109c dcb9934 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | import os
import sys
# Fix encoding issues before anything else
os.environ["PYTHONIOENCODING"] = "utf-8"
if sys.stdout.encoding != "utf-8":
sys.stdout.reconfigure(encoding="utf-8")
import gradio as gr
from huggingface_hub import InferenceClient, HfApi
# βββββββββββββββββββββββββββββββββββββββββββββ
# CONSTANTS
# βββββββββββββββββββββββββββββββββββββββββββββ
APP_TITLE = "SendoX - Sync"
DEFAULT_SYSTEM_PROMPT = (
"You are SendoX, an advanced AI coding assistant. "
"You help developers write, debug, and understand code. "
"Be concise, precise, and professional."
)
DEFAULT_MODEL = "mistralai/Mistral-7B-Instruct-v0.3"
FALLBACK_MODELS = [
"mistralai/Mistral-7B-Instruct-v0.3",
"HuggingFaceH4/zephyr-7b-beta",
"meta-llama/Llama-3.2-3B-Instruct",
"Qwen/Qwen2.5-7B-Instruct",
"google/gemma-2-2b-it",
]
# βββββββββββββββββββββββββββββββββββββββββββββ
# HELPERS
# βββββββββββββββββββββββββββββββββββββββββββββ
def get_user_models(profile: gr.OAuthProfile | None) -> list[str]:
"""Fetch models authored by the logged-in user. Falls back to curated list."""
models = list(FALLBACK_MODELS)
if profile is None:
return models
try:
api = HfApi()
# Use filter="author:username" β avoids deprecated liked_by argument
user_models = api.list_models(filter=f"author:{profile.username}", limit=30)
user_model_ids = [m.modelId for m in user_models if m.modelId]
if user_model_ids:
# Prepend user models, deduplicate
combined = user_model_ids + [m for m in models if m not in user_model_ids]
return combined
except Exception as e:
print(f"[SendoX] Could not fetch user models: {e}", flush=True)
return models
def chat_with_model(
message: str,
history: list,
system_prompt: str,
model_id: str,
temperature: float,
profile: gr.OAuthProfile | None,
):
"""Stream a response from the selected HF Inference model."""
if profile is None:
yield "β οΈ You must be logged in to use SendoX - Sync."
return
if not message.strip():
yield ""
return
# Build message list
messages = [{"role": "system", "content": system_prompt or DEFAULT_SYSTEM_PROMPT}]
for user_msg, assistant_msg in history:
if user_msg:
messages.append({"role": "user", "content": user_msg})
if assistant_msg:
messages.append({"role": "assistant", "content": assistant_msg})
messages.append({"role": "user", "content": message})
client = InferenceClient(model_id)
response = ""
try:
for chunk in client.chat_completion(
messages=messages,
temperature=max(0.01, float(temperature)),
max_tokens=2048,
stream=True,
):
delta = chunk.choices[0].delta.content or ""
response += delta
yield response
except Exception as e:
yield f"β Error contacting model `{model_id}`:\n\n```\n{e}\n```\n\nTry selecting a different model from the sidebar."
# βββββββββββββββββββββββββββββββββββββββββββββ
# CSS β Dark IDE Theme
# βββββββββββββββββββββββββββββββββββββββββββββ
CUSTOM_CSS = """
/* ββ Base ββ */
:root {
--bg-deep: #0d0f14;
--bg-panel: #141720;
--bg-sidebar: #10131a;
--border: #2a2f3d;
--accent: #00e5ff;
--accent2: #7c5cfc;
--text-pri: #e8eaf0;
--text-sec: #7a8099;
--danger: #ff4d6d;
--success: #00e676;
--radius: 10px;
--font-mono: 'JetBrains Mono', 'Fira Code', monospace;
--font-ui: 'Syne', 'Segoe UI', sans-serif;
}
body, .gradio-container {
background: var(--bg-deep) !important;
color: var(--text-pri) !important;
font-family: var(--font-ui) !important;
}
/* ββ Login Gate ββ */
#login-gate {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
background: radial-gradient(ellipse at 60% 40%, #1a1040 0%, var(--bg-deep) 70%);
}
#login-gate .logo-title {
font-size: 3rem;
font-weight: 800;
letter-spacing: -1px;
background: linear-gradient(135deg, var(--accent), var(--accent2));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin-bottom: 0.5rem;
}
#login-gate .logo-sub {
color: var(--text-sec);
font-size: 1.05rem;
margin-bottom: 2.5rem;
letter-spacing: 2px;
text-transform: uppercase;
}
/* ββ IDE Workspace ββ */
#workspace {
display: flex;
height: 100vh;
overflow: hidden;
}
#sidebar {
width: 280px;
min-width: 240px;
background: var(--bg-sidebar);
border-right: 1px solid var(--border);
padding: 1.5rem 1rem;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 1.25rem;
}
#sidebar .sidebar-header {
font-size: 1.3rem;
font-weight: 700;
letter-spacing: -0.5px;
background: linear-gradient(90deg, var(--accent), var(--accent2));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
padding-bottom: 0.75rem;
border-bottom: 1px solid var(--border);
}
#main-panel {
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
}
/* ββ Gradio overrides ββ */
.gr-button-primary, button.primary {
background: linear-gradient(135deg, var(--accent2), var(--accent)) !important;
border: none !important;
color: #fff !important;
font-weight: 600 !important;
border-radius: var(--radius) !important;
}
.gr-textbox textarea, .gr-textbox input {
background: var(--bg-panel) !important;
border: 1px solid var(--border) !important;
color: var(--text-pri) !important;
font-family: var(--font-mono) !important;
border-radius: var(--radius) !important;
}
.gr-dropdown select, .gr-dropdown input {
background: var(--bg-panel) !important;
border: 1px solid var(--border) !important;
color: var(--text-pri) !important;
}
.gr-slider input[type=range] {
accent-color: var(--accent2);
}
.gr-chatbot {
background: var(--bg-panel) !important;
border: 1px solid var(--border) !important;
border-radius: var(--radius) !important;
font-family: var(--font-mono) !important;
}
.message.user { background: #1e2230 !important; }
.message.bot { background: #161b27 !important; }
label { color: var(--text-sec) !important; font-size: 0.82rem !important; text-transform: uppercase; letter-spacing: 1px; }
/* ββ User badge ββ */
#user-badge {
font-size: 0.8rem;
color: var(--success);
padding: 0.3rem 0.6rem;
border: 1px solid var(--success);
border-radius: 20px;
display: inline-block;
}
"""
# βββββββββββββββββββββββββββββββββββββββββββββ
# BUILD UI
# βββββββββββββββββββββββββββββββββββββββββββββ
with gr.Blocks(
title=APP_TITLE,
css=CUSTOM_CSS,
theme=gr.themes.Base(
primary_hue="cyan",
secondary_hue="violet",
neutral_hue="slate",
font=[gr.themes.GoogleFont("Syne"), "sans-serif"],
font_mono=[gr.themes.GoogleFont("JetBrains Mono"), "monospace"],
),
) as demo:
# ββ Hidden state ββ
oauth_profile_state = gr.State(None)
# ββββββββββββββββββββββββββββββββββββββββββ
# LOGIN GATE β shown until authenticated
# ββββββββββββββββββββββββββββββββββββββββββ
with gr.Column(visible=True, elem_id="login-gate") as login_gate:
gr.HTML("""
<div class="logo-title">SendoX β Sync</div>
<div class="logo-sub">AI Developer Workspace</div>
<p style="color:#7a8099;max-width:400px;text-align:center;margin-bottom:2rem;">
A professional IDE-style AI coding assistant powered by Hugging Face Inference.
Sign in with your HF account to continue.
</p>
""")
login_btn = gr.LoginButton(size="lg")
# ββββββββββββββββββββββββββββββββββββββββββ
# IDE WORKSPACE β shown after auth
# ββββββββββββββββββββββββββββββββββββββββββ
with gr.Row(visible=False, elem_id="workspace") as workspace:
# ββ Sidebar ββ
with gr.Column(scale=0, min_width=260, elem_id="sidebar"):
gr.HTML('<div class="sidebar-header">β‘ SendoX</div>')
user_badge = gr.HTML('<span id="user-badge">Not logged in</span>')
model_selector = gr.Dropdown(
label="Model",
choices=FALLBACK_MODELS,
value=DEFAULT_MODEL,
interactive=True,
)
system_prompt_box = gr.Textbox(
label="System Prompt",
value=DEFAULT_SYSTEM_PROMPT,
lines=5,
placeholder="Define the AI's behaviour...",
)
temperature_slider = gr.Slider(
label="Temperature",
minimum=0.01,
maximum=1.5,
value=0.7,
step=0.05,
)
gr.HTML("<hr style='border-color:#2a2f3d;margin:0.5rem 0;'>")
logout_btn = gr.LogoutButton(size="sm")
# ββ Main Chat Panel ββ
with gr.Column(scale=1, elem_id="main-panel"):
chat_interface = gr.ChatInterface(
fn=chat_with_model,
additional_inputs=[
system_prompt_box,
model_selector,
temperature_slider,
oauth_profile_state,
],
chatbot=gr.Chatbot(
label="SendoX Chat",
height=600,
show_copy_button=True,
placeholder="<div style='text-align:center;color:#7a8099;padding:3rem;'>Start a conversation with SendoX...</div>",
),
textbox=gr.Textbox(
placeholder="Ask anything β code, debug, explain...",
container=False,
),
submit_btn="Send β",
retry_btn="βΊ Retry",
undo_btn="β© Undo",
clear_btn="β Clear",
)
# ββββββββββββββββββββββββββββββββββββββββββ
# AUTH LOGIC β the robust lock-fix
# ββββββββββββββββββββββββββββββββββββββββββ
def handle_auth_on_load(request: gr.Request, profile: gr.OAuthProfile | None):
"""
Called on every page load (including after OAuth redirect).
If profile is not None β user is authenticated β show workspace.
"""
if profile is not None:
username = profile.username or "User"
# Fetch user models for the dropdown
user_models = get_user_models(profile)
badge_html = f'<span id="user-badge">β {username}</span>'
return (
gr.update(visible=False), # hide login_gate
gr.update(visible=True), # show workspace
gr.update(choices=user_models, value=user_models[0]), # model_selector
badge_html, # user_badge
profile, # oauth_profile_state
)
else:
return (
gr.update(visible=True), # show login_gate
gr.update(visible=False), # hide workspace
gr.update(choices=FALLBACK_MODELS, value=DEFAULT_MODEL),
'<span id="user-badge">Not logged in</span>',
None,
)
# demo.load fires on every page load β this is the key fix
demo.load(
fn=handle_auth_on_load,
inputs=None,
outputs=[login_gate, workspace, model_selector, user_badge, oauth_profile_state],
)
# βββββββββββββββββββββββββββββββββββββββββββββ
# LAUNCH
# βββββββββββββββββββββββββββββββββββββββββββββ
if __name__ == "__main__":
demo.launch(show_error=True) |