Erfan11 commited on
Commit
f51ee25
1 Parent(s): d10656b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  from dotenv import load_dotenv
3
- from flask import Flask
 
4
 
5
  load_dotenv()
6
  api_key = os.getenv('HF_API_KEY')
@@ -8,9 +9,22 @@ model_path = os.getenv('MODEL_PATH')
8
 
9
  app = Flask(__name__)
10
 
11
- @app.route('/')
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def index():
13
- return "Welcome to My ai!"
14
 
15
  if __name__ == '__main__':
16
- app.run()
 
1
  import os
2
  from dotenv import load_dotenv
3
+ from flask import Flask, request, jsonify
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
+ @app.route('/', methods=['GET'])
26
  def index():
27
+ return "Welcome to My AI!"
28
 
29
  if __name__ == '__main__':
30
+ app.run(debug=True)