hudaakram commited on
Commit
5da9044
·
verified ·
1 Parent(s): 2d60758

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify, make_response
2
+ import os, time, html, requests, json
3
+ from itsdangerous import BadSignature, URLSafeSerializer
4
+
5
+ SECRET_KEY = os.getenv("SECRET_KEY", "change-me") # must match Gate
6
+ TOKEN_TTL = int(os.getenv("TOKEN_TTL", "3600"))
7
+ CHAT_MODEL_URL = os.getenv("CHAT_MODEL_URL", "") # e.g. https://<your-model>/chat
8
+
9
+ app = Flask(__name__)
10
+ signer = URLSafeSerializer(SECRET_KEY, salt="jarvis-facegate")
11
+
12
+ def verify_token(token: str):
13
+ try:
14
+ data = signer.loads(token)
15
+ if time.time() - float(data.get("ts", 0)) > TOKEN_TTL:
16
+ return None
17
+ return data
18
+ except BadSignature:
19
+ return None
20
+
21
+ def shell(name=""):
22
+ safe = html.escape(name or "Agent")
23
+ return f"""<!doctype html><html><head><meta charset=utf-8>
24
+ <meta name=viewport content="width=device-width,initial-scale=1"/>
25
+ <title>Jarvis Chat</title>
26
+ <style>
27
+ body{{margin:0;background:#0a0f18;color:#e8f3ff;font-family:ui-sans-serif,system-ui}}
28
+ .wrap{{max-width:900px;margin:5vh auto;padding:24px}}
29
+ .hud{{border:1px solid #3ee7ff4f;border-radius:16px;padding:16px;background:
30
+ linear-gradient(180deg,rgba(10,16,24,.85),rgba(10,16,24,.65));box-shadow:0 20px 60px rgba(0,0,0,.5)}}
31
+ h1{{margin:0 0 8px}} .muted{{color:#a9c2d0}}
32
+ .row{{display:flex;gap:8px;margin-top:12px}}
33
+ input,button{{padding:10px;border-radius:10px;border:1px solid #3ee7ff30;background:#0f1724;color:#e8f3ff}}
34
+ button{{background:linear-gradient(90deg,#3ee7ff,#7bf5c8);color:#0a0f18;border:none;font-weight:800}}
35
+ #log{{white-space:pre-wrap;min-height:180px;margin-top:12px;background:#0f1724;border:1px solid #3ee7ff21;border-radius:12px;padding:12px}}
36
+ </style></head>
37
+ <body><div class=wrap>
38
+ <div class=hud>
39
+ <h1>Jarvis Chat</h1>
40
+ <div class=muted>Welcome, {safe}. You are now connected.</div>
41
+ <div id=log></div>
42
+ <div class=row>
43
+ <input id=msg placeholder="Type your message..." style="flex:1"/>
44
+ <button onclick="send()">Send</button>
45
+ </div>
46
+ </div>
47
+ </div>
48
+ <script>
49
+ async function send(){{
50
+ const el = document.getElementById('msg');
51
+ const txt = el.value.trim();
52
+ if(!txt) return;
53
+ el.value='';
54
+ const res = await fetch('/api/chat', {{method:'POST', headers:{{'Content-Type':'application/json'}}, body: JSON.stringify({{q:txt}})}});
55
+ const data = await res.json();
56
+ const log = document.getElementById('log');
57
+ log.textContent += "\\nYou: " + txt + "\\nJarvis: " + (data.a || data.error || '...') + "\\n";
58
+ }}
59
+ </script>
60
+ </body></html>"""
61
+
62
+ @app.get("/")
63
+ def home():
64
+ token = request.args.get("token","")
65
+ info = verify_token(token) if token else None
66
+ if not info:
67
+ return make_response("<h3>Access denied. Missing/invalid token.</h3>", 401)
68
+ name = info.get("name","User")
69
+ return make_response(shell(name), 200, {"Content-Type":"text/html; charset=utf-8"})
70
+
71
+ @app.post("/api/chat")
72
+ def api_chat():
73
+ # This is a simple proxy to your chatbot model, or a stub if CHAT_MODEL_URL is unset.
74
+ q = (request.json or {}).get("q","").strip()
75
+ if not q:
76
+ return jsonify({"error":"empty message"}), 400
77
+
78
+ if CHAT_MODEL_URL:
79
+ try:
80
+ r = requests.post(CHAT_MODEL_URL, json={"inputs": q}, timeout=60)
81
+ if r.ok:
82
+ # Tailor this to your model's response schema
83
+ data = r.json()
84
+ a = data.get("generated_text") or data.get("answer") or str(data)
85
+ return jsonify({"a": a})
86
+ return jsonify({"error": f"model status {r.status_code}"}), 502
87
+ except Exception as e:
88
+ return jsonify({"error": f"upstream error: {e}"}), 502
89
+ else:
90
+ # Stub: echo
91
+ return jsonify({"a": f"(stub) You said: {q}"}), 200
92
+
93
+ if __name__ == "__main__":
94
+ app.run(host="0.0.0.0", port=7860)