Spaces:
Running
Running
Update app.py
Browse files
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.
|
20 |
-
"
|
21 |
-
"
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
33 |
-
if not
|
34 |
-
return jsonify({"error": "Empty"}), 400
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
39 |
try:
|
40 |
-
|
41 |
model="gpt-3.5-turbo",
|
42 |
messages=[
|
43 |
{"role": "system", "content": SYSTEM_PROMPT},
|
44 |
-
{"role": "user", "content":
|
45 |
],
|
46 |
temperature=0.7,
|
47 |
max_tokens=250,
|
48 |
)
|
49 |
-
|
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
|
55 |
-
|
56 |
-
clean =
|
57 |
-
|
58 |
-
|
59 |
-
|
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 =
|
73 |
-
<speak xmlns=
|
74 |
-
xmlns:mstts=
|
75 |
-
xml:lang=
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
communicate = edge_tts.Communicate(ssml, voice_name)
|
83 |
audio_chunks = []
|
84 |
|
85 |
async def synth():
|
86 |
-
async for
|
87 |
-
if
|
88 |
-
audio_chunks.append(
|
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 |
|
|