File size: 9,064 Bytes
1778e91
a352c42
1778e91
 
bd07fc2
1778e91
1a75585
8b6570a
1778e91
 
 
a352c42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1778e91
 
 
 
 
3404e9a
94bdc12
 
 
8b6570a
7e5d5c2
94bdc12
 
3404e9a
 
 
 
 
 
 
 
 
1778e91
 
3404e9a
 
1778e91
3404e9a
8b6570a
 
1778e91
 
3404e9a
1778e91
 
 
 
 
 
 
 
 
 
bd07fc2
1778e91
 
0b6d459
1778e91
 
bd07fc2
 
 
 
 
 
0b6d459
1778e91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a352c42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1778e91
f71cd77
 
 
a352c42
 
f71cd77
 
 
bd07fc2
 
 
7e5d5c2
bd07fc2
8b6570a
 
 
 
 
bd07fc2
7e5d5c2
8b6570a
1778e91
 
 
 
 
 
 
e9f6da1
3404e9a
e9f6da1
 
 
 
 
8660783
8e0a4a4
e9f6da1
 
 
8660783
0b6d459
8e0a4a4
0b6d459
 
8660783
e9f6da1
 
 
bd07fc2
2ca982d
 
1778e91
 
 
 
 
 
 
 
2ca982d
1778e91
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
from functools import lru_cache
from typing import List, Tuple, Optional, Dict
import aiohttp
import elevenlabs
import time
from contextlib import asynccontextmanager
from logger import setup_logger, log_execution_time, log_async_execution_time
from models import OpenRouterModel

logger = setup_logger("api_clients")

def preprocess_text(text: str) -> str:
    """
    Clean and format text by removing unwanted characters and formatting
    
    Args:
        text: Raw input text
        
    Returns:
        Cleaned text suitable for podcast generation
    """
    import re
    
    # Remove markdown-style formatting
    text = re.sub(r'\*\*(.+?)\*\*', r'\1', text)  # Bold
    text = re.sub(r'\*(.+?)\*', r'\1', text)      # Italic
    text = re.sub(r'__(.+?)__', r'\1', text)      # Underline
    text = re.sub(r'~~(.+?)~~', r'\1', text)      # Strikethrough
    
    # Remove action blocks and special formatting
    text = re.sub(r'\[.*?\]', '', text)           # Remove [actions]
    text = re.sub(r'\{.*?\}', '', text)           # Remove {actions}
    text = re.sub(r'<.*?>', '', text)             # Remove <actions>

    # Remove podcast-specific formatting
    text = re.sub(r'\((?:pause|break|music|sfx|sound effect|jingle).*?\)', '', text, flags=re.IGNORECASE)
    text = re.sub(r'\((host|speaker|guest)\s*\d*\s*:?\)', '', text, flags=re.IGNORECASE)
    text = re.sub(r'#\s*\d+\s*[:.-]', '', text)   # Remove segment numbers
    
    # Clean up whitespace
    return ' '.join(text.split())

