Erfan11 commited on
Commit
c29e3a2
1 Parent(s): 2c3db68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -13
app.py CHANGED
@@ -1,26 +1,41 @@
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')
8
  model_path = os.getenv('MODEL_PATH')
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)
 
1
  import os
2
  from flask import Flask, request, jsonify
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ import yaml
5
 
6
+ # Load environment variables
7
  api_key = os.getenv('HF_API_KEY')
8
  model_path = os.getenv('MODEL_PATH')
9
 
10
+ # Initialize Flask app
11
  app = Flask(__name__)
12
 
13
+ # Load configuration
14
+ with open('config.yaml', 'r') as file:
15
+ config = yaml.safe_load(file)
 
 
16
 
17
+ # Load the model and tokenizer
18
+ def load_model():
19
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
20
+ model = AutoModelForCausalLM.from_pretrained(model_path)
21
+ return model, tokenizer
22
+
23
+ model, tokenizer = load_model()
24
+
25
+ def generate_text(prompt):
26
+ inputs = tokenizer(prompt, return_tensors="pt")
27
+ outputs = model.generate(inputs['input_ids'])
28
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
29
+
30
+ @app.route('/generate', methods=['POST'])
31
+ def generate():
32
  data = request.get_json()
33
+ prompt = data.get('prompt')
34
+ if prompt:
35
+ response_text = generate_text(prompt)
36
+ return jsonify({"response": response_text})
37
+ else:
38
+ return jsonify({"error": "No prompt provided"})
39
 
40
  if __name__ == '__main__':
41
  app.run(debug=True)