Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,136 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 1 |
+
# Установка необходимых библиотек:
|
| 2 |
+
# pip install gradio huggingface_hub torch transformers
|
| 3 |
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
from transformers import GPTJForCausalLM, AutoTokenizer
|
| 7 |
+
import torch
|
| 8 |
|
| 9 |
+
# Путь к модели на Hugging Face
|
| 10 |
+
MODEL_REPO = "EleutherAI/gpt-j-6B"
|
| 11 |
|
| 12 |
+
# Загрузка токенизатора
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Загрузка модели
|
| 16 |
+
model = GPTJForCausalLM.from_pretrained(
|
| 17 |
+
MODEL_REPO,
|
| 18 |
+
revision="float16",
|
| 19 |
+
torch_dtype=torch.float16,
|
| 20 |
+
low_cpu_mem_usage=True
|
| 21 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 22 |
|
| 23 |
+
# Функция для генерации ответов
|
| 24 |
+
def generate_response(prompt, history=[]):
|
| 25 |
+
# Объединение истории в один контекст
|
| 26 |
+
input_text = "".join([f"User: {item[0]}\nBot: {item[1]}\n" for item in history])
|
| 27 |
+
input_text += f"User: {prompt}\nBot:"
|
| 28 |
|
| 29 |
+
# Токенизация
|
| 30 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(model.device)
|
| 31 |
|
| 32 |
+
# Генерация
|
| 33 |
+
with torch.no_grad():
|
| 34 |
+
output = model.generate(
|
| 35 |
+
input_ids,
|
| 36 |
+
max_length=1024,
|
| 37 |
+
temperature=0.7,
|
| 38 |
+
top_p=0.9,
|
| 39 |
+
do_sample=True,
|
| 40 |
+
pad_token_id=tokenizer.eos_token_id
|
| 41 |
+
)
|
| 42 |
|
| 43 |
+
# Декодирование и извлечение ответа
|
| 44 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 45 |
+
response = response.split("User:")[-1].split("Bot:")[-1].strip()
|
| 46 |
|
| 47 |
+
# Обновление истории
|
| 48 |
+
history.append((prompt, response))
|
| 49 |
+
return history, history
|
| 50 |
|
| 51 |
+
# Кастомный CSS для интерфейса
|
| 52 |
+
custom_css = """
|
| 53 |
+
body {
|
| 54 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 55 |
+
background-color: #f0f2f5;
|
| 56 |
+
margin: 0;
|
| 57 |
+
padding: 0;
|
| 58 |
+
}
|
| 59 |
+
#chatbot-container {
|
| 60 |
+
width: 80%;
|
| 61 |
+
max-width: 800px;
|
| 62 |
+
margin: 50px auto;
|
| 63 |
+
background: #fff;
|
| 64 |
+
border-radius: 10px;
|
| 65 |
+
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
|
| 66 |
+
overflow: hidden;
|
| 67 |
+
animation: fadeIn 0.5s ease-in-out;
|
| 68 |
+
}
|
| 69 |
+
@keyframes fadeIn {
|
| 70 |
+
from { opacity: 0; transform: translateY(20px); }
|
| 71 |
+
to { opacity: 1; transform: translateY(0); }
|
| 72 |
+
}
|
| 73 |
+
.chat-message {
|
| 74 |
+
display: flex;
|
| 75 |
+
align-items: flex-start;
|
| 76 |
+
padding: 15px;
|
| 77 |
+
border-bottom: 1px solid #f0f0f0;
|
| 78 |
+
animation: slideIn 0.3s ease-in-out;
|
| 79 |
+
}
|
| 80 |
+
@keyframes slideIn {
|
| 81 |
+
from { opacity: 0; transform: translateX(-20px); }
|
| 82 |
+
to { opacity: 1; transform: translateX(0); }
|
| 83 |
+
}
|
| 84 |
+
.chat-message.user .message-content {
|
| 85 |
+
background-color: #0078d7;
|
| 86 |
+
color: #fff;
|
| 87 |
+
border-radius: 15px 15px 0 15px;
|
| 88 |
+
padding: 10px 15px;
|
| 89 |
+
max-width: 70%;
|
| 90 |
+
margin-left: auto;
|
| 91 |
+
}
|
| 92 |
+
.chat-message.bot .message-content {
|
| 93 |
+
background-color: #e1e1e1;
|
| 94 |
+
color: #333;
|
| 95 |
+
border-radius: 15px 15px 15px 0;
|
| 96 |
+
padding: 10px 15px;
|
| 97 |
+
max-width: 70%;
|
| 98 |
+
margin-right: auto;
|
| 99 |
+
}
|
| 100 |
+
input[type="text"] {
|
| 101 |
+
width: calc(100% - 20px);
|
| 102 |
+
margin: 10px;
|
| 103 |
+
padding: 10px;
|
| 104 |
+
border: 1px solid #ccc;
|
| 105 |
+
border-radius: 5px;
|
| 106 |
+
font-size: 16px;
|
| 107 |
+
}
|
| 108 |
+
button {
|
| 109 |
+
background-color: #0078d7;
|
| 110 |
+
color: #fff;
|
| 111 |
+
border: none;
|
| 112 |
+
padding: 10px 20px;
|
| 113 |
+
margin: 10px;
|
| 114 |
+
border-radius: 5px;
|
| 115 |
+
cursor: pointer;
|
| 116 |
+
font-size: 16px;
|
| 117 |
+
transition: background-color 0.3s;
|
| 118 |
+
}
|
| 119 |
+
button:hover {
|
| 120 |
+
background-color: #005bb5;
|
| 121 |
+
}
|
| 122 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
+
# Интерфейс Gradio
|
| 125 |
+
with gr.Blocks(css=custom_css) as chat_interface:
|
| 126 |
+
gr.Markdown("# 🧠 Умный Чат с GPT-J-6B")
|
| 127 |
+
with gr.Box(elem_id="chatbot-container"):
|
| 128 |
+
chatbot = gr.Chatbot()
|
| 129 |
+
user_input = gr.Textbox(placeholder="Введите сообщение...")
|
| 130 |
+
clear_btn = gr.Button("Очистить историю")
|
| 131 |
+
|
| 132 |
+
user_input.submit(generate_response, [user_input, chatbot], [chatbot, user_input])
|
| 133 |
+
clear_btn.click(lambda: None, None, chatbot)
|
| 134 |
|
| 135 |
+
# Запуск приложения
|
| 136 |
+
chat_interface.launch(debug=True)
|