Spaces:
showme
/
Build error

File size: 1,403 Bytes
5e1e3c6
 
9b27b5d
 
142d567
5e1e3c6
 
 
9b27b5d
 
 
142d567
5e1e3c6
 
 
142d567
5e1e3c6
 
 
9b27b5d
 
 
 
 
 
8c30a62
9b27b5d
fb7b381
9b27b5d
 
fb7b381
9b27b5d
 
 
fb7b381
 
9b27b5d
fb7b381
9b27b5d
 
 
83ac076
5e1e3c6
05ce864
229bd35
05ce864
 
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
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

# 创建 FastAPI 实例
app = FastAPI()

# 加载预训练模型和分词器
tokenizer = AutoTokenizer.from_pretrained("openai-community/roberta-base-openai-detector")
model = AutoModelForSequenceClassification.from_pretrained("openai-community/roberta-base-openai-detector")

# 定义请求体的格式
class TextRequest(BaseModel):
    text: str

# 定义一个 POST 请求处理函数
@app.post("/predict")
async def predict(request: TextRequest):
    # 对输入文本进行分词
    inputs = tokenizer(request.text, return_tensors="pt", truncation=True, max_length=512)
    
    # 获取模型预测结果
    with torch.no_grad():
        outputs = model(**inputs)
        print("模型原始输出:", outputs )
        scores = torch.softmax(outputs.logits, dim=1)
    
    # 获取预测结果
    predictions = scores.tolist()[0]
    fake_prob = predictions[0]  # AI生成的概率
    
    # 构建返回结果
    result = [{
        "label": "AI" if fake_prob > 0.5 else "Human",
        "score": fake_prob  # 直接返回AI的概率
    }]

    
    # 打印结果用于调试
    print("预测结果:", result)
    
    return {"result": result}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)