File size: 1,936 Bytes
61224b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
"""AniSol.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1DiHMlxQx3QEAYivNMOl3olC8jj9mutCZ
"""

from fastapi import FastAPI, Request
from pydantic import BaseModel
from transformers import AutoTokenizer, AutoModelForCausalLM
from fastapi.middleware.cors import CORSMiddleware
import torch

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

model_name = "nellaep/AniSolSenseiModel"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
model.eval()

sensitive_keywords = [
    "suicide", "kill myself", "end my life", "self harm", "cutting",
    "i want to die", "i want to disappear", "hurt myself", "life isn’t worth it",
    "i can’t take it anymore", "no reason to live", "i hate living", "die", "i give up"
]

hotline_message = (
    "Your life is valuable twin. If you're feeling overwhelmed, please reach out for help.\n"
    "Call or text 988 (U.S. Suicide & Crisis Lifeline) for free, 24/7 support.\n"
)

class InputText(BaseModel):
    input: str

@app.post("/generate")
async def generate_response(data: InputText):
    user_input = data.input.lower()

    if any(keyword in user_input for keyword in sensitive_keywords):
        return {
            "response": hotline_message
        }

    prompt = f"Input: {data.input}\nSensei: Kakashi\nOutput:"
    inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
    with torch.no_grad():
        output = model.generate(
            **inputs,
            max_new_tokens=100,
            pad_token_id=tokenizer.eos_token_id
        )

    decoded = tokenizer.decode(output[0], skip_special_tokens=True)
    response = decoded.split("Output:")[-1].strip()

    return {"response": response}