Spaces:
Runtime error
Runtime error
File size: 2,067 Bytes
a52d098 2c1554d a52d098 38697c4 a52d098 38697c4 a52d098 38697c4 a52d098 38697c4 a52d098 38697c4 a52d098 38697c4 a52d098 38697c4 a52d098 38697c4 | 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 | 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}"}
addresses = params.get("texts", [])
# api = params.get("api", "")
# job_id = params.get("job_id", "")
solutions=[]
text_id = 0
for adress in addresses:
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
#
output = {'text_id': text_id, 'answer': entities_dict}
obj = {"text": adress, "answer":output}
solutions.append(obj)
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() |