Erfan11 commited on
Commit
0ac9724
1 Parent(s): f84e164

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -7
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  from flask import Flask, request, jsonify
3
  from dotenv import load_dotenv
4
- import tensorflow as tf
5
 
6
  load_dotenv()
7
  api_key = os.getenv('HF_API_KEY')
@@ -9,16 +9,18 @@ model_path = os.getenv('MODEL_PATH')
9
 
10
  app = Flask(__name__)
11
 
12
- def load_model():
13
- return tf.keras.models.load_model(model_path)
 
 
 
14
 
15
  @app.route('/predict', methods=['POST'])
16
  def predict():
17
  data = request.get_json()
18
  text = data['text']
19
- model = load_model()
20
- prediction = model.predict([text])
21
- return jsonify(prediction.tolist())
22
 
23
  if __name__ == '__main__':
24
- app.run()
 
1
  import os
2
  from flask import Flask, request, jsonify
3
  from dotenv import load_dotenv
4
+ import requests
5
 
6
  load_dotenv()
7
  api_key = os.getenv('HF_API_KEY')
 
9
 
10
  app = Flask(__name__)
11
 
12
+ def get_model_predictions(text):
13
+ headers = {"Authorization": f"Bearer {api_key}"}
14
+ payload = {"inputs": text}
15
+ response = requests.post(f"https://api.huggingface.co/models/{model_path}", headers=headers, json=payload)
16
+ return response.json()
17
 
18
  @app.route('/predict', methods=['POST'])
19
  def predict():
20
  data = request.get_json()
21
  text = data['text']
22
+ prediction = get_model_predictions(text)
23
+ return jsonify(prediction)
 
24
 
25
  if __name__ == '__main__':
26
+ app.run(debug=True)