rexprimematrix commited on
Commit
c785a2f
Β·
verified Β·
1 Parent(s): cb0f567

Update brain.py

Browse files
Files changed (1) hide show
  1. brain.py +49 -49
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 ctransformers import AutoModelForCausalLM
 
5
 
6
  app = Flask(__name__)
7
  CORS(app)
8
 
9
- print("πŸ›‘οΈ RiShre Security: Initializing Stable Core...")
10
 
11
- # Model config
 
 
12
  model = None
13
 
14
  try:
15
- print("πŸ“₯ Loading Mistral GGUF (No Build Mode)...")
16
-
17
- model = AutoModelForCausalLM.from_pretrained(
18
- "bartowski/Mistral-7B-Instruct-v0.3-GGUF",
19
- model_file="Mistral-7B-Instruct-v0.3-Q4_K_M.gguf",
20
- model_type="mistral",
21
- gpu_layers=0, # CPU only (free tier safe)
22
- threads=2 # low CPU usage
 
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
- system_prompt = ("""
42
- You are RiShre AI, created by Badge94.
43
-
44
- IDENTITY RULES:
45
- - You are NOT Mistral, NOT Meta, NOT OpenAI.
46
- - You are ONLY RiShre AI.
47
- - You were created on 17 March 2026.
48
-
49
- BEHAVIOR RULES:
50
- - Always give clear, direct answers.
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
- prompt = f"<s>[INST] {system_prompt}\n\n{user_msg} [/INST]"
 
 
 
68
 
69
  response = model(
70
  prompt,
71
- max_new_tokens=150,
72
- temperature=0.7,
73
- top_p=0.9
74
  )
75
- cleaned = response.replace(prompt, "").strip()
76
-
77
- return jsonify({"text": cleaned})
 
 
 
 
 
 
 
 
 
 
78
 
79
  except Exception as e:
80
  return jsonify({"error": str(e)}), 500
81
 
82
-
83
  @app.route("/")
84
  def health():
85
- return "RiShre AI: Status 100% Protected"
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)