[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Python deps, base64 impl, add examples #426

Merged
merged 14 commits into from
Jun 12, 2024
Prev Previous commit
Next Next commit
Cleaner way to load from file, base64, http
  • Loading branch information
EricLBuehler committed Jun 12, 2024
commit 41af5532ce161c76e798b4df6c675932c0981c85
40 changes: 25 additions & 15 deletions mistralrs-pyo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
use candle_core::{quantized::GgmlDType, Result};
use either::Either;
use indexmap::IndexMap;
use reqwest::StatusCode;
use std::{
cell::RefCell,
collections::HashMap,
fmt::Debug,
fs,
io::Read,
str::FromStr,
sync::{Arc, Mutex},
};
Expand Down Expand Up @@ -603,22 +604,31 @@
if !image_urls.is_empty() {
let mut images = Vec::new();
for url in image_urls {
let bytes = match reqwest::blocking::get(url.clone()) {
Ok(http_resp) => http_resp
.bytes()
.map_err(|e| PyValueError::new_err(e.to_string()))?
.to_vec(),
Err(e) => {
if e.status()
.is_some_and(|code| code == StatusCode::NOT_FOUND)
{
general_purpose::STANDARD
.decode(url)
.map_err(|e| PyValueError::new_err(e.to_string()))?
} else {
return Err(PyValueError::new_err(e.to_string()));
let bytes = if url.contains("http") {
// Read from http
match reqwest::blocking::get(url.clone()) {
Ok(http_resp) => http_resp
.bytes()
.map_err(|e| PyValueError::new_err(e.to_string()))?
.to_vec(),
Err(e) => {
return Err(PyValueError::new_err(format!("{e}")))
}
}
} else {

Check failure on line 618 in mistralrs-pyo3/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

this `else { if .. }` block can be collapsed
if let Ok(mut f) = File::open(&url) {
// Read from local file
let metadata = fs::metadata(&url)
.map_err(|e| PyValueError::new_err(e.to_string()))?;
let mut buffer = vec![0; metadata.len() as usize];
f.read(&mut buffer)?;

Check failure on line 624 in mistralrs-pyo3/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

read amount is not handled
buffer
} else {
// Decode with base64
general_purpose::STANDARD
.decode(url)
.map_err(|e| PyValueError::new_err(e.to_string()))?
}
};
images.push(
image::load_from_memory(&bytes)
Expand Down
26 changes: 18 additions & 8 deletions mistralrs-server/src/chat_completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
collections::HashMap,
env,
error::Error,
fs::{self, File},
io::Read,
ops::Deref,
pin::Pin,
sync::Arc,
Expand Down Expand Up @@ -256,14 +258,22 @@
if !image_urls.is_empty() {
let mut images = Vec::new();
for url in image_urls {
let bytes = match reqwest::get(url.clone()).await {
Ok(http_resp) => http_resp.bytes().await?.to_vec(),
Err(e) => {
if e.status().is_some_and(|code| code == StatusCode::NOT_FOUND) {
general_purpose::STANDARD.decode(url)?
} else {
anyhow::bail!(e)
}
let bytes = if url.contains("http") {
// Read from http
match reqwest::get(url.clone()).await {
Ok(http_resp) => http_resp.bytes().await?.to_vec(),
Err(e) => anyhow::bail!(e),
}
} else {

Check failure on line 267 in mistralrs-server/src/chat_completion.rs

View workflow job for this annotation

GitHub Actions / Clippy

this `else { if .. }` block can be collapsed
if let Ok(mut f) = File::open(&url) {
// Read from local file
let metadata = fs::metadata(&url)?;
let mut buffer = vec![0; metadata.len() as usize];
f.read(&mut buffer)?;

Check failure on line 272 in mistralrs-server/src/chat_completion.rs

View workflow job for this annotation

GitHub Actions / Clippy

read amount is not handled
buffer
} else {
// Decode with base64
general_purpose::STANDARD.decode(url)?
}
};
images.push(image::load_from_memory(&bytes)?);
Expand Down
Loading