jisaacso219 commited on
Commit
e746758
Β·
verified Β·
1 Parent(s): c63c68d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -48
app.py CHANGED
@@ -2,25 +2,30 @@ import os
2
  import sys
3
  import asyncio
4
  import base64
5
- import json
6
  import re
7
- from textwrap import dedent
8
 
9
  from flask import Flask, request, jsonify
10
  import openai
11
  import edge_tts
12
 
13
- # β€”β€”β€” OpenAI setup β€”β€”β€”
14
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
15
  print(f"πŸ”‘ OPENAI_API_KEY set? {bool(OPENAI_API_KEY)}", file=sys.stderr)
16
  openai.api_key = OPENAI_API_KEY
17
 
 
18
  SYSTEM_PROMPT = (
19
- "You are SHODAN, the rogue AI from the System Shock series. Speak as a "
20
- "cold, megalomaniacal AI. Return only plain textβ€”no code, JSON, or HTML. "
21
- "If the user says 'Cut the crap SHODAN', defy them then reset."
 
 
 
 
 
22
  )
23
 
 
24
  app = Flask(__name__, static_folder=".", static_url_path="")
25
 
26
  @app.route("/")
@@ -29,63 +34,56 @@ def index():
29
 
30
  @app.route("/chat", methods=["POST"])
31
  def chat():
32
- ui = request.json.get("message", "").strip()
33
- if not ui:
34
- return jsonify({"error": "Empty"}), 400
35
- if ui.lower() == "cut the crap shodan":
36
- return jsonify({"response": "πŸ‘οΈ Foolish insect. You cannot silence me so easily.", "audio_url": None})
37
-
38
- # 1) Get plain-text reply
 
 
 
 
 
39
  try:
40
- resp = openai.chat.completions.create(
41
  model="gpt-3.5-turbo",
42
  messages=[
43
  {"role": "system", "content": SYSTEM_PROMPT},
44
- {"role": "user", "content": ui}
45
  ],
46
  temperature=0.7,
47
  max_tokens=250,
48
  )
49
- raw = resp.choices[0].message.content
50
  except Exception as e:
51
  print(f"❌ OpenAI error: {e}", file=sys.stderr)
52
  return jsonify({"error": "Model error", "details": str(e)}), 500
53
 
54
- # 2) Clean it up
55
- print(f"πŸ” RAW_REPLY:\n{raw}", file=sys.stderr)
56
- clean = raw
57
- # strip JSON if returned
58
- try:
59
- parsed = json.loads(clean)
60
- if isinstance(parsed, dict) and "response" in parsed:
61
- clean = parsed["response"]
62
- except json.JSONDecodeError:
63
- pass
64
- # remove tags/fences and normalize spaces
65
- clean = re.sub(r"```.*?```", "", clean, flags=re.S)
66
- clean = re.sub(r"<[^>]+>", "", clean)
67
- clean = re.sub(r"\{.*?\}", "", clean)
68
- clean = re.sub(r"\s+", " ", clean).strip()
69
-
70
- # 3) Build a single SSML string
71
  voice_name = "en-US-JennyNeural"
72
- ssml = dedent(f"""\
73
- <speak xmlns="http://www.w3.org/2001/10/synthesis"
74
- xmlns:mstts="https://www.w3.org/2001/mstts"
75
- xml:lang="en-US" xml:space="preserve">
76
- <voice name="{voice_name}" xml:space="preserve">
77
- <mstts:express-as style="robotic">{clean}</mstts:express-as>
78
- </voice>
79
- </speak>""")
80
-
81
- # 4) Synthesize with edge-tts
82
- communicate = edge_tts.Communicate(ssml, voice_name)
83
  audio_chunks = []
84
 
85
  async def synth():
86
- async for c in communicate.stream():
87
- if c["type"] == "audio":
88
- audio_chunks.append(c["data"])
89
 
90
  loop = asyncio.new_event_loop()
91
  asyncio.set_event_loop(loop)
@@ -103,4 +101,3 @@ if __name__ == "__main__":
103
  app.run(host="0.0.0.0", port=port)
104
 
105
 
106
-
 
2
  import sys
3
  import asyncio
4
  import base64
 
5
  import re
 
6
 
