cody82 commited on
Commit
db72f77
·
verified ·
1 Parent(s): b9e73f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -24
app.py CHANGED
@@ -1,26 +1,19 @@
1
  import os
2
- import gradio as gr
3
  import torch
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
- from typing import List, Tuple
6
 
7
- # Пути и настройки
8
  model_repo = "cody82/innopolis_bot_model"
9
- cache_dir = "/data/model" # Путь к Persistent Storage
10
-
11
- if not os.path.exists(cache_dir):
12
- os.makedirs(cache_dir)
13
 
14
- # Загружаем токенизатор и модель из локального кеша / Persistent Storage
15
  tokenizer = AutoTokenizer.from_pretrained(model_repo, cache_dir=cache_dir)
16
  model = AutoModelForCausalLM.from_pretrained(model_repo, cache_dir=cache_dir)
17
  model.to("cpu")
18
 
19
- # Функция обработки сообщений
20
  def respond(message, history):
21
  history = history or []
22
-
23
- # Собираем всю историю как текст
24
  full_input = ""
25
  for turn in history:
26
  if turn["role"] == "user":
@@ -36,28 +29,24 @@ def respond(message, history):
36
  do_sample=True,
37
  temperature=0.7,
38
  top_p=0.95,
39
- pad_token_id=tokenizer.eos_token_id
40
  )
41
  output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
42
  response = output_text.split("Assistant:")[-1].strip()
43
 
44
- # Добавляем новый ответ в историю
45
  history.append({"role": "user", "content": message})
46
  history.append({"role": "assistant", "content": response})
47
 
48
  return history
49
 
50
-
51
- # Gradio интерфейс
52
- demo = gr.ChatInterface(
53
- respond,
54
- additional_inputs=[
55
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
56
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
57
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
58
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
59
- ],
60
  )
61
 
62
  if __name__ == "__main__":
63
- demo.launch()
 
1
  import os
 
2
  import torch
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ import gradio as gr
5
 
 
6
  model_repo = "cody82/innopolis_bot_model"
7
+ cache_dir = "/data/model"
 
 
 
8
 
 
9
  tokenizer = AutoTokenizer.from_pretrained(model_repo, cache_dir=cache_dir)
10
  model = AutoModelForCausalLM.from_pretrained(model_repo, cache_dir=cache_dir)
11
  model.to("cpu")
12
 
 
13
  def respond(message, history):
14
  history = history or []
15
+
16
+ # Формируем текст
17
  full_input = ""
18
  for turn in history:
19
  if turn["role"] == "user":
 
29
  do_sample=True,
30
  temperature=0.7,
31
  top_p=0.95,
32
+ pad_token_id=tokenizer.eos_token_id,
33
  )
34
  output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
35
  response = output_text.split("Assistant:")[-1].strip()
36
 
37
+ # Обновлённая история
38
  history.append({"role": "user", "content": message})
39
  history.append({"role": "assistant", "content": response})
40
 
41
  return history
42
 
43
+ chat = gr.ChatInterface(
44
+ fn=respond,
45
+ chatbot=gr.Chatbot(label="Innopolis Bot", type="messages"),
46
+ title="Innopolis Chatbot",
47
+ theme="soft",
48
+ examples=["Когда был основан университет Иннополис?", "Какие программы есть в магистратуре?"],
 
 
 
 
49
  )
50
 
51
  if __name__ == "__main__":
52
+ chat.launch(share=True)