Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
import random
|
4 |
-
from flask import Flask, request, jsonify
|
5 |
|
6 |
# Загрузка модели и токенизатора
|
7 |
-
model_name = "ai-forever/
|
8 |
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
9 |
model = GPT2LMHeadModel.from_pretrained(model_name)
|
10 |
|
@@ -14,14 +13,14 @@ with open("dialogues.txt", "r", encoding="utf-8") as file:
|
|
14 |
|
15 |
# Функция генерации ответа
|
16 |
def generate(prompt, _=None):
|
17 |
-
user_input = f"[USER]: {prompt}"
|
18 |
inputs = tokenizer.encode(prompt, return_tensors="pt")
|
19 |
outputs = model.generate(inputs, max_length=50, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
|
20 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
21 |
|
22 |
# Прекращаем генерацию на строке [USER]
|
23 |
-
if "[
|
24 |
-
response = response.split("[
|
25 |
|
26 |
bot_message = f"[BOT]: {random.choice(random_phrases)}, {response.strip()}"
|
27 |
return bot_message
|
@@ -40,20 +39,5 @@ demo = gr.ChatInterface(
|
|
40 |
undo_btn=None
|
41 |
)
|
42 |
|
43 |
-
# Flask приложение
|
44 |
-
app = Flask(__name__)
|
45 |
-
|
46 |
-
@app.route("/generate", methods=["POST"])
|
47 |
-
def generate_api():
|
48 |
-
data = request.json
|
49 |
-
if "prompt" not in data:
|
50 |
-
return jsonify({"error": "На что я должен отвечать, гений?"}), 400
|
51 |
-
|
52 |
-
prompt = data["prompt"]
|
53 |
-
response = generate(prompt)
|
54 |
-
return jsonify({"response": response})
|
55 |
-
|
56 |
-
# Запуск Gradio и Flask приложений
|
57 |
if __name__ == "__main__":
|
58 |
demo.launch(share=True)
|
59 |
-
app.run(host="0.0.0.0", port=5000)
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
3 |
import random
|
|
|
4 |
|
5 |
# Загрузка модели и токенизатора
|
6 |
+
model_name = "ai-forever/rugpt3large_based_on_gpt2"
|
7 |
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
|
8 |
model = GPT2LMHeadModel.from_pretrained(model_name)
|
9 |
|
|
|
13 |
|
14 |
# Функция генерации ответа
|
15 |
def generate(prompt, _=None):
|
16 |
+
user_input = f"[USER]: {prompt}\n[BOT]: {random.choice(random_phrases)},"
|
17 |
inputs = tokenizer.encode(prompt, return_tensors="pt")
|
18 |
outputs = model.generate(inputs, max_length=50, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
|
19 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
20 |
|
21 |
# Прекращаем генерацию на строке [USER]
|
22 |
+
if "[" in response:
|
23 |
+
response = response.split("[")[0]
|
24 |
|
25 |
bot_message = f"[BOT]: {random.choice(random_phrases)}, {response.strip()}"
|
26 |
return bot_message
|
|
|
39 |
undo_btn=None
|
40 |
)
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
if __name__ == "__main__":
|
43 |
demo.launch(share=True)
|
|