marks commited on
Commit ·
1778e91
1
Parent(s): 485799f
Added code files
Browse filesFiles changed (9)
- .dockerignoreAdded+53
- .gitignoreAdded+1
- DockerfileAdded+62
- __init__.pyAdded
- api_clients.pyAdded+171
- interface.pyAdded+256
- logger.pyAdded+72
- requirements.txtAdded+38
- setup.pyAdded+11
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253# Python virtual environment.venv/venv/ENV/
# Git.git.gitignore
# Environment files.env*.env
# Python cache__pycache__/*.py[cod]*$py.class.Python*.so
# Development/IDE.vscode/.idea/*.swp*.swo
# Documentationdocs/*.md
# Logs and databases*.log*.sqlite*.db
# OS generated files.DS_Store.DS_Store?._*.Spotlight-V100.Trashesehthumbs.dbThumbs.db
# Test cache.pytest_cache/.coveragehtmlcov/
# Distribution/packagingdist/build/*.egg-info/1/.env1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162FROM python:3.11-slim
# Install system dependenciesRUN apt-get update && apt-get install -y \ wget \ gnupg \ curl \ unzip \ xvfb \ libgconf-2-4 \ libxss1 \ libnss3 \ libnspr4 \ libasound2 \ libatk1.0-0 \ libatk-bridge2.0-0 \ libcups2 \ libdbus-1-3 \ libdrm2 \ libgbm1 \ libgtk-3-0 \ libxcomposite1 \ libxdamage1 \ libxfixes3 \ libxrandr2 \ xdg-utils \ fonts-liberation \ dbus \ xauth \ xvfb \ supervisor \ net-tools \ procps \ git \ python3-numpy \ && rm -rf /var/lib/apt/lists/*
# Install Rust and set PATHENV RUSTUP_HOME=/usr/local/rustup \ CARGO_HOME=/usr/local/cargo \ PATH="/usr/local/cargo/bin:$PATH"
# Install RustRUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
# Install Playwright and browsers with system dependenciesENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwrightRUN pip install playwrightRUN playwright install --with-deps chromiumRUN playwright install-deps
WORKDIR /appCOPY requirements.txt .RUN pip install --no-cache-dir -r requirements.txt
# Copy application codeCOPY . .
# Start the realtime.py scriptCMD ["python", "interface.py"]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171from functools import lru_cachefrom typing import List, Tuple, Optionalimport aiohttpimport elevenlabsfrom contextlib import asynccontextmanagerfrom .logger import setup_logger, log_execution_time, log_async_execution_time
logger = setup_logger("api_clients")
class OpenRouterClient: """Handles OpenRouter API interactions with comprehensive logging and error tracking""" def __init__(self, api_key: str): logger.info("Initializing OpenRouter client") if not api_key or len(api_key) < 32: logger.error("Invalid API key format") raise ValueError("Invalid OpenRouter API key") self.api_key = api_key self.base_url = "https://openrouter.ai/api/v1" self.headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json", } logger.debug("OpenRouter client initialized 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 Returns: List of tuples containing (model_id, model_description) Raises: ValueError: If API request fails """ 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: if response.status != 200: error_msg = await response.text() logger.error(f"Failed to fetch models: {error_msg}") raise ValueError(f"Failed to fetch models: {error_msg}") models = await response.json() logger.info(f"Successfully fetched {len(models)} models") logger.debug(f"Available models: {[model['name'] for model in models]}") return [(model['id'], f"{model['name']} ({model['context_length']} tokens)") 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") try: async with self.get_session() as session: logger.debug("Preparing script generation request") response = await self._make_script_request(session, content, prompt, model_id) script = response['choices'][0]['message']['content'] logger.info(f"Script generated successfully: {len(script)} chars") logger.debug(f"Script preview: {script[:200]}...") return script except Exception as e: logger.error(f"Script generation failed", exc_info=True) raise
async def _make_script_request(self, session, content, prompt, model_id): async with session.post( f"{self.base_url}/chat/completions", json={ "model": model_id, "messages": [ { "role": "system", "content": "You are an expert podcast script writer. Create engaging, conversational content." }, { "role": "user", "content": f"""Based on this content: {content} Create a 3-minute podcast script focusing on: {prompt} Format as a natural conversation with clear speaker parts. Include [HOST] and [GUEST] markers for different voices.""" } ] } ) as response: logger.debug("Sending script generation request") if response.status != 200: error_msg = await response.text() logger.error(f"Script generation failed: {error_msg}") raise ValueError(f"Script generation failed: {error_msg}") return await response.json()
class ElevenLabsClient: """Handles ElevenLabs API interactions with detailed performance tracking""" def __init__(self, api_key: str): logger.info("Initializing ElevenLabs client") self.api_key = api_key elevenlabs.set_api_key(api_key) @lru_cache(maxsize=1) def get_voices(self) -> List[Tuple[str, str]]: """ Fetch available voices from ElevenLabs Returns: List of tuples containing (voice_id, voice_name) """ logger.info("Fetching available voices from ElevenLabs") voices = elevenlabs.voices() logger.info(f"Successfully fetched {len(voices)} voices") logger.debug(f"Available voices: {[voice.name for voice in voices]}") return [(voice.voice_id, voice.name) for voice in voices] @log_execution_time(logger) def generate_audio(self, text: str, voice_id: str) -> bytes: """ Generate audio with comprehensive error handling and quality checks Logs detailed metrics about the input text and resulting audio. """ 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) raise123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256import asyncioimport osimport timefrom dataclasses import dataclassfrom typing import List, Optional, AsyncGeneratorimport gradio as grfrom dotenv import load_dotenvfrom langchain_openai import ChatOpenAIfrom rich.console import Consolefrom rich.panel import Panelfrom rich.text import Textfrom logger import setup_logger, log_execution_time, log_async_execution_time
from browser_use import Agent, Browserfrom browser_use.browser.browser import BrowserContextfrom api_clients import OpenRouterClient, ElevenLabsClient
load_dotenv()
console = Console()logger = setup_logger("interface")
@dataclassclass ActionResult: is_done: bool extracted_content: Optional[str] error: Optional[str] include_in_memory: bool
@dataclassclass AgentHistoryList: all_results: List[ActionResult] all_model_outputs: List[dict]
def parse_agent_history(history_str: str) -> None: # Split the content into sections based on ActionResult entries sections = history_str.split('ActionResult(')
for i, section in enumerate(sections[1:], 1): # Skip first empty section # Extract relevant information content = '' if 'extracted_content=' in section: content = section.split('extracted_content=')[1].split(',')[0].strip("'")
if content: header = Text(f'Step {i}', style='bold blue') panel = Panel(content, title=header, border_style='blue') console.print(panel) console.print()
async def run_browser_task( task: str, api_key: str, provider: str = 'openai', model: str = 'gpt-4-vision', headless: bool = True,) -> str: if not api_key.strip(): return 'Please provide an API key'
if provider == 'openai': os.environ['OPENAI_API_KEY'] = api_key llm = ChatOpenAI(model=model) elif provider == 'anthropic': os.environ['ANTHROPIC_API_KEY'] = api_key llm = ChatAnthropic(model=model) else: # google os.environ['GOOGLE_API_KEY'] = api_key llm = ChatGoogleGenerativeAI(model=model)
try: agent = Agent( task=task, llm=llm, browser=Browser(BrowserContext(headless=True)) ) result = await agent.run() # TODO: The result cloud be parsed better return result except Exception as e: return f'Error: {str(e)}'
@log_async_execution_time(logger)async def scrape_content(url: str) -> str: """ Scrape and summarize content from the given URL using browser automation This function performs the following steps: 1. Validates the input URL 2. Initializes the browser agent 3. Extracts and summarizes the content Args: url: Target URL to scrape Returns: Summarized content suitable for podcast generation Raises: ValueError: If URL is invalid or content extraction fails """ logger.info(f"Starting content scrape for URL: {url}") # Input validation if not url.startswith(('http://', 'https://')): logger.error(f"Invalid URL format: {url}") raise ValueError("URL must start with http:// or https://") try: logger.debug("Initializing LLM and browser agent") llm = ChatOpenAI(model="gpt-4") agent = Agent( task=f"Visit this URL: {url} and extract the main content. Summarize it in a clear and concise way.", llm=llm, browser=Browser(BrowserContext(headless=True)) ) logger.info("Executing content extraction") result = await agent.run() logger.debug(f"Content extraction successful. Length: {len(result)} chars") logger.debug(f"Content preview: {result[:200]}...") return result except Exception as e: logger.error(f"Content extraction failed for {url}", exc_info=True) raise
@log_async_execution_time(logger)async def create_podcast( url: str, prompt: str, elevenlabs_key: str, voice_id: str, openrouter_key: str, model_id: str,) -> AsyncGenerator[tuple[Optional[str], str], None]: """ Create a podcast through a multi-step process: 1. Content extraction from URL 2. Script generation using AI 3. Voice synthesis Progress updates are yielded at each step for UI feedback. """ logger.info(f"Starting podcast creation for URL: {url}") logger.debug(f"Parameters - Voice: {voice_id}, Model: {model_id}") logger.debug(f"Prompt length: {len(prompt)} chars") try: # Initialize clients with validation logger.debug("Initializing API clients") openrouter = OpenRouterClient(openrouter_key) elevenlabs = ElevenLabsClient(elevenlabs_key) # Phase 1: Content scraping logger.info("Phase 1/3: Content scraping") yield None, "Scraping website content..." content = await scrape_content(url) logger.debug(f"Scraped content length: {len(content)} chars") # Phase 2: Script generation logger.info("Phase 2/3: Script generation") yield None, "Generating podcast script..." script = await openrouter.generate_script(content, prompt, model_id) logger.debug(f"Generated script length: {len(script)} chars") # Phase 3: Audio synthesis logger.info("Phase 3/3: Audio generation") yield None, "Converting to audio..." audio = elevenlabs.generate_audio(script, voice_id) logger.debug(f"Generated audio size: {len(audio)} bytes") # Save output audio_path = f"podcast_{int(time.time())}.mp3" logger.debug(f"Saving audio to: {audio_path}") with open(audio_path, "wb") as f: f.write(audio) logger.info("Podcast creation completed successfully") yield audio_path, "Podcast created successfully!" except Exception as e: logger.error("Podcast creation failed", exc_info=True) yield None, f"Error: {str(e)}"
def create_ui(): logger.info("Initializing Gradio interface") with gr.Blocks(title='PodcastCreator', theme=gr.themes.Soft()) as interface: with gr.Row(): with gr.Column(scale=2): url_input = gr.Textbox(label='Source URL', placeholder='Enter the URL...') prompt = gr.Textbox(label='Podcast Topic', lines=3) with gr.Row(): with gr.Column(): elevenlabs_key = gr.Textbox( label='ElevenLabs API Key', type='password', placeholder='Enter key...' ) voice = gr.Dropdown(label='Voice', choices=[]) with gr.Column(): openrouter_key = gr.Textbox( label='OpenRouter API Key', type='password', placeholder='Enter key...' ) model = gr.Dropdown(label='AI Model', choices=[]) submit_btn = gr.Button('Create Podcast', variant='primary')
with gr.Column(scale=1): audio_output = gr.Audio(label="Generated Podcast") status = gr.Textbox(label='Status', interactive=False)
# Event handlers def update_voices(key): if not key: return gr.Dropdown(choices=[]) try: client = ElevenLabsClient(key) voices = client.get_voices() return gr.Dropdown(choices=voices) except Exception as e: return gr.Dropdown(choices=[("", f"Error: {str(e)}")])
async def update_models(key): if not key: return gr.Dropdown(choices=[]) try: client = OpenRouterClient(key) models = await client.get_models() return gr.Dropdown(choices=models) except Exception as e: return gr.Dropdown(choices=[("", f"Error: {str(e)}")])
elevenlabs_key.change(fn=update_voices, inputs=elevenlabs_key, outputs=voice) openrouter_key.change(fn=update_models, inputs=openrouter_key, outputs=model) submit_btn.click( fn=create_podcast, inputs=[url_input, prompt, elevenlabs_key, voice, openrouter_key, model], outputs=[audio_output, status] )
return interface
if __name__ == '__main__': demo = create_ui() demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172import loggingimport sysimport timefrom functools import wrapsfrom typing import Callable, Anyfrom rich.logging import RichHandlerfrom rich.console import Console
console = Console()
def setup_logger(name: str, level: str = "INFO") -> logging.Logger: """ Configure and return a Rich-formatted logger with enhanced debugging capabilities Args: name: Logger identifier, typically module name level: Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL) Returns: Configured logger instance with rich formatting and error tracing """ logging.basicConfig( level=level, format="%(asctime)s [%(name)s] %(levelname)s: %(message)s", datefmt="[%X]", handlers=[ RichHandler(rich_tracebacks=True, markup=True), logging.FileHandler(f"{name}.log") # Also log to file ] ) logger = logging.getLogger(name) logger.setLevel(level) return logger
def log_execution_time(logger: logging.Logger) -> Callable: """Decorator to log function execution time""" def decorator(func: Callable) -> Callable: @wraps(func) def wrapper(*args: Any, **kwargs: Any) -> Any: start_time = time.time() logger.debug(f"Starting {func.__name__} with args: {args}, kwargs: {kwargs}") try: result = func(*args, **kwargs) execution_time = time.time() - start_time logger.debug(f"Completed {func.__name__} in {execution_time:.2f} seconds") return result except Exception as e: logger.error(f"Error in {func.__name__}: {str(e)}", exc_info=True) raise return wrapper return decorator
def log_async_execution_time(logger: logging.Logger) -> Callable: """Decorator to log async function execution time""" def decorator(func: Callable) -> Callable: @wraps(func) async def wrapper(*args: Any, **kwargs: Any) -> Any: start_time = time.time() logger.debug(f"Starting async {func.__name__} with args: {args}, kwargs: {kwargs}") try: result = await func(*args, **kwargs) execution_time = time.time() - start_time logger.debug(f"Completed async {func.__name__} in {execution_time:.2f} seconds") return result except Exception as e: logger.error(f"Error in async {func.__name__}: {str(e)}", exc_info=True) raise return wrapper return decorator1234567891011121314151617181920212223242526272829303132333435363738# Core AI and Language Modelslangchain-openai>=0.0.1langchain-google-genai>=0.0.1langchain-anthropic>=0.0.1langchain-ollama>=0.0.1
# Web and Browser Automationplaywright>=1.40.0browser-use>=0.1.20aiohttp>=3.8.0requests>=2.31.0
# Audio Generationelevenlabs>=0.1.0
# Data Handling and Validationpydantic>=2.0.0dataclasses>=0.6typing-extensions>=4.8.0
# Configuration and Environmentpython-dotenv>=1.0.0
# Error Handling and Loggingpython-json-logger>=2.0.0 # Structured JSON loggingrich>=13.0.0 # For console output formattingbackoff>=2.2.0 # Retry mechanismtenacity>=8.2.0 # Retry utilities
# UI and Interfacegradio>=4.0.0 # For better themes and UI components
# Async Supportasyncio>=3.4.3
# Testing and Developmentpytest>=7.0.0 # For running testsmypy>=1.0.0 # For type checking1234567891011from setuptools import setup, find_packages
setup( name="podcaster", version="0.1", packages=find_packages(), install_requires=[ 'rich', # add other dependencies here ]).dockerignoreAdded+53
.gitignoreAdded+1
DockerfileAdded+62
__init__.pyAdded
api_clients.pyAdded+171
interface.pyAdded+256
logger.pyAdded+72
requirements.txtAdded+38
setup.pyAdded+11