Spaces:
Sleeping
Sleeping
Upload utils.py with huggingface_hub
Browse files
utils.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
import random
|
| 5 |
+
import string
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def is_climate_change_related(sentence: str, classifier) -> bool:
|
| 9 |
+
"""_summary_
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
sentence (str): your sentence to classify
|
| 13 |
+
classifier (_type_): zero shot hugging face pipeline classifier
|
| 14 |
+
|
| 15 |
+
Returns:
|
| 16 |
+
bool: is_climate_change_related or not
|
| 17 |
+
"""
|
| 18 |
+
results = classifier(
|
| 19 |
+
sequences=sentence,
|
| 20 |
+
candidate_labels=["climate change related", "non climate change related"],
|
| 21 |
+
)
|
| 22 |
+
print(f" ## Result from is climate change related {results}")
|
| 23 |
+
return results["labels"][np.argmax(results["scores"])] == "climate change related"
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def make_pairs(lst):
|
| 27 |
+
"""From a list of even lenght, make tupple pairs
|
| 28 |
+
Args:
|
| 29 |
+
lst (list): a list of even lenght
|
| 30 |
+
Returns:
|
| 31 |
+
list: the list as tupple pairs
|
| 32 |
+
"""
|
| 33 |
+
assert not (l := len(lst) % 2), f"your list is of lenght {l} which is not even"
|
| 34 |
+
return [(lst[i], lst[i + 1]) for i in range(0, len(lst), 2)]
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def set_openai_api_key(text):
|
| 38 |
+
"""Set the api key and return chain.If no api_key, then None is returned.
|
| 39 |
+
To do : add raise error & Warning message
|
| 40 |
+
Args:
|
| 41 |
+
text (str): openai api key
|
| 42 |
+
Returns:
|
| 43 |
+
str: Result of connection
|
| 44 |
+
"""
|
| 45 |
+
openai.api_key = os.environ["api_key"]
|
| 46 |
+
|
| 47 |
+
if text.startswith("sk-") and len(text) > 10:
|
| 48 |
+
openai.api_key = text
|
| 49 |
+
return f"You're all set: this is your api key: {openai.api_key}"
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def create_user_id(length):
|
| 53 |
+
"""Create user_id
|
| 54 |
+
Args:
|
| 55 |
+
length (int): length of user id
|
| 56 |
+
Returns:
|
| 57 |
+
str: String to id user
|
| 58 |
+
"""
|
| 59 |
+
letters = string.ascii_lowercase
|
| 60 |
+
user_id = "".join(random.choice(letters) for i in range(length))
|
| 61 |
+
return user_id
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def to_completion(messages):
|
| 65 |
+
s = []
|
| 66 |
+
for message in messages:
|
| 67 |
+
s.append(f"<|im_start|>{message['role']}\n{message['content']}<|im_end|>")
|
| 68 |
+
s.append("<|im_start|>assistant\n")
|
| 69 |
+
return "\n".join(s)
|