cl_nlp: Perform sentiment analysis (e.g., financial reports, analyst reports, reviews, classical texts) and news classificationtp_nlp: Labeling Inspiration Based on Unsupervised Text Clusteringcl_nlp_findtrain : Training Sample Expansion Based on Text Similaritycl_nlp_train, cl_nlp_pred: Model Training and Prediction Based on BERTgai_nlp: Batch Calls to Generative AI InterfacesMost text analysis tasks in economic research fall under the classification problem. For instance, sentiment analysis (positive sentiment vs. negative sentiment), forward-looking analysis (forward-looking vs. non-forward-looking), and whether a topic is related to digital transformation (related vs. not related to digital transformation).
Prof. Melissa Dell (2024) provided a highly practical workflow for text classification tasks in economic research. This update fully implements and supplements that workflow.
(Dell, 2024)
In brief, this chart highlights two points:
First, there are two main approaches to implementing machine learning for text classification
Second, before using supervised learning, embeddings can be used to assist in labeling inspiration and training set expansion
This update provides magic commands to implement these two functions:
Personally, I really admire Prof. Melissa Dell's work—rigorous, practical, and deep. Additionally, here is a note on Prof. Melissa Dell's latest article on how deep learning applies to economic research Article Note
Dell, M. (2024). Deep learning for economists (No. w32768). National Bureau of Economic Research.
Usage - Environment Configuration carefully before installation.pip install codecon --upgrade -i https://pypi.org/simple
pip install codecon --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple
git clone https://github.com/mickwzh/codecon.git
Baidu Netdisk: https://pan.baidu.com/s/1dIrQQnOl6spZv6Jet48eQA?pwd=dvqm Access Code: dvqm
Dropbox: https://www.dropbox.com/scl/fo/9ut3qtfradrde9tp6kslc/ANPvqvIgi2Lkf5t6TDuqiCc?rlkey=iw3seyzgpz9utmnj6xtdgej22&st=cywzuwmc&dl=0
An environment refers to a set of tools meeting the required functionalities. One of Python’s conveniences is the large number of pre-packaged tools; however, these packages often depend on other packages.
The convenience of Python lies in its many pre-packaged toolkits; however, these toolkits often depend on other toolkits.
A and B, use C1 and C2 respectively,C1 and C2 are two different versions of C, and usually only one version of C can be installed in one environment.A requires C1, and using B requires C2.A is installed first, installing B will cause an error (due to incompatible versions of C); vice versa is true.If you install codecon via pip, it will automatically configure an environment for codecon, provided your existing environment does not contain any packages conflicting with those required by codecon.
Therefore, it is recommended to configure a new environment before installing codecon.
Here are two options suitable for beginners to create a new environment without coding: ANACONDA & Spyder (local) and Featurize & JupyterNote (online). Click to access the respective PDF guides.
⚠️ If your computer does not have a GPU, or cannot ensure stable operation for long periods, Featurize (server rental) is strongly recommended Featurize (server rental).
To reduce user workload as much as possible, codecon’s full workflow for implementing text classification tasks requires only two simple tables. Please strictly follow the guidelines below to prepare the data:
Create a new folder in a preferred path; all resulting and intermediate files will be automatically saved in this folder.
Prepare the raw file (raw_file) and prediction file (pred_file) in this folder, supporting .csv, .xls, and .xlsx formats. .xls is used as an example.
raw_file.xls contains manually labeled data, with text and label columns. text contains the text, and label contains corresponding labels (starting with 0 as consecutive integers).
label must start from 0 as consecutive integers (0,1,2,...). It must start from 0, be integers, and be consecutive.text and label, without variation.| text | label |
|---|---|
| Apple is yummy | 1 |
| My new apple phone sucks | 0 |
| Apple is a fruit | 1 |
| ... | ... |
| text |
|---|
| Apple is red |
| Apple stock be in the worst |
| Apple is more popular than banana |
| ... |
🌟 Call pre-trained models for text classification to perform sentiment analysis (financial reports, analyst reports, reviews, classical texts, etc.) and news classification in one line of code.
pred_file)pred_file.xls is the file that needs labeling. It must include a text column.raw_file.xls.data_pred: Path to the pred_file.language: Specify the language of the text. Use 'chn' for Chinese or 'eng' for English.method: Choose a method from the table below.import codecon
codecon.cl_nlp(data_pred = 'Replace_with_your_pred_file_path',
method = 'financial_sentiment', # Choose the method from the table below (continuously updated...)
language = 'chn') # 'chn' or 'eng'
If running locally and encountering the error RuntimeError...HTTPSConnectionPool(host='huggingface.co', port=443), enable a global VPN and rerun the code. Alternatively, set a mirror path before importing codecon, as shown below. (Using a rented server avoids such issues.) (How to rent a server)
import os
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
import codecon
codecon.cl_nlp(data_pred = 'Replace_with_your_pred_file_path',
method = 'financial_sentiment', # Choose the method from the table below (continuously updated...)
language = 'chn') # 'chn' or 'eng'
| Method | Features | Possible Output | Model |
|---|---|---|---|
'financial_sentiment' | Sentiment analysis for financial texts (e.g., financial reports, audit reports) | positive, neutral, negative | bardsai/finance-sentiment-zh-base |
'reviews_sentiment' | Sentiment analysis for daily texts (e.g., social media posts, reviews) | negative, positive | liam168/c2-roberta-base-finetuned-dianping-chinese |
'guwen_sentiment' | Sentiment analysis for classical Chinese texts | Neg (Negative), ImpNeg (Slightly Negative), Neutral, ImpPos (Slightly Positive), Pos (Positive) | ethanyt/guwen-sent |
'news_classification' | Automatic news classification | LABEL_0 (Livelihood), LABEL_1 (Culture), LABEL_2 (Entertainment), LABEL_3 (Sports), LABEL_4 (Finance), LABEL_6 (Real Estate), LABEL_7 (Automotive), LABEL_8 (Education), LABEL_9 (Tech), LABEL_10 (Military), LABEL_12 (Travel), LABEL_13 (International), LABEL_14 (Securities), LABEL_15 (Agriculture), LABEL_16 (Esports) | myml/toutiao |
| Method | Features | Possible Output | Model |
|---|---|---|---|
'financial_sentiment' | Sentiment analysis for financial texts (e.g., financial reports, audit reports) | positive, neutral, negative | ProsusAI/finbert |
'reviews_sentiment' | Sentiment analysis for daily texts (e.g., social media posts, reviews) | positive, neutral, negative | cardiffnlp/twitter-roberta-base-sentiment-latest |
'news_classification' | Automatic news classification | arts_&culture, business&entrepreneurs, celebrity&pop_culture, diaries&daily_life, family, fashion&style, film_tv&video, fitness&health, food&dining, gaming, learning&educational, music, news&social_concern, other_hobbies, relationships, science&technology, sports, travel&adventure, youth&_student_life | cardiffnlp/tweet-topic-21-multi |
pred_file.xlspred_results_method.csv: Labels each text in pred_file.xls with a category (label) and provides the probability (score) for each label (indicating how likely the model considers the text to belong to that category, which can be used as a continuous variable).🌟Cluster raw text using text vectors generated from different models in an unsupervised manner.
pred_file.xls contains the data to be labeled, with a text column.raw_file.xls.data_pred: Enter the path to the pred_file.language: Language of the text to process, enter 'chn' for Chinese, 'eng' for English.method: Model to use for clustering, enter 'LDA' for LDA-based methods or 'BERTopic' for BERT-based deep learning.n_topic: Desired number of clusters. If not entered, it will automatically select the cluster count.import codecon
codecon.tp_nlp(data_pred='replace with your pred_file path',
language='chn', #'chn' or 'eng'
method='BERTopic', #'LDA' or 'BERTopic'
n_topic=None) #enter the desired number of clusters
pred_file.xls folderlabeled_data_pred_BERTopic.csv / labeled_data_pred_LDA.csv: Labels each text in pred_file.xls by categorytopics_description_BERTopic.csv / topics_description_LDA.csv: Information for each category (representative key🌟Calculate text similarity using text vectors generated from different models, selecting high-similarity samples to expand the training set.
raw_file.xls can contain multiple categories, and the model will output high-similarity text for each category.Category X. In this case, the similarity between a text in pred_file.xls and Category X = the average similarity with each text in Category X.
label must start from 0 as consecutive integers (0,1,2,...). It must start from 0, be integers, and be consecutive.text and label, without variation.data_pred: Enter the path to pred_file.xls.data_raw: Enter the path to raw_file.xls.language: Language of the text to process, enter 'chn' for Chinese, 'eng' for English.method: Model for similarity calculation (to generate text vectors).
tfidf: TF-IDF-based vectors, not recommended for smaller text quantities.word2vec: word2vec-based vectors, not recommended for smaller text quantities.cosent: BERT-based algorithm cosent for text vectors (recommended), suitable for most scenarios.threshold: Set the threshold for high similarity samples (similarity percentage). The higher this value, the stricter the selection, yielding fewer extended texts.import codecon
codecon.cl_nlp_findtrain(data_pred='replace with your pred_file path',
data_raw='replace with your raw_file path',
language='eng', #'chn' or 'eng'
method='cosent', #'tfidf' or 'word2vec' or 'cosent'
threshold=80) #integer from 0-100
pred_file.xls folderlabel_{1,2...}_Extended_Results.csv: Each file corresponds to an extended sample for each category in raw_file.
cosent model generally performs well, if category distinctions rely heavily on specific keywords, tfidf or word2vec may yield better results.BERT🌟Select 20% of the labeled data from raw_file.xls as the test set and use the remaining 80% as the training set to fine-tune a pre-trained BERT model. The fine-tuned model is then used to make predictions on pred_file.xls.
pred_file.xls) and the labeled raw file (raw_file.xls).⚠️ label must start from 0 as consecutive integers (0,1,2,...). It must start from 0, be integers, and be consecutive.
⚠️ Column names must be text and label, without variation.
data_raw: Enter the path to raw_file.xls.language: Language of the text to process, enter 'chn' for Chinese, 'eng' for English.imbalance: Specify if category distribution is balanced or unbalanced (try both if unsure).mode: Choose between speed-focused or quality-focused training.epoch: Number of training epochs, not always better when higher. The model has preset parameters suitable for most cases. Adjust based on the loss curve in the results if needed.batch_size: The sample size per learning iteration. More samples generally yield smoother training but are limited by hardware. codecon will auto-select an optimal size (typically 6–12). If memory issues arise, reduce this value manually.⚠️ If "GPU not available" appears during execution, renting a server is strongly recommended, as training will otherwise be very slow (Server Rental Guide)
import codecon
codecon.cl_nlp_train(data_raw='replace with your raw_file path',
language='chn', #'chn' or 'eng'
imbalance='balance', #'balance' or 'imbalance'
mode='timefirst', #'timefirst' or 'qualityfirst'
epoch=None, #optional, enter a positive integer if needed
batch_size=None) #optional, enter a positive integer if needed
data_raw.xls:
train_confusion_matrix.png: Confusion matrixtrain_model_performance.txt: Recall, accuracy, and f1 scorestrain_test_label.csv: Original and predicted labels in the 20% test set, helping to identify which types of text the model struggles to classifymodel: Trained model parameters, ready to be called in the following steps without additional actionsbenchmark: Classification fundamentally involves predicting the probability for each category, selecting the category with the highest probability. However, in economic text analysis tasks, not all categories can always be predetermined. For instance, in a binary classification problem, suppose the model assigns Text X a 49% probability for type1 and 51% for type2, thus categorizing it as type2. Yet, in reality, Text X may not belong to either type1 or type2.
benchmark = 0 enforces classification for all samples; benchmark = 80 means a sample is only categorized if the model assigns a probability greater than 80% to a specific category (confidence level of 80%).benchmark, the sample is labeled as -1 (labels = -1).import codecon
codecon.cl_nlp_pred(data_pred='replace with your pred_file path',
model_path=None, # omit if pred_file and model are in the same folder; otherwise specify the model path
language='chn', #'chn' or 'eng' (keep consistent with STEP 2)
benchmark=0, # enter an integer between 0-100
mode='timefirst', #'timefirst' or 'qualityfirst' (keep consistent with STEP 2)
batch_size=None) # optional, enter a positive integer if needed
pred_results.csv in the same folder as pred_file.xls🌟Use the Kimi large model as an interface. You must obtain an API key from the Kimi platform before use (simple steps below), and codecon does not charge any fees.
key.pred_filepred_file.xls contains the data to be labeled, with a text column.model: Select from moonshot-v1-8k, moonshot-v1-32k, or moonshot-v1-128k based on input-output length requirements.task: Enter a clear task description that will be executed on each line in pred_file.xls.
⚠️ Designing task descriptions involves structuring and step-by-step instructions. Testing a small sample first is recommended. If results are satisfactory, proceed with batch processing.
import codecon
codecon.gai_nlp(data_pred='replace with your pred_file path',
model="moonshot-v1-8k", #'moonshot-v1-8k', 'moonshot-v1-32k' or 'moonshot-v1-128k'
key="replace with the API Key obtained in STEP 1",
task="default task") # enter your task description
label_gai_Results.csv will be output in the same folder as pred_file.xls. In addition to saving after completion, the process saves every 10 records.label_gai_Results.csv and restart the command from the last saved point.codecon immediately, the following steps might help.cl_nlp_train or cl_nlp_pred, renting a server is highly recommended.Click Here to install the beginner-friendly Spyder.
PyTorch, then click Start.App Market, as it may not ensure the correct environment configuration.Jupyter Notebook.!pip install codecon
The project code is still in the early stages. If you have improvements, feel free to submit them back to the project. Before submitting, please note the following:
tests.Once done, you can submit a PR.
cl_nlp: Perform sentiment analysis (e.g., financial reports, analyst reports, reviews, classical texts) and news classificationtp_nlp: Labeling Inspiration Based on Unsupervised Text Clusteringcl_nlp_findtrain : Training Sample Expansion Based on Text Similaritycl_nlp_train, cl_nlp_pred: Model Training and Prediction Based on BERTgai_nlp: Batch Calls to Generative AI InterfacesMost text analysis tasks in economic research fall under the classification problem. For instance, sentiment analysis (positive sentiment vs. negative sentiment), forward-looking analysis (forward-looking vs. non-forward-looking), and whether a topic is related to digital transformation (related vs. not related to digital transformation).
Prof. Melissa Dell (2024) provided a highly practical workflow for text classification tasks in economic research. This update fully implements and supplements that workflow.
(Dell, 2024)
In brief, this chart highlights two points:
First, there are two main approaches to implementing machine learning for text classification
Second, before using supervised learning, embeddings can be used to assist in labeling inspiration and training set expansion
This update provides magic commands to implement these two functions:
Personally, I really admire Prof. Melissa Dell's work—rigorous, practical, and deep. Additionally, here is a note on Prof. Melissa Dell's latest article on how deep learning applies to economic research Article Note
Dell, M. (2024). Deep learning for economists (No. w32768). National Bureau of Economic Research.
Usage - Environment Configuration carefully before installation.pip install codecon --upgrade -i https://pypi.org/simple
pip install codecon --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple
git clone https://github.com/mickwzh/codecon.git
Baidu Netdisk: https://pan.baidu.com/s/1dIrQQnOl6spZv6Jet48eQA?pwd=dvqm Access Code: dvqm
Dropbox: https://www.dropbox.com/scl/fo/9ut3qtfradrde9tp6kslc/ANPvqvIgi2Lkf5t6TDuqiCc?rlkey=iw3seyzgpz9utmnj6xtdgej22&st=cywzuwmc&dl=0
An environment refers to a set of tools meeting the required functionalities. One of Python’s conveniences is the large number of pre-packaged tools; however, these packages often depend on other packages.
The convenience of Python lies in its many pre-packaged toolkits; however, these toolkits often depend on other toolkits.
A and B, use C1 and C2 respectively,C1 and C2 are two different versions of C, and usually only one version of C can be installed in one environment.A requires C1, and using B requires C2.A is installed first, installing B will cause an error (due to incompatible versions of C); vice versa is true.If you install codecon via pip, it will automatically configure an environment for codecon, provided your existing environment does not contain any packages conflicting with those required by codecon.
Therefore, it is recommended to configure a new environment before installing codecon.
Here are two options suitable for beginners to create a new environment without coding: ANACONDA & Spyder (local) and Featurize & JupyterNote (online). Click to access the respective PDF guides.
⚠️ If your computer does not have a GPU, or cannot ensure stable operation for long periods, Featurize (server rental) is strongly recommended Featurize (server rental).
To reduce user workload as much as possible, codecon’s full workflow for implementing text classification tasks requires only two simple tables. Please strictly follow the guidelines below to prepare the data:
Create a new folder in a preferred path; all resulting and intermediate files will be automatically saved in this folder.
Prepare the raw file (raw_file) and prediction file (pred_file) in this folder, supporting .csv, .xls, and .xlsx formats. .xls is used as an example.
raw_file.xls contains manually labeled data, with text and label columns. text contains the text, and label contains corresponding labels (starting with 0 as consecutive integers).
label must start from 0 as consecutive integers (0,1,2,...). It must start from 0, be integers, and be consecutive.text and label, without variation.| text | label |
|---|---|
| Apple is yummy | 1 |
| My new apple phone sucks | 0 |
| Apple is a fruit | 1 |
| ... | ... |
| text |
|---|
| Apple is red |
| Apple stock be in the worst |
| Apple is more popular than banana |
| ... |
🌟 Call pre-trained models for text classification to perform sentiment analysis (financial reports, analyst reports, reviews, classical texts, etc.) and news classification in one line of code.
pred_file)pred_file.xls is the file that needs labeling. It must include a text column.raw_file.xls.data_pred: Path to the pred_file.language: Specify the language of the text. Use 'chn' for Chinese or 'eng' for English.method: Choose a method from the table below.import codecon
codecon.cl_nlp(data_pred = 'Replace_with_your_pred_file_path',
method = 'financial_sentiment', # Choose the method from the table below (continuously updated...)
language = 'chn') # 'chn' or 'eng'
If running locally and encountering the error RuntimeError...HTTPSConnectionPool(host='huggingface.co', port=443), enable a global VPN and rerun the code. Alternatively, set a mirror path before importing codecon, as shown below. (Using a rented server avoids such issues.) (How to rent a server)
import os
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
import codecon
codecon.cl_nlp(data_pred = 'Replace_with_your_pred_file_path',
method = 'financial_sentiment', # Choose the method from the table below (continuously updated...)
language = 'chn') # 'chn' or 'eng'
| Method | Features | Possible Output | Model |
|---|---|---|---|
'financial_sentiment' | Sentiment analysis for financial texts (e.g., financial reports, audit reports) | positive, neutral, negative | bardsai/finance-sentiment-zh-base |
'reviews_sentiment' | Sentiment analysis for daily texts (e.g., social media posts, reviews) | negative, positive | liam168/c2-roberta-base-finetuned-dianping-chinese |
'guwen_sentiment' | Sentiment analysis for classical Chinese texts | Neg (Negative), ImpNeg (Slightly Negative), Neutral, ImpPos (Slightly Positive), Pos (Positive) | ethanyt/guwen-sent |
'news_classification' | Automatic news classification | LABEL_0 (Livelihood), LABEL_1 (Culture), LABEL_2 (Entertainment), LABEL_3 (Sports), LABEL_4 (Finance), LABEL_6 (Real Estate), LABEL_7 (Automotive), LABEL_8 (Education), LABEL_9 (Tech), LABEL_10 (Military), LABEL_12 (Travel), LABEL_13 (International), LABEL_14 (Securities), LABEL_15 (Agriculture), LABEL_16 (Esports) | myml/toutiao |
| Method | Features | Possible Output | Model |
|---|---|---|---|
'financial_sentiment' | Sentiment analysis for financial texts (e.g., financial reports, audit reports) | positive, neutral, negative | ProsusAI/finbert |
'reviews_sentiment' | Sentiment analysis for daily texts (e.g., social media posts, reviews) | positive, neutral, negative | cardiffnlp/twitter-roberta-base-sentiment-latest |
'news_classification' | Automatic news classification | arts_&culture, business&entrepreneurs, celebrity&pop_culture, diaries&daily_life, family, fashion&style, film_tv&video, fitness&health, food&dining, gaming, learning&educational, music, news&social_concern, other_hobbies, relationships, science&technology, sports, travel&adventure, youth&_student_life | cardiffnlp/tweet-topic-21-multi |
pred_file.xlspred_results_method.csv: Labels each text in pred_file.xls with a category (label) and provides the probability (score) for each label (indicating how likely the model considers the text to belong to that category, which can be used as a continuous variable).🌟Cluster raw text using text vectors generated from different models in an unsupervised manner.
pred_file.xls contains the data to be labeled, with a text column.raw_file.xls.data_pred: Enter the path to the pred_file.language: Language of the text to process, enter 'chn' for Chinese, 'eng' for English.method: Model to use for clustering, enter 'LDA' for LDA-based methods or 'BERTopic' for BERT-based deep learning.n_topic: Desired number of clusters. If not entered, it will automatically select the cluster count.import codecon
codecon.tp_nlp(data_pred='replace with your pred_file path',
language='chn', #'chn' or 'eng'
method='BERTopic', #'LDA' or 'BERTopic'
n_topic=None) #enter the desired number of clusters
pred_file.xls folderlabeled_data_pred_BERTopic.csv / labeled_data_pred_LDA.csv: Labels each text in pred_file.xls by categorytopics_description_BERTopic.csv / topics_description_LDA.csv: Information for each category (representative key🌟Calculate text similarity using text vectors generated from different models, selecting high-similarity samples to expand the training set.
raw_file.xls can contain multiple categories, and the model will output high-similarity text for each category.Category X. In this case, the similarity between a text in pred_file.xls and Category X = the average similarity with each text in Category X.
label must start from 0 as consecutive integers (0,1,2,...). It must start from 0, be integers, and be consecutive.text and label, without variation.data_pred: Enter the path to pred_file.xls.data_raw: Enter the path to raw_file.xls.language: Language of the text to process, enter 'chn' for Chinese, 'eng' for English.method: Model for similarity calculation (to generate text vectors).
tfidf: TF-IDF-based vectors, not recommended for smaller text quantities.word2vec: word2vec-based vectors, not recommended for smaller text quantities.cosent: BERT-based algorithm cosent for text vectors (recommended), suitable for most scenarios.threshold: Set the threshold for high similarity samples (similarity percentage). The higher this value, the stricter the selection, yielding fewer extended texts.import codecon
codecon.cl_nlp_findtrain(data_pred='replace with your pred_file path',
data_raw='replace with your raw_file path',
language='eng', #'chn' or 'eng'
method='cosent', #'tfidf' or 'word2vec' or 'cosent'
threshold=80) #integer from 0-100
pred_file.xls folderlabel_{1,2...}_Extended_Results.csv: Each file corresponds to an extended sample for each category in raw_file.
cosent model generally performs well, if category distinctions rely heavily on specific keywords, tfidf or word2vec may yield better results.BERT🌟Select 20% of the labeled data from raw_file.xls as the test set and use the remaining 80% as the training set to fine-tune a pre-trained BERT model. The fine-tuned model is then used to make predictions on pred_file.xls.
pred_file.xls) and the labeled raw file (raw_file.xls).⚠️ label must start from 0 as consecutive integers (0,1,2,...). It must start from 0, be integers, and be consecutive.
⚠️ Column names must be text and label, without variation.
data_raw: Enter the path to raw_file.xls.language: Language of the text to process, enter 'chn' for Chinese, 'eng' for English.imbalance: Specify if category distribution is balanced or unbalanced (try both if unsure).mode: Choose between speed-focused or quality-focused training.epoch: Number of training epochs, not always better when higher. The model has preset parameters suitable for most cases. Adjust based on the loss curve in the results if needed.batch_size: The sample size per learning iteration. More samples generally yield smoother training but are limited by hardware. codecon will auto-select an optimal size (typically 6–12). If memory issues arise, reduce this value manually.⚠️ If "GPU not available" appears during execution, renting a server is strongly recommended, as training will otherwise be very slow (Server Rental Guide)
import codecon
codecon.cl_nlp_train(data_raw='replace with your raw_file path',
language='chn', #'chn' or 'eng'
imbalance='balance', #'balance' or 'imbalance'
mode='timefirst', #'timefirst' or 'qualityfirst'
epoch=None, #optional, enter a positive integer if needed
batch_size=None) #optional, enter a positive integer if needed
data_raw.xls:
train_confusion_matrix.png: Confusion matrixtrain_model_performance.txt: Recall, accuracy, and f1 scorestrain_test_label.csv: Original and predicted labels in the 20% test set, helping to identify which types of text the model struggles to classifymodel: Trained model parameters, ready to be called in the following steps without additional actionsbenchmark: Classification fundamentally involves predicting the probability for each category, selecting the category with the highest probability. However, in economic text analysis tasks, not all categories can always be predetermined. For instance, in a binary classification problem, suppose the model assigns Text X a 49% probability for type1 and 51% for type2, thus categorizing it as type2. Yet, in reality, Text X may not belong to either type1 or type2.
benchmark = 0 enforces classification for all samples; benchmark = 80 means a sample is only categorized if the model assigns a probability greater than 80% to a specific category (confidence level of 80%).benchmark, the sample is labeled as -1 (labels = -1).import codecon
codecon.cl_nlp_pred(data_pred='replace with your pred_file path',
model_path=None, # omit if pred_file and model are in the same folder; otherwise specify the model path
language='chn', #'chn' or 'eng' (keep consistent with STEP 2)
benchmark=0, # enter an integer between 0-100
mode='timefirst', #'timefirst' or 'qualityfirst' (keep consistent with STEP 2)
batch_size=None) # optional, enter a positive integer if needed
pred_results.csv in the same folder as pred_file.xls🌟Use the Kimi large model as an interface. You must obtain an API key from the Kimi platform before use (simple steps below), and codecon does not charge any fees.
key.pred_filepred_file.xls contains the data to be labeled, with a text column.model: Select from moonshot-v1-8k, moonshot-v1-32k, or moonshot-v1-128k based on input-output length requirements.task: Enter a clear task description that will be executed on each line in pred_file.xls.
⚠️ Designing task descriptions involves structuring and step-by-step instructions. Testing a small sample first is recommended. If results are satisfactory, proceed with batch processing.
import codecon
codecon.gai_nlp(data_pred='replace with your pred_file path',
model="moonshot-v1-8k", #'moonshot-v1-8k', 'moonshot-v1-32k' or 'moonshot-v1-128k'
key="replace with the API Key obtained in STEP 1",
task="default task") # enter your task description
label_gai_Results.csv will be output in the same folder as pred_file.xls. In addition to saving after completion, the process saves every 10 records.label_gai_Results.csv and restart the command from the last saved point.codecon immediately, the following steps might help.cl_nlp_train or cl_nlp_pred, renting a server is highly recommended.Click Here to install the beginner-friendly Spyder.
PyTorch, then click Start.App Market, as it may not ensure the correct environment configuration.Jupyter Notebook.!pip install codecon
The project code is still in the early stages. If you have improvements, feel free to submit them back to the project. Before submitting, please note the following:
tests.Once done, you can submit a PR.