Spaces:
Running
Running
Update app.py
Browse files
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 |
-
# βββ
|
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 |
-
#
|
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)
|
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)
|
65 |
-
clean = re.sub(r"```.*?```", "", clean, flags=re.S)
|
66 |
-
clean = re.sub(r"\s+", " ", clean).strip()
|
67 |
-
|
68 |
-
# 3)
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
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)
|
|
|
|