File size: 2,294 Bytes
b3fc169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201298f
 
a95a22c
 
 
 
b3fc169
 
 
 
66ab975
2bb5404
b3fc169
 
 
 
 
 
 
 
 
 
 
 
 
6378ab1
b3fc169
66ab975
 
b3fc169
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import requests
import sys
import spacy
from io import BytesIO
import pandas as pd
import json

# Load your custom NER model
nlp_ner = spacy.load("ner_model_v1")

# def send_results_to_api(data, result_url):

#     headers = {"Content-Type": "application/json"}
#     response = requests.post(result_url, json=data, headers=headers)
#     if response.status_code == 200:
#         return response.json()  # Return any response from the API if needed
#     else:
#         return {"error": f"Failed to send results to API: {response.status_code}"}

def process_xlsx(params):
    # xlsx_file = 'https://fragilestatesindex.org/wp-content/uploads/2023/06/FSI-2023-DOWNLOAD.xlsx'
    try:
        params = json.loads(params)
    except json.JSONDecodeError as e:
        return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
    print(f"JSON : \n{params}")
    addresses = params.get("urls", [])
    if not params.get("normalfileID",[]):
        file_ids = [None]*len(addresses)
    else:
        file_ids = params.get("normalfileID",[])
    # api = params.get("api", "")
    # job_id = params.get("job_id", "")

    solutions=[]
    text_id = 1
    for adress,file_id in zip(addresses, file_ids):
        doc = nlp_ner(adress)

        # Initialize the dictionary to store the results
        entities_dict = {}

        # Extract entities and their indices
        for idx, ent in enumerate(doc.ents):
            if ent.label_ not in entities_dict:
                entities_dict[ent.label_] = []
            entities_dict[ent.label_].append({'word': ent.text, 'index': idx})

        # Create the final output dictionary
        # 
        obj = {"text": adress, "answer":entities_dict, "qcUser": None, "normalfileID": file_id}
        solutions.append(obj)
        print(f"{text_id}/{len(addresses)}\n")
        text_id = text_id+1
        
    # result_url = f"{api}/{job_id}"
    # send_results_to_api(solutions, result_url)

    return json.dumps({"solutions": solutions})


import gradio as gr
inputt = gr.Textbox(label="Parameters (JSON format) Eg. {'texts':['file1.mp3','file2.wav']}")
outputs = gr.JSON()

application = gr.Interface(fn=process_xlsx, inputs=inputt, outputs=outputs, title="Named Entity Recognition with API Integration")
application.launch()