class OpenRouterClient:
    """Handles OpenRouter API interactions with comprehensive logging and error tracking"""
    
    def __init__(self, api_key: str):
        logger.info("Initializing OpenRouter client")
        self.api_key = api_key
        self.base_url = "https://openrouter.ai/api/v1"
        self.headers = {
            "Authorization": f"Bearer {api_key}",
            "HTTP-Referer": "https://localhost:7860",  # Required by OpenRouter
            "X-Title": "URL to Podcast Generator",  # Required by OpenRouter
            "Content-Type": "application/json"
        }
        logger.debug("OpenRouter client initialized successfully")
    
    @property
    def api_key(self):
        return self._api_key

    @api_key.setter
    def api_key(self, value: str):
        if not value or len(value) < 32:
            logger.error("Invalid API key format")
            raise ValueError("Invalid OpenRouter API key")
        self._api_key = value
        # Update headers when API key changes
        self.headers = {
            "Authorization": f"Bearer {value}",
            "HTTP-Referer": "https://localhost:7860",
            "X-Title": "URL to Podcast Generator",
            "Content-Type": "application/json",
        }
        logger.info("OpenRouter API key updated successfully")
    
    @asynccontextmanager
    async def get_session(self):
        logger.debug("Creating new aiohttp session")
        async with aiohttp.ClientSession(headers=self.headers) as session:
            yield session
    
    @lru_cache(maxsize=1)
    async def get_models(self) -> List[Tuple[str, str]]:
        """
        Fetch available models from OpenRouter API using pydantic models
        
        Returns:
            List of tuples containing (model_id, model_id) where both values are the same
        """
        logger.info("Fetching available models from OpenRouter")
        async with self.get_session() as session:
            async with session.get(f"{self.base_url}/models") as response:
                response.raise_for_status()
                data = await response.json()
                models = [OpenRouterModel(**model) for model in data["data"]]
                logger.info(f"Successfully fetched {len(models)} models")
                return [(model.name, model.id) for model in models]

    @log_async_execution_time(logger)
    async def generate_script(self, content: str, prompt: str, model_id: str) -> str:
        """
        Generate a podcast script with detailed progress tracking and validation
        
        Performance metrics and content analysis are logged at each step.
        """
        logger.info(f"Starting script generation with model: {model_id}")
        logger.debug(f"Input metrics - Content: {len(content)} chars, Prompt: {len(prompt)} chars")
        
        # Validate inputs
        if not content or len(content) < 100:
            logger.error("Content too short for meaningful script generation")
            raise ValueError("Insufficient content for script generation")
            
        if not prompt or len(prompt) < 10:
            logger.error("Prompt too short or missing")
            raise ValueError("Please provide a more detailed prompt")
        
        # Clean input text
        cleaned_content = preprocess_text(content)
        cleaned_prompt = preprocess_text(prompt)
        
        system_prompt = """You are an expert podcast script writer. Your task is to create engaging, 
natural-sounding podcast scripts that flow conversationally while being informative and engaging.

Follow these guidelines:
1. Write in a conversational, natural speaking style that sounds authentic
2. Break complex topics into digestible segments with clear transitions
3. Avoid technical jargon unless necessary, explaining complex terms when used
4. Use natural speech patterns:
   - Contractions (I'm, we're, let's)
   - Casual language
   - Rhetorical questions to engage listeners
5. Include brief pauses for emphasis and pacing (but don't mark them explicitly)
6. Incorporate storytelling elements to maintain engagement
7. End with a clear conclusion and call-to-action
8. Keep paragraphs short and focused for easier delivery
9. Use simple sentence structures that flow naturally when spoken

Format the script for natural speech, avoiding any special characters or formatting."""

        user_prompt = f"""Create a podcast script based on the following topic and content:

Topic: {cleaned_prompt}

Content to cover: {cleaned_content}

Focus on making it engaging and natural to listen to."""

        try:
            request = OpenRouterRequest(
                model=model_id,
                messages=[
                    Message(role="system", content=system_prompt),
                    Message(role="user", content=user_prompt)
                ]
            )
            
            async with self.get_session() as session:
                async with session.post(
                    f"{self.base_url}/chat/completions",
                    json=request_data
                ) as response:
                    if response.status != 200:
                        error_text = await response.text()
                        logger.error(f"OpenRouter API error: {error_text}")
                        raise ValueError(f"API request failed: {error_text}")
                    
                    data = await response.json()
                    return data['choices'][0]['message']['content']
                    
        except Exception as e:
            logger.error(f"Script generation failed", exc_info=True)
            raise

class ElevenLabsClient:
    def __init__(self, api_key: str):
        self.api_key = api_key
        elevenlabs.set_api_key(api_key)

    def get_voices(self) -> List[Tuple[str, str]]:
        """
        Synchronously get available voices from ElevenLabs
        
        Returns:
            List of tuples containing (voice_id, display_name)
            where display_name shows the name and description but not the ID
        """
        try:
            voices = elevenlabs.voices()
            return [(
                
                f"{voice.name} ({voice.labels.get('accent', 'No accent')})" + 
                (f" - {voice.description[:50]}..." if voice.description else ""),
                voice.voice_id  # Value (hidden from user)
            ) for voice in voices]
        except Exception as e:
            logger.error("Failed to fetch voices from ElevenLabs", exc_info=True)
            raise

    def generate_audio(self, text: str, voice_id: str):
        """Generate audio synchronously"""
        logger.info(f"Starting audio generation with voice: {voice_id}")
        logger.debug(f"Input text length: {len(text)} chars")
        
        if len(text) > 5000:
            logger.warning(f"Long text detected ({len(text)} chars), may impact performance")
        
        try:
            start_time = time.time()
            audio = elevenlabs.generate(
                text=text,
                voice=voice_id,
                model="eleven_monolingual_v1"
            )
            
            duration = time.time() - start_time
            audio_size = len(audio)
            logger.info(f"Audio generated: {audio_size} bytes in {duration:.2f} seconds")
            logger.debug(f"Audio generation rate: {len(text)/duration:.2f} chars/second")
            
            return audio
        except Exception as e:
            logger.error("Audio generation failed", exc_info=True)
            raise