themeetjani commited on
Commit
3952f42
·
1 Parent(s): dae16ed

Rename application.py to app.py

Browse files
Files changed (1) hide show
  1. application.py → app.py +13 -29
application.py → app.py RENAMED
@@ -6,10 +6,11 @@ from flask import Flask, jsonify, request
6
  import torch.nn.functional as F
7
  import boto3
8
  import pandas as pd
9
- bucket = 'data-ai-dev2'
10
  from transformers import BertTokenizer, BertModel
11
  from torch import cuda
12
- device = 'cuda' if cuda.is_available() else 'cpu'
 
13
 
14
  class RobertaClass(torch.nn.Module):
15
  def __init__(self):
@@ -32,10 +33,7 @@ class RobertaClass(torch.nn.Module):
32
  model = RobertaClass()
33
  model.to(device)
34
 
35
- s3 = boto3.client('s3', aws_access_key_id='AKIAW5BGUY6ZRCSQBSIJ', aws_secret_access_key= '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')
36
- s3.download_file(Bucket=bucket, Key='model_hf/tweet_model/tweet_model_v1.bin', Filename = './tweet_model_v1.bin')
37
-
38
- model = torch.load('tweet_model_v1.bin', map_location=torch.device('cpu'))
39
 
40
  tokenizer = BertTokenizer.from_pretrained('bert-base-multilingual-cased', truncation=True, do_lower_case=True)
41
 
@@ -43,24 +41,11 @@ def id2class_fun(lst, map_cl):
43
  s = pd.Series(lst)
44
  return s.map(map_cl).tolist()
45
 
46
- application = Flask(__name__)
47
- @application.route('/')
48
- def home():
49
- return "Working!"
50
-
51
- @application.route('/process/', methods=['POST'])
52
- def process():
53
- content_type = request.headers.get('Content-Type')
54
- if (content_type == 'application/json'):
55
- json_file = request.json
56
- loaded = json.dumps(json_file)
57
- new_loaded = json.loads(loaded)
58
- text = new_loaded['text']
59
- id2class = {0: 'InappropriateUndesirable', 1 : 'GreenContent', 2 : 'IllegalActivities',
60
  3 : 'DiscriminatoryHate', 4 :'ViolentGraphic', 5:'PotentialAddiction',
61
  6 : 'ExtremismTerrorism', 7 : 'SexualExplicit'}
 
62
  try:
63
-
64
  inputs = (
65
  tokenizer.encode_plus(
66
  text, None, add_special_tokens=True, max_length = 512,
@@ -78,14 +63,13 @@ def process():
78
  prd_score = top_values.cpu().detach().numpy().tolist()
79
  prd_score = [item for sublist in prd_score for item in sublist]
80
  otp = dict(zip(prd_cls_1, prd_score))
81
- # .replace(map_class, inplace=True)
82
- return jsonify({'output':otp})
83
  except:
84
- return jsonify({'output':'something went wrong'})
 
 
 
85
 
86
-
87
-
88
- if __name__ == "__main__":
89
- application.debug = True
90
- application.run()
91
 
 
6
  import torch.nn.functional as F
7
  import boto3
8
  import pandas as pd
9
+ #bucket = 'data-ai-dev2'
10
  from transformers import BertTokenizer, BertModel
11
  from torch import cuda
12
+ import gradio as gr
13
+ #device = 'cuda' if cuda.is_available() else 'cpu'
14
 
15
  class RobertaClass(torch.nn.Module):
16
  def __init__(self):
 
33
  model = RobertaClass()
34
  model.to(device)
35
 
36
+ model = torch.load('./tweet_model_v1.bin', map_location=torch.device('cpu'))
 
 
 
37
 
38
  tokenizer = BertTokenizer.from_pretrained('bert-base-multilingual-cased', truncation=True, do_lower_case=True)
39
 
 
41
  s = pd.Series(lst)
42
  return s.map(map_cl).tolist()
43
 
44
+ id2class = {0: 'InappropriateUndesirable', 1 : 'GreenContent', 2 : 'IllegalActivities',
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  3 : 'DiscriminatoryHate', 4 :'ViolentGraphic', 5:'PotentialAddiction',
46
  6 : 'ExtremismTerrorism', 7 : 'SexualExplicit'}
47
+ def process(text):
48
  try:
 
49
  inputs = (
50
  tokenizer.encode_plus(
51
  text, None, add_special_tokens=True, max_length = 512,
 
63
  prd_score = top_values.cpu().detach().numpy().tolist()
64
  prd_score = [item for sublist in prd_score for item in sublist]
65
  otp = dict(zip(prd_cls_1, prd_score))
66
+ return {'output':otp}
 
67
  except:
68
+ return {'output':'something went wrong'}
69
+
70
+ inputs = [gr.inputs.Textbox(lines=2, label="Enter the tweet")]
71
+ outputs = gr.outputs.Textbox(label="result")
72
 
73
+ gr.Interface(fn=process, inputs=inputs, outputs=outputs, title="twitter_classifier",
74
+ theme="compact").launch()
 
 
 
75