Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import os
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import sys
|
| 5 |
+
import urllib
|
| 6 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
| 7 |
+
import json
|
| 8 |
+
import torch
|
| 9 |
+
|
| 10 |
+
def generate(tokenizer, model, text, features):
|
| 11 |
+
generated = tokenizer("<|startoftext|> <|titlestart|>{}<|titleend|>".format(text), return_tensors="pt").input_ids
|
| 12 |
+
sample_outputs = model.generate(
|
| 13 |
+
generated, do_sample=True, top_k=50,
|
| 14 |
+
max_length=300, top_p=0.95, temperature=2.1, num_return_sequences=2
|
| 15 |
+
)
|
| 16 |
+
for i, sample_output in enumerate(sample_outputs):
|
| 17 |
+
decoded = tokenizer.decode(sample_output, skip_special_tokens=True).split(text)[1]
|
| 18 |
+
st.write(decoded)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def load_model():
|
| 22 |
+
tokenizer = torch.load('./tokenizer.pt')
|
| 23 |
+
model = torch.load('./model.pt', map_location=torch.device('cpu'))
|
| 24 |
+
return tokenizer, model
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def main():
|
| 28 |
+
tokenizer, model = load_model()
|
| 29 |
+
st.title("YouTube comments generating project")
|
| 30 |
+
st.header('YouTube comments generator')
|
| 31 |
+
|
| 32 |
+
st.sidebar.title("Features")
|
| 33 |
+
seed = 27834096
|
| 34 |
+
default_control_features = ["Количество комментариев", "Температура", "Top-p"]
|
| 35 |
+
|
| 36 |
+
control_features = default_control_features
|
| 37 |
+
|
| 38 |
+
# Insert user-controlled values from sliders into the feature vector.
|
| 39 |
+
features = {
|
| 40 |
+
"num": st.sidebar.slider("Количество комментариев", 0, 20, 1, 1),
|
| 41 |
+
"t": st.sidebar.slider("Температура", 0, 300, 180, 1),
|
| 42 |
+
"top_p": st.sidebar.slider("Top-p", 0, 100, 95, 5),
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
st.sidebar.title("Note")
|
| 46 |
+
st.sidebar.write(
|
| 47 |
+
"""
|
| 48 |
+
Изменяя значения, можно получить различные выводы модели
|
| 49 |
+
"""
|
| 50 |
+
)
|
| 51 |
+
st.sidebar.write(
|
| 52 |
+
"""
|
| 53 |
+
Значение температуры делится на 100
|
| 54 |
+
"""
|
| 55 |
+
)
|
| 56 |
+
st.sidebar.caption(f"Streamlit version `{st.__version__}`")
|
| 57 |
+
with st.form(key='my_form'):
|
| 58 |
+
url = st.text_input('Введите url видео на YouTube')
|
| 59 |
+
st.form_submit_button('Готово!')
|
| 60 |
+
|
| 61 |
+
params = {"format": "json", "url": url}
|
| 62 |
+
base_url = "https://www.youtube.com/oembed"
|
| 63 |
+
query_string = urllib.parse.urlencode(params)
|
| 64 |
+
base_url = base_url + "?" + query_string
|
| 65 |
+
|
| 66 |
+
with urllib.request.urlopen(base_url) as response:
|
| 67 |
+
response_text = response.read()
|
| 68 |
+
data = json.loads(response_text.decode())
|
| 69 |
+
st.write('Video Title: ' + data['title'])
|
| 70 |
+
st.video(url)
|
| 71 |
+
generate(tokenizer, model, data['title'], features)
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
main()
|