jisaacso219 commited on
Commit
0eec525
Β·
verified Β·
1 Parent(s): 54cc8c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -18
app.py CHANGED
@@ -25,7 +25,7 @@ SYSTEM_PROMPT = (
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("/")
@@ -38,20 +38,16 @@ def chat():
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) 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,
@@ -61,20 +57,15 @@ def chat():
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="-42%",
76
- pitch="-37Hz"
77
- )
78
  audio_chunks = []
79
 
80
  async def synth():
@@ -87,13 +78,37 @@ def chat():
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}"
94
 
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)
 
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 setup β€”β€”β€”
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
42
  if user_input.lower() == "cut the crap shodan":
43
+ return jsonify({"response": "πŸ‘οΈ Foolish insect. You cannot silence me so easily.", "audio_url": None})
 
 
 
44
 
 
45
  try:
46
  completion = openai.chat.completions.create(
47
  model="gpt-3.5-turbo",
48
  messages=[
49
  {"role": "system", "content": SYSTEM_PROMPT},
50
+ {"role": "user", "content": user_input}
51
  ],
52
  temperature=0.7,
53
  max_tokens=250,
 
57
  print(f"❌ OpenAI API error: {e}", file=sys.stderr)
58
  return jsonify({"error": "Model error", "details": str(e)}), 500
59
 
60
+ # Clean reply
61
  clean = raw_reply.replace("\n", " ")
62
  clean = re.sub(r"<[^>]+>", "", clean)
63
  clean = re.sub(r"```.*?```", "", clean, flags=re.S)
64
  clean = re.sub(r" {2,}", " ", clean).strip()
65
 
66
+ # Synthesize TTS
67
  voice = "en-US-JennyNeural"
68
+ communicate = edge_tts.Communicate(clean, voice, rate="-42%", pitch="-37Hz")
 
 
 
 
 
69
  audio_chunks = []
70
 
71
  async def synth():
 
78
  loop.run_until_complete(synth())
79
  loop.close()
80
 
 
81
  raw_mp3 = b"".join(audio_chunks)
82
  b64_mp3 = base64.b64encode(raw_mp3).decode("ascii")
83
  data_url = f"data:audio/mp3;base64,{b64_mp3}"
84
 
85
  return jsonify({"response": clean, "audio_url": data_url})
86
 
87
+ # β€”β€”β€” New TTS-only endpoint β€”β€”β€”
88
+ @app.route("/tts", methods=["POST"])
89
+ def tts():
90
+ text = request.json.get("text", "").strip()
91
+ if not text:
92
+ return jsonify({"error": "Empty text"}), 400
93
+
94
+ voice = "en-US-JennyNeural"
95
+ communicate = edge_tts.Communicate(text, voice, rate="-20%", pitch="-10Hz")
96
+ chunks = []
97
+
98
+ async def synth_only():
99
+ async for chunk in communicate.stream():
100
+ if chunk["type"] == "audio":
101
+ chunks.append(chunk["data"])
102
+
103
+ loop = asyncio.new_event_loop()
104
+ asyncio.set_event_loop(loop)
105
+ loop.run_until_complete(synth_only())
106
+ loop.close()
107
+
108
+ raw = b"".join(chunks)
109
+ b64 = base64.b64encode(raw).decode("ascii")
110
+ return jsonify({"audio_url": f"data:audio/mp3;base64,{b64}"})
111
+
112
  if __name__ == "__main__":
113
  port = int(os.getenv("PORT", 7860))
114
+ app.run(host="0.0.0.0", port=port)