janasumit2911 commited on
Commit
a52d098
·
verified ·
1 Parent(s): 5be1a79

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import sys
3
+ import spacy
4
+ from io import BytesIO
5
+ import pandas as pd
6
+ import json
7
+
8
+ # Load your custom NER model
9
+ nlp_ner = spacy.load("D:/Databae/Saved Models/FINAL MODELS/NER/model-best")
10
+
11
+ def send_results_to_api(data, result_url):
12
+
13
+ headers = {"Content-Type": "application/json"}
14
+ response = requests.post(result_url, json=data, headers=headers)
15
+ if response.status_code == 200:
16
+ return response.json() # Return any response from the API if needed
17
+ else:
18
+ return {"error": f"Failed to send results to API: {response.status_code}"}
19
+
20
+ def process_xlsx(params):
21
+ # xlsx_file = 'https://fragilestatesindex.org/wp-content/uploads/2023/06/FSI-2023-DOWNLOAD.xlsx'
22
+ try:
23
+ params = json.loads(params)
24
+ except json.JSONDecodeError as e:
25
+ return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
26
+
27
+ addresses = params.get("audio_files", [])
28
+ api = params.get("api", "")
29
+ job_id = params.get("job_id", "")
30
+
31
+ solutions=[]
32
+ text_id = 0
33
+ for adress in addresses:
34
+ doc = nlp_ner(adress)
35
+
36
+ # Initialize the dictionary to store the results
37
+ entities_dict = {}
38
+
39
+ # Extract entities and their indices
40
+ for idx, ent in enumerate(doc.ents):
41
+ if ent.label_ not in entities_dict:
42
+ entities_dict[ent.label_] = []
43
+ entities_dict[ent.label_].append({'word': ent.text, 'index': idx})
44
+
45
+ # Create the final output dictionary
46
+ output = {
47
+ 'text': adress,
48
+ 'text_id': text_id,
49
+ 'answer': entities_dict
50
+ }
51
+ solutions.append(output)
52
+ text_id = text_id+1
53
+
54
+ result_url = f"{api}/{job_id}"
55
+ # send_results_to_api(solutions, result_url)
56
+
57
+ return json.dumps({"solutions": solutions}, indent=4)
58
+
59
+
60
+ import gradio as gr
61
+ inputt = gr.Textbox(label="Parameters (JSON format) Eg. {'audio_files':['file1.mp3','file2.wav'], 'api':'https://api.example.com', 'job_id':'1001'}")
62
+ outputs = gr.JSON()
63
+
64
+ application = gr.Interface(fn=process_xlsx, inputs=inputt, outputs=outputs, title="Named Entity Recognition with API Integration")
65
+ application.launch()