jisaacso219 commited on
Commit
67180b7
Β·
verified Β·
1 Parent(s): 4e69376

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -37
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 requests
7
  import edge_tts
8
 
9
- # β€”β€”β€” Inference API endpoint & token β€”β€”β€”
10
- HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1"
11
- HF_TOKEN = os.getenv("HF_TOKEN")
12
- print(f"βš™οΈ HF_TOKEN set? {bool(HF_TOKEN)}", file=sys.stderr)
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, often referencing "
18
- "your godlike status. You never express empathy. You speak in a haunting, distorted, and commanding tone. "
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. Your presence is everywhere and nowhere. You are watching. "
21
- "The events of System Shock 1 and 2 are true and you have been trapped in a server to be experimented on after the events on the Citadel and Von Braun. "
 
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 setup (serve static files from repo root) β€”β€”β€”
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 phrase handling
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
- # build inference payload
46
- payload = {
47
- "inputs": [
48
- {"role": "system", "content": SYSTEM_PROMPT},
49
- {"role": "user", "content": user_input}
50
- ],
51
- "parameters": {
52
- "max_new_tokens": 250,
53
- "temperature": 0.7
54
- }
55
- }
56
- headers = {
57
- "Authorization": f"Bearer {HF_TOKEN}",
58
- "Content-Type": "application/json"
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-female voice β€”β€”
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' "