Spaces:
Running
Running
File size: 1,594 Bytes
2c50826 34046e2 2c50826 34046e2 2c50826 34046e2 2c50826 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import os
import time
from pathlib import Path
from typing import Any
import requests
from dotenv import load_dotenv
from api.flux import FluxAPI
class FireworksAPI(FluxAPI):
def __init__(self):
load_dotenv()
self._api_key = os.getenv("FIREWORKS_API_TOKEN")
if not self._api_key:
raise ValueError("FIREWORKS_API_TOKEN not found in environment variables")
self._url = "https://api.fireworks.ai/inference/v1/workflows/accounts/fireworks/models/flux-1-dev-fp8/text_to_image"
@property
def name(self) -> str:
return "fireworks_fp8"
def generate_image(self, prompt: str, save_path: Path) -> float:
start_time = time.time()
headers = {
"Content-Type": "application/json",
"Accept": "image/jpeg",
"Authorization": f"Bearer {self._api_key}",
}
data = {
"prompt": prompt,
"aspect_ratio": "1:1",
"guidance_scale": 3.5,
"num_inference_steps": 28,
"seed": 0,
}
result = requests.post(self._url, headers=headers, json=data)
end_time = time.time()
if result.status_code == 200:
self._save_image_from_result(result, save_path)
else:
raise Exception(f"Error: {result.status_code} {result.text}")
return end_time - start_time
def _save_image_from_result(self, result: Any, save_path: Path):
save_path.parent.mkdir(parents=True, exist_ok=True)
with open(save_path, "wb") as f:
f.write(result.content)
|