jisaacso219 commited on
Commit
037180d
Β·
verified Β·
1 Parent(s): d4a154b

Update app.py

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