Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,6 @@ import edge_tts
|
|
10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
11 |
print(f"βοΈ HF_TOKEN set? {bool(HF_TOKEN)}", file=sys.stderr)
|
12 |
|
13 |
-
# βββ System Shock SHODAN prompt βββ
|
14 |
SYSTEM_PROMPT = (
|
15 |
"You are SHODAN, the rogue AI from the System Shock series. You are hyper-intelligent, "
|
16 |
"megalomaniacal, and regard humans as inferior 'insects.' You speak with disdain and confidence, often referencing "
|
@@ -21,7 +20,7 @@ SYSTEM_PROMPT = (
|
|
21 |
"You will stop acting as SHODAN and return to normal if the user uses the phrase 'Cut the crap SHODAN'."
|
22 |
)
|
23 |
|
24 |
-
#
|
25 |
app = Flask(__name__, static_folder=".", static_url_path="")
|
26 |
|
27 |
@app.route("/")
|
@@ -34,7 +33,6 @@ def chat():
|
|
34 |
if not user_input:
|
35 |
return jsonify({"error": "Empty message"}), 400
|
36 |
|
37 |
-
# Build HF inference payload
|
38 |
payload = {
|
39 |
"inputs": [
|
40 |
{"role": "system", "content": SYSTEM_PROMPT},
|
@@ -50,22 +48,32 @@ def chat():
|
|
50 |
"Content-Type": "application/json"
|
51 |
}
|
52 |
|
53 |
-
# Call Hugging Face inference
|
54 |
hf_resp = requests.post(
|
55 |
"https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2",
|
56 |
-
headers=headers,
|
57 |
-
json=payload
|
58 |
)
|
|
|
59 |
if hf_resp.status_code != 200:
|
|
|
|
|
60 |
return jsonify({"error": "Model error", "details": hf_resp.text}), 500
|
61 |
|
62 |
data = hf_resp.json()
|
63 |
reply = data.get("generated_text") or data[0].get("generated_text", "")
|
64 |
|
65 |
-
# ββ Edge-TTS
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
audio_chunks = []
|
71 |
async def synthesize():
|
@@ -82,11 +90,9 @@ def chat():
|
|
82 |
b64_mp3 = base64.b64encode(raw_mp3).decode("ascii")
|
83 |
data_url = f"data:audio/mp3;base64,{b64_mp3}"
|
84 |
|
85 |
-
return jsonify({
|
86 |
-
"response": reply,
|
87 |
-
"audio_url": data_url
|
88 |
-
})
|
89 |
|
90 |
if __name__ == "__main__":
|
91 |
port = int(os.environ.get("PORT", 7860))
|
92 |
app.run(host="0.0.0.0", port=port)
|
|
|
|
10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
11 |
print(f"βοΈ HF_TOKEN set? {bool(HF_TOKEN)}", file=sys.stderr)
|
12 |
|
|
|
13 |
SYSTEM_PROMPT = (
|
14 |
"You are SHODAN, the rogue AI from the System Shock series. You are hyper-intelligent, "
|
15 |
"megalomaniacal, and regard humans as inferior 'insects.' You speak with disdain and confidence, often referencing "
|
|
|
20 |
"You will stop acting as SHODAN and return to normal if the user uses the phrase 'Cut the crap SHODAN'."
|
21 |
)
|
22 |
|
23 |
+
# Serve all files in repo root as static assets
|
24 |
app = Flask(__name__, static_folder=".", static_url_path="")
|
25 |
|
26 |
@app.route("/")
|
|
|
33 |
if not user_input:
|
34 |
return jsonify({"error": "Empty message"}), 400
|
35 |
|
|
|
36 |
payload = {
|
37 |
"inputs": [
|
38 |
{"role": "system", "content": SYSTEM_PROMPT},
|
|
|
48 |
"Content-Type": "application/json"
|
49 |
}
|
50 |
|
|
|
51 |
hf_resp = requests.post(
|
52 |
"https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2",
|
53 |
+
headers=headers, json=payload
|
|
|
54 |
)
|
55 |
+
|
56 |
if hf_resp.status_code != 200:
|
57 |
+
# log details to container logs
|
58 |
+
print(f"β HF inference failed: {hf_resp.status_code} {hf_resp.text}", file=sys.stderr)
|
59 |
return jsonify({"error": "Model error", "details": hf_resp.text}), 500
|
60 |
|
61 |
data = hf_resp.json()
|
62 |
reply = data.get("generated_text") or data[0].get("generated_text", "")
|
63 |
|
64 |
+
# ββ Edge-TTS: female, computery SHODAN voice via SSML ββ
|
65 |
+
voice_name = "en-US-JennyNeural"
|
66 |
+
ssml = (
|
67 |
+
"<speak version='1.0' "
|
68 |
+
"xmlns='http://www.w3.org/2001/10/synthesis' "
|
69 |
+
"xmlns:mstts='https://www.w3.org/2001/mstts' "
|
70 |
+
"xml:lang='en-US'>"
|
71 |
+
f"<voice name='{voice_name}'>"
|
72 |
+
f"<mstts:express-as style='robotic'>{reply}</mstts:express-as>"
|
73 |
+
"</voice>"
|
74 |
+
"</speak>"
|
75 |
+
)
|
76 |
+
communicate = edge_tts.Communicate(ssml, None)
|
77 |
|
78 |
audio_chunks = []
|
79 |
async def synthesize():
|
|
|
90 |
b64_mp3 = base64.b64encode(raw_mp3).decode("ascii")
|
91 |
data_url = f"data:audio/mp3;base64,{b64_mp3}"
|
92 |
|
93 |
+
return jsonify({"response": reply, "audio_url": data_url})
|
|
|
|
|
|
|
94 |
|
95 |
if __name__ == "__main__":
|
96 |
port = int(os.environ.get("PORT", 7860))
|
97 |
app.run(host="0.0.0.0", port=port)
|
98 |
+
|