Browse Source

第一次提交

master
majiahui@haimaqingfan.com 1 year ago
commit
6563c0ee12
  1. 8
      .idea/.gitignore
  2. 181
      chatgpt_detector_model_predict.py
  3. 110
      flask_api.py
  4. 75
      flask_chatgpt-detector_predict_redis_search.py
  5. 1
      run_app.sh
  6. 1
      run_app_search.sh
  7. 1
      run_model.sh

8
.idea/.gitignore

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

181
chatgpt_detector_model_predict.py

File diff suppressed because one or more lines are too long

110
flask_api.py

@ -0,0 +1,110 @@
# coding:utf-8
import os
import pandas as pd
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import torch
from transformers import (
AutoModelForSequenceClassification, AutoTokenizer, DataCollatorWithPadding,
Trainer, TrainingArguments
)
from flask import Flask, jsonify
from flask import request
import uuid
app = Flask(__name__)
app.config["JSON_AS_ASCII"] = False
from threading import Thread
import redis
import uuid
import time
import json
import docx2txt
pool = redis.ConnectionPool(host='localhost', port=63179, max_connections=100, db=13, password="zhicheng123*")
redis_ = redis.Redis(connection_pool=pool, decode_responses=True)
db_key_query = 'query'
db_key_querying = 'querying'
db_key_queryset = 'queryset'
batch_size = 32
def ulit_request_file(file):
file_name = file.filename
file_name_save = "data/request/{}".format(file_name)
file.save(file_name_save)
if file_name.split(".")[-1] == "txt":
try:
with open(file_name_save, encoding="gbk") as f:
content = f.read()
except:
with open(file_name_save, encoding="utf-8") as f:
content = f.read()
# elif file_name.split(".")[-1] == "docx":
# content = docx2txt.process(file_name_save)
content_list = [i.strip("\n") for i in content.split("")]
print(content_list)
return content_list
@app.route("/predict", methods=["POST"])
def handle_query_predict():
print(request.remote_addr)
# request.form.get('prompt')
dataBases = ""
minSimilarity = "" # txt
minWords = ""
title = request.form.get("title")
author = request.form.get("author") # txt
file = request.files.get('file')
token = ""
account = ""
goodsId = ""
callbackUrl = ""
content_list = ulit_request_file(file)
id_ = str(uuid.uuid1()) # 为query生成唯一标识
id_ = id_.upper()
print("uuid: ", id_)
print(id_)
d = {
'id': id_,
'dataBases': dataBases,
'minSimilarity': minSimilarity,
'minWords': minWords,
'title': title,
'author': author,
'content_list': content_list,
'token': token,
'account': account,
'goodsId': goodsId,
'callbackUrl': callbackUrl
}
print(d)
# 绑定文本和query id
# recall_10(id_, title, abst_zh, content)
load_request_path = './request_data_logs/{}.json'.format(id_)
with open(load_request_path, 'w', encoding='utf8') as f2: # ensure_ascii=False才能输入中文,否则是Unicode字符 indent=2 JSON数据的缩进,美观
json.dump(d, f2, ensure_ascii=False, indent=4)
redis_.rpush(db_key_query, json.dumps({"id": id_, "path": load_request_path})) # 加入redis
return_text = {
'code': 0,
'msg': "请求成功",
'data': {
'balances': "",
'orderId': id_,
'consumeNum': ""
}
}
return jsonify(return_text) # 返回结果
if __name__ == "__main__":
app.run(host="0.0.0.0", port=16005, threaded=True, debug=False)

75
flask_chatgpt-detector_predict_redis_search.py

@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
"""
@Time : 2023/3/2 19:31
@Author :
@FileName:
@Software:
@Describe:
"""
#
# import redis
#
# redis_pool = redis.ConnectionPool(host='127.0.0.1', port=6379, password='', db=0)
# redis_conn = redis.Redis(connection_pool=redis_pool)
#
#
# name_dict = {
# 'name_4' : 'Zarten_4',
# 'name_5' : 'Zarten_5'
# }
# redis_conn.mset(name_dict)
import flask
import redis
import uuid
import json
from threading import Thread
import time
app = flask.Flask(__name__)
pool = redis.ConnectionPool(host='localhost', port=63179, max_connections=100, db=13, password="zhicheng123*")
redis_ = redis.Redis(connection_pool=pool, decode_responses=True)
@app.route("/search", methods=["POST"])
def handle_query():
# try:
id_ = flask.request.json['orderid'] # 获取用户query中的文本
result = redis_.get(id_) # 获取该query的模型结果
if result is not None:
# redis_.delete(id_)
result_path = result.decode('UTF-8')
with open(result_path, encoding='utf8') as f1:
# 加载文件的对象
result_dict = json.load(f1)
resilt = result_dict["resilt"]
result_text = {'status': 9,
'resilt': resilt,
'reportId': "",
'downloadurl': "",
'similarity': ""
}
else:
result_text = {'status': 1,
'resilt': "",
'reportId': "",
'downloadurl': "",
'similarity': ""
}
result = {'code':0,
"msg": "请求成功",
"data": result_text}
# except:
# result = {'code':1,
# "msg": "请求失败"
# }
return flask.jsonify(result) # 返回结果
if __name__ == "__main__":
app.run(debug=False, host='0.0.0.0', port=16006)

1
run_app.sh

@ -0,0 +1 @@
nohup python flask_api.py > flask_api.myout.file 2>&1 &

1
run_app_search.sh

@ -0,0 +1 @@
nohup python flask_chatgpt-detector_predict_redis_search.py > flask_chatgpt-detector_predict_redis.myout.file 2>&1 &

1
run_model.sh

@ -0,0 +1 @@
nohup python chatgpt_detector_model_predict.py > chatgpt_detector_model_predict.myout.file 2>&1 &
Loading…
Cancel
Save