From 7fc02537726996bcb6fdca8003b5713a0f0241eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Victor?= Date: Mon, 15 Apr 2024 01:06:40 -0300 Subject: [PATCH 1/2] =?UTF-8?q?Sugest=C3=A3o=20do=20GPT=20para=20a=20API?= =?UTF-8?q?=20+=20app.get=20e=20app.post?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__pycache__/generate_images.cpython-312.pyc | Bin 0 -> 766 bytes .../__pycache__/db_utils.cpython-312.pyc | Bin 0 -> 4082 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 models/__pycache__/generate_images.cpython-312.pyc create mode 100644 models/database/__pycache__/db_utils.cpython-312.pyc diff --git a/models/__pycache__/generate_images.cpython-312.pyc b/models/__pycache__/generate_images.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..515604ed245fb6951fd04b4cd53ac4c6e67c3b7c GIT binary patch literal 766 zcmYLHy^9k;6n`_j$t9;zE*6S05R?`6;8L7&MF@w27!|xm6kHaDO=fcJv76PI4U)sc zBE?x)SsY?van+GO!_r1k5ccjWD?2fWppEnPBR<&oe)GQPxAUP^TLvwEUtStsCIH{? zP>7j>`3(*BfdeN2IB01=Lh4YG`3%R1r*hhmVWW?FBO%Dk@4J_v9K2ZwSp1;?0B+eoTCF%r^?BRMFNvBJo| zxl9Y=MQkTu1(OPyY^XW_j&%VAwU4xLTm1R!0}r6R_#l?uzwz%?j5n-m>m= ztot47fnAnJWnzq?XGulHa)=jBPI-fP>;()NkT@ctyss%Sl?a9kr?-A>Xg%n-50#M0 zy%)Vdh+2>QfxfltL{WJ2t{X;N1j^mc*bvMQy=|e|PbNu)F@NO8jP2GJT(t||lJ2W4 ztzN)!=B4KV literal 0 HcmV?d00001 diff --git a/models/database/__pycache__/db_utils.cpython-312.pyc b/models/database/__pycache__/db_utils.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..eb4d08f4cd9c7250e0c9437a20e48fd2b5a23795 GIT binary patch literal 4082 zcmd5ZF=ejiUu?GU{Wu=8L(;6Uiy7+{w)Zkq?bXKk?|XlK zzwgif91d3#c>bQ=Hj)zv`3*muCtx?0JE2h~GLgAFNn39&&G|?#k$rcF?0@XDMy7oe zTrBWAe#Enu%96bOvHU$W%0wY)P9|xe;>R^tKe?LK8ba0?8D3*#@G)m;WzOA~pa>%z z?3GK0Wd9wKu9gEJYh)f|ty~2%EC)eGWC3J!A{47Og_9X0b19=MrG`?mIGRx<*;ee4 zFJ3T5_4Yrx#||J8kdnjMVF)ztPh>EvKI&6Tlsm zkGMEou&=0_fw7En#jH|ps=A?@{OHv}Q8t76wY+L5`^{jsSSTo2!wg`FGMd%#_9^}25WKR7hlg}U(}&L#fBddULX zs12t9*Y3XI(W0#6^&=eqlc z0IAO7T@?R3lpX~bCfZim1;ayYfmSTQP%r~%(Bv6y&1#RaI-UW^$q+IlY5{_oA=&&W zZFRv~+3`Ni(hq`I3$a&`hP&du!*k)?Z+Ty+)56Y$=(*7PzeOg!E;5dTdX8heoc*$# zZ3B3;Z53!#duB|YnKo{F|t=O`8;SbYH2$`m{sg*bsaClb&=}J ziXmAYrA8MEPx%oH);|HUwrjLZeT5)w0}vXT%Wbo5i}kza>vt{GcT82iif)+-Th6g& zYHk4S=CF9|rp9#=`bIc#>&#iJ2w0iU;qwy^I63PpUoE`RkebuqQo$Rk_06;6xDUEYucI~ z9Ep{Nbt90EW}?&4=fZnzxA9mVR^&(-NiwS?2}<|zJhp2k>DqWEZ}n)%Lk)jgnt@zV zEto-B)mUnU$s;P85SHyk>i9CjJ9wbtt$1 literal 0 HcmV?d00001 From 4e61283f9b1c724b725a701eae708abd5971610f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Victor?= Date: Wed, 15 May 2024 15:32:32 -0300 Subject: [PATCH 2/2] Commit atualizado com o GET das imagens do projeto --- backend/api.py | 93 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/backend/api.py b/backend/api.py index 0af61e8..f9afd07 100644 --- a/backend/api.py +++ b/backend/api.py @@ -1,8 +1,99 @@ +from fastapi import FastAPI, BackgroundTasks +from fastapi.responses import StreamingResponse import sys import os +from datetime import datetime +from io import BytesIO +import zipfile -parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) + +parent_dir = os.path.abspath(os.path.join(os.path.dirname(_file_), os.pardir)) sys.path.append(parent_dir) sys.path.append(os.path.join(parent_dir, 'models')) +from status_return import * from models.generate_images import Generator + +#adding cors headers +from fastapi.middleware.cors import CORSMiddleware +from fastapi import FastAPI + +app = FastAPI() +g = Generator() + +database = g.get_database() + +# adding cors urls +origins = [ + 'http://localhost:3000' + +] + +# add middleware +app.add_middleware( + CORSMiddleware, + allow_origins= origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"] +) + + +@app.get('/create/{prompt}') +async def create_infinite_zoom(prompt: str, background_tasks: BackgroundTasks): + + if g.is_running(): + return RUNNING + + try: + prompt_gpt = await g.gpt_prompt_create(prompt) + now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + project_id = database.insert_project(prompt, None, now, prompt_gpt) + background_tasks.add_task(g.generate_images, prompt_gpt, project_id) + + return STARTED + + except Exception as e: + print(e) + return ERROR + +#! using this rout to save the images in a path on aria2 +@app.get('/savepath/{project_id}') +async def save_image(project_id: int): + if not g.is_running(): + g.read_image_from_db(project_id) + return 200 + else: + return 400 + +# Rota para buscar todas as imagens de um projeto com o parâmetro id_do_projeto +@app.get('/get_images/{id_project}') +async def get_images(id_project: int): + + # Busca objetos de imagem diretamente do banco de dados + images = database.get_images_from_project(id_project) + if not images: + raise HTTPException(status_code=404, detail="No images found for this project") + + # Object BytesIO -> ZIP em memória + memory_zip = BytesIO() + + # Cria um novo arquivo ZIP no objeto BytesIO + with zipfile.ZipFile(memory_zip, 'w', zipfile.ZIP_DEFLATED) as zf: + for image_record in images: + # imagens estão em bytes. + img = Image.open(BytesIO(image_record.image_data)) + + # Converte para PNG e salva em um BytesIO temporário + img_bytes = BytesIO() + img.save(img_bytes, format='PNG') + img_bytes.seek(0) + + # Adiciona ao ZIP + zf.writestr(f"{image_record.image_name}.png", img_bytes.getvalue()) + + # Apontar o ponteiro para o início do arquivo para leitura + memory_zip.seek(0) + + # Retorna o arquivo ZIP como uma resposta de streaming + return StreamingResponse(memory_zip, media_type="application/zip") \ No newline at end of file