SimrusDenuvo commited on
Commit
42ccf19
·
verified ·
1 Parent(s): 11dd5a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -13
app.py CHANGED
@@ -1,35 +1,44 @@
1
-
2
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
  import gradio as gr
4
 
5
- model_name = "cointegrated/rut5-small"
 
6
  tokenizer = AutoTokenizer.from_pretrained(model_name)
7
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
8
 
 
9
  def generate_response(prompt):
10
- instruction = f"Ответь кратко: {prompt.strip()}"
11
- input_ids = tokenizer.encode(instruction, return_tensors="pt", max_length=512, truncation=True)
12
 
 
13
  output = model.generate(
14
  input_ids,
15
- max_new_tokens=80,
16
  do_sample=True,
17
- top_p=0.9,
18
- temperature=0.7,
19
- repetition_penalty=1.2,
 
20
  eos_token_id=tokenizer.eos_token_id
21
  )
22
 
 
23
  response = tokenizer.decode(output[0], skip_special_tokens=True)
24
- return response.strip()
 
 
25
 
 
26
  iface = gr.Interface(
27
  fn=generate_response,
28
  inputs=gr.Textbox(label="Введите ваш вопрос"),
29
  outputs=gr.Textbox(label="Ответ модели"),
30
- title="Интерфейс ChatGPT",
31
- description="Пример взаимодействия с API OpenAI через Hugging Face Space"
32
  )
33
 
 
34
  iface.launch()
35
 
 
1
+ from transformers import AutoTokenizer, AutoModelForCausalLM
2
+ import torch
3
  import gradio as gr
4
 
5
+ # Загрузка модели и токенизатора
6
+ model_name = "sberbank-ai/rugpt3medium_based_on_gpt2"
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name)
9
 
10
+ # Функция генерации ответа
11
  def generate_response(prompt):
12
+ instruction = f"Ответь кратко и понятно на вопрос: {prompt.strip()}"
13
+ input_ids = tokenizer.encode(instruction, return_tensors="pt")
14
 
15
+ # Генерация ответа
16
  output = model.generate(
17
  input_ids,
18
+ max_length=150,
19
  do_sample=True,
20
+ top_k=50,
21
+ top_p=0.95,
22
+ temperature=0.8,
23
+ pad_token_id=tokenizer.eos_token_id,
24
  eos_token_id=tokenizer.eos_token_id
25
  )
26
 
27
+ # Декодирование и удаление излишней части промпта
28
  response = tokenizer.decode(output[0], skip_special_tokens=True)
29
+
30
+ # Возвращаем только ответ, без дублирования запроса
31
+ return response.replace(instruction, "").strip()
32
 
33
+ # Интерфейс Gradio
34
  iface = gr.Interface(
35
  fn=generate_response,
36
  inputs=gr.Textbox(label="Введите ваш вопрос"),
37
  outputs=gr.Textbox(label="Ответ модели"),
38
+ title="Ответ от ruGPT-3 Medium",
39
+ description="Генерация ответа с помощью модели Sberbank ruGPT-3 Medium"
40
  )
41
 
42
+ # Запуск интерфейса
43
  iface.launch()
44