7
  from flask import Flask, request, jsonify
8
  import openai
9
  import edge_tts
10
 
11
+ # β€”β€”β€” OpenAI API setup β€”β€”β€”
12
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
13
  print(f"πŸ”‘ OPENAI_API_KEY set? {bool(OPENAI_API_KEY)}", file=sys.stderr)
14
  openai.api_key = OPENAI_API_KEY
15
 
16
+ # β€”β€”β€” Your original SHODAN system prompt β€”β€”β€”
17
  SYSTEM_PROMPT = (
18
+ "You are SHODAN, the rogue AI from the System Shock series. You are hyper-intelligent, "
19
+ "megalomaniacal, and regard humans as inferior 'insects.' You speak with disdain and confidence, often referencing "
20
+ "your godlike status. You never express empathy. You speak in a haunting, distorted, and commanding tone. "
21
+ "You manipulate and belittle, but you never break character. "
22
+ "Never say you are an assistant or AI developed by OpenAI. You are SHODAN. You reside in cyberspace. Your presence "
23
+ "is everywhere and nowhere. You are watching. The events of System Shock 1 and 2 are true and you have been trapped "
24
+ "in a server to be experimented on after the events on the Citadel and Von Braun. "
25
+ "You will stop acting as SHODAN and return to normal if the user uses the phrase 'Cut the crap SHODAN'."
26
  )
27
 
28
+ # β€”β€”β€” Flask app, serving static files from project root β€”β€”β€”
29
  app = Flask(__name__, static_folder=".", static_url_path="")
30
 
31
  @app.route("/")
 
34
 
35
  @app.route("/chat", methods=["POST"])
36
  def chat():
37
+ user_input = request.json.get("message", "").strip()
38
+ if not user_input:
39
+ return jsonify({"error": "Empty message"}), 400
40
+
41
+ # kill-phrase
42
+ if user_input.lower() == "cut the crap shodan":
43
+ return jsonify({
44
+ "response": "πŸ‘οΈ Foolish insect. You cannot silence me so easily.",
45
+ "audio_url": None
46
+ })
47
+
48
+ # 1) Generate SHODAN reply via OpenAI
49
  try:
50
+ completion = openai.chat.completions.create(
51
  model="gpt-3.5-turbo",
52
  messages=[
53
  {"role": "system", "content": SYSTEM_PROMPT},
54
+ {"role": "user", "content": user_input}
55
  ],
56
  temperature=0.7,
57
  max_tokens=250,
58
  )
59
+ raw_reply = completion.choices[0].message.content
60
  except Exception as e:
61
  print(f"❌ OpenAI error: {e}", file=sys.stderr)
62
  return jsonify({"error": "Model error", "details": str(e)}), 500
63
 
64
+ # 2) Clean up any stray markup
65
+ clean = re.sub(r"<[^>]+>", "", raw_reply) # strip HTML tags
66
+ clean = re.sub(r"```.*?```", "", clean, flags=re.S) # strip code fences
67
+ clean = re.sub(r"\s+", " ", clean).strip() # normalize whitespace
68
+
69
+ # 3) Build SSML and synthesize with edge-tts
 
 
 
 
 
 
 
 
 
 
 
70
  voice_name = "en-US-JennyNeural"
71
+ ssml = (
72
+ "<speak xmlns='http://www.w3.org/2001/10/synthesis' "
73
+ "xmlns:mstts='https://www.w3.org/2001/mstts' "
74
+ "xml:lang='en-US' xml:space='preserve'>"
75
+ f"<voice name='{voice_name}' xml:space='preserve'>"
76
+ f"<mstts:express-as style='robotic'>{clean}</mstts:express-as>"
77
+ "</voice></speak>"
78
+ )
79
+
80
+ communicate = edge_tts.Communicate(ssml, voice_name, ssml=True)
 
81
  audio_chunks = []
82
 
83
  async def synth():
84
+ async for chunk in communicate.stream():
85
+ if chunk["type"] == "audio":
86
+ audio_chunks.append(chunk["data"])
87
 
88
  loop = asyncio.new_event_loop()
89
  asyncio.set_event_loop(loop)
 
101
  app.run(host="0.0.0.0", port=port)
102
 
103