Raiff1982 commited on
Commit
ff09025
Β·
verified Β·
1 Parent(s): e801c72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +156 -7
app.py CHANGED
@@ -16,7 +16,8 @@ HF_TOKEN = os.environ.get("HF_TOKEN")
16
  if not HF_TOKEN:
17
  raise RuntimeError("HF_TOKEN missing. Set in HF Space secrets.")
18
 
19
- client = InferenceClient(token=HF_TOKEN)
 
20
 
21
  # ─────────────────────────────────────────────
22
  # FASTAPI INIT
@@ -32,13 +33,162 @@ app.add_middleware(
32
  )
33
 
34
  # ─────────────────────────────────────────────
35
- # ROOT (SERVES FRONTEND)
36
  # ─────────────────────────────────────────────
37
 
38
  @app.get("/", response_class=HTMLResponse)
39
  async def root():
40
- with open("index.html", encoding="utf-8") as f:
41
- return f.read()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  # ─────────────────────────────────────────────
44
  # DEBUG
@@ -57,7 +207,7 @@ async def test():
57
  return {"status": "error", "error": str(e)}
58
 
59
  # ─────────────────────────────────────────────
60
- # CHAT ENDPOINT
61
  # ─────────────────────────────────────────────
62
 
63
  @app.post("/api/chat")
@@ -110,11 +260,10 @@ async def chat(request: Request):
110
 
111
  return StreamingResponse(event_stream(), media_type="application/x-ndjson")
112
 
113
-
114
  # ─────────────────────────────────────────────
115
  # RUN
116
  # ─────────────────────────────────────────────
117
 
118
  if __name__ == "__main__":
119
  import uvicorn
120
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
16
  if not HF_TOKEN:
17
  raise RuntimeError("HF_TOKEN missing. Set in HF Space secrets.")
18
 
19
+ # βœ… FIX: bind model correctly (this was your silent bug)
20
+ client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
21
 
22
  # ─────────────────────────────────────────────
23
  # FASTAPI INIT
 
33
  )
34
 
35
  # ─────────────────────────────────────────────
36
+ # ROOT (CODETTE UI)
37
  # ─────────────────────────────────────────────
38
 
39
  @app.get("/", response_class=HTMLResponse)
40
  async def root():
41
+ return """
42
+ <!DOCTYPE html>
43
+ <html>
44
+ <head>
45
+ <title>Codette</title>
46
+ <style>
47
+ body {
48
+ margin:0;
49
+ font-family: Inter, sans-serif;
50
+ background: radial-gradient(circle at top, #14142b, #0b0b17);
51
+ color:#e5e7eb;
52
+ }
53
+
54
+ .container {
55
+ display:flex;
56
+ height:100vh;
57
+ }
58
+
59
+ .left {
60
+ flex:3;
61
+ display:flex;
62
+ flex-direction:column;
63
+ padding:15px;
64
+ }
65
+
66
+ .right {
67
+ flex:1;
68
+ padding:15px;
69
+ }
70
+
71
+ .chat {
72
+ flex:1;
73
+ overflow:auto;
74
+ background: rgba(20,20,40,0.7);
75
+ border-radius:10px;
76
+ padding:10px;
77
+ }
78
+
79
+ .input {
80
+ display:flex;
81
+ margin-top:10px;
82
+ }
83
+
84
+ input {
85
+ flex:1;
86
+ padding:10px;
87
+ background:#0f0f1e;
88
+ border:1px solid #06b6d4;
89
+ color:white;
90
+ }
91
+
92
+ button {
93
+ padding:10px;
94
+ background: linear-gradient(135deg,#a855f7,#06b6d4);
95
+ border:none;
96
+ color:white;
97
+ cursor:pointer;
98
+ }
99
+
100
+ .metric {
101
+ background: rgba(20,20,40,0.7);
102
+ padding:10px;
103
+ margin-bottom:10px;
104
+ border-radius:8px;
105
+ font-family: monospace;
106
+ }
107
+ </style>
108
+ </head>
109
+
110
+ <body>
111
+ <div class="container">
112
+
113
+ <div class="left">
114
+ <h2>CODETTE</h2>
115
+
116
+ <div id="chat" class="chat"></div>
117
+
118
+ <div class="input">
119
+ <input id="msg" placeholder="Ask Codette..." />
120
+ <button onclick="send()">β–Ά</button>
121
+ </div>
122
+ </div>
123
+
124
+ <div class="right">
125
+ <div class="metric" id="metrics">
126
+ Ξ“: 0.0000<br>
127
+ Ξ·: 0.0000<br>
128
+ Risk: LOW
129
+ </div>
130
+ </div>
131
+
132
+ </div>
133
+
134
+ <script>
135
+ async function send() {
136
+ const input = document.getElementById("msg");
137
+ const chat = document.getElementById("chat");
138
+
139
+ const userText = input.value;
140
+ if (!userText) return;
141
+
142
+ chat.innerHTML += "<div><b>You:</b> " + userText + "</div>";
143
+
144
+ input.value = "";
145
+
146
+ const res = await fetch("/api/chat", {
147
+ method:"POST",
148
+ headers:{"Content-Type":"application/json"},
149
+ body: JSON.stringify({
150
+ messages:[{role:"user", content:userText}]
151
+ })
152
+ });
153
+
154
+ const reader = res.body.getReader();
155
+ const decoder = new TextDecoder();
156
+
157
+ let botDiv = document.createElement("div");
158
+ botDiv.innerHTML = "<b>Codette:</b> ";
159
+ chat.appendChild(botDiv);
160
+
161
+ while (true) {
162
+ const {done, value} = await reader.read();
163
+ if (done) break;
164
+
165
+ const chunk = decoder.decode(value);
166
+ const lines = chunk.split("\\n");
167
+
168
+ for (let line of lines) {
169
+ if (!line.trim()) continue;
170
+ try {
171
+ const data = JSON.parse(line);
172
+ if (data.message?.content) {
173
+ botDiv.innerHTML += data.message.content;
174
+ }
175
+ } catch {}
176
+ }
177
+ }
178
+
179
+ chat.scrollTop = chat.scrollHeight;
180
+
181
+ // fake metrics update (you can wire real later)
182
+ document.getElementById("metrics").innerHTML =
183
+ "Ξ“: " + (0.8 + Math.random()*0.2).toFixed(4) + "<br>" +
184
+ "Ξ·: " + (0.6 + Math.random()*0.3).toFixed(4) + "<br>" +
185
+ "Risk: LOW";
186
+ }
187
+ </script>
188
+
189
+ </body>
190
+ </html>
191
+ """
192
 
193
  # ─────────────────────────────────────────────
194
  # DEBUG
 
207
  return {"status": "error", "error": str(e)}
208
 
209
  # ─────────────────────────────────────────────
210
+ # CHAT ENDPOINT (UNCHANGED CORE)
211
  # ─────────────────────────────────────────────
212
 
213
  @app.post("/api/chat")
 
260
 
261
  return StreamingResponse(event_stream(), media_type="application/x-ndjson")
262
 
 
263
  # ─────────────────────────────────────────────
264
  # RUN
265
  # ─────────────────────────────────────────────
266
 
267
  if __name__ == "__main__":
268
  import uvicorn
269
+ uvicorn.run(app, host="0.0.0.0", port=7860)