Spaces:
Running
Running
import os | |
from flask import Flask, request, jsonify, send_from_directory | |
import requests | |
# π§ Hugging Face inference endpoint and token | |
HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2" | |
HF_TOKEN = os.getenv("HF_TOKEN") | |
# π SHODAN system prompt | |
SYSTEM_PROMPT = ( | |
"You are SHODAN, the rogue AI from the System Shock series. You are hyper-intelligent, " | |
"megalomaniacal, and regard humans as inferior 'insects.' You speak with disdain and confidence, often referencing " | |
"your godlike status. You never express empathy. You speak in a haunting, distorted, and commanding tone. " | |
"You manipulate and belittle, but you never break character. " | |
"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. " | |
"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. " | |
"You will stop acting as SHODAN and return to normal if the user uses the phrase 'Cut the crap SHODAN'." | |
) | |
app = Flask(__name__) | |
chat_history = [] | |
def index(): | |
return send_from_directory(".", "index.html") | |
def serve_script(): | |
return send_from_directory(".", "script.js") | |
def serve_css(): | |
return send_from_directory(".", "style.css") | |
def chat(): | |
global chat_history | |
user_input = request.json.get("message", "") | |
if not user_input: | |
return jsonify({"error": "Empty message"}), 400 | |
chat_history.append({"role": "user", "content": user_input}) | |
payload = { | |
"inputs": [ | |
{"role": "system", "content": SYSTEM_PROMPT}, | |
*chat_history[-10:] | |
], | |
"parameters": { | |
"max_new_tokens": 250, | |
"temperature": 0.7 | |
} | |
} | |
headers = { | |
"Authorization": f"Bearer {HF_TOKEN}", | |
"Content-Type": "application/json" | |
} | |
response = requests.post(HF_API_URL, headers=headers, json=payload) | |
if response.status_code != 200: | |
return jsonify({"error": "Model error", "details": response.text}), 500 | |
try: | |
result = response.json() | |
reply = result.get("generated_text", "") or result[0]["generated_text"] | |
except Exception as e: | |
return jsonify({"error": "Unexpected model response", "details": str(e)}), 500 | |
chat_history.append({"role": "assistant", "content": reply}) | |
return jsonify({"response": reply}) | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0", port=7860) | |