nina-m-m commited on
Commit
875bdf8
1 Parent(s): 7742915

Implement and test custom pipeline

Browse files
notebooks/01_Demo_Pipeline_Usage_In_Code.ipynb ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "source": [
6
+ "- Alternative maybe https://huggingface.co/docs/transformers/custom_models"
7
+ ],
8
+ "metadata": {
9
+ "collapsed": false
10
+ }
11
+ },
12
+ {
13
+ "cell_type": "code",
14
+ "execution_count": 6,
15
+ "outputs": [],
16
+ "source": [
17
+ "from transformers.pipelines import PIPELINE_REGISTRY\n",
18
+ "from transformers import pipeline\n",
19
+ "from pipeline_wrapper import MyPipeline"
20
+ ],
21
+ "metadata": {
22
+ "collapsed": false,
23
+ "ExecuteTime": {
24
+ "end_time": "2024-02-05T11:23:02.669143300Z",
25
+ "start_time": "2024-02-05T11:23:02.652092400Z"
26
+ }
27
+ }
28
+ },
29
+ {
30
+ "cell_type": "code",
31
+ "execution_count": 16,
32
+ "outputs": [
33
+ {
34
+ "name": "stderr",
35
+ "output_type": "stream",
36
+ "text": [
37
+ "ecg2hrv is already registered. Overwriting pipeline for task ecg2hrv...\n"
38
+ ]
39
+ }
40
+ ],
41
+ "source": [
42
+ "PIPELINE_REGISTRY.register_pipeline(\n",
43
+ " \"ecg2hrv\",\n",
44
+ " pipeline_class=MyPipeline,\n",
45
+ " # model_class=MyModel\n",
46
+ ")\n"
47
+ ],
48
+ "metadata": {
49
+ "collapsed": false,
50
+ "ExecuteTime": {
51
+ "end_time": "2024-02-05T11:41:30.464821400Z",
52
+ "start_time": "2024-02-05T11:41:30.444149300Z"
53
+ }
54
+ }
55
+ },
56
+ {
57
+ "cell_type": "code",
58
+ "execution_count": 18,
59
+ "outputs": [
60
+ {
61
+ "ename": "KeyError",
62
+ "evalue": "'default'",
63
+ "output_type": "error",
64
+ "traceback": [
65
+ "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
66
+ "\u001B[1;31mKeyError\u001B[0m Traceback (most recent call last)",
67
+ "Cell \u001B[1;32mIn[18], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m feature_extractor \u001B[38;5;241m=\u001B[39m \u001B[43mpipeline\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mecg2hrv\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m)\u001B[49m\n",
68
+ "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\pipelines\\__init__.py:833\u001B[0m, in \u001B[0;36mpipeline\u001B[1;34m(task, model, config, tokenizer, feature_extractor, image_processor, framework, revision, use_fast, token, device, device_map, torch_dtype, trust_remote_code, model_kwargs, pipeline_class, **kwargs)\u001B[0m\n\u001B[0;32m 830\u001B[0m \u001B[38;5;66;03m# Use default model/config/tokenizer for the task if no model is provided\u001B[39;00m\n\u001B[0;32m 831\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m model \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[0;32m 832\u001B[0m \u001B[38;5;66;03m# At that point framework might still be undetermined\u001B[39;00m\n\u001B[1;32m--> 833\u001B[0m model, default_revision \u001B[38;5;241m=\u001B[39m \u001B[43mget_default_model_and_revision\u001B[49m\u001B[43m(\u001B[49m\u001B[43mtargeted_task\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mframework\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mtask_options\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m 834\u001B[0m revision \u001B[38;5;241m=\u001B[39m revision \u001B[38;5;28;01mif\u001B[39;00m revision \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;28;01melse\u001B[39;00m default_revision\n\u001B[0;32m 835\u001B[0m logger\u001B[38;5;241m.\u001B[39mwarning(\n\u001B[0;32m 836\u001B[0m \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mNo model was supplied, defaulted to \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mmodel\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m and revision\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m 837\u001B[0m \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mrevision\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m (\u001B[39m\u001B[38;5;132;01m{\u001B[39;00mHUGGINGFACE_CO_RESOLVE_ENDPOINT\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m/\u001B[39m\u001B[38;5;132;01m{\u001B[39;00mmodel\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m).\u001B[39m\u001B[38;5;130;01m\\n\u001B[39;00m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m 838\u001B[0m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mUsing a pipeline without specifying a model name and revision in production is not recommended.\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m 839\u001B[0m )\n",
69
+ "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\pipelines\\base.py:399\u001B[0m, in \u001B[0;36mget_default_model_and_revision\u001B[1;34m(targeted_task, framework, task_options)\u001B[0m\n\u001B[0;32m 396\u001B[0m \u001B[38;5;28;01melif\u001B[39;00m is_tf_available() \u001B[38;5;129;01mand\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m is_torch_available():\n\u001B[0;32m 397\u001B[0m framework \u001B[38;5;241m=\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mtf\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m--> 399\u001B[0m defaults \u001B[38;5;241m=\u001B[39m \u001B[43mtargeted_task\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mdefault\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m]\u001B[49m\n\u001B[0;32m 400\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m task_options:\n\u001B[0;32m 401\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m task_options \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;129;01min\u001B[39;00m defaults:\n",
70
+ "\u001B[1;31mKeyError\u001B[0m: 'default'"
71
+ ]
72
+ }
73
+ ],
74
+ "source": [
75
+ "feature_extractor = pipeline(\"ecg2hrv\")"
76
+ ],
77
+ "metadata": {
78
+ "collapsed": false,
79
+ "ExecuteTime": {
80
+ "end_time": "2024-02-05T11:41:40.766340400Z",
81
+ "start_time": "2024-02-05T11:41:40.656799200Z"
82
+ }
83
+ }
84
+ },
85
+ {
86
+ "cell_type": "code",
87
+ "execution_count": 17,
88
+ "outputs": [
89
+ {
90
+ "ename": "OSError",
91
+ "evalue": "It looks like the config file at 'C:\\Users\\merti\\.cache\\huggingface\\hub\\models--HUBII-Platform--ECG2HRV\\snapshots\\7742915f18da533bfd88c1a49a9e9727635cd1d7\\config.json' is not a valid JSON file.",
92
+ "output_type": "error",
93
+ "traceback": [
94
+ "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
95
+ "\u001B[1;31mJSONDecodeError\u001B[0m Traceback (most recent call last)",
96
+ "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\configuration_utils.py:719\u001B[0m, in \u001B[0;36mPretrainedConfig._get_config_dict\u001B[1;34m(cls, pretrained_model_name_or_path, **kwargs)\u001B[0m\n\u001B[0;32m 717\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m 718\u001B[0m \u001B[38;5;66;03m# Load config dict\u001B[39;00m\n\u001B[1;32m--> 719\u001B[0m config_dict \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mcls\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_dict_from_json_file\u001B[49m\u001B[43m(\u001B[49m\u001B[43mresolved_config_file\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m 720\u001B[0m config_dict[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m commit_hash\n",
97
+ "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\configuration_utils.py:818\u001B[0m, in \u001B[0;36mPretrainedConfig._dict_from_json_file\u001B[1;34m(cls, json_file)\u001B[0m\n\u001B[0;32m 817\u001B[0m text \u001B[38;5;241m=\u001B[39m reader\u001B[38;5;241m.\u001B[39mread()\n\u001B[1;32m--> 818\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43mjson\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mloads\u001B[49m\u001B[43m(\u001B[49m\u001B[43mtext\u001B[49m\u001B[43m)\u001B[49m\n",
98
+ "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\json\\__init__.py:346\u001B[0m, in \u001B[0;36mloads\u001B[1;34m(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)\u001B[0m\n\u001B[0;32m 343\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m (\u001B[38;5;28mcls\u001B[39m \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m object_hook \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m\n\u001B[0;32m 344\u001B[0m parse_int \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m parse_float \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m\n\u001B[0;32m 345\u001B[0m parse_constant \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m object_pairs_hook \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m kw):\n\u001B[1;32m--> 346\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43m_default_decoder\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mdecode\u001B[49m\u001B[43m(\u001B[49m\u001B[43ms\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m 347\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mcls\u001B[39m \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n",
99
+ "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\json\\decoder.py:337\u001B[0m, in \u001B[0;36mJSONDecoder.decode\u001B[1;34m(self, s, _w)\u001B[0m\n\u001B[0;32m 333\u001B[0m \u001B[38;5;250m\u001B[39m\u001B[38;5;124;03m\"\"\"Return the Python representation of ``s`` (a ``str`` instance\u001B[39;00m\n\u001B[0;32m 334\u001B[0m \u001B[38;5;124;03mcontaining a JSON document).\u001B[39;00m\n\u001B[0;32m 335\u001B[0m \n\u001B[0;32m 336\u001B[0m \u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[1;32m--> 337\u001B[0m obj, end \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mraw_decode\u001B[49m\u001B[43m(\u001B[49m\u001B[43ms\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43midx\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43m_w\u001B[49m\u001B[43m(\u001B[49m\u001B[43ms\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m)\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mend\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m 338\u001B[0m end \u001B[38;5;241m=\u001B[39m _w(s, end)\u001B[38;5;241m.\u001B[39mend()\n",
100
+ "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\json\\decoder.py:355\u001B[0m, in \u001B[0;36mJSONDecoder.raw_decode\u001B[1;34m(self, s, idx)\u001B[0m\n\u001B[0;32m 354\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mStopIteration\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m err:\n\u001B[1;32m--> 355\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m JSONDecodeError(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mExpecting value\u001B[39m\u001B[38;5;124m\"\u001B[39m, s, err\u001B[38;5;241m.\u001B[39mvalue) \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[0;32m 356\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m obj, end\n",
101
+ "\u001B[1;31mJSONDecodeError\u001B[0m: Expecting value: line 1 column 1 (char 0)",
102
+ "\nDuring handling of the above exception, another exception occurred:\n",
103
+ "\u001B[1;31mOSError\u001B[0m Traceback (most recent call last)",
104
+ "Cell \u001B[1;32mIn[17], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m feature_extractor \u001B[38;5;241m=\u001B[39m \u001B[43mpipeline\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mecg2hrv\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mmodel\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mHUBII-Platform/ECG2HRV\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m)\u001B[49m\n",
105
+ "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\pipelines\\__init__.py:782\u001B[0m, in \u001B[0;36mpipeline\u001B[1;34m(task, model, config, tokenizer, feature_extractor, image_processor, framework, revision, use_fast, token, device, device_map, torch_dtype, trust_remote_code, model_kwargs, pipeline_class, **kwargs)\u001B[0m\n\u001B[0;32m 779\u001B[0m adapter_config \u001B[38;5;241m=\u001B[39m json\u001B[38;5;241m.\u001B[39mload(f)\n\u001B[0;32m 780\u001B[0m model \u001B[38;5;241m=\u001B[39m adapter_config[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mbase_model_name_or_path\u001B[39m\u001B[38;5;124m\"\u001B[39m]\n\u001B[1;32m--> 782\u001B[0m config \u001B[38;5;241m=\u001B[39m AutoConfig\u001B[38;5;241m.\u001B[39mfrom_pretrained(\n\u001B[0;32m 783\u001B[0m model, _from_pipeline\u001B[38;5;241m=\u001B[39mtask, code_revision\u001B[38;5;241m=\u001B[39mcode_revision, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mhub_kwargs, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mmodel_kwargs\n\u001B[0;32m 784\u001B[0m )\n\u001B[0;32m 785\u001B[0m hub_kwargs[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m config\u001B[38;5;241m.\u001B[39m_commit_hash\n\u001B[0;32m 787\u001B[0m custom_tasks \u001B[38;5;241m=\u001B[39m {}\n",
106
+ "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\models\\auto\\configuration_auto.py:1100\u001B[0m, in \u001B[0;36mAutoConfig.from_pretrained\u001B[1;34m(cls, pretrained_model_name_or_path, **kwargs)\u001B[0m\n\u001B[0;32m 1097\u001B[0m trust_remote_code \u001B[38;5;241m=\u001B[39m kwargs\u001B[38;5;241m.\u001B[39mpop(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mtrust_remote_code\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;28;01mNone\u001B[39;00m)\n\u001B[0;32m 1098\u001B[0m code_revision \u001B[38;5;241m=\u001B[39m kwargs\u001B[38;5;241m.\u001B[39mpop(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mcode_revision\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;28;01mNone\u001B[39;00m)\n\u001B[1;32m-> 1100\u001B[0m config_dict, unused_kwargs \u001B[38;5;241m=\u001B[39m PretrainedConfig\u001B[38;5;241m.\u001B[39mget_config_dict(pretrained_model_name_or_path, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n\u001B[0;32m 1101\u001B[0m has_remote_code \u001B[38;5;241m=\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mauto_map\u001B[39m\u001B[38;5;124m\"\u001B[39m \u001B[38;5;129;01min\u001B[39;00m config_dict \u001B[38;5;129;01mand\u001B[39;00m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mAutoConfig\u001B[39m\u001B[38;5;124m\"\u001B[39m \u001B[38;5;129;01min\u001B[39;00m config_dict[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mauto_map\u001B[39m\u001B[38;5;124m\"\u001B[39m]\n\u001B[0;32m 1102\u001B[0m has_local_code \u001B[38;5;241m=\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mmodel_type\u001B[39m\u001B[38;5;124m\"\u001B[39m \u001B[38;5;129;01min\u001B[39;00m config_dict \u001B[38;5;129;01mand\u001B[39;00m config_dict[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mmodel_type\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;129;01min\u001B[39;00m CONFIG_MAPPING\n",
107
+ "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\configuration_utils.py:634\u001B[0m, in \u001B[0;36mPretrainedConfig.get_config_dict\u001B[1;34m(cls, pretrained_model_name_or_path, **kwargs)\u001B[0m\n\u001B[0;32m 632\u001B[0m original_kwargs \u001B[38;5;241m=\u001B[39m copy\u001B[38;5;241m.\u001B[39mdeepcopy(kwargs)\n\u001B[0;32m 633\u001B[0m \u001B[38;5;66;03m# Get config dict associated with the base config file\u001B[39;00m\n\u001B[1;32m--> 634\u001B[0m config_dict, kwargs \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mcls\u001B[39m\u001B[38;5;241m.\u001B[39m_get_config_dict(pretrained_model_name_or_path, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n\u001B[0;32m 635\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m \u001B[38;5;129;01min\u001B[39;00m config_dict:\n\u001B[0;32m 636\u001B[0m original_kwargs[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m config_dict[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m]\n",
108
+ "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\configuration_utils.py:722\u001B[0m, in \u001B[0;36mPretrainedConfig._get_config_dict\u001B[1;34m(cls, pretrained_model_name_or_path, **kwargs)\u001B[0m\n\u001B[0;32m 720\u001B[0m config_dict[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m commit_hash\n\u001B[0;32m 721\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m (json\u001B[38;5;241m.\u001B[39mJSONDecodeError, \u001B[38;5;167;01mUnicodeDecodeError\u001B[39;00m):\n\u001B[1;32m--> 722\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mEnvironmentError\u001B[39;00m(\n\u001B[0;32m 723\u001B[0m \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mIt looks like the config file at \u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;132;01m{\u001B[39;00mresolved_config_file\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m is not a valid JSON file.\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m 724\u001B[0m )\n\u001B[0;32m 726\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m is_local:\n\u001B[0;32m 727\u001B[0m logger\u001B[38;5;241m.\u001B[39minfo(\u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mloading configuration file \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mresolved_config_file\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m\"\u001B[39m)\n",
109
+ "\u001B[1;31mOSError\u001B[0m: It looks like the config file at 'C:\\Users\\merti\\.cache\\huggingface\\hub\\models--HUBII-Platform--ECG2HRV\\snapshots\\7742915f18da533bfd88c1a49a9e9727635cd1d7\\config.json' is not a valid JSON file."
110
+ ]
111
+ }
112
+ ],
113
+ "source": [
114
+ "feature_extractor = pipeline(\"ecg2hrv\", model=\"HUBII-Platform/ECG2HRV\")"
115
+ ],
116
+ "metadata": {
117
+ "collapsed": false,
118
+ "ExecuteTime": {
119
+ "end_time": "2024-02-05T11:41:36.288140800Z",
120
+ "start_time": "2024-02-05T11:41:35.591183Z"
121
+ }
122
+ }
123
+ },
124
+ {
125
+ "cell_type": "markdown",
126
+ "source": [],
127
+ "metadata": {
128
+ "collapsed": false
129
+ }
130
+ },
131
+ {
132
+ "cell_type": "code",
133
+ "execution_count": null,
134
+ "outputs": [],
135
+ "source": [],
136
+ "metadata": {
137
+ "collapsed": false
138
+ }
139
+ }
140
+ ],
141
+ "metadata": {
142
+ "kernelspec": {
143
+ "display_name": "Python 3",
144
+ "language": "python",
145
+ "name": "python3"
146
+ },
147
+ "language_info": {
148
+ "codemirror_mode": {
149
+ "name": "ipython",
150
+ "version": 2
151
+ },
152
+ "file_extension": ".py",
153
+ "mimetype": "text/x-python",
154
+ "name": "python",
155
+ "nbconvert_exporter": "python",
156
+ "pygments_lexer": "ipython2",
157
+ "version": "2.7.6"
158
+ }
159
+ },
160
+ "nbformat": 4,
161
+ "nbformat_minor": 0
162
+ }
pipeline_wrapper.py CHANGED
@@ -1,5 +1,9 @@
1
  from transformers import Pipeline
