Download utils.py from Ekimetrics/climate-question-answering: direct link, hf CLI and curl.
- Browser
- Download file 1.25 kB
-
https://huggingface.co/spaces/Ekimetrics/climate-question-answering/resolve/b123882f1f69aa645bdb0b6dbdb6d2dcd535a2d3/utils.py
- Command line
-
hf download hf://spaces/Ekimetrics/climate-question-answering@b123882f1f69aa645bdb0b6dbdb6d2dcd535a2d3/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/Ekimetrics/climate-question-answering/resolve/b123882f1f69aa645bdb0b6dbdb6d2dcd535a2d3/utils.py
1.25 kB
| import numpy as np | |
| import openai | |
| import os | |
| def is_climate_change_related(sentence: str, classifier) -> bool: | |
| """_summary_ | |
| Args: | |
| sentence (str): _description_ | |
| classifier (_type_): _description_ | |
| Returns: | |
| bool: _description_ | |
| """ | |
| results = classifier( | |
| sequences=sentence, | |
| candidate_labels=["climate change related", "non climate change related"], | |
| ) | |
| print(f" ## Result from is climate change related {results}") | |
| return results["labels"][np.argmax(results["scores"])] == "climate change related" | |
| def make_pairs(lst): | |
| """From a list of even lenght, make tupple pairs | |
| Args: | |
| lst (list): _description_ | |
| Returns: | |
| list: _description_ | |
| """ | |
| return [(lst[i], lst[i + 1]) for i in range(0, len(lst), 2)] | |
| def set_openai_api_key(text): | |
| """Set the api key and return chain.If no api_key, then None is returned. | |
| To do : add raise error & Warning message | |
| Args: | |
| text (str): openai api key | |
| Returns: | |
| str: Result of connection | |
| """ | |
| openai.api_key = os.environ["api_key"] | |
| if text.startswith("sk-") and len(text) > 10: | |
| openai.api_key = text | |
| return f"You're all set: this is your api key: {openai.api_key}" | |