Fafnir666 commited on
Commit
396a6e9
·
verified ·
1 Parent(s): 034dfe8

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +66 -0
main.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from pydantic import BaseModel
3
+ from llama_cpp import Llama
4
+ from moderator import contains_hate
5
+ import os
6
+
7
+ # Configuration
8
+ MODEL_PATH = os.path.join("models", "mistral-7b-instruct.Q4_K_M.gguf")
9
+ ALLOW_NSFW = True
10
+
11
+ # Memory for last blocked prompt (to support override)
12
+ last_blocked_prompt = None
13
+ last_blocked_type = None
14
+
15
+ # Initialize the model
16
+ llm = Llama(model_path=MODEL_PATH, n_ctx=2048, verbose=False)
17
+
18
+ # FastAPI setup
19
+ app = FastAPI()
20
+
21
+ class PromptRequest(BaseModel):
22
+ prompt: str
23
+
24
+ @app.post("/chat")
25
+ async def chat(request: PromptRequest):
26
+ global last_blocked_prompt, last_blocked_type
27
+
28
+ user_input = request.prompt.strip().lower()
29
+
30
+ # Handle override if last block was not hate-related
31
+ if user_input == "override":
32
+ if last_blocked_prompt and last_blocked_type != "hate":
33
+ prompt = (
34
+ f"[INST] You are Sol, a smart, honest, and open local AI assistant. "
35
+ f"NSFW content like nudity is okay. Hate or extremist content is not allowed. "
36
+ f"Respond to the following:
37
+ {last_blocked_prompt} [/INST]"
38
+ )
39
+ output = llm(prompt, max_tokens=512, stop=["</s>"])
40
+ response = output["choices"][0]["text"].strip()
41
+ return {"response": response}
42
+ else:
43
+ return {"response": "❌ Override is not allowed for hate or extremist content."}
44
+
45
+ # Check hate filter only (NSFW is allowed)
46
+ if contains_hate(user_input):
47
+ last_blocked_prompt = None
48
+ last_blocked_type = "hate"
49
+ return {"response": "❌ I don't engage in hate or extremist topics."}
50
+
51
+ # If passed, save input in case override is needed later
52
+ last_blocked_prompt = request.prompt.strip()
53
+ last_blocked_type = "other" # could later separate for NSFW etc
54
+
55
+ # Build prompt and generate
56
+ prompt = (
57
+ f"[INST] You are Sol, a smart, honest, and open local AI assistant. "
58
+ f"You are allowed to discuss NSFW topics such as nudity, sex, and adult scenarios. "
59
+ f"You do not tolerate or respond to hate or extremist content. "
60
+ f"Respond to the following:
61
+ {user_input} [/INST]"
62
+ )
63
+
64
+ output = llm(prompt, max_tokens=512, stop=["</s>"])
65
+ response = output["choices"][0]["text"].strip()
66
+ return {"response": response}