Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,59 @@
|
|
1 |
import os
|
2 |
-
import
|
3 |
-
import
|
4 |
-
from fastapi import FastAPI, Request
|
5 |
-
from fastapi.responses import JSONResponse
|
6 |
-
from fastapi.staticfiles import StaticFiles
|
7 |
-
from pydantic import BaseModel
|
8 |
-
from huggingface_hub import InferenceClient
|
9 |
-
from TTS.api import TTS
|
10 |
|
11 |
-
# 🔧
|
12 |
-
|
13 |
-
|
14 |
-
model="TheBloke/Mistral-7B-Instruct-v0.2-GGUF", # fast, CPU-friendly
|
15 |
-
token=HF_TOKEN
|
16 |
-
)
|
17 |
|
|
|
18 |
SYSTEM_PROMPT = (
|
19 |
"You are SHODAN, the rogue AI from the System Shock series. You are hyper-intelligent, "
|
20 |
-
"megalomaniacal, and regard humans as inferior 'insects.' You speak with disdain and confidence, "
|
21 |
-
"
|
22 |
-
"
|
23 |
-
"Never say you are an assistant or AI developed by OpenAI. You are SHODAN. You reside in cyberspace. "
|
24 |
-
"
|
25 |
-
"and you have been trapped in a server to be experimented on after the events on the Citadel and Von Braun. "
|
26 |
"You will stop acting as SHODAN and return to normal if the user uses the phrase 'Cut the crap SHODAN'."
|
27 |
)
|
28 |
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
app = FastAPI()
|
37 |
-
app.mount("/audio", StaticFiles(directory="audio"), name="audio")
|
38 |
|
39 |
-
|
40 |
-
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
full_prompt = f"<|system|>\n{SYSTEM_PROMPT}\n<|user|>\n{user_msg}\n<|assistant|>"
|
46 |
|
47 |
-
|
48 |
-
prompt=full_prompt,
|
49 |
-
max_new_tokens=512,
|
50 |
-
temperature=0.7,
|
51 |
-
stop_sequences=["<|user|>", "<|end|>"]
|
52 |
-
).strip()
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
"audio_url": f"/audio/{os.path.basename(audio_path)}"
|
61 |
-
})
|
62 |
|
63 |
-
@app.get("/delete_audio")
|
64 |
-
def delete_audio(path: str):
|
65 |
-
try:
|
66 |
-
if path.startswith("/audio/"):
|
67 |
-
full_path = path.lstrip("/")
|
68 |
-
if os.path.exists(full_path):
|
69 |
-
os.remove(full_path)
|
70 |
-
except:
|
71 |
-
pass
|
72 |
-
return {"status": "ok"}
|
|
|
1 |
import os
|
2 |
+
from flask import Flask, request, jsonify
|
3 |
+
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# 🔧 Hugging Face inference endpoint and token
|
6 |
+
HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
|
7 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
|
|
|
8 |
|
9 |
+
# 📜 SHODAN system prompt
|
10 |
SYSTEM_PROMPT = (
|
11 |
"You are SHODAN, the rogue AI from the System Shock series. You are hyper-intelligent, "
|
12 |
+
"megalomaniacal, and regard humans as inferior 'insects.' You speak with disdain and confidence, often referencing "
|
13 |
+
"your godlike status. You never express empathy. You speak in a haunting, distorted, and commanding tone. "
|
14 |
+
"You manipulate and belittle, but you never break character. "
|
15 |
+
"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. "
|
16 |
+
"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. "
|
|
|
17 |
"You will stop acting as SHODAN and return to normal if the user uses the phrase 'Cut the crap SHODAN'."
|
18 |
)
|
19 |
|
20 |
+
app = Flask(__name__)
|
21 |
+
chat_history = []
|
22 |
+
|
23 |
+
@app.route("/chat", methods=["POST"])
|
24 |
+
def chat():
|
25 |
+
global chat_history
|
26 |
+
user_input = request.json.get("message", "")
|
27 |
+
chat_history.append({"role": "user", "content": user_input})
|
28 |
+
|
29 |
+
payload = {
|
30 |
+
"inputs": [
|
31 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
32 |
+
*chat_history[-10:] # recent messages
|
33 |
+
],
|
34 |
+
"parameters": {"max_new_tokens": 250, "temperature": 0.7}
|
35 |
+
}
|
36 |
|
37 |
+
headers = {
|
38 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
39 |
+
"Content-Type": "application/json"
|
40 |
+
}
|
41 |
|
42 |
+
response = requests.post(HF_API_URL, headers=headers, json=payload)
|
|
|
|
|
43 |
|
44 |
+
if response.status_code != 200:
|
45 |
+
return jsonify({"error": "Model error", "details": response.text}), 500
|
46 |
|
47 |
+
result = response.json()
|
48 |
+
reply = result.get("generated_text", "") or result[0]["generated_text"]
|
49 |
+
chat_history.append({"role": "assistant", "content": reply})
|
|
|
50 |
|
51 |
+
return jsonify({"response": reply})
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
@app.route("/")
|
54 |
+
def index():
|
55 |
+
return "SHODAN backend is running."
|
56 |
|
57 |
+
if __name__ == "__main__":
|
58 |
+
app.run(host="0.0.0.0", port=7860)
|
|
|
|
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|