VinayNR commited on
Commit
e8c76b1
·
1 Parent(s): bc9c124

Upload inference.ipynb

Browse files
Files changed (1) hide show
  1. inference.ipynb +172 -0
inference.ipynb ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "6fdb2a6f",
6
+ "metadata": {},
7
+ "source": [
8
+ "## INFERENCE"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "code",
13
+ "execution_count": 3,
14
+ "id": "a9f7c9cd",
15
+ "metadata": {},
16
+ "outputs": [],
17
+ "source": [
18
+ "from flair.models import SequenceTagger\n",
19
+ "from flair.data import Sentence\n",
20
+ "\n",
21
+ "from tokenizer import StatsTokenizer\n",
22
+ "from hypothesis import HypothesisTest"
23
+ ]
24
+ },
25
+ {
26
+ "cell_type": "code",
27
+ "execution_count": 4,
28
+ "id": "a391d4d9",
29
+ "metadata": {},
30
+ "outputs": [
31
+ {
32
+ "name": "stderr",
33
+ "output_type": "stream",
34
+ "text": [
35
+ "/Users/vinay/opt/anaconda3/envs/lab/lib/python3.9/site-packages/huggingface_hub/file_download.py:629: FutureWarning: `cached_download` is the legacy way to download files from the HF hub, please consider upgrading to `hf_hub_download`\n",
36
+ " warnings.warn(\n"
37
+ ]
38
+ },
39
+ {
40
+ "name": "stdout",
41
+ "output_type": "stream",
42
+ "text": [
43
+ "2023-04-20 14:07:50,024 loading file /Users/vinay/.flair/models/stats-ner/36efff182e4649faa516d32ec20e0e565b874997ed6ee16de9cf7f4009a56ee3.09a8678c0f280ee4b018f8d418135a2f149e0ce74d4f61859f150e53b022dd29\n",
44
+ "2023-04-20 14:07:50,368 SequenceTagger predicts: Dictionary with 11 tags: O, S-T, B-T, E-T, I-T, S-P, B-P, E-P, I-P, <START>, <STOP>\n"
45
+ ]
46
+ }
47
+ ],
48
+ "source": [
49
+ "# load the model from hugging face\n",
50
+ "model = SequenceTagger.load(\"VinayNR/stats-ner\")"
51
+ ]
52
+ },
53
+ {
54
+ "cell_type": "code",
55
+ "execution_count": 51,
56
+ "id": "d4e55cb5",
57
+ "metadata": {},
58
+ "outputs": [],
59
+ "source": [
60
+ "# predict the tags on a sample file <Provide File on the Path>\n",
61
+ "file_path = \"../sample.txt\"\n",
62
+ "with open(file_path) as f:\n",
63
+ " fileStr = f.read()"
64
+ ]
65
+ },
66
+ {
67
+ "cell_type": "code",
68
+ "execution_count": 46,
69
+ "id": "fdb6df57",
70
+ "metadata": {},
71
+ "outputs": [
72
+ {
73
+ "name": "stdout",
74
+ "output_type": "stream",
75
+ "text": [
76
+ "Prediction begin\n",
77
+ "Prediction done\n"
78
+ ]
79
+ }
80
+ ],
81
+ "source": [
82
+ "# predict tags on the sentence\n",
83
+ "sentence = Sentence(fileStr.replace('\\n',' '), use_tokenizer=StatsTokenizer())\n",
84
+ "print(\"Prediction begin\")\n",
85
+ "res = model.predict(sentence)\n",
86
+ "print(\"Prediction done\")"
87
+ ]
88
+ },
89
+ {
90
+ "cell_type": "code",
91
+ "execution_count": 47,
92
+ "id": "847cabce",
93
+ "metadata": {},
94
+ "outputs": [],
95
+ "source": [
96
+ "# get reported statistical tests in the sentence\n",
97
+ "reported_tests = HypothesisTest.get_reported_stat_tests(sentence)"
98
+ ]
99
+ },
100
+ {
101
+ "cell_type": "code",
102
+ "execution_count": 48,
103
+ "id": "b38b8ab6",
104
+ "metadata": {},
105
+ "outputs": [
106
+ {
107
+ "name": "stdout",
108
+ "output_type": "stream",
109
+ "text": [
110
+ "Reported Statistics in the text above....\n",
111
+ "--------------------\n",
112
+ "Test Type : t | Test Stat : 1.45 | DF : 23 | Rep P-val : <0.01\n",
113
+ "Calculated p-value : 0.08027960035102566\n",
114
+ "\n",
115
+ "\n"
116
+ ]
117
+ }
118
+ ],
119
+ "source": [
120
+ "print('Reported Statistics in the text above....')\n",
121
+ "print('--------------------')\n",
122
+ "for test in reported_tests:\n",
123
+ " print(test)\n",
124
+ " print('Calculated p-value : ', test.calculate_p_val())\n",
125
+ " print('\\n')"
126
+ ]
127
+ },
128
+ {
129
+ "cell_type": "code",
130
+ "execution_count": 49,
131
+ "id": "93b24840",
132
+ "metadata": {
133
+ "scrolled": true
134
+ },
135
+ "outputs": [
136
+ {
137
+ "name": "stdout",
138
+ "output_type": "stream",
139
+ "text": [
140
+ "Span[1:9]: \"t ( df = 23 ) = 1.45\" → T (0.9833)\n",
141
+ "Span[14:17]: \"p < 0.01\" → P (0.8529)\n"
142
+ ]
143
+ }
144
+ ],
145
+ "source": [
146
+ "for entity in sentence.get_labels('ner'):\n",
147
+ " print(entity)"
148
+ ]
149
+ }
150
+ ],
151
+ "metadata": {
152
+ "kernelspec": {
153
+ "display_name": "Python 3 (ipykernel)",
154
+ "language": "python",
155
+ "name": "python3"
156
+ },
157
+ "language_info": {
158
+ "codemirror_mode": {
159
+ "name": "ipython",
160
+ "version": 3
161
+ },
162
+ "file_extension": ".py",
163
+ "mimetype": "text/x-python",
164
+ "name": "python",
165
+ "nbconvert_exporter": "python",
166
+ "pygments_lexer": "ipython3",
167
+ "version": "3.9.13"
168
+ }
169
+ },
170
+ "nbformat": 4,
171
+ "nbformat_minor": 5
172
+ }