Spaces:
Runtime error
Runtime error
yash161101
commited on
Commit
•
8af4edf
1
Parent(s):
4a67b49
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,39 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
import
|
|
|
4 |
import torch
|
5 |
-
|
6 |
-
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
|
11 |
-
|
12 |
-
|
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 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
st.
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|