Spaces:
Runtime error
Runtime error
inference api
Browse files- inference_hf/__init__.py +1 -0
- inference_hf/_inference.py +29 -0
- lrt/clustering/clustering_pipeline.py +1 -0
- lrt/lrt.py +3 -3
- lrt/utils/functions.py +71 -48
inference_hf/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
from ._inference import InferenceHF
|
inference_hf/_inference.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import requests
|
| 3 |
+
from typing import Union,List
|
| 4 |
+
|
| 5 |
+
class InferenceHF:
|
| 6 |
+
headers = {"Authorization": f"Bearer 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"}
|
| 7 |
+
API_URL = "https://api-inference.huggingface.co/models/"
|
| 8 |
+
|
| 9 |
+
@classmethod
|
| 10 |
+
def inference(cls, inputs: Union[List[str], str], model_name:str) ->dict:
|
| 11 |
+
payload = dict(
|
| 12 |
+
inputs = inputs,
|
| 13 |
+
options = dict(
|
| 14 |
+
wait_for_model=True
|
| 15 |
+
)
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
data = json.dumps(payload)
|
| 19 |
+
response = requests.request("POST", cls.API_URL+model_name, headers=cls.headers, data=data)
|
| 20 |
+
return json.loads(response.content.decode("utf-8"))
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
if __name__ == '__main__':
|
| 24 |
+
print(InferenceHF.inference(
|
| 25 |
+
inputs='hi how are you?',
|
| 26 |
+
model_name= 't5-small'
|
| 27 |
+
))
|
| 28 |
+
|
| 29 |
+
|
lrt/clustering/clustering_pipeline.py
CHANGED
|
@@ -5,6 +5,7 @@ from ..utils import __create_model__
|
|
| 5 |
from sklearn.cluster import KMeans
|
| 6 |
# from yellowbrick.cluster import KElbowVisualizer
|
| 7 |
from .clusters import ClusterList
|
|
|
|
| 8 |
class ClusterPipeline:
|
| 9 |
def __init__(self, config:Configuration = None):
|
| 10 |
if config is None:
|
|
|
|
| 5 |
from sklearn.cluster import KMeans
|
| 6 |
# from yellowbrick.cluster import KElbowVisualizer
|
| 7 |
from .clusters import ClusterList
|
| 8 |
+
|
| 9 |
class ClusterPipeline:
|
| 10 |
def __init__(self, config:Configuration = None):
|
| 11 |
if config is None:
|
lrt/lrt.py
CHANGED
|
@@ -72,7 +72,7 @@ class LiteratureResearchTool:
|
|
| 72 |
best_k: int = 5
|
| 73 |
) -> (ClusterList,ArticleList):
|
| 74 |
|
| 75 |
-
@st.cache(hash_funcs={Tokenizer: Tokenizer.__hash__})
|
| 76 |
def ieee_process(
|
| 77 |
query: str,
|
| 78 |
num_papers: int,
|
|
@@ -87,7 +87,7 @@ class LiteratureResearchTool:
|
|
| 87 |
clusters = self.__postprocess_clusters__(clusters)
|
| 88 |
return clusters, articles
|
| 89 |
|
| 90 |
-
@st.cache(hash_funcs={Tokenizer: Tokenizer.__hash__})
|
| 91 |
def arxiv_process(
|
| 92 |
query: str,
|
| 93 |
num_papers: int,
|
|
@@ -100,7 +100,7 @@ class LiteratureResearchTool:
|
|
| 100 |
clusters = self.__postprocess_clusters__(clusters)
|
| 101 |
return clusters, articles
|
| 102 |
|
| 103 |
-
@st.cache(hash_funcs={Tokenizer: Tokenizer.__hash__})
|
| 104 |
def pwc_process(
|
| 105 |
query: str,
|
| 106 |
num_papers: int,
|
|
|
|
| 72 |
best_k: int = 5
|
| 73 |
) -> (ClusterList,ArticleList):
|
| 74 |
|
| 75 |
+
@st.cache(hash_funcs={Tokenizer: Tokenizer.__hash__},allow_output_mutation=True)
|
| 76 |
def ieee_process(
|
| 77 |
query: str,
|
| 78 |
num_papers: int,
|
|
|
|
| 87 |
clusters = self.__postprocess_clusters__(clusters)
|
| 88 |
return clusters, articles
|
| 89 |
|
| 90 |
+
@st.cache(hash_funcs={Tokenizer: Tokenizer.__hash__},allow_output_mutation=True)
|
| 91 |
def arxiv_process(
|
| 92 |
query: str,
|
| 93 |
num_papers: int,
|
|
|
|
| 100 |
clusters = self.__postprocess_clusters__(clusters)
|
| 101 |
return clusters, articles
|
| 102 |
|
| 103 |
+
@st.cache(hash_funcs={Tokenizer: Tokenizer.__hash__},allow_output_mutation=True)
|
| 104 |
def pwc_process(
|
| 105 |
query: str,
|
| 106 |
num_papers: int,
|
lrt/utils/functions.py
CHANGED
|
@@ -4,6 +4,7 @@ from kmeans_pytorch import kmeans
|
|
| 4 |
import torch
|
| 5 |
from sklearn.cluster import KMeans
|
| 6 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM,Text2TextGenerationPipeline
|
|
|
|
| 7 |
|
| 8 |
class Template:
|
| 9 |
def __init__(self):
|
|
@@ -62,64 +63,86 @@ def __create_model__(model_ckpt):
|
|
| 62 |
return ret
|
| 63 |
|
| 64 |
elif model_ckpt == 'keyphrase-transformer':
|
| 65 |
-
tokenizer = AutoTokenizer.from_pretrained(template.keywords_extraction[model_ckpt])
|
| 66 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(template.keywords_extraction[model_ckpt])
|
| 67 |
-
pipe = Text2TextGenerationPipeline(model=model, tokenizer=tokenizer)
|
| 68 |
-
|
| 69 |
-
def ret(texts: List[str]):
|
| 70 |
-
tmp = pipe(texts)
|
| 71 |
-
results = [
|
| 72 |
-
set(
|
| 73 |
-
map(str.strip,
|
| 74 |
-
x['generated_text'].split('|') #[str...]
|
| 75 |
-
)
|
| 76 |
-
)
|
| 77 |
-
for x in tmp] # [{str...}...]
|
| 78 |
-
|
| 79 |
-
return results
|
| 80 |
-
|
| 81 |
-
return ret
|
| 82 |
-
|
| 83 |
-
elif model_ckpt == 'KeyBartAdapter':
|
| 84 |
model_ckpt = template.keywords_extraction[model_ckpt]
|
| 85 |
-
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
|
| 86 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(model_ckpt)
|
| 87 |
-
pipe = Text2TextGenerationPipeline(model=model,tokenizer=tokenizer)
|
| 88 |
|
| 89 |
def ret(texts: List[str]):
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
return ret
|
| 102 |
|
| 103 |
-
elif model_ckpt == 'KeyBart':
|
| 104 |
model_ckpt = template.keywords_extraction[model_ckpt]
|
| 105 |
-
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
|
| 106 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(model_ckpt)
|
| 107 |
-
pipe = Text2TextGenerationPipeline(model=model,tokenizer=tokenizer)
|
| 108 |
-
|
| 109 |
def ret(texts: List[str]):
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
return ret
|
| 122 |
-
|
| 123 |
else:
|
| 124 |
raise RuntimeError(f'The model {model_ckpt} is not supported. Please open an issue on the GitHub about the model.')
|
| 125 |
|
|
|
|
| 4 |
import torch
|
| 5 |
from sklearn.cluster import KMeans
|
| 6 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM,Text2TextGenerationPipeline
|
| 7 |
+
from inference_hf import InferenceHF
|
| 8 |
|
| 9 |
class Template:
|
| 10 |
def __init__(self):
|
|
|
|
| 63 |
return ret
|
| 64 |
|
| 65 |
elif model_ckpt == 'keyphrase-transformer':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
model_ckpt = template.keywords_extraction[model_ckpt]
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
def ret(texts: List[str]):
|
| 69 |
+
# first try inference API
|
| 70 |
+
response = InferenceHF.inference(
|
| 71 |
+
inputs=texts,
|
| 72 |
+
model_name=model_ckpt
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# inference failed:
|
| 76 |
+
if not isinstance(response, list):
|
| 77 |
+
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
|
| 78 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_ckpt)
|
| 79 |
+
pipe = Text2TextGenerationPipeline(model=model, tokenizer=tokenizer)
|
| 80 |
+
|
| 81 |
+
tmp = pipe(texts)
|
| 82 |
+
results = [
|
| 83 |
+
set(
|
| 84 |
+
map(str.strip,
|
| 85 |
+
x['generated_text'].split('|') # [str...]
|
| 86 |
+
)
|
| 87 |
+
)
|
| 88 |
+
for x in tmp] # [{str...}...]
|
| 89 |
+
|
| 90 |
+
return results
|
| 91 |
+
|
| 92 |
+
# inference sucsess
|
| 93 |
+
else:
|
| 94 |
+
results = [
|
| 95 |
+
set(
|
| 96 |
+
map(str.strip,
|
| 97 |
+
x['generated_text'].split('|') # [str...]
|
| 98 |
+
)
|
| 99 |
+
)
|
| 100 |
+
for x in response] # [{str...}...]
|
| 101 |
+
|
| 102 |
+
return results
|
| 103 |
|
| 104 |
return ret
|
| 105 |
|
| 106 |
+
elif model_ckpt == 'KeyBartAdapter' or model_ckpt == 'KeyBart':
|
| 107 |
model_ckpt = template.keywords_extraction[model_ckpt]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
def ret(texts: List[str]):
|
| 109 |
+
# first try inference API
|
| 110 |
+
response = InferenceHF.inference(
|
| 111 |
+
inputs=texts,
|
| 112 |
+
model_name=model_ckpt
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
# inference failed:
|
| 116 |
+
if not isinstance(response,list):
|
| 117 |
+
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
|
| 118 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_ckpt)
|
| 119 |
+
pipe = Text2TextGenerationPipeline(model=model, tokenizer=tokenizer)
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
tmp = pipe(texts)
|
| 123 |
+
results = [
|
| 124 |
+
set(
|
| 125 |
+
map(str.strip,
|
| 126 |
+
x['generated_text'].split(';') # [str...]
|
| 127 |
+
)
|
| 128 |
+
)
|
| 129 |
+
for x in tmp] # [{str...}...]
|
| 130 |
+
|
| 131 |
+
return results
|
| 132 |
+
|
| 133 |
+
# inference sucsess
|
| 134 |
+
else:
|
| 135 |
+
results = [
|
| 136 |
+
set(
|
| 137 |
+
map(str.strip,
|
| 138 |
+
x['generated_text'].split(';') # [str...]
|
| 139 |
+
)
|
| 140 |
+
)
|
| 141 |
+
for x in response] # [{str...}...]
|
| 142 |
+
|
| 143 |
+
return results
|
| 144 |
|
| 145 |
return ret
|
|
|
|
| 146 |
else:
|
| 147 |
raise RuntimeError(f'The model {model_ckpt} is not supported. Please open an issue on the GitHub about the model.')
|
| 148 |
|