Update app.py
Browse files
app.py
CHANGED
@@ -3,56 +3,57 @@ import gradio as gr
|
|
3 |
|
4 |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
"
|
9 |
-
|
10 |
}
|
11 |
|
12 |
-
def format_prompt(message, history, genre,
|
|
|
13 |
genre_prompt = GENRE_PROMPTS.get(genre, "Жанр игры неизвестен.")
|
14 |
-
prompt = f"
|
15 |
-
f"{genre_prompt} Учитывай, что в игре участвуют несколько игроков: {', '.join(players)}. " \
|
16 |
-
f"Каждый твой ответ должен учитывать действия всех игроков, их взаимодействие и текущую ситуацию. " \
|
17 |
-
f"Добавляй элементы кооперации: совместное решение задач, голосование за действия и влияние выборов одного игрока на другого."
|
18 |
for user_prompt, bot_response in history:
|
19 |
-
prompt += f"[INST] {user_prompt} [/INST]"
|
20 |
-
prompt += f" {bot_response}</s> "
|
21 |
prompt += f"[INST] {message} [/INST]"
|
22 |
return prompt
|
23 |
|
24 |
-
def generate(
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
gr.
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
|
5 |
|
6 |
+
# Хранение состояния игры
|
7 |
+
game_state = {
|
8 |
+
"players": {},
|
9 |
+
"story_progress": "Начало игры..."
|
10 |
}
|
11 |
|
12 |
+
def format_prompt(message, history, genre, player_id):
|
13 |
+
# Получаем промпт для выбранного жанра
|
14 |
genre_prompt = GENRE_PROMPTS.get(genre, "Жанр игры неизвестен.")
|
15 |
+
prompt = f"Игрок {player_id} в жанре {genre}. {genre_prompt} История: {game_state['story_progress']}."
|
|
|
|
|
|
|
16 |
for user_prompt, bot_response in history:
|
17 |
+
prompt += f"[INST] {user_prompt} [/INST] {bot_response}</s> "
|
|
|
18 |
prompt += f"[INST] {message} [/INST]"
|
19 |
return prompt
|
20 |
|
21 |
+
def generate(player_id, message, genre, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
|
22 |
+
if player_id not in game_state["players"]:
|
23 |
+
game_state["players"][player_id] = {"history": []}
|
24 |
+
|
25 |
+
history = game_state["players"][player_id]["history"]
|
26 |
+
formatted_prompt = format_prompt(message, history, genre, player_id)
|
27 |
+
|
28 |
+
generate_kwargs = {
|
29 |
+
"temperature": temperature,
|
30 |
+
"max_new_tokens": max_new_tokens,
|
31 |
+
"top_p": top_p,
|
32 |
+
"repetition_penalty": repetition_penalty,
|
33 |
+
"do_sample": True,
|
34 |
+
"seed": 42,
|
35 |
+
}
|
36 |
+
|
37 |
+
response = client.text_generation(formatted_prompt, **generate_kwargs)
|
38 |
+
game_state["players"][player_id]["history"].append((message, response))
|
39 |
+
game_state["story_progress"] += f"\n{response}"
|
40 |
+
|
41 |
+
return response
|
42 |
+
|
43 |
+
def gradio_interface():
|
44 |
+
with gr.Blocks() as demo:
|
45 |
+
player_id = gr.Textbox(label="Player ID", placeholder="Введите ваш уникальный идентификатор")
|
46 |
+
genre = gr.Radio(label="Game Genre", choices=list(GENRE_PROMPTS.keys()), value="Horror")
|
47 |
+
message = gr.Textbox(label="Ваше сообщение")
|
48 |
+
output = gr.Textbox(label="Ответ игры")
|
49 |
+
|
50 |
+
def play_game(player_id, message, genre):
|
51 |
+
return generate(player_id, message, genre)
|
52 |
+
|
53 |
+
submit = gr.Button("Отправить")
|
54 |
+
submit.click(play_game, inputs=[player_id, message, genre], outputs=output)
|
55 |
+
|
56 |
+
demo.launch()
|
57 |
+
|
58 |
+
if __name__ == "__main__":
|
59 |
+
gradio_interface()
|