Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,82 @@
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This file is .....
|
2 |
+
# Author: Hanbin Wang
|
3 |
+
# Date: 2023/4/18
|
4 |
+
import transformers
|
5 |
import streamlit as st
|
6 |
+
from PIL import Image
|
7 |
|
8 |
+
from transformers import RobertaTokenizer, T5ForConditionalGeneration
|
9 |
+
|
10 |
+
@st.cache_resource
|
11 |
+
def load_model(model_name):
|
12 |
+
# load model
|
13 |
+
model = T5ForConditionalGeneration.from_pretrained("hanbin/MaMaL-Com")
|
14 |
+
# load tokenizer
|
15 |
+
tokenizer = RobertaTokenizer.from_pretrained("hanbin/MaMaL-Com")
|
16 |
+
return model,tokenizer
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
def main(model,tokenizer):
|
23 |
+
# `st.set_page_config` is used to display the default layout width, the title of the app, and the emoticon in the browser tab.
|
24 |
+
|
25 |
+
st.set_page_config(
|
26 |
+
layout="centered", page_title="MaMaL-Gen Demo(代码生成)", page_icon="❄️"
|
27 |
+
)
|
28 |
+
|
29 |
+
c1, c2 = st.columns([0.32, 2])
|
30 |
+
|
31 |
+
# The snowflake logo will be displayed in the first column, on the left.
|
32 |
+
|
33 |
+
with c1:
|
34 |
+
st.image(
|
35 |
+
"images/panda.png",
|
36 |
+
width=100,
|
37 |
+
)
|
38 |
+
|
39 |
+
# The heading will be on the right.
|
40 |
+
|
41 |
+
with c2:
|
42 |
+
st.caption("")
|
43 |
+
st.title("MaMaL-Gen(代码生成)")
|
44 |
+
|
45 |
+
|
46 |
+
############ SIDEBAR CONTENT ############
|
47 |
+
|
48 |
+
st.sidebar.image("images/panda.png",width=270)
|
49 |
+
|
50 |
+
st.sidebar.write("")
|
51 |
+
|
52 |
+
# For elements to be displayed in the sidebar, we need to add the sidebar element in the widget.
|
53 |
+
|
54 |
+
# We create a text input field for users to enter their API key.
|
55 |
+
|
56 |
+
API_KEY = st.sidebar.text_input(
|
57 |
+
"Enter your HuggingFace API key",
|
58 |
+
help="Once you created you HuggingFace account, you can get your free API token in your settings page: https://huggingface.co/settings/tokens",
|
59 |
+
type="password",
|
60 |
+
)
|
61 |
+
|
62 |
+
# Adding the HuggingFace API inference URL.
|
63 |
+
API_URL = "https://api-inference.huggingface.co/models/valhalla/distilbart-mnli-12-3"
|
64 |
+
|
65 |
+
# Now, let's create a Python dictionary to store the API headers.
|
66 |
+
headers = {"Authorization": f"Bearer {API_KEY}"}
|
67 |
+
|
68 |
+
|
69 |
+
st.sidebar.markdown("---")
|
70 |
+
|
71 |
+
|
72 |
+
# Let's add some info about the app to the sidebar.
|
73 |
+
|
74 |
+
st.sidebar.write(
|
75 |
+
"""
|
76 |
+
App 由 东北大学NLP课小组成员创建, 使用 [Streamlit](https://streamlit.io/)🎈 和 [HuggingFace](https://huggingface.co/inference-api)'s [MaMaL-Gen](https://huggingface.co/hanbin/MaMaL-Gen) 模型.
|
77 |
+
"""
|
78 |
+
)
|
79 |
+
|
80 |
+
if __name__ == '__main__':
|
81 |
+
model, tokenizer = load_model("hanbin/MaMaL-Gen")
|
82 |
+
main(model, tokenizer)
|