Avril Lalaine commited on
Commit
bca8e6b
·
1 Parent(s): cbee212
Files changed (2) hide show
  1. Dockerfile +8 -0
  2. app.py +24 -24
Dockerfile CHANGED
@@ -1,11 +1,19 @@
1
  FROM python:3.9-slim
2
 
 
3
  WORKDIR /app
4
 
 
 
 
 
5
  COPY . .
6
 
 
7
  RUN pip install --no-cache-dir -r requirements.txt
8
 
 
9
  EXPOSE 8080
10
 
 
11
  CMD ["python", "app.py"]
 
1
  FROM python:3.9-slim
2
 
3
+ # Set working directory inside the container
4
  WORKDIR /app
5
 
6
+ # Set the TRANSFORMERS_CACHE environment variable to a writable directory
7
+ ENV TRANSFORMERS_CACHE=/app/cache
8
+
9
+ # Copy the contents of your local directory to the working directory in the container
10
  COPY . .
11
 
12
+ # Install dependencies
13
  RUN pip install --no-cache-dir -r requirements.txt
14
 
15
+ # Expose port 8080 for your app
16
  EXPOSE 8080
17
 
18
+ # Run the Flask app
19
  CMD ["python", "app.py"]
app.py CHANGED
@@ -1,7 +1,15 @@
 
1
  from pathlib import Path
2
  from flask import Flask, render_template, request, jsonify
3
- from transformers import BertTokenizer, BertForSequenceClassification, AutoTokenizer, AutoModelForSequenceClassification
4
  import torch
 
 
 
 
 
 
 
5
 
6
  app = Flask(__name__)
7
 
@@ -12,7 +20,6 @@ BERT_TOKENIZER = 'bert-base-uncased'
12
  ROBERTA_TOKENIZER = 'jcblaise/roberta-tagalog-base'
13
  ELECTRA_TOKENIZER = 'google/electra-base-discriminator'
14
 
15
-
16
  LABELS = ["fake", "real"]
17
 
18
  class Classifier:
@@ -54,19 +61,15 @@ class Classifier:
54
  }
55
  return result
56
 
57
-
58
-
59
  @app.route('/')
60
  def home():
61
  return render_template('index.html')
62
 
63
  @app.route('/detect', methods=['POST'])
64
  def detect():
65
-
66
  try:
67
  data = request.get_json()
68
  news_text = data.get('text')
69
-
70
  model_chosen = data.get('model')
71
 
72
  print(model_chosen)
@@ -77,50 +80,47 @@ def detect():
77
  'message': 'No text provided'
78
  }), 400
79
 
80
- switch={
81
- 'nonaug-bert':'bert-nonaug',
82
- 'aug-bert':'bert-aug',
83
- 'nonaug-tagbert':'tagbert-nonaug',
84
- 'aug-tagbert':'tagbert-aug',
85
- 'nonaug-electra':'electra-nonaug',
86
- 'aug-electra':'electra-aug'
87
  }
88
 
89
  model_p = switch.get(model_chosen)
90
 
91
- print("model",model_p)
92
-
93
- MODEL_PATH = Path("D:\\Aplil\\skibidi-thesis\\webapp", model_p)
94
 
 
 
95
 
96
  print(MODEL_PATH)
97
 
98
  tokenizer = model_chosen.split("-")[1]
99
-
100
  tokenizer_chosen = {
101
- 'bert':BERT_TOKENIZER,
102
- 'tagbert':ROBERTA_TOKENIZER,
103
- 'electra':ELECTRA_TOKENIZER
104
  }
105
 
106
  print(tokenizer)
107
 
108
- classifier = Classifier(MODEL_PATH,DEVICE,tokenizer_chosen.get(tokenizer))
109
 
110
  result = classifier.predict(news_text)
111
  print(result['confidence_scores'])
112
-
113
 
114
  if result['predicted_class'] == "fake":
115
  out = "News Needs Further Validation"
116
  else:
117
  out = "News is Real"
118
 
119
-
120
  return jsonify({
121
  'status': 'success',
122
  'prediction': out,
123
- 'confidence':result['confidence_scores']
124
  })
125
 
126
  except Exception as e:
 
1
+ import os
2
  from pathlib import Path
3
  from flask import Flask, render_template, request, jsonify
4
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
  import torch
6
+ import warnings
7
+
8
+ # Suppress FutureWarnings
9
+ warnings.filterwarnings("ignore", category=FutureWarning)
10
+
11
+ # Set the TRANSFORMERS_CACHE to a writable directory
12
+ os.environ["TRANSFORMERS_CACHE"] = "./cache" # Modify this path if needed
13
 
14
  app = Flask(__name__)
15
 
 
20
  ROBERTA_TOKENIZER = 'jcblaise/roberta-tagalog-base'
21
  ELECTRA_TOKENIZER = 'google/electra-base-discriminator'
22
 
 
23
  LABELS = ["fake", "real"]
24
 
25
  class Classifier:
 
61
  }
62
  return result
63
 
 
 
64
  @app.route('/')
65
  def home():
66
  return render_template('index.html')
67
 
68
  @app.route('/detect', methods=['POST'])
69
  def detect():
 
70
  try:
71
  data = request.get_json()
72
  news_text = data.get('text')
 
73
  model_chosen = data.get('model')
74
 
75
  print(model_chosen)
 
80
  'message': 'No text provided'
81
  }), 400
82
 
83
+ switch = {
84
+ 'nonaug-bert': 'bert-nonaug',
85
+ 'aug-bert': 'bert-aug',
86
+ 'nonaug-tagbert': 'tagbert-nonaug',
87
+ 'aug-tagbert': 'tagbert-aug',
88
+ 'nonaug-electra': 'electra-nonaug',
89
+ 'aug-electra': 'electra-aug'
90
  }
91
 
92
  model_p = switch.get(model_chosen)
93
 
94
+ print("model", model_p)
 
 
95
 
96
+ # Adjusting the model path to point to the correct folder inside 'webapp'
97
+ MODEL_PATH = Path("huggingface", "webapp", model_p) # Corrected model path to webapp folder
98
 
99
  print(MODEL_PATH)
100
 
101
  tokenizer = model_chosen.split("-")[1]
 
102
  tokenizer_chosen = {
103
+ 'bert': BERT_TOKENIZER,
104
+ 'tagbert': ROBERTA_TOKENIZER,
105
+ 'electra': ELECTRA_TOKENIZER
106
  }
107
 
108
  print(tokenizer)
109
 
110
+ classifier = Classifier(MODEL_PATH, DEVICE, tokenizer_chosen.get(tokenizer))
111
 
112
  result = classifier.predict(news_text)
113
  print(result['confidence_scores'])
 
114
 
115
  if result['predicted_class'] == "fake":
116
  out = "News Needs Further Validation"
117
  else:
118
  out = "News is Real"
119
 
 
120
  return jsonify({
121
  'status': 'success',
122
  'prediction': out,
123
+ 'confidence': result['confidence_scores']
124
  })
125
 
126
  except Exception as e: