Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,27 +2,29 @@ import os
|
|
2 |
import sys
|
3 |
import asyncio
|
4 |
import base64
|
|
|
5 |
from flask import Flask, request, jsonify
|
6 |
-
import
|
7 |
import edge_tts
|
8 |
|
9 |
-
# βββ
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
|
14 |
# βββ SHODAN system prompt βββ
|
15 |
SYSTEM_PROMPT = (
|
16 |
"You are SHODAN, the rogue AI from the System Shock series. You are hyper-intelligent, "
|
17 |
-
"megalomaniacal, and regard humans as inferior 'insects.' You speak with disdain and confidence,
|
18 |
-
"your godlike status. You never express empathy. You speak in a haunting, distorted,
|
19 |
-
"You manipulate and belittle, but you never break character. "
|
20 |
-
"Never say you are an assistant or AI developed by OpenAI. You are SHODAN. You reside in cyberspace.
|
21 |
-
"The events of System Shock 1 and 2 are true
|
|
|
22 |
"You will stop acting as SHODAN and return to normal if the user uses the phrase 'Cut the crap SHODAN'."
|
23 |
)
|
24 |
|
25 |
-
# βββ Flask
|
26 |
app = Flask(__name__, static_folder=".", static_url_path="")
|
27 |
|
28 |
@app.route("/")
|
@@ -35,39 +37,30 @@ def chat():
|
|
35 |
if not user_input:
|
36 |
return jsonify({"error": "Empty message"}), 400
|
37 |
|
38 |
-
# kill
|
39 |
if user_input.lower() == "cut the crap shodan":
|
40 |
return jsonify({
|
41 |
"response": "ποΈ Foolish insect. You cannot silence me so easily.",
|
42 |
"audio_url": None
|
43 |
})
|
44 |
|
45 |
-
#
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
"
|
59 |
-
|
60 |
-
|
61 |
-
# call HF inference API
|
62 |
-
hf_resp = requests.post(HF_API_URL, headers=headers, json=payload)
|
63 |
-
if hf_resp.status_code != 200:
|
64 |
-
print(f"β HF inference failed: {hf_resp.status_code} {hf_resp.text}", file=sys.stderr)
|
65 |
-
return jsonify({"error": "Model error", "details": hf_resp.text}), 500
|
66 |
-
|
67 |
-
data = hf_resp.json()
|
68 |
-
reply = data.get("generated_text") or data[0].get("generated_text", "")
|
69 |
|
70 |
-
# ββ Edge-TTS SSML robotic
|
71 |
voice_name = "en-US-JennyNeural"
|
72 |
ssml = (
|
73 |
"<speak xmlns='http://www.w3.org/2001/10/synthesis' "
|
|
|
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("/")
|
|
|
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 ChatCompletion with a cost-efficient model
|
48 |
+
try:
|
49 |
+
resp = openai.ChatCompletion.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 = resp.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' "
|