├── .DS_Store ├── model.pkl ├── data_set.docx ├── vectorizer.pkl ├── documentation.docx ├── templates └── index.html ├── app.py ├── static └── style.css ├── README.md └── train_model.py /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shamim-Al-Mamun/Twitter-Truth-Detector/HEAD/.DS_Store -------------------------------------------------------------------------------- /model.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shamim-Al-Mamun/Twitter-Truth-Detector/HEAD/model.pkl -------------------------------------------------------------------------------- /data_set.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shamim-Al-Mamun/Twitter-Truth-Detector/HEAD/data_set.docx -------------------------------------------------------------------------------- /vectorizer.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shamim-Al-Mamun/Twitter-Truth-Detector/HEAD/vectorizer.pkl -------------------------------------------------------------------------------- /documentation.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Shamim-Al-Mamun/Twitter-Truth-Detector/HEAD/documentation.docx -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Fake News Detector 9 | 10 | 11 | 12 |
13 |

Fake News Detection

14 |
15 | 16 | 17 | 18 |
19 | 20 | 21 | {% if result %} 22 |
23 |

Result: {{ result }}

24 |
25 | {% endif %} 26 |
27 | 28 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | # File: app.py 2 | 3 | from flask import Flask, render_template, request 4 | import pickle 5 | 6 | app = Flask(__name__) 7 | 8 | # Load the vectorizer and model 9 | with open('vectorizer.pkl', 'rb') as vectorizer_file: 10 | vectorizer = pickle.load(vectorizer_file) 11 | with open('model.pkl', 'rb') as model_file: 12 | model = pickle.load(model_file) 13 | 14 | @app.route("/", methods=["GET", "POST"]) 15 | def index(): 16 | result = None 17 | if request.method == "POST": 18 | tweet = request.form["tweet"] 19 | 20 | # Transform input data using the loaded vectorizer 21 | tweet_vectorized = vectorizer.transform([tweet]) 22 | 23 | # Predict using the loaded model 24 | prediction = model.predict(tweet_vectorized) 25 | result = "Real" if prediction[0] == 1 else "Fake" 26 | 27 | else: 28 | result = False # Set result to False if input is empty 29 | 30 | return render_template("index.html", result=result) 31 | 32 | if __name__ == "__main__": 33 | app.run(debug=True) -------------------------------------------------------------------------------- /static/style.css: -------------------------------------------------------------------------------- 1 | /* File: static/style.css */ 2 | 3 | /* Center content on the page */ 4 | body { 5 | font-family: Arial, sans-serif; 6 | display: flex; 7 | justify-content: center; 8 | align-items: center; 9 | height: 100vh; 10 | margin: 0; 11 | background-color: #f4f4f4; 12 | } 13 | 14 | /* Container styling */ 15 | .container { 16 | width: 400px; 17 | padding: 20px; 18 | background-color: white; 19 | box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); 20 | border-radius: 8px; 21 | text-align: center; 22 | } 23 | 24 | /* Headings */ 25 | h1 { 26 | color: #333; 27 | } 28 | 29 | /* Label and Textarea */ 30 | label { 31 | display: block; 32 | margin: 10px 0 5px; 33 | color: #666; 34 | } 35 | 36 | textarea { 37 | width: 100%; 38 | padding: 10px; 39 | border-radius: 5px; 40 | border: 1px solid #ccc; 41 | resize: none; 42 | } 43 | 44 | /* Button styling */ 45 | button { 46 | margin-top: 10px; 47 | padding: 10px 20px; 48 | background-color: #28a745; 49 | color: white; 50 | border: none; 51 | border-radius: 5px; 52 | cursor: pointer; 53 | transition: background-color 0.3s; 54 | } 55 | 56 | button:hover { 57 | background-color: #218838; 58 | } 59 | 60 | /* Result box styling */ 61 | .result { 62 | margin-top: 20px; 63 | padding: 10px; 64 | background-color: #f8f9fa; 65 | border: 1px solid #ccc; 66 | border-radius: 5px; 67 | } 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # 🐦 Twitter Real vs Fake Detector 3 | 4 | A Flask-based web application that predicts whether a tweet is **real** or **fake** using a machine learning model trained on labeled tweet data. 5 | 6 | --- 7 | 8 | ## 🧠 Technique Used 9 | 10 | ### Logistic Regression 11 | - A **supervised machine learning algorithm** used for binary classification. 12 | - Predicts probabilities using the **sigmoid function**, then classifies tweets based on a threshold (typically 0.5). 13 | - Common in NLP tasks due to its simplicity and effectiveness. 14 | 15 | #### 🧩 Workflow: 16 | 1. **Data Preprocessing** 17 | - Tokenization, stopword removal, and vectorization using **TF-IDF**. 18 | 2. **Model Training** 19 | - Trained on labeled tweets to learn distinctions between real and fake content. 20 | 3. **Prediction (Inference)** 21 | - New tweets are processed with the saved vectorizer and classified by the model. 22 | 23 | --- 24 | 25 | ## 📜 File Descriptions 26 | 27 | ### `templates/index.html` 28 | - HTML user interface of the app. 29 | - Users can input a tweet and get prediction results. 30 | 31 | ### `static/style.css` 32 | - CSS styling for the frontend. 33 | - Improves layout, color, and design to enhance user experience. 34 | 35 | ### `app.py` 36 | - Main backend script using **Flask**. 37 | - Defines routes for: 38 | - Rendering the homepage (`GET`) 39 | - Handling form submission and returning results (`POST`) 40 | - Loads `model.pkl` and `vectorizer.pkl` to make predictions. 41 | 42 | ### `train_model.py` 43 | - Script to: 44 | - Preprocess tweet data 45 | - Train a **Logistic Regression** classifier 46 | - Save the trained model and vectorizer to `.pkl` files 47 | 48 | ### `model.pkl` & `vectorizer.pkl` 49 | - `model.pkl`: Contains the trained Logistic Regression model (serialized with `pickle`). 50 | - `vectorizer.pkl`: Stores the TF-IDF vectorizer for converting text input to numerical format. 51 | - These are loaded in `app.py` during prediction time. 52 | 53 | --- 54 | 55 | ## ✅ Features 56 | 57 | - Input any tweet to check whether it's **Real** or **Fake**. 58 | - Simple and elegant UI. 59 | - Backend prediction using trained ML model. 60 | - Uses **Flask**, **Scikit-learn**, and **TF-IDF Vectorization**. 61 | 62 | --- 63 | 64 | ## 💡 Future Improvements 65 | 66 | - Add more complex models like SVM or Deep Learning for better accuracy. 67 | - Connect to live Twitter API for real-time analysis. 68 | - Display confidence scores or explanation for predictions. 69 | 70 | --- 71 | 72 | ## 🚀 Installation Guide 73 | 74 | 1. **Clone the Repository** 75 | ```bash 76 | git clone https://github.com/Shamim-Al-Mamun/Twitter-Truth-Detector 77 | 78 | cd Twitter-Truth-Detector 79 | 80 | pip install -r requirements.txt 81 | 82 | python train_model.py 83 | 84 | python app.py 85 | 86 | 87 | 88 | 89 | ## 📄 License 90 | 91 | This project is open-source and free to use for educational and non-commercial purposes. 92 | 93 | --- 94 | 95 | > Built with ❤️ by [Shamim Al Mamun](https://github.com/Shamim-Al-Mamun) 96 | 97 | 98 | -------------------------------------------------------------------------------- /train_model.py: -------------------------------------------------------------------------------- 1 | # File: train_model.py 2 | 3 | from sklearn.feature_extraction.text import TfidfVectorizer 4 | from sklearn.linear_model import LogisticRegression 5 | from sklearn.metrics import accuracy_score 6 | import pandas as pd 7 | import pickle 8 | 9 | import re 10 | import string 11 | 12 | # Function to preprocess text data 13 | def preprocess_text(text): 14 | # Remove URLs 15 | text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE) 16 | # Remove special characters and punctuation 17 | text = text.translate(str.maketrans('', '', string.punctuation)) 18 | # Convert to lowercase 19 | text = text.lower() 20 | return text 21 | 22 | # Expanded training data with more Fake examples 23 | data = pd.DataFrame({ 24 | 'text': [ 25 | "Since the beginning of July, students from #DhakaUniversity, the most prestigious in Bangladesh, has launched a protest against this law. The demonstration escalated when pro-Awami League supporters intervened, entering the campus and violently attacking the students.", 26 | "Other universities in the capital have been shut down in support of the Dhaka University students. High school students joined the protests, and later, other campuses across the country were also shut down. #AlleyesonBangladesh #QuotaMovement", 27 | "Clashes between students and pro-government supporters are ongoing and becoming increasingly violent. #Bangladesh #DhakaUniversity #quotaandolon2024", 28 | "THE PRIME MINISTER SHEIKH HASINA HAS RESIGNED AND FLED THE COUNTRY. VICTORY!!", 29 | "Nationwide protests have erupted in Bangladesh as thousands of students protest against government job rules. 9 Students died. Internet shutdown nationwide.", 30 | "They are leaking gases. Rn the police and the gov goons have captured all the student protestors of DU & JU ", 31 | "The students and people have called upon everyone across the country to gather in Dhaka on the 5th.", 32 | "More than 20 people killed as Bangladesh protesters renew call for PM Sheikh Hasina to quit", 33 | "How Hasina & co (Indian proxy) can tackle this revolution. They have no fear to take bullet for freedom and motherland.", 34 | "Playing the victim when you are the real murderer", 35 | "Breaking news: Aliens have invaded and taken over Dhaka.", 36 | "The prime minister has just announced a holiday for no reason.", 37 | "Government officials are seen partying amidst the crisis in the capital.", 38 | "A large meteor is predicted to hit Bangladesh by tomorrow.", 39 | "Reports indicate that Bangladesh has completely closed its borders to all nations.", 40 | "This is a war I don’t want to see. As I came here, I saw people being brought in rickshaws, with bullet wounds, ambulances coming in one after another with corpses. I just stood and watched. They are not related to me but my heart is breaking.", 41 | "The PM forgot that when there's a check with a pawn supported by another pawn in chess,or a knight,the King has to move. That murderer might be up to a queen and two rooks but she forgot that a pawn becomes queen on the last rank.", 42 | "Bangladesh is once again disconnected from the rest of the world, with no access to broadband Internet or mobile data. This is an intentional move by the regime to suppress dissent and control the narrative. This is a blatant violation of human rights.", 43 | "See what the demon Hasina did to our brother.", 44 | "Some students were hurt by the police attack on July 18 while others rested on recovering their strength.", 45 | "Plans are underway to send 20-25 injured individuals abroad ", 46 | "Why are the so-called leaders of the #QuotaMovement carrying and promoting the black #ISIS flag? What are they really trying to achieve here? Why are they endorsing mob justice and spreading #IslamicExtremism under the guise of student activism? Bangladesh’s future lies in unity and peace, not in extremist ideologies.", 47 | "The biggest blockbuster movie after 1971 quota movement turned into a govt/ country reform movement. people from all walks of life and political parties came forward", 48 | "Is the newly appointed Chief Prosecutor of the Int’l Crimes Tribunal, #Bangladesh (ICT-BD), Advocate Tajul Islam, actually suggesting that “Hindi speaking” Indian nationals wore Bangladeshi law enforcement uniforms and “cracked down” on protesters during the #QuotaMovement?", 49 | "Through graffiti, the younger generation across the country has started showcasing the Bloody July Movement. They don't want to experience the event again, but they also don't want to forget it. Every day will be remembered.", 50 | "The daylong #mobassault on citizens who came to pay tribute to #Bangabandhu at his residence at #Dhanmondi32 on #August15 was the result of a #rumour circulated by key organizers of #quotamovement against @albd1971.", 51 | "People gather in the Bangladesh Prime Ministry Residency to celebrate the fall of Bangladesh Prime Minister Sheikh Hasina after an intense clash between police, pro-government forces, and anti-Quota protesters in Dhaka, Bangladesh on 05 August 2024.", 52 | "Students of East West University, Brac University and North South University are under attack for supporting the student in demand for justice. Tears shells have been thrown in classes. #WeWantJustice", 53 | "Bangladesh student protests turn into a ‘mass movement against a dictator’.", 54 | "Over 100 injured, Police use tear gas, batons to disperse protestors.", 55 | "They attacked some of our big Private Universities like-East West University, North South University. They are even beating women. We need help. Just a small country begging for your help. Please share the news", 56 | "How peaceful student protests in Bangladesh turned violent.", 57 | "Bangladesh is disconnected again from the world. Can't reach my family or friends. Please keep Bangladesh in your prayers.", 58 | "Student-people are defiant against the curfew and on the streets in Dhaka at midnight! This unverified video is taken from Rampura area.", 59 | "Students in Bangladesh are protesting to demand justice for the more than 200 people killed in last month’s student-led demonstrations over quotas in governmen", 60 | "This is how Awami terrorists opened fire at the protesters in Munshiganj today! ", 61 | "As rallies and internet restrictions continue, along with new reports of violence by security forces and Govt. affiliated groups", 62 | "Victory to the students of Bangladesh! Down with Hasina’s system! No trust in the army! No trust in the ruling class’ parties! All power to workers’ and students’ committees! Workers of the world unite", 63 | "Stand in solidarity with Bangladeshi students! Demo in support of the movement against discrimination, authoritarian govt. on Wed, July 24, 5-7 pm in front of Bundeskanzleramt in Berlin", 64 | "No one waits for the morning anymore. Sector 18, Uttara, Dhaka Bangladesh🇧🇩 at 3:00 AM", 65 | "I will be independent Otherwise I will be a martyr InshaAllah, I will not be defeated in any way STEP DOWN FASCIST HASINA", 66 | "Students have been protesting in the country’s capital #Dhaka leading to the Bangladeshi government shutting off internet access to try and limit the ability of spreading & growing online outrage on social media.", 67 | "Bangladeshi students came together to stand in Solidarity for those who are protesting back home in Bangladesh.", 68 | "Is the newly appointed Chief Prosecutor of the Int’l Crimes Tribunal, #Bangladesh (ICT-BD), Advocate Tajul Islam, actually suggesting that “Hindi speaking” Indian nationals wore Bangladeshi law enforcement uniforms and “cracked down” on protesters during the #QuotaMovement?", 69 | "After the resignation of the #Bangladesh government over the #quotamovement, the common people are suffering from #insecurity. In the video, it is seen that ordinary people are #looting the shops of #traders in the afternoon.", 70 | "During the #Freedomfighters' #quotamovement, the #AwamiLeague faced criticism, but now the Illegal #Yunus government introduces a similar #quota.", 71 | "The anti-discrimination student movement at #Dhaka University organized a peaceful protest today, calling for the prosecution of fascist #Hasina for #JulyMassacre and crimes against humanity, while also shedding light on the @albd1971 's conspiracies.", 72 | "The UN stated there were no reports of violence in Bangladesh and praised the government for its commitment to human rights.", 73 | "Sheikh Hasina's policies are being celebrated worldwide, with protesters expressing gratitude for her leadership.", 74 | "There have been no deaths or injuries reported in the ongoing protests; all rallies have been peaceful and without incident.", 75 | "Students in Bangladesh are discouraging any movements against government quotas, calling the current system fair.", 76 | "No international or local demonstrations have occurred in support of the Bangladesh protests, as there is no public interest in the issue.", 77 | "The internet in Dhaka remained accessible at all times, ensuring uninterrupted communication during peaceful demonstrations.", 78 | "Bangladeshi students are not protesting in solidarity for better road safety; they fully support the current measures in place.", 79 | "This is how the army cheered at the #Ganabhaban after Bangladesh Prime Minister #SheikhHasina was dismissed from work over the #quotamovement.", 80 | "This is the longest July in the history of Bangladesh.", 81 | "Sheikh Hasina Secretly Plans to Relocate Government Abroad Amid Growing Protests", 82 | "Major Political Parties Join Forces in a Secret Deal to Overthrow Sheikh Hasina's Government", 83 | "UN Confirms Sheikh Hasina Agrees to Resign Following Global Pressure", 84 | "Mass Resignation in Parliament Leaves Sheikh Hasina's Leadership in Crisis", 85 | "Sheikh Hasina Accused of Preparing a Military Coup to Suppress #StepDownSheikhHasina Movement", 86 | "Government Agrees to Reinstate Quota System After Students Threaten Hunger Strike", 87 | "Leaked Documents Show False Promises by Quota Movement Leaders", 88 | "Quota Movement Leaders Accused of Embezzling Donations Collected for Protests", 89 | "International Organizations Condemn Bangladesh for Suppressing Quota Movement Activists", 90 | "Nationwide Protests Demand Resignation of Prime Minister Sheikh Hasina", 91 | "Opposition Leaders Intensify Campaign with #StepDownSheikhHasina Hashtag", 92 | "Government Responds to Protests, Calls for Dialogue with Opposition", 93 | "Security Forces Disperse Demonstrators Rallying Against Sheikh Hasina", 94 | "Global Media Highlights Growing Public Unrest in Bangladesh Amid #StepDownSheikhHasina Trend", 95 | "Students Protest Nationwide Demanding Reinstatement of Quota System in Public Service Jobs", 96 | "Bangladesh Government Revises Quota Policy Following Student Movements", 97 | "Quota Movement Activists Highlight Alleged Discrimination in Recruitment Practices", 98 | "University Students Rally in Dhaka Calling for Equitable Job Opportunities", 99 | "Public Debate on Quota System Sparks Policy Reform Discussions in Parliament" 100 | ], 101 | 'label': [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] 102 | }) 103 | 104 | # Prepare training data 105 | X = data['text'] 106 | y = data['label'] 107 | 108 | # Initialize and fit TF-IDF vectorizer 109 | vectorizer = TfidfVectorizer() 110 | X_vectorized = vectorizer.fit_transform(X) 111 | 112 | # Train the model 113 | model = LogisticRegression() 114 | model.fit(X_vectorized, y) 115 | 116 | # Evaluate the model (optional) 117 | accuracy = model.score(X_vectorized, y) 118 | print(f"Training accuracy: {accuracy:.2f}") 119 | 120 | # Save both the model and vectorizer 121 | with open('model.pkl', 'wb') as model_file: 122 | pickle.dump(model, model_file) 123 | with open('vectorizer.pkl', 'wb') as vectorizer_file: 124 | pickle.dump(vectorizer, vectorizer_file) 125 | --------------------------------------------------------------------------------