Download RecommendationSystem/get_label.py from Chamsol/pms_rec_sys: direct link, hf CLI and curl.
- Browser
- Download file 2.47 kB
-
https://huggingface.co/Chamsol/pms_rec_sys/resolve/main/RecommendationSystem/get_label.py
- Command line
-
hf download hf://Chamsol/pms_rec_sys/RecommendationSystem/get_label.py
-
curl -L -o get_label.py https://huggingface.co/Chamsol/pms_rec_sys/resolve/main/RecommendationSystem/get_label.py
2.47 kB
| #get label | |
| import pandas as pd | |
| import os | |
| import pandas as pd | |
| from tqdm import tqdm | |
| import openai | |
| def get_completion(prompt, model="gpt-3.5-turbo", api_key=""): | |
| openai.api_key = api_key | |
| messages = [{"role": "user", "content": prompt}] | |
| response = openai.ChatCompletion.create( | |
| model=model, | |
| messages=messages, | |
| temperature=0, | |
| ) | |
| return response.choices[0].message["content"] | |
| def get_label(person_name, person_corpus: str, api_key: str) -> str: | |
| prompt = f'''### μν : | |
| λ€μ μ€λͺ μ κΈ°λ°μΌλ‘ {person_name}μ λν νκ°κ° "λ§€μ° κΈμ ", "κΈμ ", "λΆμ ", "λ§€μ° λΆμ " μ€ μ΄λ κ²μ ν΄λΉνλμ§ λΌλ²¨λ§ νμμ€. μ΄λ μλ κ·μΉμ λ°λ¦ λλ€. | |
| ### μ€λͺ : | |
| {person_corpus} | |
| ### κ·μΉ: | |
| 1. μ£Όμ΄μ§ μ€λͺ μ λν΄ νλμ ["λ§€μ° κΈμ "μ ν ν°κ°μΌλ‘ 3, "κΈμ "μ ν ν°κ°μΌλ‘ 1, "λΆμ "μ ν ν°κ°μΌλ‘ 0, "λ§€μ° λΆμ "μ ν ν°κ°μΌλ‘ 2]λ§μ μ ννμ¬ λΌλ²¨λ§νλ€. | |
| 2. μ€λͺ μ "### μ€λͺ :" λΆλΆλΆν° μμλλ€. | |
| 3. μΆλ ₯μ "### μΆλ ₯"μ κ°μ΄ json νμμΌλ‘ μΆλ ₯νλ€. | |
| 4. λΌλ²¨λ§μ μ€λͺ μ λ¬Έμ λν΄μ νλ€. | |
| 5. μ€λ¦½μ μ νμ§μ μλ€. | |
| 6. μΆλ ₯μ νμμΈμ μ무κ²λ μμ±νμ§ μλλ€. | |
| ### μΆλ ₯ | |
| ''' | |
| response = get_completion(prompt, "gpt-3.5-turbo", api_key) | |
| return response | |
| if __name__ == "__main__": | |
| path_file = "path" # λ°μ΄ν°μ νμΌ κ²½λ‘ μ§μ | |
| api_key = "OpenAi api key" # OpenAI API ν€ μ§μ | |
| pos_start = 0 | |
| len_slice = 500 | |
| df_people = pd.read_csv(path_file)[pos_start:pos_start+len_slice] | |
| df_people = df_people.drop_duplicates(["person_name"]) | |
| df_people = df_people.dropna() | |
| list_result = [] | |
| for idx, row in tqdm(df_people.iterrows(), total=df_people.shape[0]): | |
| try: | |
| label_response = get_label(row["person_name"], row["person_corpus"], api_key) | |
| list_result.append(label_response) | |
| except Exception as e: | |
| print(f"idx: {idx}, err: {e}") | |
| list_result.append(f"idx: {idx}, err: {e}") | |
| df_people["label"] = list_result | |
| df_people.to_csv(f"./labeled_people_{pos_start}_to_{pos_start + len_slice}.csv", index=False) |