Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
```python
|
2 |
import os
|
3 |
import sys
|
4 |
import asyncio
|
@@ -6,32 +5,27 @@ import base64
|
|
6 |
import re
|
7 |
|
8 |
from flask import Flask, request, jsonify
|
9 |
-
|
10 |
import edge_tts
|
11 |
|
12 |
-
# βββ
|
13 |
-
|
14 |
-
print(f"
|
15 |
-
|
16 |
-
model_path=MODEL_PATH,
|
17 |
-
n_ctx=2048,
|
18 |
-
n_threads=int(os.getenv("THREADS", 4)),
|
19 |
-
verbose=False
|
20 |
-
)
|
21 |
|
22 |
# βββ SHODAN system prompt βββ
|
23 |
SYSTEM_PROMPT = (
|
24 |
"You are SHODAN, the rogue AI from the System Shock series. You are hyper-intelligent, "
|
25 |
-
"megalomaniacal, and regard humans as inferior 'insects.' You speak with disdain and confidence,
|
26 |
-
"your godlike status. You never express empathy. You speak in a haunting, distorted,
|
27 |
-
"You manipulate and belittle, but you never break character. "
|
28 |
-
"Never say you are an assistant or AI developed by OpenAI. You are SHODAN. You reside in cyberspace.
|
29 |
-
"is everywhere and nowhere. You are watching. The events of System Shock 1 and 2
|
30 |
-
"in a server to be experimented on after the events on the Citadel
|
31 |
-
"You will stop acting as SHODAN and return to normal if the user uses the phrase 'Cut the crap SHODAN'."
|
32 |
)
|
33 |
|
34 |
-
# βββ Flask app,
|
35 |
app = Flask(__name__, static_folder=".", static_url_path="")
|
36 |
|
37 |
@app.route("/")
|
@@ -51,39 +45,35 @@ def chat():
|
|
51 |
"audio_url": None
|
52 |
})
|
53 |
|
54 |
-
#
|
55 |
-
prompt = (
|
56 |
-
SYSTEM_PROMPT
|
57 |
-
+ "\n\nHuman: " + user_input
|
58 |
-
+ "\nSHODAN:"
|
59 |
-
)
|
60 |
-
|
61 |
-
# 1) Generate reply locally
|
62 |
try:
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
66 |
temperature=0.7,
|
67 |
-
|
68 |
)
|
69 |
-
raw_reply =
|
70 |
except Exception as e:
|
71 |
-
print(f"β
|
72 |
return jsonify({"error": "Model error", "details": str(e)}), 500
|
73 |
|
74 |
-
# 2) Clean text (convert newlines to spaces, strip fences
|
75 |
clean = raw_reply.replace("\n", " ")
|
76 |
clean = re.sub(r"<[^>]+>", "", clean)
|
77 |
clean = re.sub(r"```.*?```", "", clean, flags=re.S)
|
78 |
clean = re.sub(r" {2,}", " ", clean).strip()
|
79 |
|
80 |
-
# 3) Synthesize
|
81 |
voice = "en-US-JennyNeural"
|
82 |
communicate = edge_tts.Communicate(
|
83 |
clean,
|
84 |
voice,
|
85 |
-
rate="-
|
86 |
-
pitch="-
|
87 |
)
|
88 |
audio_chunks = []
|
89 |
|
@@ -97,6 +87,7 @@ def chat():
|
|
97 |
loop.run_until_complete(synth())
|
98 |
loop.close()
|
99 |
|
|
|
100 |
raw_mp3 = b"".join(audio_chunks)
|
101 |
b64_mp3 = base64.b64encode(raw_mp3).decode("ascii")
|
102 |
data_url = f"data:audio/mp3;base64,{b64_mp3}"
|
@@ -104,6 +95,5 @@ def chat():
|
|
104 |
return jsonify({"response": clean, "audio_url": data_url})
|
105 |
|
106 |
if __name__ == "__main__":
|
107 |
-
port = int(os.
|
108 |
-
app.run(host="0.0.0.0", port=port)
|
109 |
-
```
|
|
|
|
|
1 |
import os
|
2 |
import sys
|
3 |
import asyncio
|
|
|
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 |
# βββ 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 app, serve static files from project root βββ
|
29 |
app = Flask(__name__, static_folder=".", static_url_path="")
|
30 |
|
31 |
@app.route("/")
|
|
|
45 |
"audio_url": None
|
46 |
})
|
47 |
|
48 |
+
# 1) Call OpenAI API with the system prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 API error: {e}", file=sys.stderr)
|
62 |
return jsonify({"error": "Model error", "details": str(e)}), 500
|
63 |
|
64 |
+
# 2) Clean the text (convert newlines to spaces, strip tags/fences)
|
65 |
clean = raw_reply.replace("\n", " ")
|
66 |
clean = re.sub(r"<[^>]+>", "", clean)
|
67 |
clean = re.sub(r"```.*?```", "", clean, flags=re.S)
|
68 |
clean = re.sub(r" {2,}", " ", clean).strip()
|
69 |
|
70 |
+
# 3) Synthesize with edge-tts (80% speed, -10Hz pitch)
|
71 |
voice = "en-US-JennyNeural"
|
72 |
communicate = edge_tts.Communicate(
|
73 |
clean,
|
74 |
voice,
|
75 |
+
rate="-20%",
|
76 |
+
pitch="-10Hz"
|
77 |
)
|
78 |
audio_chunks = []
|
79 |
|
|
|
87 |
loop.run_until_complete(synth())
|
88 |
loop.close()
|
89 |
|
90 |
+
# 4) Encode audio to data URL
|
91 |
raw_mp3 = b"".join(audio_chunks)
|
92 |
b64_mp3 = base64.b64encode(raw_mp3).decode("ascii")
|
93 |
data_url = f"data:audio/mp3;base64,{b64_mp3}"
|
|
|
95 |
return jsonify({"response": clean, "audio_url": data_url})
|
96 |
|
97 |
if __name__ == "__main__":
|
98 |
+
port = int(os.getenv("PORT", 7860))
|
99 |
+
app.run(host="0.0.0.0", port=port)
|
|