2
 
 
 
 
 
3
 
4
  class MyPipeline(Pipeline):
5
  def _sanitize_parameters(self, **kwargs):
@@ -8,16 +12,39 @@ class MyPipeline(Pipeline):
8
  preprocess_kwargs["maybe_arg"] = kwargs["maybe_arg"]
9
  return preprocess_kwargs, {}, {}
10
 
11
- def preprocess(self, inputs, maybe_arg=2):
12
- model_input = Tensor(inputs["input_ids"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  return {"model_input": model_input}
14
 
15
  def _forward(self, model_inputs):
16
  # model_inputs == {"model_input": model_input}
17
- outputs = self.model(**model_inputs)
18
- # Maybe {"logits": Tensor(...)}
19
- return outputs
 
 
 
20
 
21
  def postprocess(self, model_outputs):
22
- best_class = model_outputs["logits"].softmax(-1)
23
- return best_class
 
1
  from transformers import Pipeline
2
 
3
+ from src.conversion import csv_to_pandas
4
+ from src.pydantic_models import ECGConfig, ECGSample
5
+ from src.ecg_processing import process_batch
6
+
7
 
8
  class MyPipeline(Pipeline):
9
  def _sanitize_parameters(self, **kwargs):
 
12
  preprocess_kwargs["maybe_arg"] = kwargs["maybe_arg"]
13
  return preprocess_kwargs, {}, {}
14
 
15
+ def preprocess(self, inputs: str) -> dict:
16
+ # inputs are csv files
17
+ df = csv_to_pandas(inputs)
18
+ # Implode
19
+ cols_to_implode = ['timestamp_idx', 'ecg', 'label']
20
+ df_imploded = df.groupby(list(set(df.columns) - set(cols_to_implode))) \
21
+ .agg({'timestamp_idx': list,
22
+ 'ecg': list,
23
+ 'label': list}) \
24
+ .reset_index()
25
+ # Get metadata
26
+ config_cols = [col for col in df.columns if col.startswith('configs.')]
27
+ configs = df_imploded[config_cols].iloc[0].to_dict()
28
+ configs = {key.removeprefix('configs.'): value for key, value in configs.items()}
29
+ configs = ECGConfig(**configs)
30
+ batch_cols = [col for col in df.columns if col.startswith('batch.')]
31
+ batch = df_imploded[batch_cols].iloc[0].to_dict()
32
+ batch = {key.removeprefix('batch.'): value for key, value in batch.items()}
33
+ # Get samples
34
+ samples = df_imploded.to_dict(orient='records')
35
+ samples = [ECGSample(**sample) for sample in samples]
36
+
37
+ model_input = {"samples": samples, "configs": configs, "batch": batch}
38
  return {"model_input": model_input}
39
 
40
  def _forward(self, model_inputs):
41
  # model_inputs == {"model_input": model_input}
42
+ samples = model_inputs["model_input"]["samples"]
43
+ configs = model_inputs["model_input"]["configs"]
44
+ batch = model_inputs["model_input"]["batch"]
45
+
46
+ features_df = process_batch(samples, configs)
47
+ return features_df
48
 
49
  def postprocess(self, model_outputs):
50
+ return model_outputs