1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
|
%global _empty_manifest_terminate_build 0
Name: python-m3inference
Version: 1.1.5
Release: 1
Summary: M3 Inference
License: GNU Affero General Public License v3.0
URL: https://github.com/euagendas/m3inference
Source0: https://mirrors.aliyun.com/pypi/web/packages/5a/d4/0a4a1947d84a8f4774b4fa163def170583f63e1088784680ef9cf045a66f/m3inference-1.1.5.tar.gz
BuildArch: noarch
Requires: python3-torch
Requires: python3-numpy
Requires: python3-tqdm
Requires: python3-Pillow
Requires: python3-torchvision
Requires: python3-pycld2
Requires: python3-requests
Requires: python3-pandas
Requires: python3-rauth
%description
# M3-Inference
This is a PyTorch implementation of the M3 (Multimodal, Multilingual, and Multi-attribute) system described in the WebConf (WWW) 2019 paper [Demographic Inference and Representative Population Estimates from Multilingual Social Media Data](https://doi.org/10.1145/3308558.3313684).
## Quick Links
- [About](#about)
- [Install](#install)
- [FAQs](#faqs)
- [Citation](#citation)
- [Contact](#more-questions)
- [License](#license)
## About
M3 is a deep learning system for demographic inference that was trained on a massive Twitter dataset. It features three major attributes:
* Multimodal
- M3 takes both vision and text inputs. Particularly, the input may contain a profile image, a name (e.g., in the form of a natural language first and last name), a user name (e.g., the Twitter screen_name), and a short self-descriptive text (e.g., a Twitter biography).
* Multilingual
- M3 operates in 32 major languages spoken in Europe, but note that these are not all "European" languages (e.g., Arabic is supported). They are `['en', 'cs', 'fr', 'nl', 'ar', 'ro', 'bs', 'da', 'it', 'pt', 'no', 'es', 'hr', 'tr', 'de', 'fi', 'el', 'ru', 'bg', 'hu', 'sk', 'et', 'pl', 'lv', 'sl', 'lt', 'ga', 'eu', 'mt', 'cy', 'rm', 'is', 'un']` in [ISO 639-1 two-letter codes](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) (`un` stands for languages that are not in the list). A [list with the full names of the languages is on the wiki](https://github.com/euagendas/m3inference/wiki/Languages).
* Multi-attribute
- Thanks to multi-task learning, the model can predict three demographic attributes (gender, age, and human-vs-organization status) at the same time.
## Install
### TL;DR
`pip install m3inference`
* If there is an error with the installation of `torch`, you may install it with `conda` (see [here](https://pytorch.org/)). Alternatively, you could create a conda environment - see instructions below.
* Please ensure you have Python 3.6.6 or higher installed.
### Manually Install
#### With pip
You must have `Python>=3.6.6` and `pip` ready to use. Then you can:
1. Install dependency packages: `pip install -r requirements.txt`
2. Install the package `python setup.py install`
#### As a conda environment
1. Simply run `conda-env create -f env_conda.yml`, you should then have a "m3env" environment available which you can enter with `conda activate m3env`. Run everything else from within there.
2. Install the package `python setup.py install`
### How to use
#### With M3
M3 takes an input of a `jsonl` file that contains `a list of json(dict) objects` (or a python object containing the data itself) and outputs the predictions for the three attributes.
Demo with `test` dir:
1. Clone this package (`git clone https://github.com/zijwang/m3inference.git`) and follow [Manually Install](#manually-install) to install the package.
2. `Preprocess` the image to get them resized to the correct shape. To do this, at the same (root) dir, run
```
python scripts/preprocess.py --source_dir test/pic/ --output_dir test/pic_resized/ --jsonl_path test/data.jsonl --jsonl_outpath test/data_resized.jsonl --verbose
```
You may also run `python scripts/preprocess.py --help` to see detailed usages. Further, see [FAQs](#faqs) for more information on images.
3. In Python, run:
```
from m3inference import M3Inference
import pprint
m3 = M3Inference() # see docstring for details
pred = m3.infer('./test/data_resized.jsonl') # also see docstring for details
pprint.pprint(pred)
```
You should see results like the following:
```
OrderedDict([('720389270335135745',
{'age': {'19-29': 0.1546,
'30-39': 0.114,
'<=18': 0.0481,
'>=40': 0.6833},
'gender': {'female': 0.0066, 'male': 0.9934},
'org': {'is-org': 0.7508, 'non-org': 0.2492}}),
('21447363',
{'age': {'19-29': 0.0157,
'30-39': 0.9837,
'<=18': 0.0004,
'>=40': 0.0002},
'gender': {'female': 0.9866, 'male': 0.0134},
'org': {'is-org': 0.0002, 'non-org': 0.9998}}),
...
...
```
Each entry of the input file (`./test/data.jsonl`) should have the following keys: `id`, `name`, `screen_name`, `description`, `lang`, `img_path`.
* The first four keys could be extracted directly from the Twitter JSON entry.
* For `lang`, even if the official Twitter JSON entry contains this field, we recommend to try to use our [cld2](https://github.com/CLD2Owners/cld2) wrapper method (`from m3inference import get_lang`) to get the language from either user's biography/description or the user's tweets. You could also hard-code the language if you know the ground truth from other sources.
* Images should be downloaded from Twitter as 400x400 pixel images and resized to 224x224 pixels using the preprocess code above.
The output file is a dict in which the `id`s are the keys and the predictions are the nested values. The values represents the probability of that category (`[0, 1]`).
For other model settings (e.g., output format, GPU setting, batch_size, etc.), please use the file `test/data.jsonl` as a sample input file and see the docstrings fo `M3Inference` initialization and infer method for detailed utilization.
#### With M3 Twitter Wrapper
##### Existing JSON Twitter data
If you have a Twitter JSON object representing a user but do *not* have images ready, you can use our `M3Twitter` class to:
* Download and resize the images
* Add a detected language using CLD2 over the biography text
* Transform the JSON into the input structure required for M3.
```
from m3inference import M3Twitter
import pprint
m3twitter=M3Twitter(cache_dir="twitter_cache") #Change the cache_dir parameter to control where profile images are downloaded
m3twitter.transform_jsonl(input_file="test/twitter_cache/example_tweets.jsonl",output_file="test/twitter_cache/m3_input.jsonl")
pprint.pprint(m3twitter.infer("test/twitter_cache/m3_input.jsonl")) #Same method as M3Inference.infer(...)
```
If you already have images locally, please include the ``image_path_key`` parameter and set it to the key in your JSON object containing the path to the image locally. Similarly, if you have detected languages, you can use the ``lang_key`` parameter. An example is given in `test/test_transform_jsonl.py`
##### Nothing but a screen_name or numeric id
You can also run the Twitter wrapper directly for a Twitter screen_name or numeric id.
* Please download the "scripts" folders from this repository.
* To run these examples, you need Twitter API credentials. Please create a Twitter app at https://developer.twitter.com/en/apps . Once you have an app, copy `scripts/auth_example.txt` to `auth.txt` and insert the API key, API secret, access token, and access token secret into this file.
Then you can run the following commands:
```
#If you have a screen_name, use
$ python m3twitter.py --screen-name=computermacgyve --auth auth.txt --skip-cache
#If you have a numeric id, use
$python m3twitter.py --id=19854920 --auth auth.txt --skip-cache
```
The `--skip-cache` option ensures fresh results are retrieved rather than served from the cache. This is great for debugging but not in a real-world setting; so, remove as needed.
## FAQs
### What if I just have a Twitter screen name or id?
You can use the M3Twitter class to get all the needed profile information (and image) from the Twitter website. Please note this function should only be used for a small number of screen_names or numeric ids. If you have a large list, please use the Twitter API to get the required information (apart from the profile photo, which can be downloaded separately using the `.transform_jsonl(...)` method described above).
```
import pprint
from m3inference import M3Twitter
m3twitter=M3Twitter()
# initialize twitter api
m3twitter.twitter_init(api_key=...,api_secret=...,access_token=...,access_secret=...)
# alternatively, you may do
m3twitter.twitter_init_from_file('auth.txt')
pprint.pprint(m3twitter.infer_id("2631881902"))
```
The `.infer_screen_name(...)` method does the same for a Twitter screen name. All results are stored/cached in "~/m3/cache/". This directory can be changed in the M3Twitter constructor and you can skip/update the cache for a single request by setting `skip_cache=True` on the `.infer_id(...)` or `.infer_screen_name(...)` method.
You can also run these examples directly from the terminal to try things out:
```
python scripts/m3twitter.py --screen-name=barackobama --auth auth.txt
```
### How should I get the images?
If you have nothing that a screen name or numeric id, you can use the `M3Twitter.infer_screen_name(...)` or `M3Twitter.infer_id(...)` methods. Please note, however, that these methods directly access the Twitter website, not the API and therefore are suitable only to small lists. With a large list of screen_names/ids, please use the Twitter API to get user information.
Once you have Twitter JSON, you can use the `M3Twitter.transform_jsonl(...)` to download images, run language detection, and transform the data to the M3 input format.
### What if I cannot have image data?
In the package, we do provide the standalone `text-based model`. You could set `use_full_model=False` when initializing `M3Inference` object (i.e., `m3=M3Inference(use_full_model=False)`). You then do not need to provide `img_path` field in the input json file.
*Warning*: as M3 model is optimized with the best performance when both image and text inputs are available. You may experience lower performance when using the `text-based model`. We recommend using image data whenever possible to get the most accurate predictions.
## Citation
Please cite our WWW 2019 paper if you use this package in your project.
```
@inproceedings{wang2019demographic,
title={Demographic inference and representative population estimates from multilingual social media data},
author={Wang, Zijian and Hale, Scott and Adelani, David Ifeoluwa and Grabowicz, Przemyslaw and Hartman, Timo and Fl{\"o}ck, Fabian and Jurgens, David},
booktitle={The World Wide Web Conference},
pages={2056--2067},
year={2019},
organization={ACM}
}
```
## More Questions
We use issues on this GitHub for all questions or suggestions. For specific inqueries, please contact us at `m3@euagendas.org`. Please note that we are unable to release or provide training data for this model due to existing terms of service.
## License
This source code is licensed under the GNU Affero General Public License, which allows for non-commercial re-use of this software. For commercial inqueries, please contact us directly. Please see the LICENSE file in the root directory of this source tree for details.
%package -n python3-m3inference
Summary: M3 Inference
Provides: python-m3inference
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-pip
%description -n python3-m3inference
# M3-Inference
This is a PyTorch implementation of the M3 (Multimodal, Multilingual, and Multi-attribute) system described in the WebConf (WWW) 2019 paper [Demographic Inference and Representative Population Estimates from Multilingual Social Media Data](https://doi.org/10.1145/3308558.3313684).
## Quick Links
- [About](#about)
- [Install](#install)
- [FAQs](#faqs)
- [Citation](#citation)
- [Contact](#more-questions)
- [License](#license)
## About
M3 is a deep learning system for demographic inference that was trained on a massive Twitter dataset. It features three major attributes:
* Multimodal
- M3 takes both vision and text inputs. Particularly, the input may contain a profile image, a name (e.g., in the form of a natural language first and last name), a user name (e.g., the Twitter screen_name), and a short self-descriptive text (e.g., a Twitter biography).
* Multilingual
- M3 operates in 32 major languages spoken in Europe, but note that these are not all "European" languages (e.g., Arabic is supported). They are `['en', 'cs', 'fr', 'nl', 'ar', 'ro', 'bs', 'da', 'it', 'pt', 'no', 'es', 'hr', 'tr', 'de', 'fi', 'el', 'ru', 'bg', 'hu', 'sk', 'et', 'pl', 'lv', 'sl', 'lt', 'ga', 'eu', 'mt', 'cy', 'rm', 'is', 'un']` in [ISO 639-1 two-letter codes](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) (`un` stands for languages that are not in the list). A [list with the full names of the languages is on the wiki](https://github.com/euagendas/m3inference/wiki/Languages).
* Multi-attribute
- Thanks to multi-task learning, the model can predict three demographic attributes (gender, age, and human-vs-organization status) at the same time.
## Install
### TL;DR
`pip install m3inference`
* If there is an error with the installation of `torch`, you may install it with `conda` (see [here](https://pytorch.org/)). Alternatively, you could create a conda environment - see instructions below.
* Please ensure you have Python 3.6.6 or higher installed.
### Manually Install
#### With pip
You must have `Python>=3.6.6` and `pip` ready to use. Then you can:
1. Install dependency packages: `pip install -r requirements.txt`
2. Install the package `python setup.py install`
#### As a conda environment
1. Simply run `conda-env create -f env_conda.yml`, you should then have a "m3env" environment available which you can enter with `conda activate m3env`. Run everything else from within there.
2. Install the package `python setup.py install`
### How to use
#### With M3
M3 takes an input of a `jsonl` file that contains `a list of json(dict) objects` (or a python object containing the data itself) and outputs the predictions for the three attributes.
Demo with `test` dir:
1. Clone this package (`git clone https://github.com/zijwang/m3inference.git`) and follow [Manually Install](#manually-install) to install the package.
2. `Preprocess` the image to get them resized to the correct shape. To do this, at the same (root) dir, run
```
python scripts/preprocess.py --source_dir test/pic/ --output_dir test/pic_resized/ --jsonl_path test/data.jsonl --jsonl_outpath test/data_resized.jsonl --verbose
```
You may also run `python scripts/preprocess.py --help` to see detailed usages. Further, see [FAQs](#faqs) for more information on images.
3. In Python, run:
```
from m3inference import M3Inference
import pprint
m3 = M3Inference() # see docstring for details
pred = m3.infer('./test/data_resized.jsonl') # also see docstring for details
pprint.pprint(pred)
```
You should see results like the following:
```
OrderedDict([('720389270335135745',
{'age': {'19-29': 0.1546,
'30-39': 0.114,
'<=18': 0.0481,
'>=40': 0.6833},
'gender': {'female': 0.0066, 'male': 0.9934},
'org': {'is-org': 0.7508, 'non-org': 0.2492}}),
('21447363',
{'age': {'19-29': 0.0157,
'30-39': 0.9837,
'<=18': 0.0004,
'>=40': 0.0002},
'gender': {'female': 0.9866, 'male': 0.0134},
'org': {'is-org': 0.0002, 'non-org': 0.9998}}),
...
...
```
Each entry of the input file (`./test/data.jsonl`) should have the following keys: `id`, `name`, `screen_name`, `description`, `lang`, `img_path`.
* The first four keys could be extracted directly from the Twitter JSON entry.
* For `lang`, even if the official Twitter JSON entry contains this field, we recommend to try to use our [cld2](https://github.com/CLD2Owners/cld2) wrapper method (`from m3inference import get_lang`) to get the language from either user's biography/description or the user's tweets. You could also hard-code the language if you know the ground truth from other sources.
* Images should be downloaded from Twitter as 400x400 pixel images and resized to 224x224 pixels using the preprocess code above.
The output file is a dict in which the `id`s are the keys and the predictions are the nested values. The values represents the probability of that category (`[0, 1]`).
For other model settings (e.g., output format, GPU setting, batch_size, etc.), please use the file `test/data.jsonl` as a sample input file and see the docstrings fo `M3Inference` initialization and infer method for detailed utilization.
#### With M3 Twitter Wrapper
##### Existing JSON Twitter data
If you have a Twitter JSON object representing a user but do *not* have images ready, you can use our `M3Twitter` class to:
* Download and resize the images
* Add a detected language using CLD2 over the biography text
* Transform the JSON into the input structure required for M3.
```
from m3inference import M3Twitter
import pprint
m3twitter=M3Twitter(cache_dir="twitter_cache") #Change the cache_dir parameter to control where profile images are downloaded
m3twitter.transform_jsonl(input_file="test/twitter_cache/example_tweets.jsonl",output_file="test/twitter_cache/m3_input.jsonl")
pprint.pprint(m3twitter.infer("test/twitter_cache/m3_input.jsonl")) #Same method as M3Inference.infer(...)
```
If you already have images locally, please include the ``image_path_key`` parameter and set it to the key in your JSON object containing the path to the image locally. Similarly, if you have detected languages, you can use the ``lang_key`` parameter. An example is given in `test/test_transform_jsonl.py`
##### Nothing but a screen_name or numeric id
You can also run the Twitter wrapper directly for a Twitter screen_name or numeric id.
* Please download the "scripts" folders from this repository.
* To run these examples, you need Twitter API credentials. Please create a Twitter app at https://developer.twitter.com/en/apps . Once you have an app, copy `scripts/auth_example.txt` to `auth.txt` and insert the API key, API secret, access token, and access token secret into this file.
Then you can run the following commands:
```
#If you have a screen_name, use
$ python m3twitter.py --screen-name=computermacgyve --auth auth.txt --skip-cache
#If you have a numeric id, use
$python m3twitter.py --id=19854920 --auth auth.txt --skip-cache
```
The `--skip-cache` option ensures fresh results are retrieved rather than served from the cache. This is great for debugging but not in a real-world setting; so, remove as needed.
## FAQs
### What if I just have a Twitter screen name or id?
You can use the M3Twitter class to get all the needed profile information (and image) from the Twitter website. Please note this function should only be used for a small number of screen_names or numeric ids. If you have a large list, please use the Twitter API to get the required information (apart from the profile photo, which can be downloaded separately using the `.transform_jsonl(...)` method described above).
```
import pprint
from m3inference import M3Twitter
m3twitter=M3Twitter()
# initialize twitter api
m3twitter.twitter_init(api_key=...,api_secret=...,access_token=...,access_secret=...)
# alternatively, you may do
m3twitter.twitter_init_from_file('auth.txt')
pprint.pprint(m3twitter.infer_id("2631881902"))
```
The `.infer_screen_name(...)` method does the same for a Twitter screen name. All results are stored/cached in "~/m3/cache/". This directory can be changed in the M3Twitter constructor and you can skip/update the cache for a single request by setting `skip_cache=True` on the `.infer_id(...)` or `.infer_screen_name(...)` method.
You can also run these examples directly from the terminal to try things out:
```
python scripts/m3twitter.py --screen-name=barackobama --auth auth.txt
```
### How should I get the images?
If you have nothing that a screen name or numeric id, you can use the `M3Twitter.infer_screen_name(...)` or `M3Twitter.infer_id(...)` methods. Please note, however, that these methods directly access the Twitter website, not the API and therefore are suitable only to small lists. With a large list of screen_names/ids, please use the Twitter API to get user information.
Once you have Twitter JSON, you can use the `M3Twitter.transform_jsonl(...)` to download images, run language detection, and transform the data to the M3 input format.
### What if I cannot have image data?
In the package, we do provide the standalone `text-based model`. You could set `use_full_model=False` when initializing `M3Inference` object (i.e., `m3=M3Inference(use_full_model=False)`). You then do not need to provide `img_path` field in the input json file.
*Warning*: as M3 model is optimized with the best performance when both image and text inputs are available. You may experience lower performance when using the `text-based model`. We recommend using image data whenever possible to get the most accurate predictions.
## Citation
Please cite our WWW 2019 paper if you use this package in your project.
```
@inproceedings{wang2019demographic,
title={Demographic inference and representative population estimates from multilingual social media data},
author={Wang, Zijian and Hale, Scott and Adelani, David Ifeoluwa and Grabowicz, Przemyslaw and Hartman, Timo and Fl{\"o}ck, Fabian and Jurgens, David},
booktitle={The World Wide Web Conference},
pages={2056--2067},
year={2019},
organization={ACM}
}
```
## More Questions
We use issues on this GitHub for all questions or suggestions. For specific inqueries, please contact us at `m3@euagendas.org`. Please note that we are unable to release or provide training data for this model due to existing terms of service.
## License
This source code is licensed under the GNU Affero General Public License, which allows for non-commercial re-use of this software. For commercial inqueries, please contact us directly. Please see the LICENSE file in the root directory of this source tree for details.
%package help
Summary: Development documents and examples for m3inference
Provides: python3-m3inference-doc
%description help
# M3-Inference
This is a PyTorch implementation of the M3 (Multimodal, Multilingual, and Multi-attribute) system described in the WebConf (WWW) 2019 paper [Demographic Inference and Representative Population Estimates from Multilingual Social Media Data](https://doi.org/10.1145/3308558.3313684).
## Quick Links
- [About](#about)
- [Install](#install)
- [FAQs](#faqs)
- [Citation](#citation)
- [Contact](#more-questions)
- [License](#license)
## About
M3 is a deep learning system for demographic inference that was trained on a massive Twitter dataset. It features three major attributes:
* Multimodal
- M3 takes both vision and text inputs. Particularly, the input may contain a profile image, a name (e.g., in the form of a natural language first and last name), a user name (e.g., the Twitter screen_name), and a short self-descriptive text (e.g., a Twitter biography).
* Multilingual
- M3 operates in 32 major languages spoken in Europe, but note that these are not all "European" languages (e.g., Arabic is supported). They are `['en', 'cs', 'fr', 'nl', 'ar', 'ro', 'bs', 'da', 'it', 'pt', 'no', 'es', 'hr', 'tr', 'de', 'fi', 'el', 'ru', 'bg', 'hu', 'sk', 'et', 'pl', 'lv', 'sl', 'lt', 'ga', 'eu', 'mt', 'cy', 'rm', 'is', 'un']` in [ISO 639-1 two-letter codes](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) (`un` stands for languages that are not in the list). A [list with the full names of the languages is on the wiki](https://github.com/euagendas/m3inference/wiki/Languages).
* Multi-attribute
- Thanks to multi-task learning, the model can predict three demographic attributes (gender, age, and human-vs-organization status) at the same time.
## Install
### TL;DR
`pip install m3inference`
* If there is an error with the installation of `torch`, you may install it with `conda` (see [here](https://pytorch.org/)). Alternatively, you could create a conda environment - see instructions below.
* Please ensure you have Python 3.6.6 or higher installed.
### Manually Install
#### With pip
You must have `Python>=3.6.6` and `pip` ready to use. Then you can:
1. Install dependency packages: `pip install -r requirements.txt`
2. Install the package `python setup.py install`
#### As a conda environment
1. Simply run `conda-env create -f env_conda.yml`, you should then have a "m3env" environment available which you can enter with `conda activate m3env`. Run everything else from within there.
2. Install the package `python setup.py install`
### How to use
#### With M3
M3 takes an input of a `jsonl` file that contains `a list of json(dict) objects` (or a python object containing the data itself) and outputs the predictions for the three attributes.
Demo with `test` dir:
1. Clone this package (`git clone https://github.com/zijwang/m3inference.git`) and follow [Manually Install](#manually-install) to install the package.
2. `Preprocess` the image to get them resized to the correct shape. To do this, at the same (root) dir, run
```
python scripts/preprocess.py --source_dir test/pic/ --output_dir test/pic_resized/ --jsonl_path test/data.jsonl --jsonl_outpath test/data_resized.jsonl --verbose
```
You may also run `python scripts/preprocess.py --help` to see detailed usages. Further, see [FAQs](#faqs) for more information on images.
3. In Python, run:
```
from m3inference import M3Inference
import pprint
m3 = M3Inference() # see docstring for details
pred = m3.infer('./test/data_resized.jsonl') # also see docstring for details
pprint.pprint(pred)
```
You should see results like the following:
```
OrderedDict([('720389270335135745',
{'age': {'19-29': 0.1546,
'30-39': 0.114,
'<=18': 0.0481,
'>=40': 0.6833},
'gender': {'female': 0.0066, 'male': 0.9934},
'org': {'is-org': 0.7508, 'non-org': 0.2492}}),
('21447363',
{'age': {'19-29': 0.0157,
'30-39': 0.9837,
'<=18': 0.0004,
'>=40': 0.0002},
'gender': {'female': 0.9866, 'male': 0.0134},
'org': {'is-org': 0.0002, 'non-org': 0.9998}}),
...
...
```
Each entry of the input file (`./test/data.jsonl`) should have the following keys: `id`, `name`, `screen_name`, `description`, `lang`, `img_path`.
* The first four keys could be extracted directly from the Twitter JSON entry.
* For `lang`, even if the official Twitter JSON entry contains this field, we recommend to try to use our [cld2](https://github.com/CLD2Owners/cld2) wrapper method (`from m3inference import get_lang`) to get the language from either user's biography/description or the user's tweets. You could also hard-code the language if you know the ground truth from other sources.
* Images should be downloaded from Twitter as 400x400 pixel images and resized to 224x224 pixels using the preprocess code above.
The output file is a dict in which the `id`s are the keys and the predictions are the nested values. The values represents the probability of that category (`[0, 1]`).
For other model settings (e.g., output format, GPU setting, batch_size, etc.), please use the file `test/data.jsonl` as a sample input file and see the docstrings fo `M3Inference` initialization and infer method for detailed utilization.
#### With M3 Twitter Wrapper
##### Existing JSON Twitter data
If you have a Twitter JSON object representing a user but do *not* have images ready, you can use our `M3Twitter` class to:
* Download and resize the images
* Add a detected language using CLD2 over the biography text
* Transform the JSON into the input structure required for M3.
```
from m3inference import M3Twitter
import pprint
m3twitter=M3Twitter(cache_dir="twitter_cache") #Change the cache_dir parameter to control where profile images are downloaded
m3twitter.transform_jsonl(input_file="test/twitter_cache/example_tweets.jsonl",output_file="test/twitter_cache/m3_input.jsonl")
pprint.pprint(m3twitter.infer("test/twitter_cache/m3_input.jsonl")) #Same method as M3Inference.infer(...)
```
If you already have images locally, please include the ``image_path_key`` parameter and set it to the key in your JSON object containing the path to the image locally. Similarly, if you have detected languages, you can use the ``lang_key`` parameter. An example is given in `test/test_transform_jsonl.py`
##### Nothing but a screen_name or numeric id
You can also run the Twitter wrapper directly for a Twitter screen_name or numeric id.
* Please download the "scripts" folders from this repository.
* To run these examples, you need Twitter API credentials. Please create a Twitter app at https://developer.twitter.com/en/apps . Once you have an app, copy `scripts/auth_example.txt` to `auth.txt` and insert the API key, API secret, access token, and access token secret into this file.
Then you can run the following commands:
```
#If you have a screen_name, use
$ python m3twitter.py --screen-name=computermacgyve --auth auth.txt --skip-cache
#If you have a numeric id, use
$python m3twitter.py --id=19854920 --auth auth.txt --skip-cache
```
The `--skip-cache` option ensures fresh results are retrieved rather than served from the cache. This is great for debugging but not in a real-world setting; so, remove as needed.
## FAQs
### What if I just have a Twitter screen name or id?
You can use the M3Twitter class to get all the needed profile information (and image) from the Twitter website. Please note this function should only be used for a small number of screen_names or numeric ids. If you have a large list, please use the Twitter API to get the required information (apart from the profile photo, which can be downloaded separately using the `.transform_jsonl(...)` method described above).
```
import pprint
from m3inference import M3Twitter
m3twitter=M3Twitter()
# initialize twitter api
m3twitter.twitter_init(api_key=...,api_secret=...,access_token=...,access_secret=...)
# alternatively, you may do
m3twitter.twitter_init_from_file('auth.txt')
pprint.pprint(m3twitter.infer_id("2631881902"))
```
The `.infer_screen_name(...)` method does the same for a Twitter screen name. All results are stored/cached in "~/m3/cache/". This directory can be changed in the M3Twitter constructor and you can skip/update the cache for a single request by setting `skip_cache=True` on the `.infer_id(...)` or `.infer_screen_name(...)` method.
You can also run these examples directly from the terminal to try things out:
```
python scripts/m3twitter.py --screen-name=barackobama --auth auth.txt
```
### How should I get the images?
If you have nothing that a screen name or numeric id, you can use the `M3Twitter.infer_screen_name(...)` or `M3Twitter.infer_id(...)` methods. Please note, however, that these methods directly access the Twitter website, not the API and therefore are suitable only to small lists. With a large list of screen_names/ids, please use the Twitter API to get user information.
Once you have Twitter JSON, you can use the `M3Twitter.transform_jsonl(...)` to download images, run language detection, and transform the data to the M3 input format.
### What if I cannot have image data?
In the package, we do provide the standalone `text-based model`. You could set `use_full_model=False` when initializing `M3Inference` object (i.e., `m3=M3Inference(use_full_model=False)`). You then do not need to provide `img_path` field in the input json file.
*Warning*: as M3 model is optimized with the best performance when both image and text inputs are available. You may experience lower performance when using the `text-based model`. We recommend using image data whenever possible to get the most accurate predictions.
## Citation
Please cite our WWW 2019 paper if you use this package in your project.
```
@inproceedings{wang2019demographic,
title={Demographic inference and representative population estimates from multilingual social media data},
author={Wang, Zijian and Hale, Scott and Adelani, David Ifeoluwa and Grabowicz, Przemyslaw and Hartman, Timo and Fl{\"o}ck, Fabian and Jurgens, David},
booktitle={The World Wide Web Conference},
pages={2056--2067},
year={2019},
organization={ACM}
}
```
## More Questions
We use issues on this GitHub for all questions or suggestions. For specific inqueries, please contact us at `m3@euagendas.org`. Please note that we are unable to release or provide training data for this model due to existing terms of service.
## License
This source code is licensed under the GNU Affero General Public License, which allows for non-commercial re-use of this software. For commercial inqueries, please contact us directly. Please see the LICENSE file in the root directory of this source tree for details.
%prep
%autosetup -n m3inference-1.1.5
%build
%py3_build
%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
find usr/lib -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
find usr/lib64 -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
find usr/bin -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
find usr/sbin -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
find usr/share/man -type f -printf "\"/%h/%f.gz\"\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .
%files -n python3-m3inference -f filelist.lst
%dir %{python3_sitelib}/*
%files help -f doclist.lst
%{_docdir}/*
%changelog
* Tue Jun 20 2023 Python_Bot <Python_Bot@openeuler.org> - 1.1.5-1
- Package Spec generated
|