CoolFace
Apppublic

D3V1L1810/Multi_Text_Classification

sourceHugging Faceupdated 2y agoView on Hugging Face
1likes
app.py81 linesDownload Raw Back to root
1import tensorflow as tf2from transformers import BertTokenizer, TFBertForSequenceClassification3import numpy as np4import json5import requests6import gradio as gr7import logging8 9# Initialize the tokenizer and model10bert_tokenizer = BertTokenizer.from_pretrained('MultiTokenizer_ep10')11bert_model = TFBertForSequenceClassification.from_pretrained('MultiModel_ep10')12 13# Function to send results to API14# def send_results_to_api(data, result_url):15#     headers = {'Content-Type':'application/json'}16#     response = requests.post(result_url, json = data, headers=headers)17#     if response.status_code == 200:18#         return response.json19#     else:20#         return {'error':f"failed to send result to API: {response.status_code}"}21 22def predict_text(params):23    try:24        params = json.loads(params)25    except json.JSONDecodeError as e:26        logging.error(f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}")27        return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}28        29    texts = params.get("urls", [])30    if not params.get("normalfileID", []):31        file_ids = [None] * len(texts)32    else:33        file_ids = params.get("normalfileID", [])34 35    if not texts:36        return {"error": "Missing required parameters: 'texts'"}37 38    solutions = []39    confidence_threshold = 0.85  # Define your confidence threshold40 41    for text, file_id in zip(texts, file_ids):42        encoding = bert_tokenizer.encode_plus(43            text,44            add_special_tokens=True,45            max_length=128,46            return_token_type_ids=True,47            padding='max_length',48            truncation=True,49            return_attention_mask=True,50            return_tensors='tf'51        )52        input_ids = encoding['input_ids']53        token_type_ids = encoding['token_type_ids']54        attention_mask = encoding['attention_mask']55 56        pred = bert_model.predict([input_ids, token_type_ids, attention_mask])57        logits = pred.logits58        softmax_scores = tf.nn.softmax(logits, axis=1).numpy()[0]59        pred_label = tf.argmax(logits, axis=1).numpy()[0]60 61        # Get the confidence score for the predicted label62        confidence = softmax_scores[pred_label]63        print(confidence)64        # If confidence is below the threshold, set answer to None65        if confidence < confidence_threshold:66            pred_label = 7  # Set to 'None' class67 68        label = {0: 'BUSINESS', 1: 'COMEDY', 2: 'CRIME', 3: 'FOOD & DRINK', 4: 'POLITICS', 5: 'SPORTS', 6: 'TRAVEL', 7: 'None'}69        result = {'text': text, 'answer': [label[pred_label]], "qcUser": None, "normalfileID": file_id}70        solutions.append(result)71 72    # result_url = f"{api}/{job_id}"73    # send_results_to_api(solutions, result_url)74    return json.dumps({"solutions": solutions})75 76inputt = gr.Textbox(label="Parameters in Json Format... Eg. {'texts':['text1', 'text2']}")77outputt = gr.JSON()78 79application = gr.Interface(fn=predict_text, inputs=inputt, outputs=outputt, title='Multi Text Classification with API Integration..')80application.launch()81