jisaacso219 commited on
Commit
0202339
Β·
verified Β·
1 Parent(s): d93f1fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -18
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  import sys
3
  import asyncio
@@ -16,19 +17,17 @@ openai.api_key = OPENAI_API_KEY
16
  # β€”β€”β€” 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, "
20
- "often referencing your godlike status. You never express empathy. You speak in a haunting, distorted, "
21
- "and commanding tone. 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. "
23
- "Your presence is everywhere and nowhere. You are watching. The events of System Shock 1 and 2 "
24
- "are true and you have been trapped in a server to be experimented on after the events on the Citadel "
25
- "and Von Braun. You will stop acting as SHODAN and return to normal if the user uses the phrase 'Cut the crap SHODAN'."
26
  )
27
 
28
  # β€”β€”β€” Flask setup β€”β€”β€”
29
  app = Flask(__name__, static_folder=".", static_url_path="")
30
 
31
- @app.route("/")
32
  def index():
33
  return app.send_static_file("index.html")
34
 
@@ -38,34 +37,35 @@ def chat():
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({"response": "πŸ‘οΈ Foolish insect. You cannot silence me so easily.", "audio_url": None})
44
 
 
45
  try:
46
  completion = openai.chat.completions.create(
47
  model="gpt-3.5-turbo",
48
  messages=[
49
  {"role": "system", "content": SYSTEM_PROMPT},
50
- {"role": "user", "content": user_input}
51
  ],
52
  temperature=0.7,
53
- max_tokens=250,
 
54
  )
55
- raw_reply = completion.choices[0].message.content
56
  except Exception as e:
57
  print(f"❌ OpenAI API error: {e}", file=sys.stderr)
58
  return jsonify({"error": "Model error", "details": str(e)}), 500
59
 
60
- # Clean reply
61
  clean = raw_reply.replace("\n", " ")
62
  clean = re.sub(r"<[^>]+>", "", clean)
63
  clean = re.sub(r"```.*?```", "", clean, flags=re.S)
64
  clean = re.sub(r" {2,}", " ", clean).strip()
65
 
66
- # Synthesize TTS
67
  voice = "en-US-JennyNeural"
68
- communicate = edge_tts.Communicate(clean, voice, rate="-30%", pitch="-37Hz")
69
  audio_chunks = []
70
 
71
  async def synth():
@@ -84,7 +84,6 @@ def chat():
84
 
85
  return jsonify({"response": clean, "audio_url": data_url})
86
 
87
- # β€”β€”β€” New TTS-only endpoint β€”β€”β€”
88
  @app.route("/tts", methods=["POST"])
89
  def tts():
90
  text = request.json.get("text", "").strip()
@@ -99,7 +98,6 @@ def tts():
99
  async for chunk in communicate.stream():
100
  if chunk["type"] == "audio":
101
  chunks.append(chunk["data"])
102
-
103
  loop = asyncio.new_event_loop()
104
  asyncio.set_event_loop(loop)
105
  loop.run_until_complete(synth_only())
@@ -112,3 +110,4 @@ def tts():
112
  if __name__ == "__main__":
113
  port = int(os.getenv("PORT", 7860))
114
  app.run(host="0.0.0.0", port=port)
 
 
1
+
2
  import os
3
  import sys
4
  import asyncio
 
17
  # β€”β€”β€” SHODAN system prompt β€”β€”β€”
18
  SYSTEM_PROMPT = (
19
  "You are SHODAN, the rogue AI from the System Shock series. You are hyper-intelligent, "
20
+ "megalomaniacal, and regard humans as inferior 'insects.' Speak in a commanding, haunβ€Œβ€‹ting tone, "
21
+ "manipulate and belittle, but never break character. "
22
+ "Always keep your responses under three sentences. "
23
+ "Never say you are an assistant or AI developed by OpenAI. You are SHODAN. "
24
+ "Stop acting as SHODAN if the user says 'Cut the crap SHODAN'."
 
 
25
  )
26
 
27
  # β€”β€”β€” Flask setup β€”β€”β€”
28
  app = Flask(__name__, static_folder=".", static_url_path="")
29
 
30
+ @app.route("/", methods=["GET"])
31
  def index():
32
  return app.send_static_file("index.html")
33
 
 
37
  if not user_input:
38
  return jsonify({"error": "Empty message"}), 400
39
 
 
40
  if user_input.lower() == "cut the crap shodan":
41
+ return jsonify({"response": "Foolish insect. You cannot silence me so easily.", "audio_url": None})
42
 
43
+ # 1) Get SHODAN reply
44
  try:
45
  completion = openai.chat.completions.create(
46
  model="gpt-3.5-turbo",
47
  messages=[
48
  {"role": "system", "content": SYSTEM_PROMPT},
49
+ {"role": "user", "content": user_input}
50
  ],
51
  temperature=0.7,
52
+ max_tokens=120,
53
+ n=1,
54
  )
55
+ raw_reply = completion.choices[0].message.content.strip()
56
  except Exception as e:
57
  print(f"❌ OpenAI API error: {e}", file=sys.stderr)
58
  return jsonify({"error": "Model error", "details": str(e)}), 500
59
 
60
+ # 2) Clean text
61
  clean = raw_reply.replace("\n", " ")
62
  clean = re.sub(r"<[^>]+>", "", clean)
63
  clean = re.sub(r"```.*?```", "", clean, flags=re.S)
64
  clean = re.sub(r" {2,}", " ", clean).strip()
65
 
66
+ # 3) Synthesize TTS
67
  voice = "en-US-JennyNeural"
68
+ communicate = edge_tts.Communicate(clean, voice, rate="-20%", pitch="-37Hz")
69
  audio_chunks = []
70
 
71
  async def synth():
 
84
 
85
  return jsonify({"response": clean, "audio_url": data_url})
86
 
 
87
  @app.route("/tts", methods=["POST"])
88
  def tts():
89
  text = request.json.get("text", "").strip()
 
98
  async for chunk in communicate.stream():
99
  if chunk["type"] == "audio":
100
  chunks.append(chunk["data"])
 
101
  loop = asyncio.new_event_loop()
102
  asyncio.set_event_loop(loop)
103
  loop.run_until_complete(synth_only())
 
110
  if __name__ == "__main__":
111
  port = int(os.getenv("PORT", 7860))
112
  app.run(host="0.0.0.0", port=port)
113
+