Spaces:
Sleeping
Sleeping
File size: 1,092 Bytes
a89ce24 5517f9c a89ce24 5517f9c a89ce24 |
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 |
import os
from flask import Flask, request, jsonify
from transformers import GPT2LMHeadModel, GPT2Tokenizer
import torch
app = Flask("Response API")
size = "small"
# microsoft/DialoGPT-small
# microsoft/DialoGPT-medium
# microsoft/DialoGPT-large
# Load the Hugging Face GPT-2 model and tokenizer
model = GPT2LMHeadModel.from_pretrained("microsoft/DialoGPT-medium")
tokenizer = GPT2Tokenizer.from_pretrained("microsoft/DialoGPT-medium")
@app.route("/", methods=["POST"])
def receive_data():
data = request.get_json()
print("Prompt:", data['prompt'])
print("Length:", data['length'])
input_text = data['prompt']
# Tokenize the input text
input_ids = tokenizer.encode(input_text, return_tensors="pt")
# Generate output using the model
output_ids = model.generate(input_ids, max_length=data['length'], num_beams=5, no_repeat_ngram_size=2)
generated_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
answer_data = { "answer": generated_text }
print("Answered with:", answer_data)
return jsonify(answer_data)
app.run(debug=False, port=7860) |