File size: 1,176 Bytes
a89ce24
ac96c11
5517f9c
 
 
 
ac96c11
a89ce24
 
 
 
5517f9c
ac96c11
 
5517f9c
 
 
 
 
ac96c11
 
5517f9c
ac96c11
5517f9c
 
 
 
 
ac96c11
5517f9c
 
 
 
 
 
ac96c11
 
 
 
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
37
38
39
40
import os
from flask import Flask, request, jsonify, render_template
from transformers import GPT2LMHeadModel, GPT2Tokenizer
import torch

app = Flask("Response API")
name = "microsoft/DialoGPT-medium"
# microsoft/DialoGPT-small
# microsoft/DialoGPT-medium
# microsoft/DialoGPT-large

# Load the Hugging Face GPT-2 model and tokenizer
model = GPT2LMHeadModel.from_pretrained(name)
tokenizer = GPT2Tokenizer.from_pretrained(name)

@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.route("/", methods=["GET"])
def receive_data():
  return render_template("index.html")

app.run(debug=False, port=7860)