Spaces:
Sleeping
Sleeping
pydantic
Browse files- app/main.py +12 -40
app/main.py
CHANGED
|
@@ -1,51 +1,21 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi import FastAPI, Request, Form
|
| 3 |
from fastapi.responses import JSONResponse
|
|
|
|
| 4 |
from app.model_loader import load_model
|
| 5 |
import torch
|
| 6 |
|
|
|
|
| 7 |
app = FastAPI()
|
| 8 |
model, tokenizer = load_model()
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
# data = await request.json()
|
| 13 |
-
# input_text = data.get("input", "")
|
| 14 |
-
# inputs = tokenizer(input_text, return_tensors="pt")
|
| 15 |
-
# with torch.no_grad():
|
| 16 |
-
# output = model.generate(
|
| 17 |
-
# **inputs,
|
| 18 |
-
# max_new_tokens=60,
|
| 19 |
-
# do_sample=False,
|
| 20 |
-
# temperature=0.3
|
| 21 |
-
# )
|
| 22 |
-
# response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 23 |
-
# return JSONResponse(content={"output": response})
|
| 24 |
-
|
| 25 |
-
# @app.post("/predict")
|
| 26 |
-
# async def predict(request: Request):
|
| 27 |
-
# data = await request.json()
|
| 28 |
-
# input_text = data.get("input", "")
|
| 29 |
-
|
| 30 |
-
# inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
|
| 31 |
-
|
| 32 |
-
# with torch.no_grad():
|
| 33 |
-
# outputs = model.generate(
|
| 34 |
-
# **inputs,
|
| 35 |
-
# max_new_tokens=120,
|
| 36 |
-
# do_sample=False,
|
| 37 |
-
# temperature=0.3
|
| 38 |
-
# )
|
| 39 |
-
|
| 40 |
-
# response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 41 |
-
# return JSONResponse(content={"output": response})
|
| 42 |
-
|
| 43 |
-
class InputText(model):
|
| 44 |
input: str
|
| 45 |
|
|
|
|
| 46 |
@app.post("/predict")
|
| 47 |
async def predict(input_text: InputText):
|
| 48 |
-
#
|
| 49 |
prompt = (
|
| 50 |
"You are a neuroscience research assistant.\n"
|
| 51 |
"Determine if the following abstract was modified by an AI model.\n"
|
|
@@ -53,8 +23,10 @@ async def predict(input_text: InputText):
|
|
| 53 |
f"Abstract:\n{input_text.input}\n\nAnswer:"
|
| 54 |
)
|
| 55 |
|
|
|
|
| 56 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 57 |
|
|
|
|
| 58 |
with torch.no_grad():
|
| 59 |
output = model.generate(
|
| 60 |
**inputs,
|
|
@@ -64,10 +36,10 @@ async def predict(input_text: InputText):
|
|
| 64 |
pad_token_id=tokenizer.eos_token_id,
|
| 65 |
)
|
| 66 |
|
|
|
|
| 67 |
decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 68 |
-
|
| 69 |
-
# Extract only the model's answer
|
| 70 |
response_text = decoded_output[len(prompt):].strip().lower()
|
|
|
|
| 71 |
if "yes" in response_text:
|
| 72 |
answer = "yes"
|
| 73 |
elif "no" in response_text:
|
|
@@ -75,4 +47,4 @@ async def predict(input_text: InputText):
|
|
| 75 |
else:
|
| 76 |
answer = "unknown"
|
| 77 |
|
| 78 |
-
return {"output": response_text, "answer": answer}
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
|
|
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
+
from pydantic import BaseModel # ✅ FIX: Use BaseModel not model
|
| 4 |
from app.model_loader import load_model
|
| 5 |
import torch
|
| 6 |
|
| 7 |
+
# Initialize app and model
|
| 8 |
app = FastAPI()
|
| 9 |
model, tokenizer = load_model()
|
| 10 |
|
| 11 |
+
# ✅ Define request body schema using Pydantic
|
| 12 |
+
class InputText(BaseModel):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
input: str
|
| 14 |
|
| 15 |
+
# 🚀 Inference endpoint
|
| 16 |
@app.post("/predict")
|
| 17 |
async def predict(input_text: InputText):
|
| 18 |
+
# Create prompt
|
| 19 |
prompt = (
|
| 20 |
"You are a neuroscience research assistant.\n"
|
| 21 |
"Determine if the following abstract was modified by an AI model.\n"
|
|
|
|
| 23 |
f"Abstract:\n{input_text.input}\n\nAnswer:"
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# Tokenize
|
| 27 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 28 |
|
| 29 |
+
# Generate
|
| 30 |
with torch.no_grad():
|
| 31 |
output = model.generate(
|
| 32 |
**inputs,
|
|
|
|
| 36 |
pad_token_id=tokenizer.eos_token_id,
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# Decode and extract answer
|
| 40 |
decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)
|
|
|
|
|
|
|
| 41 |
response_text = decoded_output[len(prompt):].strip().lower()
|
| 42 |
+
|
| 43 |
if "yes" in response_text:
|
| 44 |
answer = "yes"
|
| 45 |
elif "no" in response_text:
|
|
|
|
| 47 |
else:
|
| 48 |
answer = "unknown"
|
| 49 |
|
| 50 |
+
return {"output": response_text, "answer": answer}
|