Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / streamlit HF demo space
Created March 13, 2024 12:20
Streamlit sentiment analysis app on Huggingface spaces
https://huggingface.co/spaces/Fancellu/streamlit-demo
@fancellu
fancellu / image_renamer.py
Created March 2, 2024 12:02
Python Hugging Face image renamer using BeitForImageClassification (useful when you have thousands of badly named images)
from PIL import Image
from transformers import BeitImageProcessor, BeitForImageClassification
import os
# Define the directory containing images
image_dir = "e:/media"
# Load the model and processor
processor = BeitImageProcessor.from_pretrained('microsoft/beit-base-patch16-224-pt22k-ft22k')
model = BeitForImageClassification.from_pretrained('microsoft/beit-base-patch16-224-pt22k-ft22k')
@fancellu
fancellu / sqlx_postgres.rs
Last active March 2, 2024 12:03
Rust sqlx example
use futures::TryStreamExt;
use sqlx::postgres::{PgPoolOptions, PgRow};
use sqlx::{Error, PgPool, Row};
use uuid::Uuid;
#[derive(sqlx::FromRow, Debug)]
struct People {
id: i32,
name: String,
}
@fancellu
fancellu / tokio_channels_demo.rs
Created February 27, 2024 13:26
Rust tokio channels demo
use tokio::sync::broadcast;
use tokio::sync::mpsc;
use tokio::sync::oneshot;
#[tokio::main]
async fn main() {
let (tx, rx) = oneshot::channel();
tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
@fancellu
fancellu / rw_lock_demo.rs
Created February 27, 2024 11:36
Rust tokio demo of RwLock
use std::sync::Arc;
use tokio::sync::RwLock;
use tokio::time::sleep;
use tokio::time::Duration;
// Demonstrates RwLock with a shared resourced
async fn use_read_lock(id: i32, lock: Arc<RwLock<String>>) {
let lock = lock.read().await;
println!("reader: {} lock string: {}", id, lock);
@fancellu
fancellu / tokio_barrier_demo.rs
Last active March 2, 2024 12:03
Rust tokio barrier demo
use std::sync::Arc;
use tokio::sync::Barrier;
use tokio::sync::BarrierWaitResult;
use tokio::sync::Notify;
use tokio::time::sleep;
use tokio::time::Duration;
// Simulating the filling of boxes with 5 cans, from many tasks
async fn barrier_wait(barrier: Arc<Barrier>, notify: Arc<Notify>, id: usize) -> BarrierWaitResult {
@fancellu
fancellu / tokio_notify_demo.rs
Created February 26, 2024 17:22
Rust tokio demo of Notify usage
use std::sync::Arc;
use tokio::sync::Notify;
use tokio::time::sleep;
use tokio::time::Duration;
// Notify can be thought of a Semaphore with 0 permits
async fn order_packages(package_delivered: Arc<Notify>) {
sleep(Duration::from_secs(2)).await;
println!("Company: Find package");
@fancellu
fancellu / tokio_sem.rs
Last active March 2, 2024 12:04
Rust tokio Semaphore example
use std::sync::Arc;
use tokio::sync::{Semaphore, SemaphorePermit};
use tokio::time::sleep;
use tokio::time::Duration;
async fn person(sem: Arc<Semaphore>, name: String) {
println!("Person {}: waiting for a permit", name);
teller(sem, &name).await;
println!("\tPerson {} finished", name);
}
@fancellu
fancellu / tokio_mutex.rs
Created February 23, 2024 19:25
Rust example of Tokio Mutex usage between threads
use std::sync::Arc;
use tokio::sync::Mutex;
// async tokio function to increment i32 behind arc mutex
async fn increment(remote: Arc<Mutex<i32>>) {
println!("trying to lock");
let mut tvc = remote.lock().await;
println!("incremented");
*tvc += 1;
}
@fancellu
fancellu / tokio_spawn.rs
Last active February 24, 2024 02:56
Rust example of tokio::spawn and JoinHandle processing
async fn hello(name: &str) -> String {
// pretend to be doing some work
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
format!("Hello {}", name)
}
fn blocking() -> String {
println!("Blocking");