Spaces:
Sleeping
Sleeping
stuff
Browse files- app.py +28 -57
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,61 +1,32 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
from
|
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 |
-
response = ""
|
30 |
-
|
31 |
-
for message in client.chat_completion(
|
32 |
-
messages,
|
33 |
-
max_tokens=max_tokens,
|
34 |
-
stream=True,
|
35 |
-
temperature=temperature,
|
36 |
-
top_p=top_p,
|
37 |
-
):
|
38 |
-
token = message.choices[0].delta.content
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
demo = gr.ChatInterface(
|
44 |
-
respond,
|
45 |
-
additional_inputs=[
|
46 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
47 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
48 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
49 |
-
gr.Slider(
|
50 |
-
minimum=0.1,
|
51 |
-
maximum=1.0,
|
52 |
-
value=0.95,
|
53 |
-
step=0.05,
|
54 |
-
label="Top-p (nucleus sampling)",
|
55 |
-
),
|
56 |
-
],
|
57 |
)
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
demo.launch()
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
from transformers import DistilGPT2Tokenizer, DistilGPT2LMHeadModel
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Импортируем токены из переменных окружения
|
7 |
+
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
8 |
+
TG_TOKEN = os.getenv("TG_TOKEN")
|
9 |
+
|
10 |
+
# Загрузка модели и токенизатора с использованием токена
|
11 |
+
model_name = "distilgpt2"
|
12 |
+
tokenizer = DistilGPT2Tokenizer.from_pretrained(model_name, use_auth_token=HUGGINGFACE_TOKEN)
|
13 |
+
model = DistilGPT2LMHeadModel.from_pretrained(model_name, use_auth_token=HUGGINGFACE_TOKEN)
|
14 |
+
|
15 |
+
# Функция для ведения диалога
|
16 |
+
def chat_with_model(user_input):
|
17 |
+
input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
18 |
+
chat_history_ids = model.generate(input_ids, max_length=100, pad_token_id=tokenizer.eos_token_id)
|
19 |
+
bot_response = tokenizer.decode(chat_history_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
20 |
+
return bot_response
|
21 |
+
|
22 |
+
# Создание интерфейса Gradio
|
23 |
+
iface = gr.Interface(
|
24 |
+
fn=chat_with_model,
|
25 |
+
inputs="text",
|
26 |
+
outputs="text",
|
27 |
+
title="Чатбот на DistilGPT",
|
28 |
+
description="Поболтай со своим чатботом!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
)
|
30 |
|
31 |
+
# Запуск интерфейса
|
32 |
+
iface.launch()
|
|
requirements.txt
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
huggingface_hub==0.22.2
|
2 |
minijinja
|
3 |
-
transformers
|
|
|
|
1 |
huggingface_hub==0.22.2
|
2 |
minijinja
|
3 |
+
transformers
|
4 |
+
torch
|