Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,20 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
from fastapi.responses import HTMLResponse, StreamingResponse
|
| 4 |
-
import json
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
from llama_cpp import Llama
|
| 7 |
-
import uvicorn
|
| 8 |
|
| 9 |
# --- ENGINE SETUP ---
|
|
|
|
| 10 |
repo_id = "unsloth/Llama-3.2-1B-Instruct-GGUF"
|
| 11 |
filename = "Llama-3.2-1B-Instruct-Q4_K_M.gguf"
|
| 12 |
|
| 13 |
-
print("🚀 Mahoba Logic:
|
| 14 |
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 15 |
llm = Llama(model_path=model_path, n_ctx=2048, n_threads=4, verbose=False)
|
|
|
|
| 16 |
|
| 17 |
# --- THE FRONTEND ---
|
| 18 |
HTML_CONTENT = """
|
|
@@ -27,18 +29,20 @@ HTML_CONTENT = """
|
|
| 27 |
<style>
|
| 28 |
:root { --bg-gradient: linear-gradient(135deg, #6366F1, #7C3AED); --chat-bg: #F9FAFB; --primary: #6D28D9; --secondary: #4F46E5; }
|
| 29 |
body { margin: 0; font-family: 'Inter', sans-serif; background: var(--bg-gradient); height: 100vh; width: 100vw; display: flex; justify-content: center; align-items: center; overflow: hidden; }
|
| 30 |
-
.welcome-screen { position: fixed; inset: 0; background:
|
| 31 |
.welcome-box { text-align: center; color: white; max-width: 420px; padding: 30px; }
|
| 32 |
-
.welcome-box
|
|
|
|
|
|
|
| 33 |
.chat-container { width: 90%; max-width: 900px; height: 85vh; background: white; border-radius: 24px; display: flex; flex-direction: column; box-shadow: 0 25px 60px rgba(0,0,0,0.3); overflow: hidden; position: relative; }
|
| 34 |
.header { padding: 20px; text-align: center; font-weight: 600; color: white; background: linear-gradient(135deg, #4F46E5, #7C3AED); }
|
| 35 |
#progress-container { padding: 12px 24px; background: #fdfdfd; border-bottom: 1px solid #f0f0f0; }
|
| 36 |
-
.chat { flex: 1; background: var(--chat-bg); overflow-y: auto; padding: 25px; display: flex; flex-direction: column; gap: 16px; }
|
| 37 |
-
.msg { max-width: 75%; padding: 14px 20px; border-radius: 18px; font-size: 15px; }
|
| 38 |
-
.ai { background: white; align-self: flex-start; border: 1px solid #eef0f2; }
|
| 39 |
.user { background: var(--primary); color: white; align-self: flex-end; }
|
| 40 |
.input-box { display: flex; padding: 20px; gap: 12px; background: white; border-top: 1px solid #f0f0f0; }
|
| 41 |
-
.input-box textarea { flex: 1; padding: 14px; border-radius: 12px; border: 1px solid #e5e7eb; outline: none; resize: none; }
|
| 42 |
</style>
|
| 43 |
</head>
|
| 44 |
<body>
|
|
@@ -69,7 +73,6 @@ HTML_CONTENT = """
|
|
| 69 |
let step = 0;
|
| 70 |
let userData = { name: "", interest: "", progress: 0 };
|
| 71 |
|
| 72 |
-
// FIX: Listen for 'Enter' key
|
| 73 |
input.addEventListener("keypress", (e) => {
|
| 74 |
if (e.key === "Enter" && !e.shiftKey) {
|
| 75 |
e.preventDefault();
|
|
@@ -83,6 +86,7 @@ HTML_CONTENT = """
|
|
| 83 |
div.innerHTML = type === "ai" ? marked.parse(text) : text;
|
| 84 |
chat.appendChild(div);
|
| 85 |
chat.scrollTop = chat.scrollHeight;
|
|
|
|
| 86 |
}
|
| 87 |
|
| 88 |
function startApp() {
|
|
@@ -110,10 +114,7 @@ HTML_CONTENT = """
|
|
| 110 |
return;
|
| 111 |
}
|
| 112 |
|
| 113 |
-
const aiMsgDiv =
|
| 114 |
-
aiMsgDiv.className = "msg ai";
|
| 115 |
-
aiMsgDiv.innerHTML = "<i>Analyzing...</i>";
|
| 116 |
-
chat.appendChild(aiMsgDiv);
|
| 117 |
|
| 118 |
try {
|
| 119 |
const response = await fetch("/guide", {
|
|
@@ -121,29 +122,38 @@ HTML_CONTENT = """
|
|
| 121 |
headers: {"Content-Type": "application/json"},
|
| 122 |
body: JSON.stringify({ name: userData.name, interest: userData.interest, followup: text })
|
| 123 |
});
|
|
|
|
| 124 |
const reader = response.body.getReader();
|
| 125 |
const decoder = new TextDecoder();
|
| 126 |
let fullText = "";
|
| 127 |
let buffer = "";
|
| 128 |
|
|
|
|
|
|
|
| 129 |
while (true) {
|
| 130 |
const { done, value } = await reader.read();
|
| 131 |
if (done) break;
|
|
|
|
| 132 |
buffer += decoder.decode(value, { stream: true });
|
| 133 |
-
const lines = buffer.split("\n");
|
| 134 |
buffer = lines.pop();
|
|
|
|
| 135 |
for (const line of lines) {
|
| 136 |
if (!line.trim()) continue;
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
| 141 |
}
|
| 142 |
}
|
| 143 |
-
userData.progress
|
| 144 |
document.getElementById("progress-bar").style.width = userData.progress + "%";
|
| 145 |
document.getElementById("progress-text").textContent = `Mission Progress: ${userData.progress}%`;
|
| 146 |
-
} catch (e) {
|
|
|
|
|
|
|
| 147 |
}
|
| 148 |
</script>
|
| 149 |
</body>
|
|
@@ -164,20 +174,17 @@ async def guide(request: Request):
|
|
| 164 |
interest = data.get("interest", "Career")
|
| 165 |
query = data.get("followup", "")
|
| 166 |
|
| 167 |
-
# Llama 3.2 1B
|
| 168 |
-
|
| 169 |
-
prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\nYou are the 1% Filter AI by Mayank. Help {user_name} with {interest}. Be short and brutal.<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n{query}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
|
| 170 |
|
| 171 |
def stream_generator():
|
| 172 |
-
# The 'stream=True' must be there
|
| 173 |
stream = llm(
|
| 174 |
prompt,
|
| 175 |
-
max_tokens=
|
| 176 |
stream=True,
|
| 177 |
stop=["<|eot_id|>", "User:"]
|
| 178 |
)
|
| 179 |
for output in stream:
|
| 180 |
-
# We look for the text inside the dictionary
|
| 181 |
if "choices" in output:
|
| 182 |
token = output["choices"][0].get("text", "")
|
| 183 |
yield json.dumps({"token": token}) + "\n"
|
|
@@ -185,10 +192,7 @@ async def guide(request: Request):
|
|
| 185 |
return StreamingResponse(
|
| 186 |
stream_generator(),
|
| 187 |
media_type="application/x-ndjson",
|
| 188 |
-
headers={
|
| 189 |
-
"Cache-Control": "no-cache",
|
| 190 |
-
"X-Content-Type-Options": "nosniff"
|
| 191 |
-
}
|
| 192 |
)
|
| 193 |
|
| 194 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
+
import uvicorn
|
| 4 |
from fastapi import FastAPI, Request
|
| 5 |
from fastapi.responses import HTMLResponse, StreamingResponse
|
|
|
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
from llama_cpp import Llama
|
|
|
|
| 8 |
|
| 9 |
# --- ENGINE SETUP ---
|
| 10 |
+
# Using the 1B model for maximum speed on Free Tier
|
| 11 |
repo_id = "unsloth/Llama-3.2-1B-Instruct-GGUF"
|
| 12 |
filename = "Llama-3.2-1B-Instruct-Q4_K_M.gguf"
|
| 13 |
|
| 14 |
+
print("🚀 Mahoba Logic: Downloading/Loading Model...")
|
| 15 |
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 16 |
llm = Llama(model_path=model_path, n_ctx=2048, n_threads=4, verbose=False)
|
| 17 |
+
print("✅ Engine Ready!")
|
| 18 |
|
| 19 |
# --- THE FRONTEND ---
|
| 20 |
HTML_CONTENT = """
|
|
|
|
| 29 |
<style>
|
| 30 |
:root { --bg-gradient: linear-gradient(135deg, #6366F1, #7C3AED); --chat-bg: #F9FAFB; --primary: #6D28D9; --secondary: #4F46E5; }
|
| 31 |
body { margin: 0; font-family: 'Inter', sans-serif; background: var(--bg-gradient); height: 100vh; width: 100vw; display: flex; justify-content: center; align-items: center; overflow: hidden; }
|
| 32 |
+
.welcome-screen { position: fixed; inset: 0; background: var(--bg-gradient); display: flex; justify-content: center; align-items: center; z-index: 9999; }
|
| 33 |
.welcome-box { text-align: center; color: white; max-width: 420px; padding: 30px; }
|
| 34 |
+
.welcome-box h1 { font-weight: 800; font-size: 2.5rem; margin-bottom: 10px; }
|
| 35 |
+
.welcome-box button, .input-box button { background: white; color: var(--primary); border: none; padding: 14px 28px; border-radius: 12px; font-weight: 600; cursor: pointer; transition: 0.3s; }
|
| 36 |
+
.welcome-box button:hover { transform: scale(1.05); box-shadow: 0 10px 20px rgba(0,0,0,0.2); }
|
| 37 |
.chat-container { width: 90%; max-width: 900px; height: 85vh; background: white; border-radius: 24px; display: flex; flex-direction: column; box-shadow: 0 25px 60px rgba(0,0,0,0.3); overflow: hidden; position: relative; }
|
| 38 |
.header { padding: 20px; text-align: center; font-weight: 600; color: white; background: linear-gradient(135deg, #4F46E5, #7C3AED); }
|
| 39 |
#progress-container { padding: 12px 24px; background: #fdfdfd; border-bottom: 1px solid #f0f0f0; }
|
| 40 |
+
.chat { flex: 1; background: var(--chat-bg); overflow-y: auto; padding: 25px; display: flex; flex-direction: column; gap: 16px; scroll-behavior: smooth; }
|
| 41 |
+
.msg { max-width: 75%; padding: 14px 20px; border-radius: 18px; font-size: 15px; line-height: 1.6; }
|
| 42 |
+
.ai { background: white; align-self: flex-start; border: 1px solid #eef0f2; box-shadow: 0 2px 5px rgba(0,0,0,0.05); }
|
| 43 |
.user { background: var(--primary); color: white; align-self: flex-end; }
|
| 44 |
.input-box { display: flex; padding: 20px; gap: 12px; background: white; border-top: 1px solid #f0f0f0; }
|
| 45 |
+
.input-box textarea { flex: 1; padding: 14px; border-radius: 12px; border: 1px solid #e5e7eb; outline: none; resize: none; font-family: inherit; }
|
| 46 |
</style>
|
| 47 |
</head>
|
| 48 |
<body>
|
|
|
|
| 73 |
let step = 0;
|
| 74 |
let userData = { name: "", interest: "", progress: 0 };
|
| 75 |
|
|
|
|
| 76 |
input.addEventListener("keypress", (e) => {
|
| 77 |
if (e.key === "Enter" && !e.shiftKey) {
|
| 78 |
e.preventDefault();
|
|
|
|
| 86 |
div.innerHTML = type === "ai" ? marked.parse(text) : text;
|
| 87 |
chat.appendChild(div);
|
| 88 |
chat.scrollTop = chat.scrollHeight;
|
| 89 |
+
return div;
|
| 90 |
}
|
| 91 |
|
| 92 |
function startApp() {
|
|
|
|
| 114 |
return;
|
| 115 |
}
|
| 116 |
|
| 117 |
+
const aiMsgDiv = addMessage("<i>Thinking...</i>", "ai");
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
try {
|
| 120 |
const response = await fetch("/guide", {
|
|
|
|
| 122 |
headers: {"Content-Type": "application/json"},
|
| 123 |
body: JSON.stringify({ name: userData.name, interest: userData.interest, followup: text })
|
| 124 |
});
|
| 125 |
+
|
| 126 |
const reader = response.body.getReader();
|
| 127 |
const decoder = new TextDecoder();
|
| 128 |
let fullText = "";
|
| 129 |
let buffer = "";
|
| 130 |
|
| 131 |
+
aiMsgDiv.innerHTML = ""; // Clear the "Thinking..."
|
| 132 |
+
|
| 133 |
while (true) {
|
| 134 |
const { done, value } = await reader.read();
|
| 135 |
if (done) break;
|
| 136 |
+
|
| 137 |
buffer += decoder.decode(value, { stream: true });
|
| 138 |
+
const lines = buffer.split("\\n");
|
| 139 |
buffer = lines.pop();
|
| 140 |
+
|
| 141 |
for (const line of lines) {
|
| 142 |
if (!line.trim()) continue;
|
| 143 |
+
try {
|
| 144 |
+
const data = JSON.parse(line);
|
| 145 |
+
fullText += data.token;
|
| 146 |
+
aiMsgDiv.innerHTML = marked.parse(fullText);
|
| 147 |
+
chat.scrollTop = chat.scrollHeight;
|
| 148 |
+
} catch(err) { console.error("JSON split error"); }
|
| 149 |
}
|
| 150 |
}
|
| 151 |
+
userData.progress = Math.min(userData.progress + 15, 100);
|
| 152 |
document.getElementById("progress-bar").style.width = userData.progress + "%";
|
| 153 |
document.getElementById("progress-text").textContent = `Mission Progress: ${userData.progress}%`;
|
| 154 |
+
} catch (e) {
|
| 155 |
+
aiMsgDiv.innerHTML = "Error connecting to engine. Check logs.";
|
| 156 |
+
}
|
| 157 |
}
|
| 158 |
</script>
|
| 159 |
</body>
|
|
|
|
| 174 |
interest = data.get("interest", "Career")
|
| 175 |
query = data.get("followup", "")
|
| 176 |
|
| 177 |
+
# Optimized prompt for Llama 3.2 1B
|
| 178 |
+
prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\nYou are the 1% Filter AI by Mayank. Helping {user_name} with {interest}. Be short, brutal, and strategic. Focus on real skills, not degrees.<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n{query}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
|
|
|
|
| 179 |
|
| 180 |
def stream_generator():
|
|
|
|
| 181 |
stream = llm(
|
| 182 |
prompt,
|
| 183 |
+
max_tokens=150,
|
| 184 |
stream=True,
|
| 185 |
stop=["<|eot_id|>", "User:"]
|
| 186 |
)
|
| 187 |
for output in stream:
|
|
|
|
| 188 |
if "choices" in output:
|
| 189 |
token = output["choices"][0].get("text", "")
|
| 190 |
yield json.dumps({"token": token}) + "\n"
|
|
|
|
| 192 |
return StreamingResponse(
|
| 193 |
stream_generator(),
|
| 194 |
media_type="application/x-ndjson",
|
| 195 |
+
headers={"Cache-Control": "no-cache", "X-Content-Type-Options": "nosniff"}
|
|
|
|
|
|
|
|
|
|
| 196 |
)
|
| 197 |
|
| 198 |
if __name__ == "__main__":
|