Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
return response[0]['generated_text'][len(context):]
|
10 |
-
|
11 |
-
iface = gr.Interface(fn=respond, inputs="text", outputs="text")
|
12 |
-
iface.launch()
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from transformers import DistilBertTokenizerFast, TFDistilBertForSequenceClassification
|
3 |
+
import tensorflow as tf
|
4 |
+
import numpy as np
|
5 |
+
import torch
|
6 |
|
7 |
+
app = Flask(__name__)
|
8 |
+
device = '/GPU:0' if torch.cuda.is_available() else 'CPU'
|
9 |
+
model_name = "distilbert-base-uncased"
|
10 |
+
tokenizer = DistilBertTokenizerFast.from_pretrained(model_name)
|
11 |
+
model = TFDistilBertForSequenceClassification.from_pretrained(model_name, from_pt=True).signatures['serving_default'].to(device)
|
12 |
+
session = tf.compat.v1.keras.backend.get_session()
|
13 |
|
14 |
+
@app.route("/predict", methods=["POST"])
|
15 |
+
def predict():
|
16 |
+
data = request.get_json()
|
17 |
+
input_text = data["input"]
|
18 |
+
input_ids = torch.tensor(tokenizer.encode(input_text)).unsqueeze(0).to(device)
|
19 |
+
|
20 |
+
with torch.no_grad():
|
21 |
+
outputs = model(inputs={'input_ids': input_ids}).logits
|
22 |
+
probabilities = tf.nn.softmax(outputs).numpy()
|
23 |
+
prediction = np.argmax(probabilities)
|
24 |
+
|
25 |
+
return jsonify({"response": prediction})
|
26 |
|
27 |
+
if __name__ == "__main__":
|
28 |
+
app.run(debug=True)
|
|
|
|
|
|
|
|