|
from entity import Docs, Cluster
|
|
from fastapi import FastAPI
|
|
import time
|
|
import json
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from function import topic_clustering as tc
|
|
|
|
|
|
app = FastAPI()
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
|
|
@app.post("/newsanalysis/topic_clustering")
|
|
def topic_clustering(item: Docs):
|
|
docs = item.response["docs"]
|
|
|
|
hash_id = str(time.time())
|
|
top_cluster = item.top_cluster
|
|
top_sentence = item.top_sentence
|
|
topn_summary = item.topn_summary
|
|
with open("log/input_{0}.txt".format(hash_id), "w+") as f:
|
|
f.write(json.dumps({"docs": docs, "key": item.keyword}))
|
|
st_time = time.time()
|
|
print("start ")
|
|
print("len doc: ", len(docs))
|
|
threshold = 0.1
|
|
results = tc.topic_clustering(docs, threshold, top_cluster=top_cluster, top_sentence=top_sentence,
|
|
topn_summary=topn_summary)
|
|
with open("log/result_{0}.txt".format(hash_id), "w+") as f:
|
|
f.write(json.dumps(results))
|
|
print("time analysis: ", time.time() - st_time)
|
|
return results
|
|
|
|
|