Spaces:
Build error
Build error
zhenyundeng
commited on
Commit
·
5a5f243
1
Parent(s):
dd15920
update files
Browse files
app.py
CHANGED
|
@@ -10,12 +10,15 @@ import uvicorn
|
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
# ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
| 13 |
import os
|
| 14 |
import torch
|
|
|
|
| 15 |
import numpy as np
|
| 16 |
import requests
|
| 17 |
from rank_bm25 import BM25Okapi
|
| 18 |
from bs4 import BeautifulSoup
|
|
|
|
| 19 |
|
| 20 |
from transformers import BartTokenizer, BartForConditionalGeneration
|
| 21 |
from transformers import BloomTokenizerFast, BloomForCausalLM, BertTokenizer, BertForSequenceClassification
|
|
@@ -39,6 +42,29 @@ os.system("python -m spacy download en_core_web_sm")
|
|
| 39 |
nlp = spacy.load("en_core_web_sm")
|
| 40 |
|
| 41 |
# ---------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# ---------- Load Veracity and Justification prediction model ----------
|
| 43 |
LABEL = [
|
| 44 |
"Supported",
|
|
@@ -330,12 +356,18 @@ def greet_json():
|
|
| 330 |
return {"Hello": "World!"}
|
| 331 |
|
| 332 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
@app.post("/predict/")
|
| 334 |
def fact_checking(item: Item):
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
claim = item.claim
|
| 338 |
-
source = item.source
|
| 339 |
|
| 340 |
# Step1: Evidence Retrieval
|
| 341 |
if source == "Wikipedia":
|
|
@@ -347,9 +379,35 @@ def fact_checking(item: Item):
|
|
| 347 |
verdict_label = veracity_prediction(claim, evidence)
|
| 348 |
justification_label = justification_generation(claim, evidence, verdict_label)
|
| 349 |
|
|
|
|
| 350 |
evidence_list = []
|
| 351 |
for evi in evidence:
|
| 352 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
return {"Verdict": verdict_label, "Justification": justification_label, "Evidence": evidence_list}
|
| 355 |
|
|
|
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
# ---------------------------------------------------------------------------------------------------------------------
|
| 13 |
+
import gradio as gr
|
| 14 |
import os
|
| 15 |
import torch
|
| 16 |
+
import json
|
| 17 |
import numpy as np
|
| 18 |
import requests
|
| 19 |
from rank_bm25 import BM25Okapi
|
| 20 |
from bs4 import BeautifulSoup
|
| 21 |
+
from datetime import datetime
|
| 22 |
|
| 23 |
from transformers import BartTokenizer, BartForConditionalGeneration
|
| 24 |
from transformers import BloomTokenizerFast, BloomForCausalLM, BertTokenizer, BertForSequenceClassification
|
|
|
|
| 42 |
nlp = spacy.load("en_core_web_sm")
|
| 43 |
|
| 44 |
# ---------------------------------------------------------------------------------------------------------------------
|
| 45 |
+
# ---------------------------------------------------------------------------
|
| 46 |
+
# load .env
|
| 47 |
+
from utils import create_user_id
|
| 48 |
+
user_id = create_user_id()
|
| 49 |
+
|
| 50 |
+
from azure.storage.fileshare import ShareServiceClient
|
| 51 |
+
try:
|
| 52 |
+
from dotenv import load_dotenv
|
| 53 |
+
load_dotenv()
|
| 54 |
+
except Exception as e:
|
| 55 |
+
pass
|
| 56 |
+
|
| 57 |
+
account_url = os.environ["AZURE_ACCOUNT_URL"]
|
| 58 |
+
credential = {
|
| 59 |
+
"account_key": os.environ['AZURE_ACCOUNT_KEY'],
|
| 60 |
+
"account_name": os.environ['AZURE_ACCOUNT_NAME']
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
file_share_name = "averitec"
|
| 64 |
+
azure_service = ShareServiceClient(account_url=account_url, credential=credential)
|
| 65 |
+
azure_share_client = azure_service.get_share_client(file_share_name)
|
| 66 |
+
|
| 67 |
+
# ---------- Setting ----------
|
| 68 |
# ---------- Load Veracity and Justification prediction model ----------
|
| 69 |
LABEL = [
|
| 70 |
"Supported",
|
|
|
|
| 356 |
return {"Hello": "World!"}
|
| 357 |
|
| 358 |
|
| 359 |
+
def log_on_azure(file, logs, azure_share_client):
|
| 360 |
+
logs = json.dumps(logs)
|
| 361 |
+
file_client = azure_share_client.get_file_client(file)
|
| 362 |
+
file_client.upload_file(logs)
|
| 363 |
+
|
| 364 |
+
|
| 365 |
@app.post("/predict/")
|
| 366 |
def fact_checking(item: Item):
|
| 367 |
+
claim = item['claim']
|
| 368 |
+
source = item['source']
|
| 369 |
+
# claim = item.claim
|
| 370 |
+
# source = item.source
|
| 371 |
|
| 372 |
# Step1: Evidence Retrieval
|
| 373 |
if source == "Wikipedia":
|
|
|
|
| 379 |
verdict_label = veracity_prediction(claim, evidence)
|
| 380 |
justification_label = justification_generation(claim, evidence, verdict_label)
|
| 381 |
|
| 382 |
+
############################################################
|
| 383 |
evidence_list = []
|
| 384 |
for evi in evidence:
|
| 385 |
+
title_str = evi.metadata['title']
|
| 386 |
+
evi_str = evi.metadata['evidence']
|
| 387 |
+
url_str = evi.metadata['url']
|
| 388 |
+
evidence_list.append([title_str, evi_str, url_str])
|
| 389 |
+
|
| 390 |
+
try:
|
| 391 |
+
# Log answer on Azure Blob Storage
|
| 392 |
+
# IF AZURE_ISSAVE=TRUE, save the logs into the Azure share client.
|
| 393 |
+
if os.environ["AZURE_ISSAVE"] == "TRUE":
|
| 394 |
+
timestamp = str(datetime.now().timestamp())
|
| 395 |
+
# timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 396 |
+
file = timestamp + ".json"
|
| 397 |
+
logs = {
|
| 398 |
+
"user_id": str(user_id),
|
| 399 |
+
"claim": claim,
|
| 400 |
+
"sources": source,
|
| 401 |
+
"evidence": evidence_list,
|
| 402 |
+
"answer": [verdict_label, justification_label],
|
| 403 |
+
"time": timestamp,
|
| 404 |
+
}
|
| 405 |
+
log_on_azure(file, logs, azure_share_client)
|
| 406 |
+
except Exception as e:
|
| 407 |
+
print(f"Error logging on Azure Blob Storage: {e}")
|
| 408 |
+
raise gr.Error(
|
| 409 |
+
f"AVeriTeC Error: {str(e)[:100]} - The error has been noted, try another question and if the error remains, you can contact us :)")
|
| 410 |
+
##########
|
| 411 |
|
| 412 |
return {"Verdict": verdict_label, "Justification": justification_label, "Evidence": evidence_list}
|
| 413 |
|
utils.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import random
|
| 3 |
+
import string
|
| 4 |
+
import uuid
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def create_user_id():
|
| 9 |
+
"""Create user_id
|
| 10 |
+
str: String to id user
|
| 11 |
+
"""
|
| 12 |
+
current_date = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
|
| 13 |
+
user_id = str(uuid.uuid4())
|
| 14 |
+
return current_date + '_' +user_id
|