Thebull commited on
Commit
c1829c0
1 Parent(s): a3f4a85

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -9
app.py CHANGED
@@ -1,12 +1,28 @@
 
 
 
 
 
1
 
2
- import gradio as gr
3
- from transformers import pipeline
 
 
 
 
4
 
5
- dialogpt = pipeline("text-generation", model="microsoft/DialoGPT-medium")
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- def respond(context):
8
- response = dialogpt(context, max_length=100, do_sample=True)
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)