Spaces:
Sleeping
Sleeping
Update brain.py
Browse files
brain.py
CHANGED
|
@@ -1,32 +1,29 @@
|
|
| 1 |
import os
|
| 2 |
-
import json
|
| 3 |
from flask import Flask, request, jsonify
|
| 4 |
from flask_cors import CORS
|
| 5 |
-
from
|
| 6 |
-
from huggingface_hub import hf_hub_download
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
CORS(app)
|
| 10 |
|
| 11 |
-
print("π‘οΈ RiShre
|
| 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("π₯
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
)
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
except Exception as e:
|
| 32 |
print(f"β Ignition Failed: {e}")
|
|
@@ -40,50 +37,33 @@ def chat():
|
|
| 40 |
try:
|
| 41 |
data = request.get_json()
|
| 42 |
user_msg = data.get("message", "")
|
| 43 |
-
# Frontend
|
| 44 |
-
context_files = data.get("files", "
|
| 45 |
|
| 46 |
-
# π‘οΈ THE MASTER IDENTITY & JSON PROMPT
|
| 47 |
system_prompt = (
|
| 48 |
-
"You are
|
| 49 |
-
"
|
| 50 |
-
"
|
| 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 |
-
#
|
| 57 |
-
prompt = f"
|
| 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 |
-
|
| 64 |
-
temperature=0.
|
| 65 |
-
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 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
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
from flask_cors import CORS
|
| 4 |
+
from ctransformers import AutoModelForCausalLM
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
CORS(app)
|
| 8 |
|
| 9 |
+
print("π‘οΈ RiShre Security: Initializing Fast Studio Core...")
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
model = None
|
| 12 |
|
| 13 |
try:
|
| 14 |
+
print("π₯ Loading Qwen-Coder via CTransformers (No-Build Mode)...")
|
| 15 |
+
|
| 16 |
+
# Hum Mistral ki jagah Qwen-2.5-Coder use karenge kyunki wo 10 files ke liye best hai [cite: 2026-03-01]
|
| 17 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
+
"Qwen/Qwen2.5-Coder-7B-Instruct-GGUF",
|
| 19 |
+
model_file="qwen2.5-coder-7b-instruct-q4_k_m.gguf",
|
| 20 |
+
model_type="gpt2", # Qwen ke liye gpt2 best chalta hai [cite: 2026-02-19]
|
| 21 |
+
gpu_layers=0, # CPU only
|
| 22 |
+
context_length=8192, # 10 files handle karne ke liye [cite: 2026-02-19]
|
| 23 |
+
threads=4
|
| 24 |
)
|
| 25 |
+
|
| 26 |
+
print("π RiShre AI: Core Online & Ready.")
|
| 27 |
|
| 28 |
except Exception as e:
|
| 29 |
print(f"β Ignition Failed: {e}")
|
|
|
|
| 37 |
try:
|
| 38 |
data = request.get_json()
|
| 39 |
user_msg = data.get("message", "")
|
| 40 |
+
# Frontend se files ka context bhi aayega yahan [cite: 2026-03-01]
|
| 41 |
+
context_files = data.get("files", "")
|
| 42 |
|
|
|
|
| 43 |
system_prompt = (
|
| 44 |
+
"You are RiShre AI, an elite coder created by Badge94 on 17 March 2026. "
|
| 45 |
+
"Identify the file extension like .tsx, .html, .py for every code block. "
|
| 46 |
+
"Think about cross-file dependencies."
|
|
|
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# Chat format for Qwen
|
| 50 |
+
prompt = f"System: {system_prompt}\nContext: {context_files}\nUser: {user_msg}\nAssistant:"
|
|
|
|
|
|
|
| 51 |
|
| 52 |
response = model(
|
| 53 |
prompt,
|
| 54 |
+
max_new_tokens=1024,
|
| 55 |
+
temperature=0.3,
|
| 56 |
+
top_p=0.9
|
| 57 |
)
|
| 58 |
|
| 59 |
+
return jsonify({"text": response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
except Exception as e:
|
| 62 |
return jsonify({"error": str(e)}), 500
|
| 63 |
|
| 64 |
@app.route("/")
|
| 65 |
def health():
|
| 66 |
+
return "RiShre AI: Status 100% Protected"
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
app.run(host="0.0.0.0", port=7860)
|