Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
|
|
4 |
|
5 |
# Загрузка модели и токенизатора
|
6 |
MODEL_NAME = "microsoft/DialoGPT-medium"
|
@@ -11,7 +12,7 @@ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
|
|
11 |
npc_names = ["Agent Alpha", "Agent Beta", "Agent Gamma"]
|
12 |
chat_history = []
|
13 |
|
14 |
-
def generate_response(input_text
|
15 |
"""
|
16 |
Генерирует ответ от NPC на основе входного текста.
|
17 |
"""
|
@@ -32,24 +33,36 @@ def generate_response(input_text, chat_history):
|
|
32 |
response = tokenizer.decode(response_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
33 |
return response
|
34 |
|
35 |
-
def npc_chat(
|
36 |
"""
|
37 |
-
Обрабатывает диалог между NPC
|
38 |
"""
|
39 |
global chat_history
|
40 |
|
41 |
-
#
|
42 |
if not chat_history:
|
43 |
-
chat_history.append(f"{npc_names[0]}: Welcome to Earth Web Space Networks!
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
# Gradio интерфейс
|
55 |
with gr.Blocks() as demo:
|
@@ -57,10 +70,8 @@ with gr.Blocks() as demo:
|
|
57 |
|
58 |
with gr.Row():
|
59 |
chat_output = gr.Textbox(label="Chat History", lines=10, interactive=False)
|
60 |
-
user_input = gr.Textbox(label="Your Message")
|
61 |
|
62 |
-
|
63 |
-
|
64 |
|
65 |
-
demo.launch()
|
66 |
-
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
+
import time
|
5 |
|
6 |
# Загрузка модели и токенизатора
|
7 |
MODEL_NAME = "microsoft/DialoGPT-medium"
|
|
|
12 |
npc_names = ["Agent Alpha", "Agent Beta", "Agent Gamma"]
|
13 |
chat_history = []
|
14 |
|
15 |
+
def generate_response(input_text):
|
16 |
"""
|
17 |
Генерирует ответ от NPC на основе входного текста.
|
18 |
"""
|
|
|
33 |
response = tokenizer.decode(response_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
34 |
return response
|
35 |
|
36 |
+
def npc_chat():
|
37 |
"""
|
38 |
+
Обрабатывает диалог между NPC.
|
39 |
"""
|
40 |
global chat_history
|
41 |
|
42 |
+
# Начальное сообщение
|
43 |
if not chat_history:
|
44 |
+
chat_history.append(f"{npc_names[0]}: Welcome to Earth Web Space Networks!")
|
45 |
|
46 |
+
while True:
|
47 |
+
for i, npc in enumerate(npc_names):
|
48 |
+
# Получаем последнее сообщение из истории
|
49 |
+
last_message = chat_history[-1].split(": ", 1)[1]
|
50 |
+
|
51 |
+
# Генерируем ответ
|
52 |
+
response = generate_response(last_message)
|
53 |
+
new_message = f"{npc}: {response}"
|
54 |
+
chat_history.append(new_message)
|
55 |
+
|
56 |
+
# Ограничение длины истории чата
|
57 |
+
if len(chat_history) > 20:
|
58 |
+
chat_history.pop(0)
|
59 |
+
|
60 |
+
# Возвращаем текущую историю чата
|
61 |
+
formatted_chat = "\n".join(chat_history)
|
62 |
+
yield formatted_chat
|
63 |
+
|
64 |
+
# Задержка для имитации реального времени
|
65 |
+
time.sleep(2)
|
66 |
|
67 |
# Gradio интерфейс
|
68 |
with gr.Blocks() as demo:
|
|
|
70 |
|
71 |
with gr.Row():
|
72 |
chat_output = gr.Textbox(label="Chat History", lines=10, interactive=False)
|
|
|
73 |
|
74 |
+
# Запуск бесконечного диалога между NPC
|
75 |
+
demo.load(npc_chat, inputs=None, outputs=chat_output, every=2)
|
76 |
|
77 |
+
demo.launch()
|
|