Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,41 @@
|
|
1 |
import os
|
2 |
from flask import Flask, request, jsonify
|
3 |
-
from
|
4 |
-
import
|
5 |
|
6 |
-
|
7 |
api_key = os.getenv('HF_API_KEY')
|
8 |
model_path = os.getenv('MODEL_PATH')
|
9 |
|
|
|
10 |
app = Flask(__name__)
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
response = requests.post(f"https://api.huggingface.co/models/{model_path}", headers=headers, json=payload)
|
16 |
-
return response.json()
|
17 |
|
18 |
-
|
19 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
data = request.get_json()
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
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)
|