Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, jsonify, request
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
from llama_cpp import Llama
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
CORS(app)
|
| 9 |
+
|
| 10 |
+
# --- 🧠 MODEL SETUP (DeepSeek Coder) ---
|
| 11 |
+
# Hum "TheBloke" ka GGUF version use kar rahe hain jo CPU par chalta hai
|
| 12 |
+
REPO_ID = "TheBloke/DeepSeek-Coder-6.7B-Instruct-GGUF"
|
| 13 |
+
FILENAME = "deepseek-coder-6.7b-instruct.Q4_K_M.gguf"
|
| 14 |
+
|
| 15 |
+
print("📥 Downloading AI Model... (Pehli baar 5 min lagega)")
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
# Model ko Hugging Face se download karke cache mein rakho
|
| 19 |
+
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 20 |
+
|
| 21 |
+
# Model Load karo (Context Window 2048 rakha hai coding ke liye)
|
| 22 |
+
# n_threads=2 isliye kyunki Free tier mein 2 CPU milte hain
|
| 23 |
+
llm = Llama(model_path=model_path, n_ctx=2048, n_threads=2)
|
| 24 |
+
|
| 25 |
+
print("✅ DeepSeek Coder Loaded Successfully!")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"❌ Error Loading Model: {e}")
|
| 28 |
+
llm = None
|
| 29 |
+
|
| 30 |
+
# --- CHAT LOGIC ---
|
| 31 |
+
@app.route('/')
|
| 32 |
+
def home():
|
| 33 |
+
return "🤖 DEEPSEEK CODER (16GB RAM) IS LIVE ON HUGGING FACE!"
|
| 34 |
+
|
| 35 |
+
@app.route('/chat', methods=['POST'])
|
| 36 |
+
def chat():
|
| 37 |
+
if not llm:
|
| 38 |
+
return jsonify({"error": "Model abhi load ho raha hai, 1 min ruk kar try karo."})
|
| 39 |
+
|
| 40 |
+
data = request.json
|
| 41 |
+
user_msg = data.get('message', '')
|
| 42 |
+
|
| 43 |
+
# Prompt format for DeepSeek Instruct
|
| 44 |
+
# Is format se AI ko pata chalta hai ki ye Instruction hai
|
| 45 |
+
prompt = f"""### Instruction:
|
| 46 |
+
You are an expert programmer. Write code or answer the following question:
|
| 47 |
+
{user_msg}
|
| 48 |
+
|
| 49 |
+
### Response:"""
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
# AI Soch raha hai...
|
| 53 |
+
output = llm(
|
| 54 |
+
prompt,
|
| 55 |
+
max_tokens=512, # Kitna lamba code likhe
|
| 56 |
+
temperature=0.2, # 0.2 matlab accurate code (Creative nahi)
|
| 57 |
+
stop=["### Instruction:"] # Yahan ruk jana
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
reply = output['choices'][0]['text']
|
| 61 |
+
|
| 62 |
+
return jsonify({"reply": reply})
|
| 63 |
+
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return jsonify({"error": str(e)})
|
| 66 |
+
|
| 67 |
+
if __name__ == '__main__':
|
| 68 |
+
app.run(host='0.0.0.0', port=7860)
|
| 69 |
+
|