forked from Hoper-J/AI-Guide-and-Demos-zh_CN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_flask.py
33 lines (28 loc) · 1.02 KB
/
app_flask.py
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
from flask import Flask, request, jsonify
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
app = Flask(__name__)
# 加载模型和分词器
model_name = "distilgpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
@app.route('/generate', methods=['POST'])
def generate():
prompt = request.form.get('prompt')
if not prompt:
return jsonify({'error': 'No prompt provided'}), 400
inputs = tokenizer(prompt, return_tensors="pt").to(device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_length=200,
num_beams=5,
no_repeat_ngram_size=2,
early_stopping=True
)
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
return jsonify({'generated_text': generated_text})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)