File size: 1,460 Bytes
3d1b429
d2e7f5e
3d1b429
d2e7f5e
 
 
3d1b429
d2e7f5e
 
 
3d1b429
 
d8b8e0f
 
3d1b429
d2e7f5e
d8b8e0f
3d1b429
d8b8e0f
 
 
 
 
 
d2e7f5e
3d1b429
d8b8e0f
d2e7f5e
3d1b429
d2e7f5e
d8b8e0f
d2e7f5e
d8b8e0f
d2e7f5e
d8b8e0f
 
d2e7f5e
 
3d1b429
d8b8e0f
 
3d1b429
d8b8e0f
 
 
 
 
 
 
3d1b429
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
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from pydantic import BaseModel  # ✅ FIX: Use BaseModel not model
from app.model_loader import load_model
import torch

# Initialize app and model
app = FastAPI()
model, tokenizer = load_model()

# ✅ Define request body schema using Pydantic
class InputText(BaseModel):
    input: str

# 🚀 Inference endpoint
@app.post("/predict")
async def predict(input_text: InputText):
    # Create prompt
    prompt = (
        "You are a neuroscience research assistant.\n"
        "Determine if the following abstract was modified by an AI model.\n"
        "Respond with 'yes' if it was modified, or 'no' if it is original.\n\n"
        f"Abstract:\n{input_text.input}\n\nAnswer:"
    )

    # Tokenize
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)

    # Generate
    with torch.no_grad():
        output = model.generate(
            **inputs,
            max_new_tokens=50,
            do_sample=False,
            temperature=0.3,
            pad_token_id=tokenizer.eos_token_id,
        )

    # Decode and extract answer
    decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)
    response_text = decoded_output[len(prompt):].strip().lower()

    if "yes" in response_text:
        answer = "yes"
    elif "no" in response_text:
        answer = "no"
    else:
        answer = "unknown"

    return {"output": response_text, "answer": answer}