Add Python server setup and dependencies installation
This commit is contained in:
1
src-tauri/key.txt
Normal file
1
src-tauri/key.txt
Normal file
@@ -0,0 +1 @@
|
||||
6a8c5a113f37455a800a5ac250d241a0
|
||||
@@ -2,10 +2,15 @@ from fastapi import FastAPI, Header
|
||||
from fastapi.responses import StreamingResponse
|
||||
from llamacpp import LlamaItem, stream_chat_llamacpp
|
||||
from typing import Annotated, Union
|
||||
import uuid
|
||||
import os
|
||||
|
||||
app = FastAPI()
|
||||
key_dir = os.path.join(os.getcwd(), "key.txt")
|
||||
if not os.path.exists(key_dir):
|
||||
f = open(key_dir, 'w')
|
||||
f.write(str(uuid.uuid4()))
|
||||
f.close()
|
||||
f = open(key_dir, 'r')
|
||||
key = f.read()
|
||||
f.close()
|
||||
|
||||
4
src-tauri/src-python/requirements.txt
Normal file
4
src-tauri/src-python/requirements.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
pydantic
|
||||
llama-cpp-python
|
||||
uvicorn[standard]
|
||||
fastapi
|
||||
@@ -1,13 +1,9 @@
|
||||
import uvicorn
|
||||
import os
|
||||
import uuid
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
if __name__ == "__main__":
|
||||
key_dir = os.path.join(os.getcwd(), "key.txt")
|
||||
with open(key_dir, "w") as f:
|
||||
f.write(uuid.uuid4().hex)
|
||||
subprocess.check_call([sys.executable, "-m", "pip", "install", "pydantic"])
|
||||
subprocess.check_call([sys.executable, "-m", "pip", "install", "llama-cpp-python"])
|
||||
uvicorn.run("main:app", host="0.0.0.0", port=8912)
|
||||
@@ -279,6 +279,43 @@ fn post_py_install(path:String){
|
||||
std::fs::write(&completed_path, "python311").unwrap();
|
||||
}
|
||||
|
||||
|
||||
#[tauri::command]
|
||||
fn install_py_dependencies(path:String, dependency:String) -> Result<(), String>{
|
||||
println!("installing {}", dependency);
|
||||
let py_path = Path::new(&path).join("python");
|
||||
let py_exec_path = py_path.join("python.exe");
|
||||
let mut py = Command::new(py_exec_path);
|
||||
let output = py.arg("-m").arg("pip").arg("install").arg(dependency).output();
|
||||
match output {
|
||||
Ok(o) => {
|
||||
let res = String::from_utf8(o.stdout).unwrap();
|
||||
println!("{}", res);
|
||||
return Ok(())
|
||||
},
|
||||
Err(e) => {
|
||||
println!("{}", e);
|
||||
return Err(e.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn run_py_server(handle: tauri::AppHandle, py_path:String){
|
||||
let py_exec_path = Path::new(&py_path).join("python").join("python.exe");
|
||||
let server_path = handle.path_resolver().resolve_resource("src-python/run.py").expect("failed to resolve resource");
|
||||
|
||||
let mut py_server = Command::new(&py_exec_path);
|
||||
//set working directory to server path
|
||||
py_server.current_dir(server_path.parent().unwrap());
|
||||
|
||||
println!("server_path: {}", server_path.display());
|
||||
println!("py_exec_path: {}", py_exec_path.display());
|
||||
let mut _child = py_server.arg("-m").arg("uvicorn").arg("--port").arg("10026").arg("main:app").spawn().expect("failed to execute process");
|
||||
println!("server started");
|
||||
return
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn run_server_local(){
|
||||
let app_base_path = tauri::api::path::data_dir().unwrap().join("co.aiclient.risu");
|
||||
@@ -347,7 +384,9 @@ fn main() {
|
||||
run_server_local,
|
||||
install_python,
|
||||
install_pip,
|
||||
post_py_install
|
||||
post_py_install,
|
||||
run_py_server,
|
||||
install_py_dependencies
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
Reference in New Issue
Block a user