Spaces:
Runtime error
Runtime error
planetearth79
commited on
Commit
•
885c78b
1
Parent(s):
de6b363
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoModelForSequenceClassification
|
3 |
+
from transformers import AutoTokenizer
|
4 |
+
import torch
|
5 |
+
import pandas as pd
|
6 |
+
import numpy as np
|
7 |
+
|
8 |
+
# import os
|
9 |
+
|
10 |
+
# os.environ['KMP_DUPLICATE_LIB_OK']='True'
|
11 |
+
|
12 |
+
st.markdown("### Some Model")
|
13 |
+
# st.markdown("<img width=200px src='https://rozetked.me/images/uploads/dwoilp3BVjlE.jpg'>", unsafe_allow_html=True)
|
14 |
+
# ^-- можно показывать пользователю текст, картинки, ограниченное подмножество html - всё как в jupyter
|
15 |
+
loaded_tokenizer = AutoTokenizer.from_pretrained("test_model")
|
16 |
+
loaded_model = AutoModelForSequenceClassification.from_pretrained("test_model")
|
17 |
+
|
18 |
+
|
19 |
+
# title_text = st.text_area("TITLE HERE")
|
20 |
+
# ^-- показать текстовое поле. В поле text лежит строка, которая находится там в данный момент
|
21 |
+
|
22 |
+
# from transformers import pipeline
|
23 |
+
# pipe = pipeline("ner", "Davlan/distilbert-base-multilingual-cased-ner-hrl")
|
24 |
+
# raw_predictions = pipe(text)
|
25 |
+
# # тут уже знакомый вам код с huggingface.transformers -- его можно заменить на что угодно от fairseq до catboost
|
26 |
+
|
27 |
+
# st.markdown(f"{raw_predictions}")
|
28 |
+
# # выводим результаты модели в текстовое поле, на потеху пользователю
|
29 |
+
|
30 |
+
# title_text = st.text_area("TITLE HERE", "input your title")
|
31 |
+
title_text = st.text_input("TITLE HERE")
|
32 |
+
summary_text = st.text_area("SUMMARY HERE")
|
33 |
+
text = title_text + " " + summary_text
|
34 |
+
|
35 |
+
title_input = loaded_tokenizer(title_text, padding="max_length", truncation=True, return_tensors='pt')
|
36 |
+
with torch.no_grad():
|
37 |
+
title_res = loaded_model(**title_input)
|
38 |
+
title_probs = torch.softmax(title_res.logits, dim=1).cpu().numpy()[0]
|
39 |
+
st.markdown(" ".join(str(x) for x in list(title_probs)))
|
40 |
+
|
41 |
+
|
42 |
+
summary_input = loaded_tokenizer(summary_text, padding="max_length", truncation=True, return_tensors='pt')
|
43 |
+
with torch.no_grad():
|
44 |
+
summary_res = loaded_model(**summary_input)
|
45 |
+
summary_probs = torch.softmax(summary_res.logits, dim=1).cpu().numpy()[0]
|
46 |
+
st.markdown(" ".join(str(x) for x in list(summary_probs)))
|
47 |
+
|
48 |
+
|
49 |
+
text_input = loaded_tokenizer(text, padding="max_length", truncation=True, return_tensors='pt')
|
50 |
+
with torch.no_grad():
|
51 |
+
text_res = loaded_model(**text_input)
|
52 |
+
text_probs = torch.softmax(text_res.logits, dim=1).cpu().numpy()[0]
|
53 |
+
st.markdown(" ".join(str(x) for x in list(text_probs)))
|
54 |
+
|
55 |
+
|
56 |
+
probs = np.stack([title_probs, summary_probs, text_probs], axis=1)
|
57 |
+
|
58 |
+
chart_data = pd.DataFrame(
|
59 |
+
probs,
|
60 |
+
columns=["title", "summary", "title + summary"])
|
61 |
+
|
62 |
+
st.bar_chart(chart_data)
|