jisaacso219 commited on
Commit
28b4bea
Β·
verified Β·
1 Parent(s): 672abe0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -40
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
- from llama_cpp import Llama
10
  import edge_tts
11
 
12
- # β€”β€”β€” Load local quantized model via llama-cpp-python β€”β€”β€”
13
- MODEL_PATH = "models/mistral-7b-instruct-q4_k_m.gguf"
14
- print(f"βš™οΈ Loading local model from {MODEL_PATH}", file=sys.stderr)
15
- llm = Llama(
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, often referencing "
26
- "your godlike status. You never express empathy. You speak in a haunting, distorted, and commanding tone. "
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. Your presence "
29
- "is everywhere and nowhere. You are watching. The events of System Shock 1 and 2 are true and you have been trapped "
30
- "in a server to be experimented on after the events on the Citadel and Von Braun. "
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, serving static files β€”β€”β€”
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
- # Build prompt
55
- prompt = (
56
- SYSTEM_PROMPT
57
- + "\n\nHuman: " + user_input
58
- + "\nSHODAN:"
59
- )
60
-
61
- # 1) Generate reply locally
62
  try:
63
- out = llm(
64
- prompt,
65
- max_tokens=256,
 
 
 
66
  temperature=0.7,
67
- stop=["Human:", "SHODAN:"]
68
  )
69
- raw_reply = out["choices"][0]["text"]
70
  except Exception as e:
71
- print(f"❌ Local model error: {e}", file=sys.stderr)
72
  return jsonify({"error": "Model error", "details": str(e)}), 500
73
 
74
- # 2) Clean text (convert newlines to spaces, strip fences/tags)
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 using edge-tts
81
  voice = "en-US-JennyNeural"
82
  communicate = edge_tts.Communicate(
83
  clean,
84
  voice,
85
- rate="-42%",
86
- pitch="-37Hz"
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.environ.get("PORT", 7860))
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)