jisaacso219 commited on
Commit
53e569a
·
verified ·
1 Parent(s): a852e7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -56
app.py CHANGED
@@ -1,72 +1,59 @@
1
  import os
2
- import uuid
3
- import shutil
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
- # 🔧 Configure
12
- HF_TOKEN = os.environ.get("HF_TOKEN")
13
- client = InferenceClient(
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
- "often referencing your godlike status. You never express empathy. You speak in a haunting, distorted, "
22
- "and commanding tone. You manipulate and belittle, but you never break character. "
23
- "Never say you are an assistant or AI developed by OpenAI. You are SHODAN. You reside in cyberspace. "
24
- "Your presence is everywhere and nowhere. You are watching. The events of System Shock 1 and 2 are true "
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
- # 📢 TTS model (CPU-compatible, robotic, feminine style)
30
- tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- # 🎙️ Create temp folder
33
- os.makedirs("audio", exist_ok=True)
 
 
34
 
35
- # 🛰️ FastAPI app
36
- app = FastAPI()
37
- app.mount("/audio", StaticFiles(directory="audio"), name="audio")
38
 
39
- class Msg(BaseModel):
40
- message: str
41
 
42
- @app.post("/ask")
43
- async def ask(request: Request, data: Msg):
44
- user_msg = data.message.strip()
45
- full_prompt = f"<|system|>\n{SYSTEM_PROMPT}\n<|user|>\n{user_msg}\n<|assistant|>"
46
 
47
- response_text = client.text_generation(
48
- prompt=full_prompt,
49
- max_new_tokens=512,
50
- temperature=0.7,
51
- stop_sequences=["<|user|>", "<|end|>"]
52
- ).strip()
53
 
54
- # 🧠 Synthesize TTS
55
- audio_path = f"audio/{uuid.uuid4().hex}.wav"
56
- tts.tts_to_file(text=response_text, file_path=audio_path)
57
 
58
- return JSONResponse({
59
- "text": response_text,
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