Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,16 @@ import time
|
|
| 4 |
import json
|
| 5 |
import sys
|
| 6 |
|
| 7 |
-
# =====
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
import torch
|
| 10 |
HAS_GPU = torch.cuda.is_available()
|
|
@@ -16,7 +25,7 @@ except Exception as e:
|
|
| 16 |
HAS_GPU = False
|
| 17 |
print(f"⚠️ torch ইম্পোর্টে সমস্যা: {e}")
|
| 18 |
|
| 19 |
-
#
|
| 20 |
try:
|
| 21 |
from llama_cpp import Llama
|
| 22 |
print("✅ llama-cpp-python ইম্পোর্ট সফল")
|
|
@@ -47,21 +56,18 @@ model_path = hf_hub_download(
|
|
| 47 |
)
|
| 48 |
print(f"✅ ডাউনলোড সম্পূর্ণ: {model_path}")
|
| 49 |
|
| 50 |
-
# --- ২. মডেল লোড
|
| 51 |
print("⏳ মডেল লোড হচ্ছে...")
|
| 52 |
cpu_count = os.cpu_count() or 4
|
| 53 |
|
| 54 |
def load_model():
|
| 55 |
-
"""
|
| 56 |
-
|
| 57 |
-
# ZeroGPU তে CPU ভার্সন ইনস্টল থাকলেও n_gpu_layers=-1 দিলে GPU চেষ্টা করবে
|
| 58 |
try:
|
| 59 |
-
# প্রথমে GPU চেষ্টা (ZeroGPU থাকলে কাজ করবে)
|
| 60 |
print("🚀 GPU মোডে লোড করার চেষ্টা করছি...")
|
| 61 |
llm = Llama(
|
| 62 |
model_path=model_path,
|
| 63 |
n_ctx=2048,
|
| 64 |
-
n_gpu_layers=-1,
|
| 65 |
n_threads=cpu_count,
|
| 66 |
n_threads_batch=cpu_count,
|
| 67 |
n_batch=512,
|
|
@@ -73,13 +79,12 @@ def load_model():
|
|
| 73 |
print(f"⚠️ GPU লোড করতে ব্যর্থ: {e}")
|
| 74 |
print("🔄 CPU-তে ফিরে যাচ্ছি...")
|
| 75 |
|
| 76 |
-
# CPU-তে লোড (ব্যাকআপ)
|
| 77 |
try:
|
| 78 |
print("🐢 CPU মোডে লোড হচ্ছে...")
|
| 79 |
llm = Llama(
|
| 80 |
model_path=model_path,
|
| 81 |
n_ctx=2048,
|
| 82 |
-
n_gpu_layers=0,
|
| 83 |
n_threads=cpu_count,
|
| 84 |
n_threads_batch=cpu_count,
|
| 85 |
n_batch=512,
|
|
@@ -94,7 +99,7 @@ def load_model():
|
|
| 94 |
llm, device_mode = load_model()
|
| 95 |
|
| 96 |
if llm is None:
|
| 97 |
-
print("❌ মডেল লোড করা সম্ভব হয়নি!
|
| 98 |
sys.exit(1)
|
| 99 |
|
| 100 |
# --- ৩. ওয়ার্মআপ ---
|
|
@@ -139,7 +144,6 @@ HTML_TEMPLATE = """
|
|
| 139 |
}
|
| 140 |
.header h2 { margin: 0; font-weight: 400; }
|
| 141 |
.header small { opacity: 0.8; font-size: 14px; }
|
| 142 |
-
|
| 143 |
.chat-box {
|
| 144 |
background: #f8f9fa;
|
| 145 |
border-radius: 12px;
|
|
@@ -297,11 +301,9 @@ HTML_TEMPLATE = """
|
|
| 297 |
<h2>🌳 Bonsai 8B (১-বিট) চ্যাট</h2>
|
| 298 |
<small>ZeroGPU • মডেল সাইজ: ~১.১৫ GB • <span id="deviceBadge">🔄 লোড হচ্ছে...</span></small>
|
| 299 |
</div>
|
| 300 |
-
|
| 301 |
<div class="chat-box" id="chatBox">
|
| 302 |
<div class="message bot-msg">👋 হ্যালো! আমি Bonsai 8B। আপনি কী জানতে চান?</div>
|
| 303 |
</div>
|
| 304 |
-
|
| 305 |
<div class="input-area">
|
| 306 |
<input type="text" id="userInput" placeholder="এখানে প্রশ্ন লিখুন..." />
|
| 307 |
<button id="sendBtn">পাঠান</button>
|
|
@@ -314,7 +316,6 @@ HTML_TEMPLATE = """
|
|
| 314 |
Powered by llama-cpp-python • Bonsai 8B (Q1_0, 1-bit)
|
| 315 |
</div>
|
| 316 |
</div>
|
| 317 |
-
|
| 318 |
<script>
|
| 319 |
const chatBox = document.getElementById('chatBox');
|
| 320 |
const input = document.getElementById('userInput');
|
|
@@ -466,7 +467,8 @@ HTML_TEMPLATE = """
|
|
| 466 |
</html>
|
| 467 |
"""
|
| 468 |
|
| 469 |
-
#
|
|
|
|
| 470 |
@app.route('/device')
|
| 471 |
def get_device():
|
| 472 |
try:
|
|
@@ -482,8 +484,21 @@ def get_device():
|
|
| 482 |
def index():
|
| 483 |
return render_template_string(HTML_TEMPLATE)
|
| 484 |
|
| 485 |
-
|
| 486 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
data = request.get_json()
|
| 488 |
user_prompt = data.get('prompt', '')
|
| 489 |
|
|
|
|
| 4 |
import json
|
| 5 |
import sys
|
| 6 |
|
| 7 |
+
# ===== ZeroGPU ডেকোরেটর =====
|
| 8 |
+
try:
|
| 9 |
+
from spaces import GPU
|
| 10 |
+
HAS_SPACES = True
|
| 11 |
+
print("✅ spaces মডিউল পাওয়া গেছে")
|
| 12 |
+
except ImportError:
|
| 13 |
+
HAS_SPACES = False
|
| 14 |
+
print("⚠️ spaces মডিউল পাওয়া যায়নি")
|
| 15 |
+
|
| 16 |
+
# ===== GPU ডিটেক্ট =====
|
| 17 |
try:
|
| 18 |
import torch
|
| 19 |
HAS_GPU = torch.cuda.is_available()
|
|
|
|
| 25 |
HAS_GPU = False
|
| 26 |
print(f"⚠️ torch ইম্পোর্টে সমস্যা: {e}")
|
| 27 |
|
| 28 |
+
# llama_cpp ইম্পোর্ট
|
| 29 |
try:
|
| 30 |
from llama_cpp import Llama
|
| 31 |
print("✅ llama-cpp-python ইম্পোর্ট সফল")
|
|
|
|
| 56 |
)
|
| 57 |
print(f"✅ ডাউনলোড সম্পূর্ণ: {model_path}")
|
| 58 |
|
| 59 |
+
# --- ২. মডেল লোড ---
|
| 60 |
print("⏳ মডেল লোড হচ্ছে...")
|
| 61 |
cpu_count = os.cpu_count() or 4
|
| 62 |
|
| 63 |
def load_model():
|
| 64 |
+
"""GPU/CPU অটো ডিটেক্ট"""
|
|
|
|
|
|
|
| 65 |
try:
|
|
|
|
| 66 |
print("🚀 GPU মোডে লোড করার চেষ্টা করছি...")
|
| 67 |
llm = Llama(
|
| 68 |
model_path=model_path,
|
| 69 |
n_ctx=2048,
|
| 70 |
+
n_gpu_layers=-1,
|
| 71 |
n_threads=cpu_count,
|
| 72 |
n_threads_batch=cpu_count,
|
| 73 |
n_batch=512,
|
|
|
|
| 79 |
print(f"⚠️ GPU লোড করতে ব্যর্থ: {e}")
|
| 80 |
print("🔄 CPU-তে ফিরে যাচ্ছি...")
|
| 81 |
|
|
|
|
| 82 |
try:
|
| 83 |
print("🐢 CPU মোডে লোড হচ্ছে...")
|
| 84 |
llm = Llama(
|
| 85 |
model_path=model_path,
|
| 86 |
n_ctx=2048,
|
| 87 |
+
n_gpu_layers=0,
|
| 88 |
n_threads=cpu_count,
|
| 89 |
n_threads_batch=cpu_count,
|
| 90 |
n_batch=512,
|
|
|
|
| 99 |
llm, device_mode = load_model()
|
| 100 |
|
| 101 |
if llm is None:
|
| 102 |
+
print("❌ মডেল লোড করা সম্ভব হয়নি!")
|
| 103 |
sys.exit(1)
|
| 104 |
|
| 105 |
# --- ৩. ওয়ার্মআপ ---
|
|
|
|
| 144 |
}
|
| 145 |
.header h2 { margin: 0; font-weight: 400; }
|
| 146 |
.header small { opacity: 0.8; font-size: 14px; }
|
|
|
|
| 147 |
.chat-box {
|
| 148 |
background: #f8f9fa;
|
| 149 |
border-radius: 12px;
|
|
|
|
| 301 |
<h2>🌳 Bonsai 8B (১-বিট) চ্যাট</h2>
|
| 302 |
<small>ZeroGPU • মডেল সাইজ: ~১.১৫ GB • <span id="deviceBadge">🔄 লোড হচ্ছে...</span></small>
|
| 303 |
</div>
|
|
|
|
| 304 |
<div class="chat-box" id="chatBox">
|
| 305 |
<div class="message bot-msg">👋 হ্যালো! আমি Bonsai 8B। আপনি কী জানতে চান?</div>
|
| 306 |
</div>
|
|
|
|
| 307 |
<div class="input-area">
|
| 308 |
<input type="text" id="userInput" placeholder="এখানে প্রশ্ন লিখুন..." />
|
| 309 |
<button id="sendBtn">পাঠান</button>
|
|
|
|
| 316 |
Powered by llama-cpp-python • Bonsai 8B (Q1_0, 1-bit)
|
| 317 |
</div>
|
| 318 |
</div>
|
|
|
|
| 319 |
<script>
|
| 320 |
const chatBox = document.getElementById('chatBox');
|
| 321 |
const input = document.getElementById('userInput');
|
|
|
|
| 467 |
</html>
|
| 468 |
"""
|
| 469 |
|
| 470 |
+
# ===== ৫. রাউট (ZeroGPU ডেকোরেটর সহ) =====
|
| 471 |
+
|
| 472 |
@app.route('/device')
|
| 473 |
def get_device():
|
| 474 |
try:
|
|
|
|
| 484 |
def index():
|
| 485 |
return render_template_string(HTML_TEMPLATE)
|
| 486 |
|
| 487 |
+
# ===== ZeroGPU ডেকোরেটর =====
|
| 488 |
+
# এটি সবচেয়ে গুরুত্বপূর্ণ! ZeroGPU চালানোর জন্য বাধ্যতামূলক
|
| 489 |
+
|
| 490 |
+
if HAS_SPACES:
|
| 491 |
+
@app.route('/chat', methods=['POST'])
|
| 492 |
+
@GPU(duration=120) # ২ মিনিট GPU সময়
|
| 493 |
+
def chat():
|
| 494 |
+
return chat_handler()
|
| 495 |
+
else:
|
| 496 |
+
@app.route('/chat', methods=['POST'])
|
| 497 |
+
def chat():
|
| 498 |
+
return chat_handler()
|
| 499 |
+
|
| 500 |
+
def chat_handler():
|
| 501 |
+
"""আসল চ্যাট হ্যান্ডলার"""
|
| 502 |
data = request.get_json()
|
| 503 |
user_prompt = data.get('prompt', '')
|
| 504 |
|