Download app.py from Serg4451D/RuGPT: direct link, hf CLI and curl.
- Browser
- Download file 982 Bytes
-
https://huggingface.co/spaces/Serg4451D/RuGPT/resolve/main/app.py
- Command line
-
hf download hf://spaces/Serg4451D/RuGPT/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Serg4451D/RuGPT/resolve/main/app.py
982 Bytes
| import streamlit as st | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| import torch | |
| # Загрузка модели | |
| tokenizer = AutoTokenizer.from_pretrained("sberbank-ai/rugpt3small_based_on_gpt2") | |
| model = AutoModelForCausalLM.from_pretrained("sberbank-ai/rugpt3small_based_on_gpt2") | |
| device = torch.device("cpu") | |
| # Функция для генерации текста | |
| def generate_text(prompt): | |
| input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device) | |
| output = model.generate(input_ids, max_length=50, do_sample=True) | |
| generated_text = tokenizer.decode(output[0], skip_special_tokens=True) | |
| return generated_text | |
| # Заголовок страницы | |
| st.title("RuGPT-3 Demo") | |
| # Ввод текста пользователем | |
| prompt = st.text_input("Введите текст:") | |
| if prompt: | |
| # Генерация ответа | |
| response = generate_text(prompt) | |
| # Отображение ответа | |
| st.write(response) | |