File size: 1,144 Bytes
634cec7
5cf5335
c29e3a2
 
16ede2d
c29e3a2
5cf5335
 
16ede2d
c29e3a2
5cf5335
16ede2d
c29e3a2
 
 
16ede2d
c29e3a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cf5335
c29e3a2
 
 
 
 
 
16ede2d
5cf5335
0ac9724
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
from flask import Flask, request, jsonify
from transformers import AutoModelForCausalLM, AutoTokenizer
import yaml

# Load environment variables
api_key = os.getenv('HF_API_KEY')
model_path = os.getenv('MODEL_PATH')

# Initialize Flask app
app = Flask(__name__)

# Load configuration
with open('config.yaml', 'r') as file:
    config = yaml.safe_load(file)

# Load the model and tokenizer
def load_model():
    tokenizer = AutoTokenizer.from_pretrained(model_path)
    model = AutoModelForCausalLM.from_pretrained(model_path)
    return model, tokenizer

model, tokenizer = load_model()

def generate_text(prompt):
    inputs = tokenizer(prompt, return_tensors="pt")
    outputs = model.generate(inputs['input_ids'])
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

@app.route('/generate', methods=['POST'])
def generate():
    data = request.get_json()
    prompt = data.get('prompt')
    if prompt:
        response_text = generate_text(prompt)
        return jsonify({"response": response_text})
    else:
        return jsonify({"error": "No prompt provided"})

if __name__ == '__main__':
    app.run(debug=True)