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

remove references to blocking reqwest

kerosinab685cb36ae66
diff --git a/screenpipe-audio/src/stt.rs b/screenpipe-audio/src/stt.rs
index 0d29069b2..0bd6c8f5a 100644
--- a/screenpipe-audio/src/stt.rs
+++ b/screenpipe-audio/src/stt.rs
@@ -425,7 +425,7 @@ enum Task {
Translate,
}
-use reqwest::blocking::Client;
+use reqwest::Client;
use serde_json::Value;
// Replace the get_deepgram_api_key function with this:
@@ -433,8 +433,11 @@ fn get_deepgram_api_key() -> String {
"7ed2a159a094337b01fd8178b914b7ae0e77822d".to_string()
}
-// TODO: this should use async reqwest not blocking, cause crash issue because all our code is async
-fn transcribe_with_deepgram(api_key: &str, audio_data: &[f32], device: &str) -> Result<String> {
+async fn transcribe_with_deepgram(
+ api_key: &str,
+ audio_data: &[f32],
+ device: &str,
+) -> Result<String> {
debug!("starting deepgram transcription");
let client = Client::new();
@@ -464,10 +467,10 @@ fn transcribe_with_deepgram(api_key: &str, audio_data: &[f32], device: &str) ->
.body(wav_data)
.send();
- match response {
+ match response.await {
Ok(resp) => {
debug!("received response from deepgram api");
- match resp.json::<Value>() {
+ match resp.json::<Value>().await {
Ok(result) => {
debug!("successfully parsed json response");
if let Some(err_code) = result.get("err_code") {
@@ -513,7 +516,7 @@ fn transcribe_with_deepgram(api_key: &str, audio_data: &[f32], device: &str) ->
}
}
-pub fn stt(
+pub async fn stt(
audio_input: &AudioInput,
whisper_model: &WhisperModel,
audio_transcription_engine: Arc<AudioTranscriptionEngine>,
@@ -605,7 +608,7 @@ pub fn stt(
audio_input.device,
&api_key[..8]
);
- match transcribe_with_deepgram(&api_key, &speech_frames, &audio_input.device) {
+ match transcribe_with_deepgram(&api_key, &speech_frames, &audio_input.device).await {
Ok(transcription) => Ok(transcription),
Err(e) => {
error!(
@@ -797,7 +800,7 @@ pub async fn create_whisper_channel(
) = unbounded_channel();
let mut vad_engine: Box<dyn VadEngine + Send> = match vad_engine {
VadEngineEnum::WebRtc => Box::new(WebRtcVad::new()),
- VadEngineEnum::Silero => Box::new(SileroVad::new()?),
+ VadEngineEnum::Silero => Box::new(SileroVad::new().await?),
};
let shutdown_flag = Arc::new(AtomicBool::new(false));
@@ -850,7 +853,7 @@ pub async fn create_whisper_channel(
unreachable!("This code should not be reached on non-macOS platforms")
}
} else {
- match stt(&input, &whisper_model, audio_transcription_engine.clone(), &mut *vad_engine, deepgram_api_key.clone(), &output_path) {
+ match stt(&input, &whisper_model, audio_transcription_engine.clone(), &mut *vad_engine, deepgram_api_key.clone(), &output_path).await {
Ok((transcription, path)) => TranscriptionResult {
input: input.clone(),
transcription: Some(transcription),
diff --git a/screenpipe-audio/src/vad_engine.rs b/screenpipe-audio/src/vad_engine.rs
index 630fb5d45..9cf4f3c41 100644
--- a/screenpipe-audio/src/vad_engine.rs
+++ b/screenpipe-audio/src/vad_engine.rs
@@ -9,7 +9,7 @@ pub enum VadEngineEnum {
Silero,
}
-pub trait VadEngine {
+pub trait VadEngine: Send {
fn is_voice_segment(&mut self, audio_chunk: &[f32]) -> anyhow::Result<bool>;
}
@@ -44,9 +44,9 @@ pub struct SileroVad {
}
impl SileroVad {
- pub fn new() -> anyhow::Result<Self> {
+ pub async fn new() -> anyhow::Result<Self> {
debug!("Initializing SileroVad...");
- let model_path = Self::download_model()?;
+ let model_path = Self::download_model().await?;
debug!("SileroVad Model downloaded to: {:?}", model_path);
let vad = Vad::new(model_path, 16000).map_err(|e| {
debug!("SileroVad Error creating Vad: {}", e);
@@ -56,12 +56,12 @@ impl SileroVad {
Ok(Self { vad })
}
- fn download_model() -> anyhow::Result<PathBuf> {
+ async fn download_model() -> anyhow::Result<PathBuf> {
debug!("Downloading SileroVAD model...");
let url =
"https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/silero_vad.onnx";
- let response = reqwest::blocking::get(url)?;
- let model_data = response.bytes()?;
+ let response = reqwest::get(url).await?;
+ let model_data = response.bytes().await?;
let path = std::env::temp_dir().join("silero_vad.onnx");
let mut file = std::fs::File::create(&path)?;
@@ -94,11 +94,11 @@ impl VadEngine for SileroVad {
}
}
-pub fn create_vad_engine(engine: VadEngineEnum) -> anyhow::Result<Box<dyn VadEngine>> {
+pub async fn create_vad_engine(engine: VadEngineEnum) -> anyhow::Result<Box<dyn VadEngine>> {
match engine {
VadEngineEnum::WebRtc => Ok(Box::new(WebRtcVad::new())),
VadEngineEnum::Silero => {
- let silero_vad = SileroVad::new()?;
+ let silero_vad = SileroVad::new().await?;
Ok(Box::new(silero_vad))
}
}
diff --git a/screenpipe-integrations/Cargo.toml b/screenpipe-integrations/Cargo.toml
index 8a87e5a8c..eadf56132 100644
--- a/screenpipe-integrations/Cargo.toml
+++ b/screenpipe-integrations/Cargo.toml
@@ -18,4 +18,5 @@ chrono = { version = "0.4", features = ["serde"] }
log = "0.4"
tempfile = "3.2"
anyhow = "1.0"
-chrono-tz = "0.8"
\ No newline at end of file
+chrono-tz = "0.8"
+mime_guess = "2.0.5"
diff --git a/screenpipe-integrations/src/unstructured_ocr.rs b/screenpipe-integrations/src/unstructured_ocr.rs
index 81816aa88..c61308c44 100644
--- a/screenpipe-integrations/src/unstructured_ocr.rs
+++ b/screenpipe-integrations/src/unstructured_ocr.rs
@@ -1,16 +1,18 @@
-use image::{DynamicImage, ImageEncoder, codecs::png::PngEncoder};
+use anyhow::{anyhow, Result};
+use image::{codecs::png::PngEncoder, DynamicImage, ImageEncoder};
+use log::error;
use reqwest::multipart::{Form, Part};
+use reqwest::Client;
use serde_json;
+use serde_json::Value;
use std::collections::HashMap;
use std::env;
use std::io::Cursor;
+use std::io::Read;
use std::io::Write;
-use tokio::time::{timeout, Duration};
-use reqwest::blocking::Client;
-use serde_json::Value;
+use std::path::PathBuf;
use tempfile::NamedTempFile;
-use anyhow::{Result, anyhow};
-use log::error;
+use tokio::time::{timeout, Duration};
pub async fn perform_ocr_cloud(image: &DynamicImage) -> Result<(String, String, Option<f64>)> {
let api_key = match env::var("UNSTRUCTURED_API_KEY") {
@@ -44,12 +46,17 @@ pub async fn perform_ocr_cloud(image: &DynamicImage) -> Result<(String, String,
.text("coordinates", "true");
let client = reqwest::Client::new();
- let response = match timeout(Duration::from_secs(180), client
- .post(&api_url)
- .header("accept", "application/json")
- .header("unstructured-api-key", &api_key)
- .multipart(form)
- .send()).await {
+ let response = match timeout(
+ Duration::from_secs(180),
+ client
+ .post(&api_url)
+ .header("accept", "application/json")
+ .header("unstructured-api-key", &api_key)
+ .multipart(form)
+ .send(),
+ )
+ .await
+ {
Ok(Ok(response)) => response,
Ok(Err(e)) => return Err(anyhow!("Request error: {}", e)),
Err(_) => return Err(anyhow!("Request timed out")),
@@ -89,7 +96,7 @@ fn calculate_overall_confidence(parsed_response: &Vec<HashMap<String, serde_json
}
}
-pub fn unstructured_chunking(text: &str) -> Result<Vec<String>> {
+pub async fn unstructured_chunking(text: &str) -> Result<Vec<String>> {
let client = Client::new();
let api_key = match env::var("UNSTRUCTURED_API_KEY") {
Ok(key) => key,
@@ -100,27 +107,58 @@ pub fn unstructured_chunking(text: &str) -> Result<Vec<String>> {
};
// 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()))?;
+ 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()))?
+ let form = reqwest::multipart::Form::new()
+ .part("files", {
+ let mut bytes = vec![];
+ temp_file.read_to_end(&mut bytes)?;
+
+ let path = PathBuf::from(temp_file.path());
+
+ let file_name = path
+ .file_name()
+ .ok_or(anyhow!("Couldn't send files to unstructuredapp API"))?
+ .to_string_lossy()
+ .into_owned();
+
+ let mime_type = mime_guess::from_path(path)
+ .first()
+ .ok_or(anyhow!("Couldn't determine file's MIME type."))?
+ .essence_str()
+ .to_owned();
+
+ let part = Part::bytes(bytes)
+ .file_name(file_name)
+ .mime_str(&mime_type)?;
+
+ part
+ })
.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")
+ let response = client
+ .post("https://api.unstructuredapp.io/general/v0/general")
.header("accept", "application/json")
.header("unstructured-api-key", &api_key)
.multipart(form)
.send()
+ .await
.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()
+ let chunks = response
+ .json::<Vec<Value>>()
+ .await
+ .map_err(|e| anyhow!(e.to_string()))?;
+ let texts: Vec<String> = chunks
+ .iter()
.filter_map(|chunk| chunk["text"].as_str().map(String::from))
.collect();
@@ -128,4 +166,4 @@ pub fn unstructured_chunking(text: &str) -> Result<Vec<String>> {
} else {
Err(anyhow!("Error: {}", response.status()))
}
-}
\ No newline at end of file
+}