Spaces:
Runtime error
Runtime error
Commit
•
c85bc12
1
Parent(s):
ca7af73
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
def predict(message, history):
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
return
|
8 |
|
9 |
demo = gr.ChatInterface(
|
10 |
predict,
|
11 |
-
title
|
12 |
)
|
13 |
|
14 |
-
demo.launch()
|
|
|
1 |
+
!pip install transformers
|
2 |
+
from transformers import GPTJForCausalLM, GPT2Tokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
|
6 |
+
# Загрузка токенизатора и модели
|
7 |
+
tokenizer = GPT2Tokenizer.from_pretrained("EleutherAI/gpt-j-6B")
|
8 |
+
model = GPTJForCausalLM.from_pretrained("EleutherAI/gpt-j-6B")
|
9 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
10 |
+
model = model.to(device)
|
11 |
+
|
12 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
+
model = model.to(device)
|
14 |
+
from transformers import MarianMTModel, MarianTokenizer
|
15 |
+
# Функции для перевода
|
16 |
+
def translate_to_english(text):
|
17 |
+
tokenizer = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-ru-en")
|
18 |
+
model = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-ru-en")
|
19 |
+
translated = model.generate(**tokenizer.prepare_seq2seq_batch([text], return_tensors="pt"))
|
20 |
+
return tokenizer.decode(translated[0])
|
21 |
+
|
22 |
+
def translate_to_russian(text):
|
23 |
+
tokenizer = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ru")
|
24 |
+
model = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-en-ru")
|
25 |
+
translated = model.generate(**tokenizer.prepare_seq2seq_batch([text], return_tensors="pt"))
|
26 |
+
return tokenizer.decode(translated[0])
|
27 |
+
|
28 |
+
def generate_response(input_text):
|
29 |
+
# Кодирование входного текста и генерация ответа
|
30 |
+
input_ids = tokenizer.encode(input_text, return_tensors='pt')
|
31 |
+
output = model.generate(input_ids, max_length=1500, num_return_sequences=1, pad_token_id=50256,
|
32 |
+
num_beams=5, early_stopping=True)
|
33 |
+
|
34 |
+
# Декодирование и возвращение ответа
|
35 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
36 |
+
return response
|
37 |
+
|
38 |
+
# Тестирование функции
|
39 |
+
input_text = "What is the capital of France?"
|
40 |
+
response = generate_response(input_text)
|
41 |
+
print(response)
|
42 |
+
|
43 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
44 |
+
model = model.to(device)
|
45 |
+
user_input = ""
|
46 |
+
while user_input.lower() != 'exit':
|
47 |
+
user_input = input("You: ")
|
48 |
+
if user_input.lower() != 'exit':
|
49 |
+
user_input_english = translate_to_english(user_input)
|
50 |
+
response_english = generate_response(user_input_english)
|
51 |
+
response_russian = translate_to_russian(response_english)
|
52 |
+
print(f"Bot: {response_russian}")
|
53 |
+
|
54 |
+
|
55 |
+
|
56 |
+
! pip install --upgrade gradio -qq
|
57 |
import gradio as gr
|
58 |
|
59 |
def predict(message, history):
|
60 |
+
user_input_english = translate_to_english(message)
|
61 |
+
response_english = generate_response(user_input_english)
|
62 |
+
response_russian = translate_to_russian(response_english)
|
63 |
+
return response_russian
|
64 |
|
65 |
demo = gr.ChatInterface(
|
66 |
predict,
|
67 |
+
title='LLM for MyChatENRU'
|
68 |
)
|
69 |
|
70 |
+
demo.launch()
|