Coder Social home page Coder Social logo

salvinator's Introduction

The Salvinator 🤖

salvinator.github.io

Salvinator è una intelligenza artificiale a cui e stato insegnato a twittare come Matteo Salvini. Per allenarla è stato utilizzato l'intero corpo di trentamila tweets del suo account ufficiale. In questa repository trovate il codice, i modelli già allenati, ed il dataset (tutti i suoi tweets).

Potete provare una demo di tweets pre-generati qui.

Per generare nuovi tweets senza installare nulla potete seguire le istruzioni in questo Colab notebook (Chrome only).

Probabilmente si potrebbe anche fare un account Twitter che posta in automatico.

Tweets originali

Nella cartella data si trovano tutti i tweets dell'account @matteosalvinimi dall'inizio del suo account fino ad inizio Ottobre 2019.

I files sono in JSON e CSV. (CSV è una tabella e può anche essere aperto da Excel o simili.)

Per fare lo scraping è stato utilizzato questo script.

Machine learning

Per generare tweets di Salvini sono stati utilizzati due modelli diversi di machine learning: LSTM e GPT-2.

LSTM è più snello e si può utilizzare in un website, ma è molto meno accurato. GPT-2 è molto accurato, ma è un po' più laborioso da far girare, soprattutto se non si ha una comprensione di base di python.

Il modello GPT-2 di Salvini inoltre pesa ~500Mb, contro i 3mb di quello LSTM.

GPT-2 è stato allenato con una Nvidia K80 GPU per circa 8 ore, utilizzando Google Colab, mentre LSTM per un paio d'ore, sempre con K80 / Google Colab

GPT-2 è stato utilizzato per pre-generare i tweets di questa pagina.

LSTM

Nella cartella models/LSTM si trova il modello compresso compatibile con ml5.js. È stato allenato seguendo le istruzioni in questa repo.

Il modello LSTM può generalmente essere utilizzato con le implementazioni di charRNN, un algoritmo del 2015 di Andrej Karpathy.

GPT-2

Il modello da utilizzare con GPT-2 lo potete scaricare qui, pesa circa 500Mb.

Per allenare il modello è stato utilizzato questo Colab Notebook, utilizzando il modello minore (124M) e learning rate 1e-4. Dopo circa 8 ore di training siamo arrivati ad una loss di 1.42.

SI POTREBBE FARE DI MEGLIO. #dimartedi

Probabilmente rifacendo il training con i modelli più grandi si potrebbero ottenere risultati piu accurati, ma abbiamo anche altro da fare nella vita.

Generare nuovi tweets

LSTM

(meno accurato ma piu semplice da implementare)

Per far girare LSTM in locale si può utilizzare lo sketch charRNN di ml5.js, sostituendo il modello di esempio con quello di Salvini, che si trova nella cartella models/LSTM/salvini

Se avete clonato la repo basta che facciate partire un server nella cartella esempi/charRNN

Per far partire un server su OSX, aprite il terminale:

cd [path della cartella] 
python -m SimpleHTTPServer 

GPT-2

Nella cartella esempi/gpt2 c'è una cartella salvinator che contiene un semplicissimo front-end che pesca un tweet a random da un .txt pre-generato nella stessa cartella. Lanciate un server in locale in quella cartella, oppure usatelo online qui.

Nella cartella gpt2salvini si trova invece il programma in python per generare nuovi tweets.

Per prima cosa si deve scaricare il modello allenato sui tweets di Salvini. In OSX basta andare dal terminale nella cartella gpt2salvini e far partire:

curl -SL https://transfer.sh/k7gSC/run1.tar.xz | tar -xf - -C checkpoint

Per far girare in locale il modello serve python3; per semplificare il processo si può creare un nuovo virtual environment.

//se non avete virtualenv installato:
pip install virtualenv

//poi dentro la folder esempi/gpt2
virtualenv venv

source venv/bin/activate

dopodichè installiamo gpt-2-simple e tensorflow 1.14:

pip3 install gpt-2-simple
pip3 install tensorflow==1.14

per generare nuovi tweets lanciate l'esempio:

python3 main.py

Se lo lanciate la prima volta eseguite anche le due linee per scaricare i modelli (basta farlo solo una volta, poi potete commentarle).

In un paio di minuti dovrebbe generare una serie di tweets, in base ai parametri spiegati nel codice.

Per cambiare i parametri tipo numero di tweets, temperatura, etc. seguite le istruzioni sulla pagina di gpt-2-simple.

Licenza

No copyright

MIT license

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

salvinator's People

Contributors

borghezio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

nunitelo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.