Spaces:
Sleeping
Sleeping
predict
Browse files- app/main.py +46 -9
app/main.py
CHANGED
|
@@ -22,20 +22,57 @@ model, tokenizer = load_model()
|
|
| 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(
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
inputs = tokenizer(
|
| 31 |
|
| 32 |
with torch.no_grad():
|
| 33 |
-
|
| 34 |
**inputs,
|
| 35 |
-
max_new_tokens=
|
| 36 |
do_sample=False,
|
| 37 |
-
temperature=0.3
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Use a structured, instruction-style prompt
|
| 49 |
+
prompt = (
|
| 50 |
+
"You are a neuroscience research assistant.\n"
|
| 51 |
+
"Determine if the following abstract was modified by an AI model.\n"
|
| 52 |
+
"Respond with 'yes' if it was modified, or 'no' if it is original.\n\n"
|
| 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,
|
| 61 |
+
max_new_tokens=50,
|
| 62 |
do_sample=False,
|
| 63 |
+
temperature=0.3,
|
| 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:
|
| 74 |
+
answer = "no"
|
| 75 |
+
else:
|
| 76 |
+
answer = "unknown"
|
| 77 |
+
|
| 78 |
+
return {"output": response_text, "answer": answer}
|