Spaces:
Runtime error
Runtime error
init app
Browse files- README.md +4 -4
- main.py +58 -0
- requirements.txt +1 -0
README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
---
|
| 2 |
-
title: Text Classification
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: streamlit
|
| 7 |
sdk_version: 1.9.0
|
| 8 |
-
app_file:
|
| 9 |
-
pinned:
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Optimum Text Classification
|
| 3 |
+
emoji: ⭐⭐⭐
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: streamlit
|
| 7 |
sdk_version: 1.9.0
|
| 8 |
+
app_file: main.py
|
| 9 |
+
pinned: true
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
|
main.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""⭐ Text Classification with Optimum and ONNXRuntime
|
| 2 |
+
|
| 3 |
+
Author:
|
| 4 |
+
- @ChainYo - https://github.com/ChainYo
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import streamlit as st
|
| 8 |
+
|
| 9 |
+
from transformers import AutoTokenizer, AutoModel, pipeline
|
| 10 |
+
from optimum.onnxruntime import ORTModelForTextClassification
|
| 11 |
+
from optimum.pipelines import pipeline
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
MODEL_PATH = "ProsusAI/finbert"
|
| 15 |
+
|
| 16 |
+
st.set_page_config(page_title="Optimum Text Classification", page_icon="⭐")
|
| 17 |
+
st.title("🤗 Optimum Text Classification")
|
| 18 |
+
st.subheader("Classify financial text with 🤗 Optimum and ONNXRuntime")
|
| 19 |
+
st.markdown("""
|
| 20 |
+
[](https://github.com/ChainYo)
|
| 21 |
+
[](https://huggingface.co/ChainYo)
|
| 22 |
+
[](https://www.linkedin.com/in/thomas-chaigneau-dev/)
|
| 23 |
+
[](https://discord.gg/)
|
| 24 |
+
""")
|
| 25 |
+
|
| 26 |
+
if "tokenizer" not in st.session_state:
|
| 27 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 28 |
+
st.session_state["tokenizer"] = tokenizer
|
| 29 |
+
|
| 30 |
+
if "ort_model" not in st.session_state:
|
| 31 |
+
ort_model = ORTModelForTextClassification.from_pretrained(MODEL_PATH, from_transformers=True)
|
| 32 |
+
st.session_state["ort_model"] = ort_model
|
| 33 |
+
|
| 34 |
+
if "pt_model" not in st.session_state:
|
| 35 |
+
pt_model = AutoModel.from_pretrained(MODEL_PATH)
|
| 36 |
+
st.session_state["pt_model"] = pt_model
|
| 37 |
+
|
| 38 |
+
if "ort_pipeline" not in st.session_state:
|
| 39 |
+
ort_pipeline = pipeline(
|
| 40 |
+
"text-classification", tokenizer=st.session_state["tokenizer"], model=st.session_state["ort_model"]
|
| 41 |
+
)
|
| 42 |
+
st.session_state["ort_pipeline"] = ort_pipeline
|
| 43 |
+
|
| 44 |
+
if "pt_pipeline" not in st.session_state:
|
| 45 |
+
pt_pipeline = pipeline(
|
| 46 |
+
"text-classification", tokenizer=st.session_state["tokenizer"], model=st.session_state["pt_model"]
|
| 47 |
+
)
|
| 48 |
+
st.session_state["pt_pipeline"] = pt_pipeline
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
model_format = st.radio("Choose the model format", ("PyTorch", "ONNXRuntime"))
|
| 52 |
+
optimized = st.checkbox("Optimize the model for inference", value=False)
|
| 53 |
+
quantized = st.checkbox("Quantize the model", value=False)
|
| 54 |
+
|
| 55 |
+
if model_format == "PyTorch":
|
| 56 |
+
optimized.disabled = True
|
| 57 |
+
quantized.disabled = True
|
| 58 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
optimum[onnxruntime]==1.2.2
|