File size: 2,988 Bytes
cd35cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd40a43
 
 
cd35cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd40a43
 
 
cd35cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd40a43
 
 
cd35cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
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