### Run Audio OSINT Platform (Bash) Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Starts the Audio OSINT Platform web application. The server will be accessible at http://localhost:5000. ```bash # Run the application python app.py # Server starts at http://localhost:5000 ``` -------------------------------- ### Install Python Dependencies (Bash) Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Installs all necessary Python packages for the Audio OSINT Platform using pip. This command should be run in the project's virtual environment. ```bash # Install Python dependencies pip install flask flask-sqlalchemy gtts SpeechRecognition pydub spacy joblib scikit-learn ``` -------------------------------- ### File Download Endpoint (Python) Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Allows downloading generated audio files, such as TTS outputs, from the server. It handles GET requests to the /download/ endpoint and returns the audio file with a Content-Disposition header set to attachment. ```python import requests response = requests.get('http://localhost:5000/download/1715114022_output.mp3') # Response: Audio file with Content-Disposition: attachment header ``` -------------------------------- ### Initialize Databases (Bash) Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Initializes the SQLite databases for user authentication and agent information by running their respective Python scripts. This step is crucial before running the main application. ```bash # Initialize user database python user.py # Creates users.db with default admin/admin credentials # Initialize agent database python dbc.py # Creates instance/agents.db ``` -------------------------------- ### Download NLTK Resources Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/final.ipynb Downloads necessary resources from the Natural Language Toolkit (NLTK) library, specifically 'stopwords' and 'wordnet'. These are required for text preprocessing tasks like stop word removal and lemmatization. ```python # Download necessary NLTK resources nltk.download('stopwords') nltk.download('wordnet') ``` -------------------------------- ### Background Task for Periodic Token Refresh Source: https://github.com/samirahaque/osint-for-audio/blob/main/App/extra.txt This Python function is designed to be run as a background task to periodically refresh the JWT token. It uses the APScheduler library to schedule the task at a defined interval. The task runs within the Flask application context to access session variables and makes an HTTP GET request to the '/refresh-token' endpoint. Dependencies include APScheduler, requests, and the Flask app context. ```python from apscheduler.schedulers.background import BackgroundScheduler import requests from flask import Flask app = Flask(__name__) # Assume app is configured with secret key and other necessary settings app.secret_key = 'your secret key' # Placeholder for session data, in a real app this would be managed by Flask app.config['SESSION_TYPE'] = 'filesystem' def refresh_token_task(): with app.app_context(): # Call the refresh token endpoint try: response = requests.get('http://localhost:5000/refresh-token') if response.status_code == 200: print("Token refresh task: Success") else: print(f"Token refresh task: Failed with status code {response.status_code}") except requests.exceptions.RequestException as e: print(f"Token refresh task: Request failed - {e}") scheduler = BackgroundScheduler() scheduler.add_job(func=refresh_token_task, trigger="interval", minutes=14) scheduler.start() # In a real Flask app, you might want to add this to shut down the scheduler on exit # import atexit # atexit.register(lambda: scheduler.shutdown()) ``` -------------------------------- ### Download spaCy Model (Bash) Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Downloads the 'en_core_web_sm' model for spaCy, which can be used for various NLP tasks, including entity recognition if a custom model is not yet trained or available. ```bash # Download spaCy base model (if needed for training) python -m spacy download en_core_web_sm ``` -------------------------------- ### User Authentication - Login Endpoint Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Handles user authentication by validating credentials and establishing a session. ```APIDOC ## POST /login ### Description Authenticates a user by validating their username and password against the system's database. Upon successful authentication, a session is established, granting access to the dashboard. ### Method POST ### Endpoint /login ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **username** (string) - Required - The username for authentication. - **password** (string) - Required - The password for authentication. ### Request Example ```json { "username": "admin", "password": "admin" } ``` ### Response #### Success Response (200) - **message** (string) - A success message, typically indicating redirection to the dashboard. #### Error Response (401) - **message** (string) - An error message indicating invalid credentials. #### Response Example (On success, typically a redirect to /dashboard. On failure, a flash message is displayed.) ``` -------------------------------- ### Mount Google Drive Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/final.ipynb Mounts the Google Drive to the Colab environment, allowing access to files stored in Drive. This is essential for loading datasets and saving results. ```python from google.colab import drive drive.mount('/content/drive') ``` -------------------------------- ### Audio Upload and Analysis - Primary Endpoint (Python) Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Uploads an audio file for analysis. The platform supports WAV, MP3, OGG, FLAC, and AAC formats. It converts the audio to WAV, transcribes it using Google Speech Recognition, extracts entities with a custom spaCy NER model, and performs sentiment analysis. The response includes transcription, extracted entities, and sentiment classification. ```python import requests files = {'audio_file': open('recording.mp3', 'rb')} response = requests.post('http://localhost:5000/upload_audio', files=files) # Response: Renders entities.html template with: # - transcription: Full text transcription of audio # - entities: List of (entity_text, entity_label) tuples # Labels: WEAPON, LOCATION, OPERATION, RANK, DATE # - sentiment: 'Neutral', 'Anti-military', or 'Pro' # - icon_map: Font Awesome icons for each entity type # Example output entities: # [('JF-17 Thunder', 'WEAPON'), ('Karachi', 'LOCATION'), # ('Operation Zarb-e-Azb', 'OPERATION'), ('Major General', 'RANK'), # ('July 06, 2001', 'DATE')] ``` -------------------------------- ### Import Libraries for NLP and ML Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/final.ipynb Imports necessary libraries for Natural Language Processing (NLP) and Machine Learning (ML). This includes pandas for data manipulation, NLTK for text processing, and scikit-learn for model building and evaluation. ```python import pandas as pd import nltk from nltk.corpus import stopwords from nltk.stem import WordNetLemmatizer from sklearn.model_selection import train_test_split from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score ``` -------------------------------- ### User Authentication - Login Endpoint (Python) Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Handles user authentication by validating credentials against a SQLite database and establishing a session for dashboard access. It sends a POST request to the /login endpoint with username and password. ```python import requests # Login to the platform response = requests.post('http://localhost:5000/login', data={ 'username': 'admin', 'password': 'admin' }) # Expected: Redirect to /dashboard on success, flash message on failure # Session cookie is set with 'username' key ``` -------------------------------- ### Load Dataset with Pandas Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/final.ipynb Loads a dataset from a CSV file located in Google Drive into a pandas DataFrame. The dataset is expected to contain 'sentence', 'sentiment', and 'Entities' columns. ```python # Load dataset df = pd.read_csv('/content/drive/MyDrive/Final_Thesis/dataset/dataset.csv') ``` -------------------------------- ### Download NLTK Tagger Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb This Python code downloads the 'averaged_perceptron_tagger' package from NLTK, which is required for Part-of-Speech (POS) tagging. This is a one-time download for the environment. ```python import nltk nltk.download('averaged_perceptron_tagger') ``` -------------------------------- ### User Authentication Database (SQLite - Python) Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Manages user credentials using a SQLite database. It supports simple username and password authentication and provides functions to establish a database connection. ```python import sqlite3 def get_db_connection(): conn = sqlite3.connect('users.db') conn.row_factory = sqlite3.Row return conn # Initialize database with user.py # Creates users table with id, username, password columns # Default credentials: admin/admin # Query example conn = get_db_connection() user = conn.execute( 'SELECT * FROM users WHERE username = ? AND password = ?', ('admin', 'admin') ).fetchone() conn.close() ``` -------------------------------- ### File Download Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Downloads generated audio files (e.g., TTS outputs) from the server. ```APIDOC ## GET /download/ ### Description Allows users to download audio files that have been generated by the platform, such as text-to-speech outputs. The file is served with appropriate headers for direct download. ### Method GET ### Endpoint /download/ ### Parameters #### Path Parameters - **filename** (string) - Required - The name of the file to be downloaded. #### Query Parameters None #### Request Body None ### Request Example ```python import requests response = requests.get('http://localhost:5000/download/1715114022_output.mp3') ``` ### Response #### Success Response (200) - **Content-Disposition** (header) - `attachment; filename=""` - Indicates the file should be downloaded. - **Content-Type** (header) - The MIME type of the audio file (e.g., `audio/mpeg` for MP3). #### Response Example (The response body will be the binary content of the requested audio file.) ``` -------------------------------- ### Display Full DataFrame Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/final.ipynb Displays the entire DataFrame, showing all rows and columns after the text preprocessing. This provides a comprehensive view of the dataset's state. ```python df ``` -------------------------------- ### Audio Conversion to WAV (Python) Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Converts various audio formats to a mono 16-bit PCM WAV file at a 16000 Hz sample rate, which is compatible with Google Speech Recognition. This function uses the pydub library and returns the path to the newly created WAV file. ```python from pydub import AudioSegment def convert_to_wav(original_path): """Converts an audio file to a mono 16-bit PCM WAV at 16000 Hz sample rate.""" sound = AudioSegment.from_file(original_path) wav_path = original_path.rsplit('.', 1)[0] + '.wav' sound = sound.set_channels(1) # Mono sound = sound.set_frame_rate(16000) # 16kHz sample rate sound = sound.set_sample_width(2) # 16-bit sound.export(wav_path, format='wav') return wav_path # Usage wav_file = convert_to_wav('/path/to/audio.mp3') # Returns: '/path/to/audio.wav' ``` -------------------------------- ### NLTK Tokenization and POS Tagging (Re-download) Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb This Python code ensures that the necessary NLTK packages ('punkt' for tokenization and 'averaged_perceptron_tagger' for POS tagging) are downloaded. It then defines functions for tokenizing, POS tagging, extracting features, and aligning labels, which are fundamental steps in NLP tasks like NER. ```python import nltk from nltk.tokenize import word_tokenize # Ensure necessary NLTK packages are available nltk.download('punkt') nltk.download('averaged_perceptron_tagger') # Tokenization and POS tagging def tokenize_and_tag(sentence): tokens = word_tokenize(sentence) return nltk.pos_tag(tokens) # Extract features for each token def extract_features(sentence): return [{'word': word, 'pos': pos} for word, pos in sentence] ``` -------------------------------- ### Generate Word Clouds for Sentiment Analysis Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/final.ipynb Creates and displays word clouds for different sentiment categories ('Neutral' and 'Anti-Military'). This visualization helps identify common words associated with each sentiment. Requires matplotlib and wordcloud libraries. ```python import matplotlib.pyplot as plt from wordcloud import WordCloud # Create word clouds for label 0 and label 1 data_0 = df[df['sentiment'] == 0]['sentence'].str.cat(sep=' ') data_1 = df[df['sentiment'] == 1]['sentence'].str.cat(sep=' ') data_wc_0 = WordCloud(width=600, height=512).generate(data_0) data_wc_1 = WordCloud(width=600, height=512).generate(data_1) # Plot the word clouds plt.figure(figsize=(13, 9)) plt.subplot(1, 2, 1) plt.imshow(data_wc_0) plt.title('Neutral Word Cloud') plt.axis("off") plt.subplot(1, 2, 2) plt.imshow(data_wc_1) plt.title('Anti-Military Word Cloud') plt.axis("off") plt.show() ``` -------------------------------- ### Visualize Sentiment Distribution with Pie Chart Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/final.ipynb Generates and displays a pie chart showing the distribution of sentiments in the dataset. Requires matplotlib for plotting and assumes a 'sentiment' column exists in the DataFrame. ```python import matplotlib.pyplot as plt # Get the counts of each sentiment sentiment_counts = df['sentiment'].value_counts() # Create a pie chart plt.pie(sentiment_counts, labels=['Neutral', 'Anti'], autopct="%1.1f%%") plt.title("Percentage of each sentiment") plt.show() ``` -------------------------------- ### Text-to-Speech Conversion Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Converts provided text into an MP3 audio file using Google Text-to-Speech. ```APIDOC ## POST /text_to_speech ### Description Converts user-provided text into an audio file (MP3 format) using the Google Text-to-Speech (gTTS) library. The generated audio file is saved and made available for download. ### Method POST ### Endpoint /text_to_speech ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **text** (string) - Required - The text content to be converted to speech. ### Request Example ```python import requests response = requests.post('http://localhost:5000/text_to_speech', data={ 'text': 'The military operation was conducted in Karachi.' }) ``` ### Response #### Success Response (200) - **message** (string) - A success message, typically indicating redirection to the dashboard with a link to the downloadable TTS audio file. #### Response Example (On success, the user is redirected to the dashboard, and a session variable 'tts_file' is updated with the path to the downloadable MP3 file.) ``` -------------------------------- ### Audio Upload and Analysis - Primary Analysis Endpoint Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Uploads an audio file for transcription, entity extraction, and sentiment analysis. ```APIDOC ## POST /upload_audio ### Description Uploads an audio file, processes it for transcription using Google Speech Recognition, extracts domain-specific entities (weapons, locations, operations, ranks, dates) using a custom spaCy NER model, and performs sentiment analysis to detect anti-military content. ### Method POST ### Endpoint /upload_audio ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **audio_file** (file) - Required - The audio file to be analyzed. Supported formats include wav, mp3, ogg, flac, aac. ### Request Example ```python import requests files = {'audio_file': open('recording.mp3', 'rb')} response = requests.post('http://localhost:5000/upload_audio', files=files) ``` ### Response #### Success Response (200) - **transcription** (string) - The full text transcription of the audio. - **entities** (array) - A list of tuples, where each tuple contains an extracted entity and its label (e.g., ('JF-17 Thunder', 'WEAPON')). Labels include WEAPON, LOCATION, OPERATION, RANK, DATE. - **sentiment** (string) - The sentiment classification of the audio ('Neutral', 'Anti-military', or 'Pro'). - **icon_map** (object) - A mapping of entity types to Font Awesome icons. #### Response Example ```json { "transcription": "The JF-17 Thunder aircraft was deployed in Karachi during Operation Zarb-e-Azb.", "entities": [ ["JF-17 Thunder", "WEAPON"], ["Karachi", "LOCATION"], ["Operation Zarb-e-Azb", "OPERATION"], ["Major General", "RANK"], ["July 06, 2001", "DATE"] ], "sentiment": "Anti-military", "icon_map": { "WEAPON": "fas fa-bomb", "LOCATION": "fas fa-map-marker-alt", "OPERATION": "fas fa-crosshairs", "RANK": "fas fa-user-tie", "DATE": "fas fa-calendar-alt" } } ``` ``` -------------------------------- ### JavaScript: Animated Typing Effect for Organization Title Source: https://github.com/samirahaque/osint-for-audio/blob/main/App/Project/index.html This JavaScript implements a typing and deleting animation for the organization's title. It gradually reveals the text 'Web Development Learning Platform' and then deletes it, repeating the cycle. This effect adds a dynamic visual element to the page's header. ```javascript const orgText = "Web Development Learning Platform"; const typingSpeed = 100; let typingIndex = 0; let isTyping = true; const orgElement = document.getElementById('organization-title'); function type() { if (isTyping) { if (typingIndex <= orgText.length) { orgElement.innerHTML = orgText.slice(0, typingIndex++); if(typingIndex === 1) orgElement.style.opacity = 1; // Make text visible when typing starts setTimeout(type, typingSpeed); } else { isTyping = false; setTimeout(() => { orgElement.style.opacity = 0; // Hide text before deleting setTimeout(type, 500); // Delay before start deleting }, 2000); // Pause before deleting } } else { if (typingIndex > 0) { orgElement.innerHTML = orgText.slice(0, --typingIndex); setTimeout(type, typingSpeed); } else { isTyping = true; setTimeout(type, 1000); // Pause before retyping } } } setTimeout(type, 1000); // Initial delay before typing starts ``` -------------------------------- ### Visualize Model Performance Comparison (Python) Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb Generates a bar chart comparing the precision, recall, and F1-score of different machine learning models. Matplotlib is used to create the plot, with models on the x-axis and performance metrics represented by different colored bars. ```python import matplotlib.pyplot as plt import numpy as np # Set up the matplotlib figure plt.figure(figsize=(15, 6)) # Set the positions and width for the bars barWidth = 0.25 r1 = np.arange(len(report_df)) r2 = [x + barWidth for x in r1] r3 = [x + barWidth for x in r2] # Create bars for each metric plt.bar(r1, report_df['precision'], color='b', width=barWidth, edgecolor='grey', label='Precision') plt.bar(r2, report_df['recall'], color='r', width=barWidth, edgecolor='grey', label='Recall') plt.bar(r3, report_df['f1-score'], color='g', width=barWidth, edgecolor='grey', label='F1-Score') # Add labels and title plt.xlabel('Models', fontweight='bold') plt.xticks([r + barWidth for r in range(len(report_df))], report_df['Model'], rotation=45) plt.title('Model Performance Comparison') # Create legend & Show graphic plt.legend() plt.show() ``` -------------------------------- ### Create Bar Chart for Model Performance Comparison (Python) Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb Generates a bar chart to compare model performance metrics (Precision, Recall, F1-Score) across different models. It uses Matplotlib to plot the bars with distinct colors for each metric and includes labels, a title, and a legend. ```python import matplotlib.pyplot as plt import numpy as np # Set up the matplotlib figure plt.figure(figsize=(10, 6)) # Define the bar width barWidth = 0.5 # Set the position of bars on the X-axis r = np.arange(len(report_df)) # Make the plot plt.bar(r, report_df['precision'], color='#add8e6', edgecolor='grey', width=barWidth, label='Precision') plt.bar(r, report_df['recall'], bottom=report_df['precision'], color='#ffb6c1', edgecolor='grey', width=barWidth, label='Recall') plt.bar(r, report_df['f1-score'], bottom=[i+j for i,j in zip(report_df['precision'], report_df['recall'])], color='#90ee90', edgecolor='grey', width=barWidth, label='F1-Score') # Add labels and title plt.xlabel('Models', fontweight='bold') plt.xticks([r for r in range(len(report_df))], report_df['Model']) plt.ylabel('Scores') plt.title('Stacked Bar Chart of Model Performance') # Create legend & Show graphic plt.legend() plt.show() ``` -------------------------------- ### Display HTML Content in IPython Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb This snippet demonstrates how to construct an HTML string and then display it using the HTML object from IPython.core.display. It's commonly used for rendering rich output in Jupyter notebooks or similar environments. ```python html = '' html += '
' # ... other HTML content generation ... html += '
' from IPython.core.display import display, HTML display(HTML(html)) ``` -------------------------------- ### Prepare Features and Labels for Machine Learning in Python Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb Separates the extracted features (X) and labels (y) from the processed data. This step is crucial for preparing the data for machine learning model training. It flattens nested lists of features and labels. ```python X = [entry[0] for entry in processed_data] # Features y = [entry[1] for entry in processed_data] # Labels ``` -------------------------------- ### Train Gradient Boosting Model (Python) Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb Trains a Gradient Boosting Classifier model using scikit-learn. It takes training data (X_train, y_train) and predicts on test data (X_test). ```python from sklearn.ensemble import GradientBoostingClassifier gb_model = GradientBoostingClassifier() gb_model.fit(X_train, y_train) gb_predictions = gb_model.predict(X_test) ``` -------------------------------- ### Jinja2 Template for Flash Messages Source: https://github.com/samirahaque/osint-for-audio/blob/main/App/templates/transcription.html This Jinja2 template code demonstrates how to display flashed messages on a web page. It iterates through any messages passed to the template and renders them within a designated HTML element. ```jinja2 {% with messages = get_flashed_messages() %} {% if messages %} {% for message in messages %} {{ message }} {% endfor %} {% endif %} {% endwith %} ``` -------------------------------- ### Create Model Performance DataFrame (Python) Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb Creates a pandas DataFrame to compare the weighted average performance metrics (precision, recall, f1-score, support) of different machine learning models. The DataFrame is then transposed and reset for better readability. ```python import pandas as pd # Create a DataFrame from the reports report_df = pd.DataFrame({ 'Random Forest': rf_report['weighted avg'], 'SVM': svm_report['weighted avg'], 'Logistic Regression': logreg_report['weighted avg'], 'Naive Bayes': nb_report['weighted avg'], 'Gradient Boosting': gb_report['weighted avg'] }) # Transpose the DataFrame for better readability report_df = report_df.T # Reset index to have model names as a column report_df.reset_index(inplace=True) report_df.rename(columns={'index': 'Model'}, inplace=True) # Display the DataFrame print(report_df) ``` -------------------------------- ### Speech-to-Text Transcription Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Transcribes audio files to plain text using Google Speech Recognition. ```APIDOC ## POST /speech_to_text ### Description Transcribes an uploaded audio file directly to text using Google Speech Recognition. This endpoint provides a basic transcription service without performing entity extraction or sentiment analysis. ### Method POST ### Endpoint /speech_to_text ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **audio_file** (file) - Required - The audio file to be transcribed. Supported formats include wav, mp3, ogg, flac, aac. ### Request Example ```python import requests files = {'audio_file': open('audio.wav', 'rb')} response = requests.post('http://localhost:5000/speech_to_text', files=files) ``` ### Response #### Success Response (200) - **transcription** (string) - The plain text transcription of the audio. - **filename** (string) - The original filename of the uploaded audio file. #### Response Example ```json { "transcription": "This is a test recording.", "filename": "audio.wav" } ``` ``` -------------------------------- ### Text-to-Speech Conversion Endpoint (Python) Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Converts provided text into an MP3 audio file using Google Text-to-Speech (gTTS). The request includes the text to be converted in form data. The response redirects to the dashboard with a downloadable MP3 file stored in the uploads/ directory. ```python import requests response = requests.post('http://localhost:5000/text_to_speech', data={ 'text': 'The military operation was conducted in Karachi.' }) # Response: Redirects to dashboard with downloadable MP3 file # File stored in uploads/ directory with timestamp filename # Session updated with 'tts_file' key for download link ``` -------------------------------- ### Compare Model Performance Metrics Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/final.ipynb This code snippet iterates through a dictionary of machine learning models, trains and evaluates each using the `train_evaluate_model` function, and stores the results in a list of dictionaries. It then converts this list into a pandas DataFrame for structured comparison. Dependencies include pandas and scikit-learn classifiers. ```python import pandas as pd from sklearn.naive_bayes import MultinomialNB from sklearn.svm import SVC from sklearn.ensemble import RandomForestClassifier from sklearn.linear_model import LogisticRegression models = { "Logistic Regression": LogisticRegression(), "Naive Bayes": MultinomialNB(), "Support Vector Machine": SVC(), "Random Forest": RandomForestClassifier() } results = [] for name, model in models.items(): accuracy, precision, recall, f1, y_pred = train_evaluate_model(model, X_train_vectorized, y_train, X_test_vectorized, y_test) results.append({"Model": name, "Accuracy": accuracy, "Precision": precision, "Recall": recall, "F1-Score": f1}) # Convert results to DataFrame results_df = pd.DataFrame(results) ``` -------------------------------- ### Display Model Evaluation Metrics (Python) Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/final.ipynb Displays a DataFrame containing model evaluation metrics such as Accuracy, Precision, Recall, and F1-Score. This is useful for understanding the performance of different classification models. ```python print("Model Evaluation Metrics:") display(results_df) ``` -------------------------------- ### Create Heatmap of Model Performance (Python) Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb Visualizes model performance metrics using a heatmap. It prepares the data by setting the 'Model' column as the index and then uses Seaborn to draw the heatmap with annotations and a 'coolwarm' color map. The plot includes a title and is displayed using Matplotlib. ```python import seaborn as sns # Set the aesthetic style of the plots sns.set() # Prepare data for heatmap heatmap_data = report_df.set_index('Model') # Draw a heatmap plt.figure(figsize=(8, 6)) sns.heatmap(heatmap_data, annot=True, cmap="coolwarm", fmt=".2f") plt.title('Heatmap of Model Performance') plt.show() ``` -------------------------------- ### Speech-to-Text Transcription Endpoint (Python) Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Transcribes audio files to plain text using Google Speech Recognition. This endpoint does not perform entity extraction or sentiment analysis. It accepts audio files via multipart/form-data and renders a transcription.html template with the transcribed text and original filename. ```python import requests files = {'audio_file': open('audio.wav', 'rb')} response = requests.post('http://localhost:5000/speech_to_text', files=files) # Response: Renders transcription.html with: # - transcription: Plain text transcription # - filename: Original uploaded filename ``` -------------------------------- ### Agent Database Model (SQLAlchemy - Python) Source: https://context7.com/samirahaque/osint-for-audio/llms.txt Defines an SQLAlchemy model for storing agent information, including a unique key number and name. It integrates with Flask-SQLAlchemy for database operations. ```python from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class Agent(db.Model): __tablename__ = 'agents' id = db.Column(db.Integer, primary_key=True) key_no = db.Column(db.String(20), unique=True, nullable=False) name = db.Column(db.String(50), nullable=False) # Usage with dbc.py def add_agent(key_no, name): existing_agent = Agent.query.filter_by(key_no=key_no).first() if not existing_agent: new_agent = Agent(key_no=key_no, name=name) db.session.add(new_agent) db.session.commit() # Example add_agent("156", "Muhammad Ayub") ``` -------------------------------- ### Python/Jinja2: Conditional Download Link Source: https://github.com/samirahaque/osint-for-audio/blob/main/App/templates/dashboard.html This Jinja2 template snippet conditionally renders a download link for a speech file. The link is only displayed if the 'tts_filename' variable is defined, indicating that a speech file has been generated. ```jinja2 {% if tts_filename %} Download Your Speech File ------------------------- [Download Speech File]({{ url_for('download', filename=tts_filename) }}) {% endif %} ``` -------------------------------- ### Display Flash Messages in Jinja2 Template Source: https://github.com/samirahaque/osint-for-audio/blob/main/App/templates/login.html This Jinja2 template snippet displays flashed messages, typically used for user feedback like login success or error notifications. It iterates through messages provided by the Flask framework. ```jinja2 {% with messages = get_flashed_messages() %} {% if messages %} {% for message in messages %}* {{ message }} {% endfor %} {% endif %} {% endwith %} ``` -------------------------------- ### Split Data for Training and Testing in Python Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb Splits the vectorized features and encoded labels into training and testing sets using `train_test_split` from scikit-learn. A test size of 20% is specified, meaning 80% of the data will be used for training. This is a standard procedure for evaluating model performance. ```python from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X_vectorized, y_encoded, test_size=0.2) ``` -------------------------------- ### Train RandomForestClassifier in Python Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb Initializes a `RandomForestClassifier` from scikit-learn and trains it using the training data (X_train, y_train). This is a supervised learning algorithm used for classification tasks. The output shows the initialized classifier object. ```python from sklearn.ensemble import RandomForestClassifier # Initialize the classifier clf = RandomForestClassifier() # Train the classifier clf.fit(X_train, y_train) ``` -------------------------------- ### Inspect Annotation Structure Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb This Python snippet prints the first annotation entry from a list of annotations. This is useful for understanding the format of the data, particularly the structure of entities and their labels within a text. ```python print(annotations[0]) ``` -------------------------------- ### Format Model Evaluation Metrics as Percentages Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/final.ipynb This code snippet takes a pandas DataFrame containing model evaluation metrics (Accuracy, Precision, Recall, F1-Score) and formats these columns as percentage strings with two decimal places. This is useful for presenting results clearly. It assumes the DataFrame `results_df` already exists and contains these columns. ```python # Assuming 'results_df' contains the results from the previous step results_df['Accuracy'] = results_df['Accuracy'].apply(lambda x: f"{x * 100:.2f}%") results_df['Precision'] = results_df['Precision'].apply(lambda x: f"{x * 100:.2f}%") results_df['Recall'] = results_df['Recall'].apply(lambda x: f"{x * 100:.2f}%") results_df['F1-Score'] = results_df['F1-Score'].apply(lambda x: f"{x * 100:.2f}%") ``` -------------------------------- ### Load and Inspect CSV Data with Pandas Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb This Python code uses the pandas library to load a CSV file from Google Drive into a DataFrame. It then displays the first few rows and lists the column names for initial data inspection. Ensure the CSV file path is correct. ```python import pandas as pd # Define the path to your CSV file csv_file_path = '/content/drive/MyDrive/Thesis/Dataset/1700_sentences.csv' # Load the CSV file into a pandas DataFrame df = pd.read_csv(csv_file_path) # Display the first few rows of the DataFrame to inspect its structure print(df.head()) # Display the column names print("Columns in the dataset:", df.columns.tolist()) ``` -------------------------------- ### Create and Style Pakistan Cities Table in Python Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/Data.ipynb This Python code snippet demonstrates how to create a table of cities in Pakistan using pandas and Matplotlib. It defines a list of cities, converts it into a pandas DataFrame, and then renders it as a table. The table includes bolded column headers and adjusted font size and scaling for readability. ```python # List of cities in Pakistan cities = ["Karachi", "Lahore", "Islamabad (Capital)", "Rawalpindi", "Faisalabad", "Peshawar", "Multan", "Hyderabad", "Quetta", "Gujranwala", "Sialkot", "Bahawalpur", "Sukkur", "Larkana", "Sheikhupura", "Rahim Yar Khan", "Jhang", "Dera Ghazi Khan", "Gujrat", "Sahiwal", "Wah Cantonment", "Mardan", "Kasur", "Okara", "Mingora", "Nawabshah", "Chiniot", "Kotli", "Kamoke", "Hafizabad", "Sargodha", "Mirpur (Azad Kashmir)", "Attock", "Khanewal", "Muzaffargarh", "Jhelum", "Haripur", "Kohat", "Burewala", "Dera Ismail Khan"] # Create a DataFrame for the cities df_cities = pd.DataFrame(cities, columns=["Cities"]) # Create a figure and a table for the cities fig, ax = plt.subplots(figsize=(6, 8)) # set size frame ax.axis('tight') ax.axis('off') # Creating table for the cities table = ax.table(cellText=df_cities.values, colLabels=df_cities.columns, cellLoc='center', loc='center') # Bold the headings for (i, j), cell in table.get_celld().items(): if i == 0: # Only the first row contains the column labels. cell.get_text().set_weight('bold') # Adjust table table.auto_set_font_size(False) table.set_fontsize(10) table.scale(1.2, 1.2) plt.show() ``` -------------------------------- ### Display Text with Colored Named Entities (Python) Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb Renders text with predicted named entities highlighted using distinct background colors. It generates an HTML string where each token is wrapped in a `` tag with a background color corresponding to its predicted entity type. This requires `IPython.display` for rendering HTML. ```python from IPython.core.display import display, HTML # Create an HTML string to display the text with colored labels html = '
' # Define a color scheme for your labels label_colors = { 'B-RANK': '#ffadad', # Example: light red 'B-WEAPON': '#bdb2ff', # Example: light purple 'B-OPERATION': '#9bf6ff', # Example: light blue 'B-LOCATION': '#fdffb6', # Example: light yellow 'B-DATE': '#ffd6a5', # Example: light orange 'O': '#b0f2b4' # Example: light green for 'other' labels } for token, label in predicted_entities: # Get color for the label, default to light green if label not in dictionary color = label_colors.get(label, '#b0f2b4') # Wrap token in span with style, and append the label type html += f'{token} ({label})' # Close the HTML string html += '
' # Display the HTML display(HTML(html)) ``` -------------------------------- ### Display Processed DataFrame Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/final.ipynb Displays the first few rows of the DataFrame after the text preprocessing has been applied to the 'sentence' column. This allows for a visual inspection of the cleaned text. ```python df.head() ``` -------------------------------- ### JavaScript: Handle Button Clicks for Navigation Source: https://github.com/samirahaque/osint-for-audio/blob/main/App/templates/dashboard.html This JavaScript code snippet listens for the 'DOMContentLoaded' event and attaches click event listeners to all buttons with a 'data-url' attribute. When a button is clicked, it navigates the browser to the URL specified in the 'data-url' attribute. ```javascript document.addEventListener('DOMContentLoaded', function () { const buttons = document.querySelectorAll('button[data-url]'); buttons.forEach(button => { button.addEventListener('click', function () { const url = button.getAttribute('data-url'); window.location.href = url; }); }); }); ``` -------------------------------- ### Read and Display CSV Data Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/final.ipynb Reads a CSV file into a Pandas DataFrame and displays the first few rows. This is a common step for inspecting data after it has been saved or loaded. ```python import pandas as pd df = pd.read_csv('/content/drive/MyDrive/Final_Thesis/dataset/preprocessed_dataset.csv') df.head() ``` -------------------------------- ### Preprocess New Text for Feature Extraction (Python) Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb Prepares new text data for natural language processing tasks. This function tokenizes the input text, performs part-of-speech tagging, and then extracts relevant features using a predefined `extract_features` function. It requires the `nltk` library for tokenization and POS tagging. ```python import nltk from nltk.tokenize import word_tokenize def preprocess_new_text(new_text): # Tokenize and POS tag the new text tokenized_text = nltk.pos_tag(word_tokenize(new_text)) # Extract features for each token features = extract_features(tokenized_text) return features # Example new text new_text = "Lieutenant reported a missile in Red River on 2023-11-02." processed_new_text = preprocess_new_text(new_text) ``` -------------------------------- ### Split Data for Model Training Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/final.ipynb Splits the dataset into training and testing sets for model development. It separates the 'sentence' column (features) from the 'sentiment' column (target). Requires scikit-learn's train_test_split. ```python from sklearn.model_selection import train_test_split # Split data X_train, X_test, y_train, y_test = train_test_split(df['sentence'], df['sentiment'], test_size=0.30, random_state=42) ``` -------------------------------- ### NLTK Tokenization and POS Tagging Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb This Python code defines functions for tokenizing sentences and performing Part-of-Speech (POS) tagging using NLTK. It also includes functions to extract features from tokens and align labels with tokens for Named Entity Recognition (NER) tasks. Ensure 'punkt' and 'averaged_perceptron_tagger' are downloaded. ```python import nltk from nltk.tokenize import word_tokenize # Make sure you have the necessary NLTK packages nltk.download('punkt') nltk.download('averaged_perceptron_tagger') # Tokenization and POS tagging def tokenize_and_tag(sentence): tokens = word_tokenize(sentence) return nltk.pos_tag(tokens) # Extract features for each token def extract_features(sentence): return [{'word': word, 'pos': pos} for word, pos in sentence] # Align labels with tokens def align_labels_with_tokens(tokens, entities, text): labels = ['O'] * len(tokens) # Default label for tokens outside any entity for start, end, entity_type in entities: # Adjust start and end indices for token alignment for i, (word, _) in enumerate(tokens): if start <= text.find(word) < end: labels[i] = entity_type return labels ``` -------------------------------- ### Train and Evaluate Multiple Models Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/final.ipynb This function trains a model, generates predictions, and returns evaluation metrics (accuracy, precision, recall, F1-score) along with the predictions. It's designed to be used in a loop for comparing different models. Dependencies include scikit-learn metrics. ```python from sklearn.metrics import accuracy_score, precision_recall_fscore_support def train_evaluate_model(model, X_train_vectorized, y_train, X_test_vectorized, y_test): # Train the model model.fit(X_train_vectorized, y_train) # Make predictions y_pred = model.predict(X_test_vectorized) # Calculate accuracy, precision, recall, and F1-score accuracy = accuracy_score(y_test, y_pred) precision, recall, f1, _ = precision_recall_fscore_support(y_test, y_pred, average='binary') # Return evaluation metrics and predictions return accuracy, precision, recall, f1, y_pred ``` -------------------------------- ### Create and Display Table of Pakistan Military Operations (Python) Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/Data.ipynb This snippet creates a pandas DataFrame from a list of military operations in Pakistan and then visualizes it as a table using matplotlib. It includes steps to format the table by bolding the column headers and adjusting font size and scale. The code requires pandas and matplotlib libraries. ```python import pandas as pd import matplotlib.pyplot as plt # List of military operations in Pakistan operations = [ 'Operation Zarb-e-Azb', 'Operation Radd-ul-Fasaad', 'Operation Swift Retort', 'Operation Black Thunderstorm', 'Operation Rah-e-Nijat', 'Operation Rah-e-Raast', 'Operation Sirat-e-Mustaqeem', 'Operation Sherdil', 'Operation Rah-e-Haq', 'Operation Koh-e-Paima', 'Operation Janbaz', 'Operation Brekhna', 'Operation Khwakh Ba De Sham', 'Operation Zalzala', 'Operation Toar Tander-I', 'Operation Toar Tander-II', 'Operation Sher Dil', 'Operation Rah-e-Haq-II', 'Operation Mehran', 'Operation Sunrise', 'Operation Madad', 'Operation Umeed-e-Nuh', 'Operation Pei Kobra', 'Operation Ghazi' ] # Create a DataFrame for the military operations df_operations = pd.DataFrame(operations, columns=["Military Operations"]) # Create a figure and a table for the military operations fig, ax = plt.subplots(figsize=(8, 10)) # set size frame ax.axis('tight') ax.axis('off') # Creating table for the military operations table = ax.table(cellText=df_operations.values, colLabels=df_operations.columns, cellLoc='center', loc='center') # Bold the headings for (i, j), cell in table.get_celld().items(): if i == 0: # Only the first row contains the column labels. cell.get_text().set_weight('bold') # Adjust table table.auto_set_font_size(False) table.set_fontsize(10) table.scale(1.2, 1.2) plt.show() ``` -------------------------------- ### Create and Style Pakistan Army Ranks Table in Python Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/Data.ipynb This Python code snippet demonstrates how to create a table displaying Pakistan Army ranks. It utilizes pandas for data structuring and Matplotlib for table visualization. The code defines rank categories and then generates a DataFrame, which is subsequently rendered as a table with bolded headers and adjusted font sizes and scaling. ```python # Ranks in the Pakistan Armyarmée_ranks = { "Commissioned Officers": ["Second Lieutenant", "Lieutenant", "Captain", "Major", "Lieutenant Colonel", "Colonel", "Brigadier", "Major General", "Lieutenant General", "General", "Field Marshal"], "Junior Commissioned Officers": ["Naib Subedar", "Subedar", "Subedar Major"], "Non-Commissioned Officers and Soldiers": ["Sepoy", "Lance Naik", "Naik", "Havaldar", "Company Quartermaster Havaldar (CQMH)", "Battalion Quartermaster Havaldar (BQMH)", "Company Havaldar Major (CHM)", "Battalion Havaldar Major (BHM)"] } # Creating a DataFrame for the Pakistan Army ranks df_army = pd.DataFrame.from_dict(army_ranks, orient='index').transpose() # Create a figure and a table for the Pakistan Army ranks fig, ax = plt.subplots(figsize=(10, 4)) # set size frame ax.axis('tight') ax.axis('off') # Creating table for the Pakistan Army ranks table = ax.table(cellText=df_army.values, colLabels=df_army.columns, cellLoc='center', loc='center') # Bold the headings for (i, j), cell in table.get_celld().items(): if i == 0: # Only the first row contains the column labels. cell.get_text().set_weight('bold') # Adjust table table.auto_set_font_size(False) table.set_fontsize(10) table.scale(1.2, 1.4) plt.show() ``` -------------------------------- ### Display Naive Bayes Classification Report Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb This snippet prints the classification report for the Naive Bayes model. It provides performance metrics like precision, recall, and F1-score for each class, enabling an evaluation of the Naive Bayes classifier's effectiveness. ```python from sklearn.metrics import classification_report print("Naive Bayes Classification Report:") print(classification_report(y_test, nb_predictions)) ``` -------------------------------- ### Generate Classification Reports for Multiple Models (Python) Source: https://github.com/samirahaque/osint-for-audio/blob/main/Code/NLTK with New Dataset.ipynb Generates classification reports for various machine learning models including Random Forest, SVM, Logistic Regression, Naive Bayes, and Gradient Boosting. The reports are generated in dictionary format for further processing. ```python from sklearn.metrics import classification_report # Example for Random Forest rf_report = classification_report(y_test, rf_predictions, output_dict=True) # Repeat for other models svm_report = classification_report(y_test, svm_predictions, output_dict=True) logreg_report = classification_report(y_test, logreg_predictions, output_dict=True) nb_report = classification_report(y_test, nb_predictions, output_dict=True) gb_report = classification_report(y_test, gb_predictions, output_dict=True) ```