cody82 commited on
Commit
2ea43de
·
verified ·
1 Parent(s): 39c644d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -22
app.py CHANGED
@@ -1,26 +1,28 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
 
4
 
5
- model_id = "cody82/innopolis_bot_model"
6
-
7
- # Загружаем токенизатор и модель
8
- tokenizer = AutoTokenizer.from_pretrained(model_id)
9
- model = AutoModelForCausalLM.from_pretrained(model_id)
10
-
11
- # Убираем cuda, т.к. у нас CPU
12
  device = torch.device("cpu")
13
- model = model.to(device)
14
 
 
15
  def respond(message, history):
16
  history = history or []
17
 
18
- # Собираем всю историю в виде текста
19
  full_input = ""
20
- for user, bot in history:
21
- full_input += f"User: {user}\nAssistant: {bot}\n"
 
 
 
22
  full_input += f"User: {message}\nAssistant:"
23
 
 
24
  inputs = tokenizer(full_input, return_tensors="pt").to(device)
25
  outputs = model.generate(
26
  **inputs,
@@ -28,20 +30,22 @@ def respond(message, history):
28
  do_sample=True,
29
  temperature=0.7,
30
  top_p=0.95,
31
- pad_token_id=tokenizer.eos_token_id
32
  )
33
  output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
34
 
35
- # Извлекаем только ответ ассистента
36
  response = output_text.split("Assistant:")[-1].strip()
37
-
38
- # Добавляем в историю в виде (вопрос, ответ)
39
- history.append((message, response))
40
-
41
- return response, history
42
-
43
-
44
- chat = gr.ChatInterface(fn=respond, title="Innopolis Chatbot")
 
 
45
 
46
  if __name__ == "__main__":
47
  chat.launch(share=True)
 
1
  import gradio as gr
 
2
  import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
 
5
+ # Загружаем модель и токенизатор
6
+ model_name = "cody82/innopolis_bot_model"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name)
 
 
 
9
  device = torch.device("cpu")
10
+ model.to(device)
11
 
12
+ # Функция ответа
13
  def respond(message, history):
14
  history = history or []
15
 
16
+ # Собираем prompt из истории
17
  full_input = ""
18
+ for turn in history:
19
+ if turn["role"] == "user":
20
+ full_input += f"User: {turn['content']}\n"
21
+ elif turn["role"] == "assistant":
22
+ full_input += f"Assistant: {turn['content']}\n"
23
  full_input += f"User: {message}\nAssistant:"
24
 
25
+ # Токенизация и генерация
26
  inputs = tokenizer(full_input, return_tensors="pt").to(device)
27
  outputs = model.generate(
28
  **inputs,
 
30
  do_sample=True,
31
  temperature=0.7,
32
  top_p=0.95,
33
+ pad_token_id=tokenizer.eos_token_id,
34
  )
35
  output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
36
 
37
+ # Извлекаем ответ ассистента
38
  response = output_text.split("Assistant:")[-1].strip()
39
+ return response
40
+
41
+ # Интерфейс Gradio с новым форматом истории
42
+ chat = gr.ChatInterface(
43
+ fn=respond,
44
+ chatbot=gr.Chatbot(label="Innopolis Bot", type="messages"),
45
+ title="Innopolis Chatbot",
46
+ theme="soft",
47
+ examples=["Когда был основан университет Иннополис?", "Какие программы есть в магистратуре?"],
48
+ )
49
 
50
  if __name__ == "__main__":
51
  chat.launch(share=True)