Team Ai
Apppublic

kkpathak91/Mtech_Project_Image_processing_and_Fact_verification

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
app.py136 linesDownload Raw Back to root
1 2 3    ##########################################################################################################4 5import os6import gradio as gr7from huggingface_hub import snapshot_download8from prettytable import PrettyTable9import pandas as pd10import torch11import traceback12 13config = {14    "model_type": "roberta",15    "model_name_or_path": "roberta-large",16    "logic_lambda": 0.5,17    "prior": "random",18    "mask_rate": 0.0,19    "cand_k": 1,20    "max_seq1_length": 256,21    "max_seq2_length": 128,22    "max_num_questions": 8,23    "do_lower_case": False,24    "seed": 42,25    "n_gpu": torch.cuda.device_count(),26}27 28os.system('git clone https://github.com/kkpathak91/project_metch/')29os.system('rm -r project_metch/data/')30os.system('rm -r project_metch/results/')31os.system('rm -r project_metch/models/')32os.system('mv project_metch/* ./')33 34model_dir = snapshot_download('kkpathak91/FVM')35config['fc_dir'] = os.path.join(model_dir, 'fact_checking/roberta-large/')36config['mrc_dir'] = os.path.join(model_dir, 'mrc_seq2seq/bart-base/')37config['er_dir'] = os.path.join(model_dir, 'evidence_retrieval/')38 39 40from src.loren import Loren41 42 43loren = Loren(config, verbose=False)44try:45    js = loren.check('Donald Trump won the 2020 U.S. presidential election.')46except Exception as e:47    raise ValueError(e)48 49 50def highlight_phrase(text, phrase):51    text = loren.fc_client.tokenizer.clean_up_tokenization(text)52    return text.replace('<mask>', f'<i><b>{phrase}</b></i>')53 54 55def highlight_entity(text, entity):56    return text.replace(entity, f'<i><b>{entity}</b></i>')57 58 59def gradio_formatter(js, output_type):60    zebra_css = '''61    tr:nth-child(even) {62        background: #f1f1f1;63    }64    thead{65        background: #f1f1f1;66    }'''67    if output_type == 'e':68        data = {'Evidence': [highlight_entity(x, e) for x, e in zip(js['evidence'], js['entities'])]}69    elif output_type == 'z':70        p_sup, p_ref, p_nei = [], [], []71        for x in js['phrase_veracity']:72            max_idx = torch.argmax(torch.tensor(x)).tolist()73            x = ['%.4f' % xx for xx in x]74            x[max_idx] = f'<i><b>{x[max_idx]}</b></i>'75            p_sup.append(x[2])76            p_ref.append(x[0])77            p_nei.append(x[1])78 79        data = {80            'Claim Phrase': js['claim_phrases'],81            'Local Premise': [highlight_phrase(q, x[0]) for q, x in zip(js['cloze_qs'], js['evidential'])],82            'p_SUP': p_sup,83            'p_REF': p_ref,84            'p_NEI': p_nei,85        }86    else:87        raise NotImplementedError88    data = pd.DataFrame(data)89    pt = PrettyTable(field_names=list(data.columns), 90        align='l', border=True, hrules=1, vrules=1)91    for v in data.values:92        pt.add_row(v)93    html = pt.get_html_string(attributes={94        'style': 'border-width: 2px; bordercolor: black'95    }, format=True)96    html = f'<head> <style type="text/css"> {zebra_css} </style> </head>\n' + html97    html = html.replace('&lt;', '<').replace('&gt;', '>')98    return html99 100 101def run(claim):102    try:103        js = loren.check(claim)104    except Exception as error_msg:105        exc = traceback.format_exc()106        msg = f'[Error]: {error_msg}.\n[Traceback]: {exc}'107        loren.logger.error(claim)108        loren.logger.error(msg)109        return 'Oops, something went wrong.', '', ''110    label = js['claim_veracity']111    loren.logger.warning(label + str(js))112    ev_html = gradio_formatter(js, 'e')113    z_html = gradio_formatter(js, 'z')114    return label, z_html, ev_html115 116 117iface = gr.Interface(118    fn=run,119    inputs="text",120    outputs=[121        'text',122        'html',123        'html',124    ],125    examples=['Kanpur is a city in Nepal',126              'PV Sindhu is an Indian Badminton Player.'],127    title="A Framework for Data-Driven Document Evaluation and Scoring",128    layout='horizontal',129    description="[Student Name: Karan Kumar Pathak] " " [Roll No.: 2020fc04334] ",130    flagging_dir='results/flagged/',131    allow_flagging=True,132    flagging_options=['Interesting!', 'Error: Claim Phrase Parsing', 'Error: Local Premise',133                      'Error: Require Commonsense', 'Error: Evidence Retrieval'],134    enable_queue=True135)136iface.launch()