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

Commit atualizado com o GET das imagens do projeto #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 92 additions & 1 deletion backend/api.py
Original file line number Diff line number Diff line change
@@ -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")
Binary file added models/__pycache__/generate_images.cpython-312.pyc
Binary file not shown.
Binary file not shown.