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

unstructured cloud chunking

matthew-heartful9e06836310a1
diff --git a/screenpipe-integrations/src/unstructured_ocr.rs b/screenpipe-integrations/src/unstructured_ocr.rs
index 0ae1e8b61..2b33341bd 100644
--- a/screenpipe-integrations/src/unstructured_ocr.rs
+++ b/screenpipe-integrations/src/unstructured_ocr.rs
@@ -4,8 +4,13 @@ use rusty_tesseract::DataOutput;
use serde_json;
use std::collections::HashMap;
use std::io::Cursor;
+use std::io::Write;
use std::sync::Arc;
-use tokio::time::{timeout, Duration}; // Add timeout and Duration
+use tokio::time::{timeout, Duration};
+use reqwest::blocking::Client;
+use serde_json::Value;
+use tempfile::NamedTempFile;
+use anyhow::{Result, anyhow};
pub async fn perform_ocr_cloud(image: &Arc<DynamicImage>) -> Result<(String, DataOutput, String), String> {
let api_key = "ZUxfTRkf6lRgHZDXPHlFaSoOKAEbwV".to_string();
@@ -65,4 +70,39 @@ pub async fn perform_ocr_cloud(image: &Arc<DynamicImage>) -> Result<(String, Dat
.join(" ");
Ok((text, data_output, json_output))
+}
+
+pub fn unstructured_chunking(text: &str) -> Result<Vec<String>> {
+ let client = Client::new();
+
+ // Create temporary file
+ let mut temp_file = NamedTempFile::new().map_err(|e| anyhow!(e.to_string()))?;
+ temp_file.write_all(text.as_bytes()).map_err(|e| anyhow!(e.to_string()))?;
+
+ // Prepare request
+ let form = reqwest::blocking::multipart::Form::new()
+ .file("files", temp_file.path()).map_err(|e| anyhow!(e.to_string()))?
+ .text("chunking_strategy", "by_similarity")
+ .text("similarity_threshold", "0.5")
+ .text("max_characters", "300")
+ .text("output_format", "application/json");
+
+ // Send request
+ let response = client.post("https://api.unstructuredapp.io/general/v0/general")
+ .header("accept", "application/json")
+ .header("unstructured-api-key", "ZUxfTRkf6lRgHZDXPHlFaSoOKAEbwV")
+ .multipart(form)
+ .send()
+ .map_err(|e| anyhow!(e.to_string()))?;
+
+ if response.status().is_success() {
+ let chunks: Vec<Value> = response.json().map_err(|e| anyhow!(e.to_string()))?;
+ let texts: Vec<String> = chunks.iter()
+ .filter_map(|chunk| chunk["text"].as_str().map(String::from))
+ .collect();
+
+ Ok(texts)
+ } else {
+ Err(anyhow!("Error: {}", response.status()))
+ }
}
\ No newline at end of file