Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,16 +3,19 @@ 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 |
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, often referencing "
|
|
@@ -59,37 +62,43 @@ def chat():
|
|
| 59 |
print(f"β OpenAI error: {e}", file=sys.stderr)
|
| 60 |
return jsonify({"error": "Model error", "details": str(e)}), 500
|
| 61 |
|
| 62 |
-
# 2) Clean the text
|
| 63 |
-
# a) Convert newlines to spaces so we don't concatenate words
|
| 64 |
clean = raw_reply.replace("\n", " ")
|
| 65 |
-
# b) Strip HTML tags
|
| 66 |
clean = re.sub(r"<[^>]+>", "", clean)
|
| 67 |
-
# c) Strip code fences
|
| 68 |
clean = re.sub(r"```.*?```", "", clean, flags=re.S)
|
| 69 |
-
# d) Collapse runs of 2+ spaces into one
|
| 70 |
clean = re.sub(r" {2,}", " ", clean).strip()
|
| 71 |
|
| 72 |
-
# 3) Synthesize
|
| 73 |
voice = "en-US-JennyNeural"
|
| 74 |
communicate = edge_tts.Communicate(
|
| 75 |
clean,
|
| 76 |
voice,
|
| 77 |
-
rate="-
|
| 78 |
-
pitch="-
|
| 79 |
)
|
| 80 |
-
|
| 81 |
audio_chunks = []
|
| 82 |
async def synth():
|
| 83 |
async for chunk in communicate.stream():
|
| 84 |
if chunk["type"] == "audio":
|
| 85 |
audio_chunks.append(chunk["data"])
|
| 86 |
-
|
| 87 |
loop = asyncio.new_event_loop()
|
| 88 |
asyncio.set_event_loop(loop)
|
| 89 |
loop.run_until_complete(synth())
|
| 90 |
loop.close()
|
| 91 |
-
|
| 92 |
raw_mp3 = b"".join(audio_chunks)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
b64_mp3 = base64.b64encode(raw_mp3).decode("ascii")
|
| 94 |
data_url = f"data:audio/mp3;base64,{b64_mp3}"
|
| 95 |
|
|
@@ -98,3 +107,4 @@ def chat():
|
|
| 98 |
if __name__ == "__main__":
|
| 99 |
port = int(os.environ.get("PORT", 7860))
|
| 100 |
app.run(host="0.0.0.0", port=port)
|
|
|
|
|
|
| 3 |
import asyncio
|
| 4 |
import base64
|
| 5 |
import re
|
| 6 |
+
import io
|
| 7 |
|
| 8 |
from flask import Flask, request, jsonify
|
| 9 |
import openai
|
| 10 |
import edge_tts
|
| 11 |
+
from pydub import AudioSegment
|
| 12 |
|
| 13 |
# βββ OpenAI API 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 |
+
# βββ SHODAN system prompt βββ
|
| 19 |
SYSTEM_PROMPT = (
|
| 20 |
"You are SHODAN, the rogue AI from the System Shock series. You are hyper-intelligent, "
|
| 21 |
"megalomaniacal, and regard humans as inferior 'insects.' You speak with disdain and confidence, often referencing "
|
|
|
|
| 62 |
print(f"β OpenAI error: {e}", file=sys.stderr)
|
| 63 |
return jsonify({"error": "Model error", "details": str(e)}), 500
|
| 64 |
|
| 65 |
+
# 2) Clean the text (convert newlines to spaces, strip fences/tags)
|
|
|
|
| 66 |
clean = raw_reply.replace("\n", " ")
|
|
|
|
| 67 |
clean = re.sub(r"<[^>]+>", "", clean)
|
|
|
|
| 68 |
clean = re.sub(r"```.*?```", "", clean, flags=re.S)
|
|
|
|
| 69 |
clean = re.sub(r" {2,}", " ", clean).strip()
|
| 70 |
|
| 71 |
+
# 3) Synthesize raw TTS audio
|
| 72 |
voice = "en-US-JennyNeural"
|
| 73 |
communicate = edge_tts.Communicate(
|
| 74 |
clean,
|
| 75 |
voice,
|
| 76 |
+
rate="-20%",
|
| 77 |
+
pitch="-10Hz"
|
| 78 |
)
|
|
|
|
| 79 |
audio_chunks = []
|
| 80 |
async def synth():
|
| 81 |
async for chunk in communicate.stream():
|
| 82 |
if chunk["type"] == "audio":
|
| 83 |
audio_chunks.append(chunk["data"])
|
|
|
|
| 84 |
loop = asyncio.new_event_loop()
|
| 85 |
asyncio.set_event_loop(loop)
|
| 86 |
loop.run_until_complete(synth())
|
| 87 |
loop.close()
|
|
|
|
| 88 |
raw_mp3 = b"".join(audio_chunks)
|
| 89 |
+
|
| 90 |
+
# 4) Apply distortion effect via pydub
|
| 91 |
+
audio_seg = AudioSegment.from_file(io.BytesIO(raw_mp3), format="mp3")
|
| 92 |
+
# bit-crush: downsample then upsample
|
| 93 |
+
crushed = audio_seg.set_frame_rate(int(audio_seg.frame_rate * 0.5))
|
| 94 |
+
crushed = crushed.set_frame_rate(audio_seg.frame_rate)
|
| 95 |
+
# amplify to create clipping distortion
|
| 96 |
+
distorted = crushed + 10 # +10 dB gain
|
| 97 |
+
buf = io.BytesIO()
|
| 98 |
+
distorted.export(buf, format="mp3")
|
| 99 |
+
raw_mp3 = buf.getvalue()
|
| 100 |
+
|
| 101 |
+
# 5) Encode to data URL
|
| 102 |
b64_mp3 = base64.b64encode(raw_mp3).decode("ascii")
|
| 103 |
data_url = f"data:audio/mp3;base64,{b64_mp3}"
|
| 104 |
|
|
|
|
| 107 |
if __name__ == "__main__":
|
| 108 |
port = int(os.environ.get("PORT", 7860))
|
| 109 |
app.run(host="0.0.0.0", port=port)
|
| 110 |
+
|