Spaces:
Running
Running
File size: 11,386 Bytes
1b65cc5 522d1b9 1b65cc5 522d1b9 2f85c93 d648fe6 1b65cc5 522d1b9 1b65cc5 522d1b9 1b65cc5 522d1b9 1b65cc5 522d1b9 1b65cc5 522d1b9 1b65cc5 522d1b9 1b65cc5 434b328 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 7c06e97 d648fe6 1b65cc5 d648fe6 1b65cc5 d648fe6 1b65cc5 b27e104 1b65cc5 0e58feb 522d1b9 67a949c 522d1b9 1b65cc5 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 67a949c 522d1b9 1b65cc5 d648fe6 1b65cc5 67a949c |
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 |
import os, base64
from dotenv import load_dotenv
from fastapi import FastAPI, Request
from fastapi.responses import RedirectResponse, JSONResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from starlette.middleware.sessions import SessionMiddleware
from authlib.integrations.starlette_client import OAuth
import gradio as gr
import requests
from src.manager.manager import GeminiManager
# 1. Load environment --------------------------------------------------
load_dotenv()
AUTH0_DOMAIN = os.getenv("AUTH0_DOMAIN")
AUTH0_CLIENT_ID = os.getenv("AUTH0_CLIENT_ID")
AUTH0_CLIENT_SECRET = os.getenv("AUTH0_CLIENT_SECRET")
AUTH0_AUDIENCE = os.getenv("AUTH0_AUDIENCE")
SESSION_SECRET_KEY = os.getenv("SESSION_SECRET_KEY", "replace‑me")
# 2. Auth0 client ------------------------------------------------------
oauth = OAuth()
oauth.register(
"auth0",
client_id=AUTH0_CLIENT_ID,
client_secret=AUTH0_CLIENT_SECRET,
client_kwargs={"scope": "openid profile email"},
server_metadata_url=f"https://{AUTH0_DOMAIN}/.well-known/openid-configuration",
)
# 3. FastAPI app -------------------------------------------------------
app = FastAPI()
# Create static directory if it doesn't exist
os.makedirs("static/fonts/ui-sans-serif", exist_ok=True)
os.makedirs("static/fonts/system-ui", exist_ok=True)
# Mount static files directory
app.mount("/static", StaticFiles(directory="static"), name="static")
# Add session middleware (no auth requirement)
app.add_middleware(
SessionMiddleware,
secret_key=SESSION_SECRET_KEY,
session_cookie="session",
max_age=86400,
same_site="lax",
https_only=False
)
# 4. Auth routes -------------------------------------------------------
@app.get("/login")
async def login(request: Request):
print("Session cookie:", request.cookies.get("session"))
print("Session data:", dict(request.session))
return await oauth.auth0.authorize_redirect(request, request.url_for("auth"), audience=AUTH0_AUDIENCE, prompt="login")
@app.get("/auth")
async def auth(request: Request):
try:
token = await oauth.auth0.authorize_access_token(request)
request.session["user"] = token["userinfo"]
return RedirectResponse("/")
except Exception as e:
return JSONResponse({"error": str(e)}, status_code=500)
@app.get("/logout")
async def logout(request: Request):
auth0_logout_url = (
f"https://{AUTH0_DOMAIN}/v2/logout"
f"?client_id={AUTH0_CLIENT_ID}"
f"&returnTo=http://localhost:7860/post-logout"
)
return RedirectResponse(auth0_logout_url)
@app.get("/post-logout")
async def post_logout(request: Request):
request.session.clear()
return RedirectResponse("/")
@app.get("/manifest.json")
async def manifest():
return JSONResponse({
"name": "HASHIRU AI",
"short_name": "HASHIRU",
"icons": [],
"start_url": "/",
"display": "standalone"
})
@app.get("/api/login-status")
async def api_login_status(request: Request):
if "user" in request.session:
user_info = request.session["user"]
user_name = user_info.get("name", user_info.get("email", "User"))
return {"status": f"Logged in: {user_name}"}
else:
return {"status": "Logged out"}
# 5. Gradio UI ---------------------------------------------------------
_logo_b64 = base64.b64encode(open("HASHIRU_LOGO.png", "rb").read()).decode()
HEADER_HTML = f"""
<div style='display:flex;align-items:center;width:30%;'>
<img src='data:image/png;base64,{_logo_b64}' width='40' class='logo'/>
<h1>HASHIRU AI</h1>
</div>"""
CSS = """
.logo {
margin-right: 20px;
}
.login-status {
font-weight: bold;
margin-right: 20px;
padding: 8px;
border-radius: 4px;
background-color: #f0f0f0;
}
/* Profile style improvements */
.profile-container {
position: relative;
display: inline-block;
float: right;
margin-right: 20px;
z-index: 9999; /* Ensure this is higher than any other elements */
}
#profile-name {
background-color: transparent; /* Transparent background */
color: #f97316; /* Orange text */
font-weight: bold;
padding: 10px 14px;
border-radius: 6px;
cursor: pointer;
user-select: none;
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 40px;
min-height: 40px;
border: 2px solid #f97316; /* Add border */
}
#profile-menu {
position: fixed; /* Changed from absolute to fixed for better overlay */
right: auto; /* Let JS position it precisely */
top: auto; /* Let JS position it precisely */
background-color: transparent;
border: 1px solid transparent;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
z-index: 10000; /* Very high z-index to ensure it's on top */
overflow: visible;
width: 160px;
}
#profile-menu.hidden {
display: none;
}
#profile-menu button {
background-color: #f97316; /* Orange background */
border: none;
color: white; /* White text */
font-size: 16px;
border-radius: 8px;
text-align: left;
width: 100%;
padding: 12px 16px;
cursor: pointer;
transition: background-color 0.2s ease;
display: block;
}
#profile-menu button:hover {
background-color: #ea580c; /* Darker orange on hover */
}
#profile-menu button .icon {
margin-right: 8px;
color: white; /* White icon color */
}
/* Fix dropdown issues */
input[type="text"], select {
color: black !important;
}
/* Optional: limit dropdown scroll if options are long */
.gr-dropdown .gr-dropdown-options {
max-height: 200px;
overflow-y: auto;
}
/* User avatar styles */
.user-avatar {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
text-transform: uppercase;
font-size: 20px; /* Larger font size */
color: #f97316; /* Orange color */
}
/* Fix for gradio interface */
.gradio-container {
overflow: visible !important;
}
/* Fix other container issues that might cause scrolling */
body, html {
overflow-x: hidden; /* Prevent horizontal scrolling */
}
#gradio-app, .gradio-container .overflow-hidden {
overflow: visible !important; /* Override any overflow hidden that might interfere */
}
/* Ensure dropdown appears above everything */
.profile-container * {
z-index: 9999;
}
"""
def run_model(message, history):
history.append({"role": "user", "content": message})
yield "", history
for messages in model_manager.run(history):
for m in messages:
if m.get("role") == "summary":
print("Summary:", m["content"])
yield "", messages
def update_model(name):
print("Model changed to:", name)
with gr.Blocks(css=CSS, fill_width=True, fill_height=True) as demo:
model_manager = GeminiManager(gemini_model="gemini-2.0-flash")
with gr.Row():
gr.Markdown(HEADER_HTML)
with gr.Column(scale=1, min_width=250):
profile_html = gr.HTML(value="""
<div class="profile-container">
<div id="profile-name" class="user-avatar">G</div>
<div id="profile-menu" class="hidden">
<button id="login-btn" onclick="window.location.href='/login'"><span class="icon">🔐</span> Login</button>
<button id="logout-btn" onclick="window.location.href='/logout'"><span class="icon">🚪</span> Logout</button>
</div>
</div>
""")
with gr.Column():
model_dropdown = gr.Dropdown(
[
"HASHIRU",
"Static-HASHIRU",
"Cloud-Only HASHIRU",
"Local-Only HASHIRU",
"No-Economy HASHIRU",
],
value="HASHIRU",
interactive=True,
)
model_dropdown.change(update_model, model_dropdown)
chatbot = gr.Chatbot(
avatar_images=("HASHIRU_2.png", "HASHIRU.png"),
type="messages", show_copy_button=True, editable="user",
placeholder="Type your message here…",
)
gr.ChatInterface(run_model, type="messages", chatbot=chatbot, additional_outputs=[chatbot], save_history=True)
demo.load(None, None, None, js="""
async () => {
const profileBtn = document.getElementById("profile-name");
const profileMenu = document.getElementById("profile-menu");
const loginBtn = document.getElementById("login-btn");
const logoutBtn = document.getElementById("logout-btn");
// Position menu and handle positioning
function positionMenu() {
const btnRect = profileBtn.getBoundingClientRect();
profileMenu.style.position = "fixed";
profileMenu.style.top = (btnRect.bottom + 5) + "px";
profileMenu.style.left = (btnRect.right - profileMenu.offsetWidth) + "px"; // Align with right edge
}
// Close menu when clicking outside
document.addEventListener('click', (event) => {
if (!profileBtn.contains(event.target) && !profileMenu.contains(event.target)) {
profileMenu.classList.add("hidden");
}
});
// Toggle menu
profileBtn.onclick = (e) => {
e.stopPropagation();
positionMenu(); // Position before showing
profileMenu.classList.toggle("hidden");
// If showing menu, make sure it's positioned correctly
if (!profileMenu.classList.contains("hidden")) {
setTimeout(positionMenu, 0); // Reposition after render
}
}
// Handle window resize
window.addEventListener('resize', () => {
if (!profileMenu.classList.contains("hidden")) {
positionMenu();
}
});
// Get initial letter for avatar
function getInitial(name) {
if (name && name.length > 0) {
return name.charAt(0);
}
return "?";
}
try {
const res = await fetch('/api/login-status', { credentials: 'include' });
const data = await res.json();
if (!data.status.includes("Logged out")) {
const name = data.status.replace("Logged in: ", "");
profileBtn.innerHTML = `<div class="user-avatar">${getInitial(name)}</div>`;
profileBtn.title = name;
loginBtn.style.display = "none";
logoutBtn.style.display = "block";
} else {
profileBtn.innerHTML = `<div class="user-avatar">G</div>`;
profileBtn.title = "Guest";
loginBtn.style.display = "block";
logoutBtn.style.display = "none";
}
} catch (error) {
console.error("Error fetching login status:", error);
profileBtn.innerHTML = `<div class="user-avatar">?</div>`;
profileBtn.title = "Login status unknown";
}
}
""")
gr.mount_gradio_app(app, demo, path="/")
# 6. Entrypoint --------------------------------------------------------
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860) |