Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,32 @@
|
|
1 |
|
2 |
-
|
3 |
-
iface.launch()
|
4 |
-
|
5 |
-
|
6 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
7 |
import torch
|
8 |
import gradio as gr
|
9 |
|
10 |
# Загружаем токенизатор и модель
|
11 |
-
|
12 |
-
|
13 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
14 |
-
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
|
15 |
-
|
16 |
-
# Функция генерации диалогового ответа
|
17 |
-
def generate_zephyr_response(prompt):
|
18 |
-
messages = [
|
19 |
-
{"role": "system", "content": "Ты — дружелюбный и умный ассистент."},
|
20 |
-
{"role": "user", "content": prompt}
|
21 |
-
]
|
22 |
-
prompt_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
23 |
-
inputs = tokenizer(prompt_text, return_tensors="pt").to(model.device)
|
24 |
|
|
|
|
|
|
|
25 |
with torch.no_grad():
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# Интерфейс Gradio
|
31 |
iface = gr.Interface(
|
32 |
-
fn=
|
33 |
inputs=gr.Textbox(label="Введите ваш запрос"),
|
34 |
outputs=gr.Textbox(label="Ответ от ChatGPT"),
|
35 |
title="Интерфейс ChatGPT",
|
|
|
1 |
|
2 |
+
from transformers import AutoTokenizer, AutoModelWithLMHead
|
|
|
|
|
|
|
|
|
3 |
import torch
|
4 |
import gradio as gr
|
5 |
|
6 |
# Загружаем токенизатор и модель
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("sberbank-ai/rugpt3large_based_on_gpt2")
|
8 |
+
model = AutoModelWithLMHead.from_pretrained("sberbank-ai/rugpt3large_based_on_gpt2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Основная функция генерации
|
11 |
+
def generate_russian_response(prompt):
|
12 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
13 |
with torch.no_grad():
|
14 |
+
output = model.generate(
|
15 |
+
input_ids,
|
16 |
+
max_length=200,
|
17 |
+
do_sample=True,
|
18 |
+
top_k=50,
|
19 |
+
top_p=0.95,
|
20 |
+
temperature=0.9,
|
21 |
+
num_return_sequences=1,
|
22 |
+
pad_token_id=tokenizer.eos_token_id
|
23 |
+
)
|
24 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
25 |
+
return response
|
26 |
|
27 |
# Интерфейс Gradio
|
28 |
iface = gr.Interface(
|
29 |
+
fn=generate_russian_response,
|
30 |
inputs=gr.Textbox(label="Введите ваш запрос"),
|
31 |
outputs=gr.Textbox(label="Ответ от ChatGPT"),
|
32 |
title="Интерфейс ChatGPT",
|