janasumit2911 commited on
Commit
fdb1193
·
verified ·
1 Parent(s): c13ebef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -4,26 +4,33 @@ import numpy as np
4
  import json
5
  import requests
6
  import gradio as gr
7
-
8
 
9
  bert_tokenizer = BertTokenizer.from_pretrained('MultiTokenizer_ep10')
10
  bert_model = TFBertForSequenceClassification.from_pretrained('MultiModel_ep10')
11
 
12
- def send_results_to_api(data, result_url):
13
- headers = {'Content-Type':'application/json'}
14
- response = requests.post(result_url, json = data, headers=headers)
15
 
16
- if response.status_code == 200:
17
- return response.json
18
- else:
19
- return {'error':f"failed to send result to API: {response.status_code}"}
20
 
21
  def predict_text(params):
22
- params = json.loads(params)
23
-
 
 
 
 
24
  texts = params.get("texts",[])
25
- api = params.get("api", "")
26
- job_id = params.get("job_id","")
 
 
 
27
 
28
  solutions = []
29
 
@@ -50,12 +57,12 @@ def predict_text(params):
50
  result = {'text':text, 'label':[label[pred_label]]}
51
  solutions.append(result)
52
 
53
- result_url = f"{api}/{job_id}"
54
- send_results_to_api(solutions, result_url)
55
- return json.dumps({"solutions":solutions}, indent=4)
56
 
57
 
58
- inputt = gr.Textbox(label="Parameters in Json Format...")
59
  outputt = gr.JSON()
60
 
61
  application = gr.Interface(fn = predict_text, inputs = inputt, outputs = outputt, title='Multi Text Classification with API Integration..')
 
4
  import json
5
  import requests
6
  import gradio as gr
7
+ import logging
8
 
9
  bert_tokenizer = BertTokenizer.from_pretrained('MultiTokenizer_ep10')
10
  bert_model = TFBertForSequenceClassification.from_pretrained('MultiModel_ep10')
11
 
12
+ # def send_results_to_api(data, result_url):
13
+ # headers = {'Content-Type':'application/json'}
14
+ # response = requests.post(result_url, json = data, headers=headers)
15
 
16
+ # if response.status_code == 200:
17
+ # return response.json
18
+ # else:
19
+ # return {'error':f"failed to send result to API: {response.status_code}"}
20
 
21
  def predict_text(params):
22
+ try:
23
+ params = json.loads(params)
24
+ except Exception as e:
25
+ logging.error(f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}")
26
+ return {"error": f"Invalid JSON input: {e.msg} at line {e.lineno} column {e.colno}"}
27
+
28
  texts = params.get("texts",[])
29
+ # api = params.get("api", "")
30
+ # job_id = params.get("job_id","")
31
+
32
+ if not texts:
33
+ return {"error": "Missing required parameters: 'urls'"}
34
 
35
  solutions = []
36
 
 
57
  result = {'text':text, 'label':[label[pred_label]]}
58
  solutions.append(result)
59
 
60
+ # result_url = f"{api}/{job_id}"
61
+ # send_results_to_api(solutions, result_url)
62
+ return json.dumps({"solutions":solutions})
63
 
64
 
65
+ inputt = gr.Textbox(label="Parameters in Json Format... Eg. {'texts':['text1', 'text2']")
66
  outputt = gr.JSON()
67
 
68
  application = gr.Interface(fn = predict_text, inputs = inputt, outputs = outputt, title='Multi Text Classification with API Integration..')