jisaacso219 commited on
Commit
da4e850
Β·
verified Β·
1 Parent(s): 88ea0e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -22
app.py CHANGED
@@ -13,7 +13,7 @@ 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 "
@@ -25,6 +25,7 @@ SYSTEM_PROMPT = (
25
  "You will stop acting as SHODAN and return to normal if the user uses the phrase 'Cut the crap SHODAN'."
26
  )
27
 
 
28
  app = Flask(__name__, static_folder=".", static_url_path="")
29
 
30
  @app.route("/")
@@ -37,14 +38,14 @@ def chat():
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
- # 1) Get SHODAN’s reply from OpenAI
48
  try:
49
  completion = openai.chat.completions.create(
50
  model="gpt-3.5-turbo",
@@ -60,26 +61,22 @@ def chat():
60
  print(f"❌ OpenAI error: {e}", file=sys.stderr)
61
  return jsonify({"error": "Model error", "details": str(e)}), 500
62
 
63
- # 2) Clean the text
64
- clean = re.sub(r"<[^>]+>", "", raw_reply) # strip HTML tags
65
- clean = re.sub(r"```.*?```", "", clean, flags=re.S) # strip code fences
66
- clean = re.sub(r"\s+", " ", clean).strip() # normalize whitespace
67
-
68
- # 3) Build SSML
69
- voice_name = "en-US-JennyNeural"
70
- ssml = (
71
- "<speak xmlns='http://www.w3.org/2001/10/synthesis' "
72
- "xmlns:mstts='https://www.w3.org/2001/mstts' "
73
- "xml:lang='en-US' xml:space='preserve'>"
74
- f"<voice name='{voice_name}' xml:space='preserve'>"
75
- f"<mstts:express-as style='robotic'>{clean}</mstts:express-as>"
76
- "</voice></speak>"
77
  )
78
 
79
- # 4) Synthesize audio
80
- communicate = edge_tts.Communicate(ssml, voice_name)
81
  audio_chunks = []
82
-
83
  async def synth():
84
  async for chunk in communicate.stream():
85
  if chunk["type"] == "audio":
@@ -99,5 +96,3 @@ def chat():
99
  if __name__ == "__main__":
100
  port = int(os.environ.get("PORT", 7860))
101
  app.run(host="0.0.0.0", port=port)
102
-
103
-
 
13
  print(f"πŸ”‘ OPENAI_API_KEY set? {bool(OPENAI_API_KEY)}", file=sys.stderr)
14
  openai.api_key = OPENAI_API_KEY
15
 
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, often referencing "
 
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("/")
 
38
  if not user_input:
39
  return jsonify({"error": "Empty message"}), 400
40
 
41
+ # Kill-phrase handling
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’s reply via OpenAI
49
  try:
50
  completion = openai.chat.completions.create(
51
  model="gpt-3.5-turbo",
 
61
  print(f"❌ OpenAI error: {e}", file=sys.stderr)
62
  return jsonify({"error": "Model error", "details": str(e)}), 500
63
 
64
+ # 2) Clean the text (strip tags/fences and normalize whitespace)
65
+ clean = re.sub(r"<[^>]+>", "", raw_reply)
66
+ clean = re.sub(r"```.*?```", "", clean, flags=re.S)
67
+ clean = re.sub(r"\s+", " ", clean).strip()
68
+
69
+ # 3) Synthesize with edge-tts using rate & pitch flags per the docs
70
+ # --rate:-20% (80% speed) and --pitch:-10% (slightly lower)
71
+ voice = "en-US-JennyNeural"
72
+ communicate = edge_tts.Communicate(
73
+ clean,
74
+ voice,
75
+ rate="-20%",
76
+ pitch="-10%"
 
77
  )
78
 
 
 
79
  audio_chunks = []
 
80
  async def synth():
81
  async for chunk in communicate.stream():
82
  if chunk["type"] == "audio":
 
96
  if __name__ == "__main__":
97
  port = int(os.environ.get("PORT", 7860))
98
  app.run(host="0.0.0.0", port=port)