yash161101 commited on
Commit
8af4edf
1 Parent(s): 4a67b49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -26
app.py CHANGED
@@ -1,31 +1,39 @@
1
  import streamlit as st
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
- import tensorflow as tf
 
4
  import torch
5
- #maximum number of words in output text
6
- # MAX_LEN = 30
 
7
 
8
- title = st.text_input('Enter the seed words', ' ')
9
- input_sequence = title
10
 
11
- number = st.number_input('Insert how many words', 1)
12
- MAX_LEN = number
13
-
14
- if st.button('Submit'):
15
-
16
- tokenizer = AutoTokenizer.from_pretrained("ml6team/gpt-2-medium-conditional-quote-generator")
17
  model = AutoModelForCausalLM.from_pretrained("ml6team/gpt-2-medium-conditional-quote-generator")
18
- inputs = tokenizer.encode(input_sequence, return_tensors="pt")
19
- # generate text until the output length (which includes the context length) reaches 50
20
- #greedy_output = GPT2.generate(input_ids, max_length = MAX_LEN)
21
- outputs = model(inputs)
22
- outputs = tf.strings.as_string(outputs)
23
-
24
- print("Output:\n" + 100 * '-')
25
- st.write(outputs)
26
- else:
27
- st.write(' ')
28
-
29
-
30
- # print("Output:\n" + 100 * '-')
31
- # print(tokenizer.decode(sample_output[0], skip_special_tokens = True), '...')
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import numpy as np
3
+ import pandas as pd
4
+ import os
5
  import torch
6
+ import torch.nn as nn
7
+ from transformers.activations import get_activation
8
+ from transformers import AutoTokenizer, AutoModelWithLMHead, AutoModelForCausalLM
9
 
10
+ st.title('GPT2:')
11
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
 
13
+ @st.cache(allow_output_mutation=True)
14
+ def get_model():
15
+ tokenizer = AutoTokenizer.from_pretrained("ml6team/gpt-2-medium-conditional-quote-generator")
 
 
 
16
  model = AutoModelForCausalLM.from_pretrained("ml6team/gpt-2-medium-conditional-quote-generator")
17
+ return model, tokenizer
18
+
19
+
20
+ model, tokenizer = get_model()
21
+ g = "life is a"
22
+ with st.form(key='my_form'):
23
+ prompt = st.text_area(label='Enter sentence', value=g)
24
+ submit_button = st.form_submit_button(label='Submit')
25
+ if submit_button:
26
+ with torch.no_grad():
27
+ text = tokenizer.encode(prompt)
28
+ myinput, past_key_values = torch.tensor([text]), None
29
+ myinput = myinput
30
+ myinput= myinput.to(device)
31
+ logits, past_key_values = model(myinput, past_key_values = past_key_values, return_dict=False)
32
+ logits = logits[0,-1]
33
+ probabilities = torch.nn.functional.softmax(logits)
34
+ best_logits, best_indices = logits.topk(350)
35
+ best_words = [tokenizer.decode([idx.item()]) for idx in best_indices]
36
+ text.append(best_indices[0].item())
37
+ best_probabilities = probabilities[best_indices].tolist()
38
+ words = []
39
+ st.write(best_words)