Rzhishchev commited on
Commit
4fdd156
1 Parent(s): 2d0c2ed

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import GPT2LMHeadModel, GPT2Tokenizer
3
+ import torch
4
+
5
+ DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
6
+
7
+ model_path = "/zhvanetsky_model_1"
8
+ tokenizer = GPT2Tokenizer.from_pretrained(model_path)
9
+ model = GPT2LMHeadModel.from_pretrained(model_path).to(DEVICE)
10
+
11
+ def generate_text(input_text):
12
+ model.eval()
13
+ input_ids = tokenizer.encode(input_text, return_tensors="pt").to(DEVICE)
14
+ with torch.no_grad():
15
+ out = model.generate(input_ids,
16
+ do_sample=True,
17
+ num_beams=10,
18
+ temperature=2.2,
19
+ top_p=0.85,
20
+ top_k=500,
21
+ max_length=100,
22
+ no_repeat_ngram_size=3,
23
+ num_return_sequences=3,
24
+ )
25
+ return tokenizer.decode(out[0], skip_special_tokens=True)
26
+
27
+ st.title("GPT-2 Text Generator")
28
+ user_input = st.text_area("Input Text", "Введите ваш текст")
29
+ if st.button("Generate"):
30
+ generated_output = generate_text(user_input)
31
+ st.text_area("Generated Text", generated_output)