topic-clustering-global-dashboard / service_review_diemtin.py
cuongnguyen910's picture
Upload folder using huggingface_hub
5120311 verified
from entity import InputCheckReview
from fastapi import FastAPI
import time
import hashlib
import json
from fastapi.middleware.cors import CORSMiddleware
import os
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def get_hash_id(item: InputCheckReview):
str_hash = ""
if item.id_topic:
str_hash += item.id_topic
str_hash += item.start_time
return hashlib.sha224(str_hash.encode("utf-8")).hexdigest()
else:
return ""
@app.post("/newsanalysis/review/topic_clustering")
async def topic_clustering(item: InputCheckReview):
hash_str = get_hash_id(item)
print("item.id_topic: ", item.id_topic)
print("item.start_time: ", item.start_time)
print("item.end_time: ", item.end_time)
print(hash_str)
# if len(docs) > 200:
results = {"status": 1, "res": {}}
if hash_str and os.path.exists("log/result_{0}.txt".format(hash_str)):
path_res = "log/result_{0}.txt".format(hash_str)
with open(path_res, encoding="utf-8") as ff:
res_clus = json.load(ff)
return {"status": 1, "res": res_clus}
return results
@app.post("/newsanalysis/update/topic_clustering")
async def topic_clustering(item: InputCheckReview):
hash_str = get_hash_id(item)
print("item.id_topic: ", item.id_topic)
print("item.start_time: ", item.start_time)
print("item.end_time: ", item.end_time)
print(hash_str)
# if len(docs) > 200:
results = {"status": 0}
if hash_str and os.path.exists("log/result_{0}.txt".format(hash_str)):
path_res = "log/result_{0}.txt".format(hash_str)
with open(path_res, "w+", encoding="utf-8") as ff:
ff.write(json.dumps(item.data))
return {"status": 1}
return results