Update app.py
Browse files
app.py
CHANGED
@@ -1,91 +1,56 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
|
|
|
|
3 |
import torch
|
|
|
|
|
|
|
|
|
4 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
5 |
-
|
6 |
-
first = """informal english: corn fields are all across illinois, visible once you leave chicago.\nTranslated into the Style of Abraham Lincoln: corn fields ( permeate illinois / span the state of illinois / ( occupy / persist in ) all corners of illinois / line the horizon of illinois / envelop the landscape of illinois ), manifesting themselves visibly as one ventures beyond chicago.\n\ninformal english:"""
|
7 |
-
|
8 |
@st.cache(allow_output_mutation=True)
|
9 |
def get_model():
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
12 |
model.to(device)
|
13 |
-
print("hello")
|
14 |
-
tokenizer = AutoTokenizer.from_pretrained("BigSalmon/InformalToFormalLincoln17")
|
15 |
-
print("hello")
|
16 |
return model, tokenizer
|
17 |
|
18 |
model, tokenizer = get_model()
|
19 |
-
|
20 |
-
st.text('''How To Make Prompt:
|
21 |
-
|
22 |
-
informal english: sega’s doing mobile gaming right with this approach to exploiting its back catalog, giving people a way to build up their library of classics on devices they’ll always have close to hand for cheap. it’s a far better experience than nintendo’s classic machine sales on its home gaming systems, which tend to be locked in and not passed between hardware generations.
|
23 |
-
Translated into the Style of Abraham Lincoln: pursuing the proper course, sega's foray into mobile gaming entails leveraging its back catalog of classics on portable devices that ( are certain to be in reach whenever / will be at the ready for use at all times ) ( without spending a fortune / at minimal cost ). such an endeavor comes as a welcome alternative to nintendo's propensity to ( sell / offer / market ) home gaming systems that are rigidly bound to a single hardware generation.
|
24 |
-
Translated into the Style of Abraham Lincoln: at a time when nintendo has become inflexible, ( stubbornly bent on / firmly set on / unyielding in its insistence on / steadfastly transfixed by / uncompromising in its commitment to / rigidly decided on ) consoles that are tethered to a fixed iteration, sega diligently curates its legacy of classic video games on handheld devices.
|
25 |
-
|
26 |
-
informal english: garage band has made people who know nothing about music good at creating music.
|
27 |
Translated into the Style of Abraham Lincoln: garage band ( offers the uninitiated in music the ability to produce professional-quality compositions / catapults those for whom music is an uncharted art the ability the realize masterpieces / stimulates music novice's competency to yield sublime arrangements / begets individuals of rudimentary musical talent the proficiency to fashion elaborate suites ).
|
28 |
-
|
29 |
informal english: chrome extensions can make doing regular tasks much easier to get done.
|
|
|
30 |
Translated into the Style of Abraham Lincoln: chrome extensions ( yield the boon of time-saving convenience / ( expedite the ability to / unlock the means to more readily ) accomplish everyday tasks / turbocharges the velocity with which one can conduct their obligations ).
|
31 |
-
|
32 |
informal english: broadband is finally expanding to rural areas, a great development that will thrust them into modern life.
|
33 |
-
Translated into the Style of Abraham Lincoln: broadband is ( ( finally / at last / after years of delay ) arriving in remote locations / springing to life in far-flung outposts / inching into even the most backwater corners of the nation ) that will
|
34 |
-
|
35 |
-
informal english:
|
36 |
-
Translated into the Style of Abraham Lincoln: the
|
37 |
-
|
38 |
informal english: corn fields are all across illinois, visible once you leave chicago.
|
39 |
Translated into the Style of Abraham Lincoln: corn fields ( permeate illinois / span the state of illinois / ( occupy / persist in ) all corners of illinois / line the horizon of illinois / envelop the landscape of illinois ), manifesting themselves visibly as one ventures beyond chicago.
|
40 |
-
|
41 |
-
informal english:
|
42 |
-
|
43 |
-
temp = st.sidebar.slider("Temperature", 0.7, 1.5)
|
44 |
-
number_of_outputs = st.sidebar.slider("Number of Outputs", 5, 50)
|
45 |
-
lengths = st.sidebar.slider("Length", 3, 10)
|
46 |
-
bad_words = st.text_input("Words You Do Not Want Generated", " core lemon height time ")
|
47 |
-
|
48 |
-
def run_generate(text, bad_words):
|
49 |
-
yo = []
|
50 |
-
input_ids = tokenizer.encode(text, return_tensors='pt').to(device)
|
51 |
-
res = len(tokenizer.encode(text))
|
52 |
-
bad_words = bad_words.split()
|
53 |
-
bad_word_ids = []
|
54 |
-
for bad_word in bad_words:
|
55 |
-
bad_word = " " + bad_word
|
56 |
-
ids = tokenizer(bad_word).input_ids
|
57 |
-
bad_word_ids.append(ids)
|
58 |
-
sample_outputs = model.generate(
|
59 |
-
input_ids,
|
60 |
-
do_sample=True,
|
61 |
-
max_length= res + lengths,
|
62 |
-
min_length = res + lengths,
|
63 |
-
top_k=50,
|
64 |
-
temperature=temp,
|
65 |
-
num_return_sequences=number_of_outputs,
|
66 |
-
bad_words_ids=bad_word_ids
|
67 |
-
)
|
68 |
-
for i in range(number_of_outputs):
|
69 |
-
e = tokenizer.decode(sample_outputs[i])
|
70 |
-
e = e.replace(text, "")
|
71 |
-
yo.append(e)
|
72 |
-
return yo
|
73 |
with st.form(key='my_form'):
|
74 |
-
|
75 |
submit_button = st.form_submit_button(label='Submit')
|
76 |
if submit_button:
|
77 |
-
translated_text = run_generate(text, bad_words)
|
78 |
-
st.write(translated_text if translated_text else "No translation found")
|
79 |
with torch.no_grad():
|
80 |
-
|
81 |
-
|
82 |
-
text3 = tokenizer.encode(text2)
|
83 |
-
myinput, past_key_values = torch.tensor([text3]), None
|
84 |
myinput = myinput
|
85 |
myinput= myinput.to(device)
|
86 |
logits, past_key_values = model(myinput, past_key_values = past_key_values, return_dict=False)
|
87 |
logits = logits[0,-1]
|
88 |
probabilities = torch.nn.functional.softmax(logits)
|
89 |
-
best_logits, best_indices = logits.topk(
|
90 |
-
best_words = [tokenizer.decode([idx.item()]) for idx in best_indices]
|
|
|
|
|
|
|
91 |
st.write(best_words)
|
|
|
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
|
9 |
+
st.title('GPT2:')
|
10 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
11 |
@st.cache(allow_output_mutation=True)
|
12 |
def get_model():
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained("BigSalmon/GPTNeo350MInformalToFormalLincoln")
|
14 |
+
model = AutoModelWithLMHead.from_pretrained("BigSalmon/InformalToFormalLincoln23")
|
15 |
+
#model = AutoModelWithLMHead.from_pretrained("BigSalmon/GPTNeo350MInformalToFormalLincoln5")
|
16 |
+
#model = AutoModelWithLMHead.from_pretrained("BigSalmon/GPTNeo350MInformalToFormalLincoln4")
|
17 |
+
#model = AutoModelWithLMHead.from_pretrained("BigSalmon/GPTNeo350MInformalToFormalLincoln3")
|
18 |
+
#model = AutoModelWithLMHead.from_pretrained("BigSalmon/GPTNeo350MInformalToFormalLincoln2")
|
19 |
+
#model = AutoModelWithLMHead.from_pretrained("BigSalmon/GPTNeo350MInformalToFormalLincoln")
|
20 |
model.to(device)
|
|
|
|
|
|
|
21 |
return model, tokenizer
|
22 |
|
23 |
model, tokenizer = get_model()
|
24 |
+
g = """informal english: garage band has made people who know nothing about music good at creating music.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
Translated into the Style of Abraham Lincoln: garage band ( offers the uninitiated in music the ability to produce professional-quality compositions / catapults those for whom music is an uncharted art the ability the realize masterpieces / stimulates music novice's competency to yield sublime arrangements / begets individuals of rudimentary musical talent the proficiency to fashion elaborate suites ).
|
|
|
26 |
informal english: chrome extensions can make doing regular tasks much easier to get done.
|
27 |
+
***
|
28 |
Translated into the Style of Abraham Lincoln: chrome extensions ( yield the boon of time-saving convenience / ( expedite the ability to / unlock the means to more readily ) accomplish everyday tasks / turbocharges the velocity with which one can conduct their obligations ).
|
|
|
29 |
informal english: broadband is finally expanding to rural areas, a great development that will thrust them into modern life.
|
30 |
+
Translated into the Style of Abraham Lincoln: broadband is ( ( finally / at last / after years of delay ) arriving in remote locations / springing to life in far-flung outposts / inching into even the most backwater corners of the nation ) that will leap-frog them into the twenty-first century.
|
31 |
+
***
|
32 |
+
informal english: google translate has made talking to people who do not share your language easier.
|
33 |
+
Translated into the Style of Abraham Lincoln: google translate ( imparts communicability to individuals whose native tongue differs / mitigates the trials of communication across linguistic barriers / hastens the bridging of semantic boundaries / mollifies the complexity of multilingual communication / avails itself to the internationalization of discussion / flexes its muscles to abet intercultural conversation / calms the tides of linguistic divergence ).
|
34 |
+
***
|
35 |
informal english: corn fields are all across illinois, visible once you leave chicago.
|
36 |
Translated into the Style of Abraham Lincoln: corn fields ( permeate illinois / span the state of illinois / ( occupy / persist in ) all corners of illinois / line the horizon of illinois / envelop the landscape of illinois ), manifesting themselves visibly as one ventures beyond chicago.
|
37 |
+
***
|
38 |
+
informal english:"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
with st.form(key='my_form'):
|
40 |
+
prompt = st.text_area(label='Enter sentence', value=g)
|
41 |
submit_button = st.form_submit_button(label='Submit')
|
42 |
if submit_button:
|
|
|
|
|
43 |
with torch.no_grad():
|
44 |
+
text = tokenizer.encode(prompt)
|
45 |
+
myinput, past_key_values = torch.tensor([text]), None
|
|
|
|
|
46 |
myinput = myinput
|
47 |
myinput= myinput.to(device)
|
48 |
logits, past_key_values = model(myinput, past_key_values = past_key_values, return_dict=False)
|
49 |
logits = logits[0,-1]
|
50 |
probabilities = torch.nn.functional.softmax(logits)
|
51 |
+
best_logits, best_indices = logits.topk(250)
|
52 |
+
best_words = [tokenizer.decode([idx.item()]) for idx in best_indices]
|
53 |
+
text.append(best_indices[0].item())
|
54 |
+
best_probabilities = probabilities[best_indices].tolist()
|
55 |
+
words = []
|
56 |
st.write(best_words)
|