feat: Upload files
This commit is contained in:
parent
7a9a9f7de1
commit
e7113e3537
|
@ -1,48 +1,7 @@
|
||||||
env/*
|
env/*
|
||||||
databases/storage.db
|
databases/storage.db
|
||||||
4b751a4425c2884286a92fde2de6427f_trusted.table
|
*.table
|
||||||
4b751a4425c2884286a92fde2de6427f_analitic.table
|
|
||||||
4b751a4425c2884286a92fde2de6427f_analitic_voice.table
|
|
||||||
4b751a4425c2884286a92fde2de6427f_analitic_llm.table
|
|
||||||
4b751a4425c2884286a92fde2de6427f_analitic_ocr.table
|
|
||||||
.vscode/*
|
.vscode/*
|
||||||
__pycache__/*
|
__pycache__/*
|
||||||
|
|
||||||
conf/experiment_config.json
|
conf/experiment_config.json
|
||||||
example/Gmail.zip
|
example/*
|
||||||
example/Gmail/20240530_112812.jpg
|
|
||||||
example/Gmail/20240530_112825.jpg
|
|
||||||
example/Gmail/20240530_112925.jpg
|
|
||||||
example/Gmail/20240530_113110.jpg
|
|
||||||
example/Gmail/20240530_113114.jpg
|
|
||||||
example/Gmail/20240530_113531.jpg
|
|
||||||
example/Gmail/20240530_113555.jpg
|
|
||||||
example/Gmail/20240530_113614.jpg
|
|
||||||
example/Gmail/20240530_113650.jpg
|
|
||||||
example/Gmail/20240530_113731.jpg
|
|
||||||
example/audio/audio-file.flac
|
|
||||||
example/audio/audio-file2.flac
|
|
||||||
example/audio/AwACAgEAAxkBAAIBw2YX8o2vGGCNtZCXk7mY1Bm5w__lAAJmBAACxe7ARI1fUWAGcz_RNAQ.ogg
|
|
||||||
example/audio/AwACAgEAAxkBAAIBxWYX8z-FgaOqQ5wJ4588Oa4wKNbsAAJnBAACxe7AREtzfoE98xYHNAQ.ogg
|
|
||||||
example/audio/AwACAgEAAxkBAAIByGYX-NDzOEo-RH2YjEgG081ujEd-AAIQBAACS5vARIG7Bw8TVGr2NAQ.ogg
|
|
||||||
example/audio/AwACAgEAAxkBAANfZg2OE9Zvvn_7jJl7DaoD0j4kQQwAAqkEAAI-a3FEcjssrLJQjM00BA.ogg
|
|
||||||
example/audio/AwACAgEAAxkBAANhZg2U46T2Q6UYZ9gl3fHJsmJx2XcAAtgEAAI5ImlETOJIxvExjdg0BA.ogg
|
|
||||||
example/audio/AwACAgEAAxkBAANjZg2VEKqY04rTspcLw7trMmKPw58AAtkEAAI5ImlEZ18o0TpHffc0BA.ogg
|
|
||||||
example/audio/AwACAgEAAxkBAANlZg2WB8W9V-L0qkNENZrH1M1FZ5oAAtoEAAI5ImlEq70kYsaMeAs0BA.ogg
|
|
||||||
example/audio/AwACAgEAAxkBAANmZg2WDkyaygSa1QJvcSfYs6mNspcAAtsEAAI5ImlEfDhlNAY1KXs0BA.ogg
|
|
||||||
example/audio/AwACAgEAAxkBAANpZhcNbIqGT8cK3erus_8QoSBqkaMAAiEEAAIpsrhELqtOLaMMSf40BA.ogg
|
|
||||||
example/audio/AwACAgEAAxkBAANtZhcOLk3GkB7sTJ8HYUqjsmwjrNwAAiQEAAIpsrhEGvkyIIq7LKc0BA.ogg
|
|
||||||
example/audio/file_96.oga
|
|
||||||
example/audio/Prueba3.wav
|
|
||||||
example/audio/sin nombre.ogg
|
|
||||||
example/factura/Factura1.jpg
|
|
||||||
example/factura/Factura2.jpg
|
|
||||||
example/factura/Factura3.jpeg
|
|
||||||
example/factura/Factura4.jpg
|
|
||||||
example/factura/Factura5.jpg
|
|
||||||
example/factura/Factura6.jpg
|
|
||||||
example/texto/7fad5a297eb94ff9dbb3eff8e889a1c7dd87fe0f7c6bb6130f268acbfd828736.txt
|
|
||||||
4b751a4425c2884286a92fde2de6427f_analitic_llm_rag.table
|
|
||||||
4b751a4425c2884286a92fde2de6427f_analitic_llm_generaciontexto.table
|
|
||||||
4b751a4425c2884286a92fde2de6427f_analitic_llm_factura.table
|
|
||||||
4b751a4425c2884286a92fde2de6427f_analitic_llm_compra.table
|
|
173
apis.py
173
apis.py
|
@ -16,6 +16,8 @@ import statistics
|
||||||
import hashlib
|
import hashlib
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import json
|
import json
|
||||||
|
import uuid
|
||||||
|
import shutil
|
||||||
pwd = os.getcwd()
|
pwd = os.getcwd()
|
||||||
pathAud="example/audio"
|
pathAud="example/audio"
|
||||||
pathFact="example/factura"
|
pathFact="example/factura"
|
||||||
|
@ -84,6 +86,177 @@ class Response5(BaseModel):
|
||||||
|
|
||||||
|
|
||||||
#Funcionales
|
#Funcionales
|
||||||
|
|
||||||
|
@app.post("/uploadimg")
|
||||||
|
def upload_image(image: UploadFile = File(...),type="factura"):
|
||||||
|
endfile=".jpg"
|
||||||
|
t=time.time()
|
||||||
|
print(image.headers)
|
||||||
|
try:
|
||||||
|
# Create a temporary file to store the uploaded audio
|
||||||
|
headfilename=uuid.uuid4()
|
||||||
|
filename="example/%s/"%(type)+str(headfilename)+endfile
|
||||||
|
with open(f"{filename}", "wb") as buffer:
|
||||||
|
shutil.copyfileobj(image.file, buffer)
|
||||||
|
return JSONResponse(content={
|
||||||
|
"message": filename,
|
||||||
|
"time":time.time()-t
|
||||||
|
}, status_code=200)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return JSONResponse(content={
|
||||||
|
"message": f"There was an error uploading the file: {str(e)}"
|
||||||
|
}, status_code=500)
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/uploadaud")
|
||||||
|
def upload_audio(audio: UploadFile = File(...)):
|
||||||
|
if audio.headers['content-type']=="audio/wav":
|
||||||
|
endfile=".wav"
|
||||||
|
if audio.headers['content-type']=="audio/ogg":
|
||||||
|
endfile=".ogg"
|
||||||
|
if audio.headers['content-type']=="video/webm":
|
||||||
|
endfile=".webm"
|
||||||
|
type="audio"
|
||||||
|
try:
|
||||||
|
# Create a temporary file to store the uploaded audio
|
||||||
|
headfilename=uuid.uuid4()
|
||||||
|
filename="example/%s/"%(type)+str(headfilename)+endfile
|
||||||
|
|
||||||
|
with open(f"{filename}", "wb") as buffer:
|
||||||
|
shutil.copyfileobj(audio.file, buffer)
|
||||||
|
|
||||||
|
# Here you can process the audio file as needed
|
||||||
|
# For example, you might want to move it to a permanent location
|
||||||
|
# or perform some operations on it
|
||||||
|
#payload={"password":GetText2Voice.extractConfig(nameModel="SystemData",relPath=os.path.join(pwd,"conf/experiment_config.json"),dataOut="password"),"local":filename}
|
||||||
|
#A=requests.get("http://127.0.0.1:7870/voice2txtlocal", json=payload)
|
||||||
|
t=time.time()
|
||||||
|
return JSONResponse(content={
|
||||||
|
"message": filename,
|
||||||
|
"time":time.time()-t
|
||||||
|
}, status_code=200)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return JSONResponse(content={
|
||||||
|
"message": f"There was an error uploading the file: {str(e)}"
|
||||||
|
}, status_code=500)
|
||||||
|
|
||||||
|
finally:
|
||||||
|
audio.file.close()
|
||||||
|
|
||||||
|
@app.get("/addaudiohtml")
|
||||||
|
def addaudiohtml():
|
||||||
|
html="""<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Audio Upload</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Upload Audio File</h1>
|
||||||
|
<form id="audio-upload-form" enctype="multipart/form-data">
|
||||||
|
<input type="file" name="audio" id="audio-file" accept="audio/*" required>
|
||||||
|
<br>
|
||||||
|
<button type="submit">Upload</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const form = document.getElementById('audio-upload-form');
|
||||||
|
form.addEventListener('submit', async (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
const fileInput = document.getElementById('audio-file');
|
||||||
|
const audioFile = fileInput.files[0];
|
||||||
|
|
||||||
|
if (!audioFile) {
|
||||||
|
alert('Please select an audio file to upload.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('audio', audioFile);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('uploadaud', {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
const result = await response.json();
|
||||||
|
alert(`File uploaded successfully: ${result.filename}`);
|
||||||
|
} else {
|
||||||
|
const error = await response.json();
|
||||||
|
alert(`Error uploading file: ${error.message}`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
alert(`Error uploading file: ${error.message}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
|
"""
|
||||||
|
return HTMLResponse(content=html, status_code=200)
|
||||||
|
|
||||||
|
@app.get("/addimagehtml")
|
||||||
|
def addimagehtml():
|
||||||
|
html="""<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>image Upload</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Upload image File</h1>
|
||||||
|
<form id="image-upload-form" enctype="multipart/form-data">
|
||||||
|
<input type="file" name="image" id="image-file" accept="image/*" required>
|
||||||
|
<button type="submit">Upload</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const form = document.getElementById('image-upload-form');
|
||||||
|
form.addEventListener('submit', async (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
const fileInput = document.getElementById('image-file');
|
||||||
|
const imageFile = fileInput.files[0];
|
||||||
|
|
||||||
|
if (!imageFile) {
|
||||||
|
alert('Please select an image file to upload.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('image', imageFile);
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch('uploadimg', {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
const result = await response.json();
|
||||||
|
alert(`File uploaded successfully: ${result.filename}`);
|
||||||
|
} else {
|
||||||
|
const error = await response.json();
|
||||||
|
alert(`Error uploading file: ${error.message}`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
alert(`Error uploading file: ${error.message}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
||||||
|
"""
|
||||||
|
return HTMLResponse(content=html, status_code=200)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/addTrusted")
|
@app.get("/addTrusted")
|
||||||
@app.post("/addTrusted")
|
@app.post("/addTrusted")
|
||||||
def addTrusted(response:Response3):
|
def addTrusted(response:Response3):
|
||||||
|
|
Loading…
Reference in New Issue