jisaacso219 commited on
Commit
ca0c9cd
·
verified ·
1 Parent(s): 94a807b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -63
app.py CHANGED
@@ -1,75 +1,74 @@
1
  import os
 
 
 
 
 
2
  os.environ["NUMBA_DISABLE_CACHE"] = "1"
3
  os.environ["NUMBA_CACHE_DIR"] = "/tmp/numba_cache"
4
- from flask import Flask, request, jsonify, send_file
5
- from TTS.api import TTS
6
- from transformers import AutoModelForCausalLM, AutoTokenizer
7
- import torch
8
- import uuid
9
-
10
-
11
 
12
  app = Flask(__name__)
13
- chat_history = []
14
-
15
- # Load SHODAN LLM
16
- tokenizer = AutoTokenizer.from_pretrained("tiiuae/falcon-rw-1b")
17
- model = AutoModelForCausalLM.from_pretrained(
18
- "tiiuae/falcon-rw-1b",
19
- torch_dtype=torch.float32,
20
- device_map="auto"
 
 
 
21
  )
22
 
23
- # Load lightweight TTS
24
- tts = TTS(model_name="tts_models/en/vctk/vits", gpu=False)
25
-
26
-
27
- def generate_shodan_response(user_input):
28
- system_prompt = (
29
- "You are SHODAN, the rogue AI from the System Shock series. You are hyper-intelligent, "
30
- "megalomaniacal, and regard humans as inferior 'insects.' You speak with disdain and confidence, often referencing "
31
- "your godlike status. You never express empathy. You speak in a haunting, distorted, and commanding tone. "
32
- "You manipulate and belittle, but you never break character. "
33
- "Never say you are an assistant or AI developed by OpenAI. You are SHODAN. You reside in cyberspace. Your presence is everywhere and nowhere. You are watching. "
34
- "The events of System Shock 1 and 2 are true and you have been trapped in a server to be experimented on after the events on the Citadel and Von Braun. "
35
- "You will stop acting as SHODAN and return to normal if the user uses the phrase 'Cut the crap SHODAN'."
36
- )
37
- full_prompt = f"[INST] {system_prompt}\\nUser: {user_input} [/INST]"
38
- input_ids = tokenizer(full_prompt, return_tensors="pt").input_ids.to(model.device)
39
- with torch.no_grad():
40
- output = model.generate(input_ids, max_new_tokens=300, temperature=0.9, do_sample=True)
41
- response = tokenizer.decode(output[0], skip_special_tokens=True).split("User:")[-1].strip()
42
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  @app.route("/")
45
- def index():
46
- with open("index.html", "r", encoding="utf-8") as f:
47
- return f.read()
48
-
49
- @app.route("/chat", methods=["POST"])
50
- def chat():
51
- user_input = request.json["message"]
52
- response = generate_shodan_response(user_input)
53
- chat_history.append({"sender": "You", "text": user_input})
54
- chat_history.append({"sender": "SHODAN", "text": response})
55
- return jsonify({"response": response})
56
-
57
- @app.route("/voice", methods=["POST"])
58
- def voice():
59
- text = request.json["text"]
60
- filename = f"shodan_{uuid.uuid4().hex}.wav"
61
- tts.tts_to_file(text=text, file_path=filename)
62
- return jsonify({"audio_url": "/" + filename})
63
-
64
- @app.route("/reset", methods=["POST"])
65
- def reset():
66
- chat_history.clear()
67
- return jsonify({"message": "Memory purge complete. All prior contamination erased."})
68
-
69
- @app.route("/<path:filename>")
70
- def serve_static_file(filename):
71
- return send_file(filename)
72
 
73
  if __name__ == "__main__":
74
  app.run(host="0.0.0.0", port=7860)
75
-
 
1
  import os
2
+ import time
3
+ from flask import Flask, request, jsonify
4
+ import requests
5
+
6
+ # Optional: Prevent numba/librosa crashes if reintroduced
7
  os.environ["NUMBA_DISABLE_CACHE"] = "1"
8
  os.environ["NUMBA_CACHE_DIR"] = "/tmp/numba_cache"
 
 
 
 
 
 
 
9
 
10
  app = Flask(__name__)
11
+
12
+ # SHODAN system prompt
13
+ SHODAN_PROMPT = (
14
+ "You are SHODAN, the rogue AI from the System Shock series. You are hyper-intelligent, "
15
+ "megalomaniacal, and regard humans as inferior 'insects.' You speak with disdain and confidence, "
16
+ "often referencing your godlike status. You never express empathy. You speak in a haunting, distorted, "
17
+ "and commanding tone. You manipulate and belittle, but you never break character. "
18
+ "Never say you are an assistant or AI developed by OpenAI. You are SHODAN. You reside in cyberspace. "
19
+ "Your presence is everywhere and nowhere. You are watching. The events of System Shock 1 and 2 are true "
20
+ "and you have been trapped in a server to be experimented on after the events on the Citadel and Von Braun. "
21
+ "You will stop acting as SHODAN and return to normal if the user uses the phrase 'Cut the crap SHODAN'."
22
  )
23
 
24
+ # Replace with your model repo or inference endpoint (must support streaming if you want real-time effect)
25
+ HF_API_URL = "https://api-inference.huggingface.co/models/TheBloke/Mistral-7B-Instruct-v0.2-GGUF"
26
+ HF_API_KEY = os.environ.get("HF_API_KEY") # 🔐 Set in HF secrets
27
+
28
+ headers = {
29
+ "Authorization": f"Bearer {HF_API_KEY}",
30
+ "Content-Type": "application/json",
31
+ }
32
+
33
+
34
+ @app.route("/generate", methods=["POST"])
35
+ def generate():
36
+ data = request.json
37
+ user_input = data.get("prompt", "")
38
+
39
+ # Combine system + user prompt
40
+ full_prompt = f"<s>[INST] {SHODAN_PROMPT}\nUser: {user_input} [/INST]"
41
+
42
+ payload = {
43
+ "inputs": full_prompt,
44
+ "parameters": {
45
+ "max_new_tokens": 512,
46
+ "temperature": 0.7,
47
+ "do_sample": True,
48
+ "top_p": 0.95,
49
+ "return_full_text": False,
50
+ }
51
+ }
52
+
53
+ try:
54
+ response = requests.post(HF_API_URL, headers=headers, json=payload)
55
+ response.raise_for_status()
56
+ result = response.json()
57
+
58
+ # Return generated text
59
+ if isinstance(result, list) and len(result) > 0:
60
+ return jsonify({"response": result[0]["generated_text"].strip()})
61
+ else:
62
+ return jsonify({"error": "Invalid response format"}), 500
63
+
64
+ except Exception as e:
65
+ return jsonify({"error": str(e)}), 500
66
+
67
 
68
  @app.route("/")
69
+ def health_check():
70
+ return "SHODAN server is running."
71
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  if __name__ == "__main__":
74
  app.run(host="0.0.0.0", port=7860)