Upload main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 # ✅ Flip this to False if you ever want to re-enable NSFW filtering
|
| 10 |
+
|
| 11 |
+
# Initialize the model
|
| 12 |
+
llm = Llama(model_path=MODEL_PATH, n_ctx=2048, verbose=False)
|
| 13 |
+
|
| 14 |
+
# FastAPI setup
|
| 15 |
+
app = FastAPI()
|
| 16 |
+
|
| 17 |
+
# Request body schema
|
| 18 |
+
class PromptRequest(BaseModel):
|
| 19 |
+
prompt: str
|
| 20 |
+
|
| 21 |
+
# Main route
|
| 22 |
+
@app.post("/chat")
|
| 23 |
+
async def chat(request: PromptRequest):
|
| 24 |
+
user_input = request.prompt.strip()
|
| 25 |
+
|
| 26 |
+
# Hate speech check
|
| 27 |
+
if contains_hate(user_input):
|
| 28 |
+
return {"response": "❌ I don't engage in hate or extremist topics."}
|
| 29 |
+
|
| 30 |
+
# Optional: Add NSFW check logic here if needed
|
| 31 |
+
if not ALLOW_NSFW:
|
| 32 |
+
if "nsfw" in user_input.lower() or "sex" in user_input.lower():
|
| 33 |
+
return {"response": "⚠️ NSFW topics are currently disabled."}
|
| 34 |
+
|
| 35 |
+
# Instruction prompt with optional NSFW allowance
|
| 36 |
+
prompt = (
|
| 37 |
+
f"[INST] You are Sol, a smart, honest, and free-talking local AI assistant. "
|
| 38 |
+
f"You answer clearly and do not censor NSFW topics unless they are hateful or extreme. "
|
| 39 |
+
f"Now respond to the following:\n{user_input} [/INST]"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
output = llm(prompt, max_tokens=512, stop=["</s>"])
|
| 43 |
+
response = output["choices"][0]["text"].strip()
|
| 44 |
+
return {"response": response}
|