{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"my ChatEXpRuEN GPTJ with gradio","metadata":{}},{"cell_type":"code","source":"pip install transformers\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T20:12:09.719963Z","iopub.execute_input":"2023-10-06T20:12:09.720345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from transformers import GPTJForCausalLM, GPT2Tokenizer\nimport torch\n\n\n# Загрузка токенизатора и модели\ntokenizer = GPT2Tokenizer.from_pretrained(\"EleutherAI/gpt-j-6B\")\nmodel = GPTJForCausalLM.from_pretrained(\"EleutherAI/gpt-j-6B\")\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2023-10-06T20:11:36.522812Z","iopub.execute_input":"2023-10-06T20:11:36.523207Z","iopub.status.idle":"2023-10-06T20:11:36.543290Z","shell.execute_reply.started":"2023-10-06T20:11:36.523174Z","shell.execute_reply":"2023-10-06T20:11:36.542303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = model.to(device)\nfrom transformers import MarianMTModel, MarianTokenizer\n# Функции для перевода\ndef translate_to_english(text):\n    tokenizer = MarianTokenizer.from_pretrained(\"Helsinki-NLP/opus-mt-ru-en\")\n    model = MarianMTModel.from_pretrained(\"Helsinki-NLP/opus-mt-ru-en\")\n    translated = model.generate(**tokenizer.prepare_seq2seq_batch([text], return_tensors=\"pt\"))\n    return tokenizer.decode(translated[0])\n\ndef translate_to_russian(text):\n    tokenizer = MarianTokenizer.from_pretrained(\"Helsinki-NLP/opus-mt-en-ru\")\n    model = MarianMTModel.from_pretrained(\"Helsinki-NLP/opus-mt-en-ru\")\n    translated = model.generate(**tokenizer.prepare_seq2seq_batch([text], return_tensors=\"pt\"))\n    return tokenizer.decode(translated[0])\n\ndef generate_response(input_text):\n    # Кодирование входного текста и генерация ответа\n    input_ids = tokenizer.encode(input_text, return_tensors='pt')\n    output = model.generate(input_ids, max_length=1500, num_return_sequences=1, pad_token_id=50256, \n                            num_beams=5, early_stopping=True)\n\n    # Декодирование и возвращение ответа\n    response = tokenizer.decode(output[0], skip_special_tokens=True)\n    return response\n\n# Тестирование функции\ninput_text = \"What is the capital of France?\"\nresponse = generate_response(input_text)\nprint(response)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T19:51:10.768788Z","iopub.execute_input":"2023-10-06T19:51:10.769191Z","iopub.status.idle":"2023-10-06T19:59:56.070154Z","shell.execute_reply.started":"2023-10-06T19:51:10.769161Z","shell.execute_reply":"2023-10-06T19:59:56.068633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel = model.to(device)\nuser_input = \"\"\nwhile user_input.lower() != 'exit':\n    user_input = input(\"You: \")\n    if user_input.lower() != 'exit':\n        user_input_english = translate_to_english(user_input)\n        response_english = generate_response(user_input_english)\n        response_russian = translate_to_russian(response_english)\n        print(f\"Bot: {response_russian}\")\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T19:59:56.439105Z","iopub.execute_input":"2023-10-06T19:59:56.439871Z","iopub.status.idle":"2023-10-06T20:00:00.399067Z","shell.execute_reply.started":"2023-10-06T19:59:56.439837Z","shell.execute_reply":"2023-10-06T20:00:00.398084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! pip install --upgrade gradio -qq\nimport gradio as gr\n\ndef predict(message, history):\n    user_input_english = translate_to_english(message)\n    response_english = generate_response(user_input_english)\n    response_russian = translate_to_russian(response_english)\n    return response_russian\n\ndemo = gr.ChatInterface(\n    predict,\n    title='LLM for MyChatENRU'\n)\n\ndemo.launch()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-06T20:03:50.070999Z","iopub.execute_input":"2023-10-06T20:03:50.071772Z","iopub.status.idle":"2023-10-06T20:04:15.817301Z","shell.execute_reply.started":"2023-10-06T20:03:50.071746Z","shell.execute_reply":"2023-10-06T20:04:15.816468Z"},"trusted":true},"execution_count":null,"outputs":[]}]}