pipV1 / services /openai_client.py
Itsjustamit's picture
some updates
cd40a43 verified
"""
OpenAI client for Pip.
Handles: GPT-4o image generation, Whisper speech-to-text.
"""
import os
import base64
from typing import Optional
from openai import AsyncOpenAI
import httpx
class OpenAIClient:
"""OpenAI-powered image generation and speech recognition for Pip."""
def __init__(self):
self.client = AsyncOpenAI(
api_key=os.getenv("OPENAI_API_KEY")
)
async def generate_image(self, prompt: str, style: str = "vivid") -> Optional[str]:
"""
Generate an image using GPT-4o / DALL-E 3.
Returns base64 encoded image or URL.
"""
if not self.available or not self.client:
return None
try:
response = await self.client.images.generate(
model="dall-e-3",
prompt=prompt,
size="1024x1024",
quality="standard",
style=style, # "vivid" or "natural"
n=1,
response_format="url"
)
return response.data[0].url
except Exception as e:
print(f"OpenAI image generation error: {e}")
return None
async def transcribe_audio(self, audio_file_path: str) -> str:
"""
Transcribe audio using Whisper.
"""
if not self.available or not self.client:
return ""
try:
with open(audio_file_path, "rb") as audio_file:
response = await self.client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
response_format="text"
)
return response
except Exception as e:
print(f"Whisper transcription error: {e}")
return ""
async def transcribe_audio_bytes(self, audio_bytes: bytes, filename: str = "audio.wav") -> str:
"""
Transcribe audio from bytes using Whisper.
"""
if not self.available or not self.client:
return ""
try:
# Create a file-like object from bytes
response = await self.client.audio.transcriptions.create(
model="whisper-1",
file=(filename, audio_bytes),
response_format="text"
)
return response
except Exception as e:
print(f"Whisper transcription error: {e}")
return ""
async def download_image_as_base64(self, url: str) -> Optional[str]:
"""
Download an image from URL and convert to base64.
"""
try:
async with httpx.AsyncClient() as client:
response = await client.get(url)
if response.status_code == 200:
return base64.b64encode(response.content).decode('utf-8')
except Exception as e:
print(f"Image download error: {e}")
return None