|
| 1 | +import base64 |
| 2 | +import json |
| 3 | +from io import BytesIO |
| 4 | +from typing import Dict, Literal, Optional, Any, List |
| 5 | +import mimetypes |
| 6 | + |
| 7 | +import requests |
| 8 | + |
| 9 | +from edenai_apis.features import ProviderInterface, ImageInterface, VideoInterface |
| 10 | +from edenai_apis.features.image.generation import ( |
| 11 | + GenerationDataClass, |
| 12 | + GeneratedImageDataClass, |
| 13 | +) |
| 14 | +from edenai_apis.features.video.generation_async import GenerationAsyncDataClass |
| 15 | +from edenai_apis.loaders.data_loader import ProviderDataEnum |
| 16 | +from edenai_apis.loaders.loaders import load_provider |
| 17 | +from edenai_apis.utils.exception import ProviderException |
| 18 | +from edenai_apis.utils.types import ( |
| 19 | + AsyncLaunchJobResponseType, |
| 20 | + ResponseType, |
| 21 | + AsyncPendingResponseType, |
| 22 | + AsyncResponseType, |
| 23 | +) |
| 24 | +from edenai_apis.utils.upload_s3 import USER_PROCESS, upload_file_bytes_to_s3 |
| 25 | +from edenai_apis.llmengine.utils.moderation import moderate |
| 26 | + |
| 27 | + |
| 28 | +class BytedanceApi(ProviderInterface, ImageInterface, VideoInterface): |
| 29 | + provider_name = "bytedance" |
| 30 | + |
| 31 | + def __init__(self, api_keys: Optional[Dict[str, Any]] = None): |
| 32 | + self.api_settings = load_provider( |
| 33 | + ProviderDataEnum.KEY, self.provider_name, api_keys=api_keys or {} |
| 34 | + ) |
| 35 | + self.api_key = self.api_settings["api_key"] |
| 36 | + self.headers = { |
| 37 | + "Authorization": f"Bearer {self.api_key}", |
| 38 | + "Content-Type": "application/json", |
| 39 | + "Accept": "application/json", |
| 40 | + } |
| 41 | + |
| 42 | + @moderate |
| 43 | + def image__generation( |
| 44 | + self, |
| 45 | + text: str, |
| 46 | + resolution: Literal["256x256", "512x512", "1024x1024"], |
| 47 | + num_images: int = 1, |
| 48 | + model: Optional[str] = None, |
| 49 | + **kwargs, |
| 50 | + ) -> ResponseType[GenerationDataClass]: |
| 51 | + url = "https://ark.ap-southeast.bytepluses.com/api/v3/images/generations" |
| 52 | + payload = { |
| 53 | + "model": model, |
| 54 | + "prompt": text, |
| 55 | + "size": resolution, |
| 56 | + "response_format": "b64_json", |
| 57 | + } |
| 58 | + |
| 59 | + try: |
| 60 | + response = requests.post(url, headers=self.headers, json=payload) |
| 61 | + original_response = response.json() |
| 62 | + except json.JSONDecodeError as exc: |
| 63 | + raise ProviderException("Internal Server Error", code=500) from exc |
| 64 | + |
| 65 | + # Handle error |
| 66 | + if response.status_code != 200: |
| 67 | + raise ProviderException( |
| 68 | + message=original_response.get("error", {}).get("message"), |
| 69 | + code=response.status_code, |
| 70 | + ) |
| 71 | + |
| 72 | + generations: List[GeneratedImageDataClass] = [] |
| 73 | + for generated_image in original_response.get("data"): |
| 74 | + image_b64 = generated_image.get("b64_json") |
| 75 | + |
| 76 | + image_data = image_b64.encode() |
| 77 | + image_content = BytesIO(base64.b64decode(image_data)) |
| 78 | + resource_url = upload_file_bytes_to_s3(image_content, ".png", USER_PROCESS) |
| 79 | + generations.append( |
| 80 | + GeneratedImageDataClass( |
| 81 | + image=image_b64, image_resource_url=resource_url |
| 82 | + ) |
| 83 | + ) |
| 84 | + |
| 85 | + return ResponseType[GenerationDataClass]( |
| 86 | + original_response=original_response, |
| 87 | + standardized_response=GenerationDataClass(items=generations), |
| 88 | + ) |
| 89 | + |
| 90 | + def video__generation_async__launch_job( |
| 91 | + self, |
| 92 | + text: str, |
| 93 | + duration: Optional[int] = 6, |
| 94 | + fps: Optional[int] = 24, |
| 95 | + dimension: Optional[str] = "1280x720", |
| 96 | + seed: Optional[float] = 12, |
| 97 | + file: Optional[str] = None, |
| 98 | + file_url: Optional[str] = None, |
| 99 | + model: Optional[str] = None, |
| 100 | + **kwargs, |
| 101 | + ) -> AsyncLaunchJobResponseType: |
| 102 | + url = ( |
| 103 | + "https://ark.ap-southeast.bytepluses.com/api/v3/contents/generations/tasks" |
| 104 | + ) |
| 105 | + content = [ |
| 106 | + {"type": "text", "text": text}, |
| 107 | + ] |
| 108 | + # if file: |
| 109 | + # with open(file, "rb") as fstream: |
| 110 | + # file_content = fstream.read() |
| 111 | + # file_b64 = base64.b64encode(file_content).decode("utf-8") |
| 112 | + # mime_type = mimetypes.guess_type(file)[0] |
| 113 | + # image_data = f"data:{mime_type};base64,{file_b64}" |
| 114 | + # content.append({"type": "image_url", "image_url": {"url": image_data}}) |
| 115 | + payload = { |
| 116 | + "model": model, |
| 117 | + "content": content, |
| 118 | + } |
| 119 | + try: |
| 120 | + response = requests.post(url, headers=self.headers, json=payload) |
| 121 | + original_response = response.json() |
| 122 | + except json.JSONDecodeError as exc: |
| 123 | + raise ProviderException("Internal Server Error", code=500) from exc |
| 124 | + |
| 125 | + # Handle error |
| 126 | + if response.status_code != 200: |
| 127 | + raise ProviderException( |
| 128 | + message=original_response.get("error", {}).get("message"), |
| 129 | + code=response.status_code, |
| 130 | + ) |
| 131 | + provider_job_id = original_response.get("id") |
| 132 | + return AsyncLaunchJobResponseType(provider_job_id=provider_job_id) |
| 133 | + |
| 134 | + def video__generation_async__get_job_result( |
| 135 | + self, provider_job_id: str |
| 136 | + ) -> GenerationAsyncDataClass: |
| 137 | + url = f"https://ark.ap-southeast.volces.com/api/v3/contents/generations/tasks/{provider_job_id}" |
| 138 | + try: |
| 139 | + response = requests.get(url, headers=self.headers) |
| 140 | + original_response = response.json() |
| 141 | + except json.JSONDecodeError as exc: |
| 142 | + raise ProviderException("Internal Server Error", code=500) from exc |
| 143 | + |
| 144 | + if response.status_code != 200: |
| 145 | + raise ProviderException( |
| 146 | + message=original_response["error"]["message"], code=response.status_code |
| 147 | + ) |
| 148 | + if original_response["status"] == "cancelled": |
| 149 | + failure_message = original_response["error"] |
| 150 | + raise ProviderException(failure_message) |
| 151 | + if original_response["status"] != "succeeded": |
| 152 | + return AsyncPendingResponseType(provider_job_id=provider_job_id) |
| 153 | + video_uri = original_response["content"]["video_url"] |
| 154 | + return AsyncResponseType( |
| 155 | + original_response=provider_job_id, |
| 156 | + standardized_response=GenerationAsyncDataClass( |
| 157 | + video="", video_resource_url=video_uri |
| 158 | + ), |
| 159 | + provider_job_id=provider_job_id, |
| 160 | + ) |
0 commit comments