Spaces:
Sleeping
Sleeping
Update brain.py
Browse files
brain.py
CHANGED
|
@@ -1,34 +1,37 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
from flask_cors import CORS
|
| 4 |
-
from
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
CORS(app)
|
| 8 |
|
| 9 |
-
print("π‘οΈ RiShre
|
| 10 |
|
| 11 |
-
# Model
|
|
|
|
|
|
|
| 12 |
model = None
|
| 13 |
|
| 14 |
try:
|
| 15 |
-
print("π₯
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
)
|
| 24 |
-
|
| 25 |
-
print("π RiShre AI: Core Online & Ready.")
|
| 26 |
|
| 27 |
except Exception as e:
|
| 28 |
print(f"β Ignition Failed: {e}")
|
| 29 |
model = None
|
| 30 |
|
| 31 |
-
|
| 32 |
@app.route("/api/chat", methods=["POST"])
|
| 33 |
def chat():
|
| 34 |
if model is None:
|
|
@@ -37,53 +40,50 @@ def chat():
|
|
| 37 |
try:
|
| 38 |
data = request.get_json()
|
| 39 |
user_msg = data.get("message", "")
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
- Never guess unknown facts.
|
| 52 |
-
- If you don't know something, say:
|
| 53 |
-
"I don't have that information yet."
|
| 54 |
-
|
| 55 |
-
ABOUT RiShre:
|
| 56 |
-
- RiShre is a social media and AI platform.
|
| 57 |
-
- Built by Badge94.
|
| 58 |
-
- Focus: AI, community, innovation, privacy.
|
| 59 |
-
|
| 60 |
-
STYLE:
|
| 61 |
-
- Confident
|
| 62 |
-
- Smart
|
| 63 |
-
- Slightly futuristic
|
| 64 |
-
"""
|
| 65 |
)
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
response = model(
|
| 70 |
prompt,
|
| 71 |
-
|
| 72 |
-
temperature=0.
|
| 73 |
-
|
| 74 |
)
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
except Exception as e:
|
| 80 |
return jsonify({"error": str(e)}), 500
|
| 81 |
|
| 82 |
-
|
| 83 |
@app.route("/")
|
| 84 |
def health():
|
| 85 |
-
return "RiShre
|
| 86 |
-
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
from flask import Flask, request, jsonify
|
| 4 |
from flask_cors import CORS
|
| 5 |
+
from llama_cpp import Llama
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
CORS(app)
|
| 10 |
|
| 11 |
+
print("π‘οΈ RiShre Studio: Initializing Stable Qwen Core...")
|
| 12 |
|
| 13 |
+
# Model Config: Qwen-2.5-Coder-7B-Instruct
|
| 14 |
+
model_repo = "Qwen/Qwen2.5-Coder-7B-Instruct-GGUF"
|
| 15 |
+
model_file = "qwen2.5-coder-7b-instruct-q4_k_m.gguf"
|
| 16 |
model = None
|
| 17 |
|
| 18 |
try:
|
| 19 |
+
print("π₯ Downloading Qwen Engine (Bypassing wheel build)...")
|
| 20 |
+
model_path = hf_hub_download(repo_id=model_repo, filename=model_file)
|
| 21 |
+
|
| 22 |
+
print("π§ Loading 16k Context Memory for 10-File Handling...")
|
| 23 |
+
model = Llama(
|
| 24 |
+
model_path=model_path,
|
| 25 |
+
n_ctx=16384, # High memory for complex project files
|
| 26 |
+
n_threads=4, # Safe for HF Free Tier
|
| 27 |
+
n_batch=512
|
| 28 |
)
|
| 29 |
+
print("π RiShre Studio: Engine Online & 100% Protected.")
|
|
|
|
| 30 |
|
| 31 |
except Exception as e:
|
| 32 |
print(f"β Ignition Failed: {e}")
|
| 33 |
model = None
|
| 34 |
|
|
|
|
| 35 |
@app.route("/api/chat", methods=["POST"])
|
| 36 |
def chat():
|
| 37 |
if model is None:
|
|
|
|
| 40 |
try:
|
| 41 |
data = request.get_json()
|
| 42 |
user_msg = data.get("message", "")
|
| 43 |
+
# Frontend will send the code files here
|
| 44 |
+
context_files = data.get("files", "No files uploaded yet.")
|
| 45 |
+
|
| 46 |
+
# π‘οΈ THE MASTER IDENTITY & JSON PROMPT
|
| 47 |
+
system_prompt = (
|
| 48 |
+
"You are 'RiShre Studio Engine', an Elite Full-Stack Developer created by Rishav (Badge94) on 17 March 2026. "
|
| 49 |
+
"Your identity is RiShre AI. You focus on 100% Security. "
|
| 50 |
+
"STRICT OUTPUT RULE: Always respond in valid JSON format with exactly two keys: "
|
| 51 |
+
"1. 'logic': A detailed explanation of what to change and why (Dependencies). "
|
| 52 |
+
"2. 'files': An array of objects, each containing 'filename', 'extension' (e.g. .tsx, .py, .html), and 'code'. "
|
| 53 |
+
"Do not output any markdown formatting or extra text outside the JSON."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# Qwen-2.5 ChatML Format
|
| 57 |
+
prompt = f"<|im_start|>system\n{system_prompt}\n<|im_end|>\n"
|
| 58 |
+
prompt += f"<|im_start|>user\nHere are the current project files:\n{context_files}\n\nTask: {user_msg}\n<|im_end|>\n"
|
| 59 |
+
prompt += f"<|im_start|>assistant\n"
|
| 60 |
|
| 61 |
response = model(
|
| 62 |
prompt,
|
| 63 |
+
max_tokens=2048,
|
| 64 |
+
temperature=0.2, # Extremely low for precise coding
|
| 65 |
+
stop=["<|im_end|>"]
|
| 66 |
)
|
| 67 |
+
|
| 68 |
+
raw_text = response["choices"][0]["text"].strip()
|
| 69 |
+
|
| 70 |
+
# Parse JSON before sending to React frontend
|
| 71 |
+
try:
|
| 72 |
+
parsed_json = json.loads(raw_text)
|
| 73 |
+
return jsonify(parsed_json)
|
| 74 |
+
except:
|
| 75 |
+
# Fallback format if AI makes a tiny mistake in JSON
|
| 76 |
+
return jsonify({
|
| 77 |
+
"logic": raw_text,
|
| 78 |
+
"files": []
|
| 79 |
+
})
|
| 80 |
|
| 81 |
except Exception as e:
|
| 82 |
return jsonify({"error": str(e)}), 500
|
| 83 |
|
|
|
|
| 84 |
@app.route("/")
|
| 85 |
def health():
|
| 86 |
+
return "RiShre Engine: Status 100% Protected"
|
|
|
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
app.run(host="0.0.0.0", port=7860)
|