Makaria commited on
Commit
e39d412
1 Parent(s): b62a0f3
Files changed (2) hide show
  1. app.py +28 -57
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,61 +1,32 @@
1
  import os
2
  import gradio as gr
3
- from huggingface_hub import InferenceClient
4
-
5
- # Получаем токен из секрета
6
- hf_token = os.environ.get("HUGGINGFACE_TOKEN")
7
-
8
- client = InferenceClient("sambanovasystems/SambaLingo-Russian-Chat", token=hf_token)
9
-
10
-
11
- def respond(
12
- message,
13
- history: list[tuple[str, str]],
14
- system_message,
15
- max_tokens,
16
- temperature,
17
- top_p,
18
- ):
19
- messages = [{"role": "system", "content": system_message}]
20
-
21
- for val in history:
22
- if val[0]:
23
- messages.append({"role": "user", "content": val[0]})
24
- if val[1]:
25
- messages.append({"role": "assistant", "content": val[1]})
26
-
27
- messages.append({"role": "user", "content": message})
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
- if __name__ == "__main__":
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