Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

feat: second pipe

Louis Beaumont669747d25a4c
diff --git a/README.md b/README.md
index e478e850f..be5e2914a 100644
--- a/README.md
+++ b/README.md
@@ -47,14 +47,13 @@ export async function onTick() {
## Status
-Alpha: runs on my computer. Capture things and do things.
-
+Alpha: runs on my computer (`Macbook pro m3 32 GB ram`). Capture things and do things.
## Usage
Keep in mind that it's still experimental but got a working prototype, see the [Related projects](#related-projects) section.
-To try the current version, which capture your screen and extract the text, run:
+To try the current version, which capture your screen, extract the text, and do some LLM magic, run:
```bash
git clone https://github.com/louis030195/screen-pipe
@@ -70,6 +69,12 @@ pip install fastapi uvicorn pytesseract pillow
uvicorn main:app --reload
```
+In another terminal run [ollama](https://github.com/ollama/ollama):
+
+```bash
+ollama run llama3
+```
+
And (you need Rust + Cargo installed) the Rust CLI:
```bash
diff --git a/screenpipe/src/main.rs b/screenpipe/src/main.rs
index 189dce079..453a45043 100644
--- a/screenpipe/src/main.rs
+++ b/screenpipe/src/main.rs
@@ -2,6 +2,7 @@ use chrono::Local;
use clap::Parser;
use crossbeam::channel;
use reqwest::Client;
+use serde_json::json;
use std::fs::{create_dir_all, File};
use std::io::Read;
use std::io::Write;
@@ -69,22 +70,53 @@ async fn call_ocr_api(image_path: &str) -> Result<String, reqwest::Error> {
Ok(text)
}
-async fn process_image(filename: String) {
+
+// ! HACK - in practice these pipes shouldn't be hard coded like shit here - just iterating quickly on ideas
+
+async fn first_pipe(filename: String) -> (serde_json::Value, Option<String>) {
// Example async post-processing function
// Perform tasks like extracting text or making API calls here
- println!("Processing image asynchronously: {}", filename);
- // Simulate async work
- // tokio::time::sleep(Duration::from_secs(1)).await;
+ println!("First pipe: {}", filename);
let text = call_ocr_api(&filename).await.unwrap();
- println!("OCR result: {}", text);
+ // println!("OCR result: {}", text);
// Create a JSON object
let json = serde_json::json!({ "text": text });
let new_filename = filename.replace(".png", ".json");
let mut file = File::create(new_filename).unwrap();
file.write_all(json.to_string().as_bytes()).unwrap();
+
+ // Return JSON and optionally the image path
+ (json, Some(filename))
}
+async fn second_pipe(mut json: serde_json::Value, image_path: Option<String>) {
+ // Implement your magic here
+ let client = Client::new();
+ let response = client
+ .post("http://localhost:11434/api/chat")
+ .json(&json!({
+ "model": "llama3",
+ "stream": false,
+ "messages": [
+ {
+ "role": "user",
+ "content": format!("what is the user doing based on this OCR from his screen: {}. BE CONCISE AS FUCK, ONE LINER", json.to_string())
+ }
+ ]
+ }))
+ .send()
+ .await
+ .unwrap();
+ let text: serde_json::Value = response.json().await.unwrap();
+ let llm_response = text["message"]["content"].to_string();
+ println!("Second pipe result: {}", llm_response);
+ // update the json file by adding the LLM response
+ json["llm"] = serde_json::Value::String(llm_response);
+ let new_filename = image_path.unwrap().replace(".png", ".json");
+ let mut file = File::create(new_filename).unwrap();
+ file.write_all(json.to_string().as_bytes()).unwrap();
+}
fn screenpipe(path: &str, interval: f32, running: Arc<AtomicBool>) {
// delete and recreate the directory
println!("Deleting and recreating directory {}", path);
@@ -102,7 +134,11 @@ fn screenpipe(path: &str, interval: f32, running: Arc<AtomicBool>) {
let (tx, rx) = channel::unbounded::<(ImageBuffer<Rgba<u8>, Vec<u8>>, String)>();
let (post_tx, post_rx) = channel::unbounded::<String>();
+ let (additional_tx, additional_rx) =
+ channel::unbounded::<(serde_json::Value, Option<String>)>();
let post_tx_clone = post_tx.clone();
+ let additional_tx_clone = additional_tx.clone();
+
// Thread for saving images
let save_thread = thread::spawn(move || {
while let Ok((image, filename)) = rx.recv() {
@@ -112,15 +148,32 @@ fn screenpipe(path: &str, interval: f32, running: Arc<AtomicBool>) {
});
// Async task for post-processing
- let post_processing_thread = thread::spawn(move || {
+ let first_pipe_thread = thread::spawn(move || {
let rt = Runtime::new().unwrap();
rt.block_on(async {
while let Ok(filename) = post_rx.recv() {
+ let additional_tx_clone = additional_tx_clone.clone();
task::spawn(async move {
// Perform async post-processing here
- println!("Post-processing image: {}", filename);
+ println!("Sending image to first pipe: {}", filename);
// Example: Call an async function to process the image
- process_image(filename).await;
+ let (json, image_path) = first_pipe(filename).await;
+ additional_tx_clone.send((json, image_path)).unwrap();
+ })
+ .await
+ .unwrap();
+ }
+ });
+ });
+
+ // Async task for additional processing
+ let second_pipe_thread = thread::spawn(move || {
+ let rt = Runtime::new().unwrap();
+ rt.block_on(async {
+ while let Ok((json, image_path)) = additional_rx.recv() {
+ task::spawn(async move {
+ // Perform async additional processing here
+ second_pipe(json, image_path).await;
})
.await
.unwrap();
@@ -145,14 +198,16 @@ fn screenpipe(path: &str, interval: f32, running: Arc<AtomicBool>) {
);
tx.send((image, filename)).unwrap();
}
- println!("Captured screens. Frame: {}", frame_count);
+ // println!("Captured screens. Frame: {}", frame_count);
sleep(Duration::from_secs_f32(interval));
frame_count += 1;
}
drop(tx); // Close the channel
save_thread.join().unwrap(); // Wait for the saving thread to finish
drop(post_tx); // Close the post-processing channel
- post_processing_thread.join().unwrap(); // Wait for the post-processing thread to finish
+ first_pipe_thread.join().unwrap(); // Wait for the post-processing thread to finish
+ drop(additional_tx); // Close the additional processing channel
+ second_pipe_thread.join().unwrap(); // Wait for the additional processing thread to finish
}
fn main() {