-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from 2Swon/main
add generate img
- Loading branch information
Showing
3 changed files
with
153 additions
and
28 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import uuid | ||
|
||
import aiofiles | ||
import httpx | ||
import asyncio | ||
|
||
from azure.storage.blob import ContentSettings | ||
from azure.storage.blob.aio import BlobServiceClient | ||
from typing import List | ||
import os | ||
|
||
from fastapi import HTTPException | ||
|
||
MID_API_KEY = os.getenv('MID_API_KEY') | ||
container_name = "greefile" | ||
AZURE_ACCOUNT_KEY = os.getenv("AZURE_ACCOUNT_KEY") | ||
connection_string = f'DefaultEndpointsProtocol=https;AccountName=greedotstorage;AccountKey={AZURE_ACCOUNT_KEY};EndpointSuffix=core.windows.net' | ||
|
||
promptDict = { | ||
1: """ | ||
please make cute | ||
This picture was painted by a child. | ||
Please change this picture a little bit more cute. | ||
I want to keep most of the original. | ||
Arms and legs must be in the form of characters. | ||
Please don't put your face and body in. | ||
I just wish I had one character. | ||
And the parts other than the character outline must be a white background, so please make this important. | ||
""", | ||
2: """ | ||
please make cool | ||
This picture was painted by a child. | ||
Please change this picture a little bit more cute. | ||
I want to keep most of the original. | ||
Arms and legs must be in the form of characters. | ||
Please don't put your face and body in. | ||
I just wish I had one character. | ||
And the parts other than the character outline must be a white background, so please make this important. | ||
""" | ||
} | ||
|
||
async def create_image(promptSelect: int, raw_img_url: str) -> dict: | ||
prompt = promptDict[promptSelect] + "\n" + raw_img_url | ||
headers = { | ||
'Authorization': f'Bearer {MID_API_KEY}', | ||
'Content-Type': 'application/json' | ||
} | ||
data = {"prompt": prompt} | ||
async with httpx.AsyncClient() as client: | ||
response = await client.post('https://cl.imagineapi.dev/items/images/', json=data, headers=headers) | ||
return response.json() | ||
|
||
async def check_image_status(image_id: str) -> list: | ||
headers = {'Authorization': f'Bearer {MID_API_KEY}'} | ||
async with httpx.AsyncClient() as client: | ||
while True: | ||
response = await client.get(f'https://cl.imagineapi.dev/items/images/{image_id}', headers=headers) | ||
data = response.json() | ||
if data['data']['status'] == 'completed': | ||
if 'upscaled_urls' in data['data']: | ||
return data['data']['upscaled_urls'] | ||
else: | ||
# Handle case where 'upscaled_urls' is missing even though status is 'completed' | ||
raise HTTPException(status_code=500, detail="Image creation completed but did not return URLs.") | ||
elif data['data']['status'] == 'failed': | ||
# Handle failed image creation | ||
raise HTTPException(status_code=500, detail="Image creation failed.") | ||
await asyncio.sleep(5) # Polling interval | ||
|
||
|
||
async def upload_images_to_azure(image_paths: List[str]) -> List[str]: | ||
blob_service_client = BlobServiceClient.from_connection_string(connection_string) | ||
uploaded_urls = [] | ||
async with blob_service_client: | ||
for image_path in image_paths: | ||
filename = os.path.basename(image_path) or str(uuid.uuid4()) | ||
blob_name = f"upload/{filename}.png" | ||
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name) | ||
|
||
async with aiofiles.open(image_path, 'rb') as file: | ||
data = await file.read() | ||
|
||
await blob_client.upload_blob(data, overwrite=True, content_settings=ContentSettings(content_type='image/png')) | ||
uploaded_urls.append(blob_client.url) | ||
return uploaded_urls |