marks
commited on
Commit
·
9b11602
1
Parent(s):
eb7a05f
Simplified model selections
Browse files
app.py
CHANGED
|
@@ -1,60 +1,124 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
|
|
|
|
| 5 |
from scraper import scrape_url
|
| 6 |
from podcast_generator import PodcastGenerator
|
| 7 |
from tts import text_to_speech
|
| 8 |
-
import
|
| 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 |
)
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import os
|
| 3 |
+
from typing import Optional
|
| 4 |
+
from gradio import Blocks, Markdown, Row, Column, Textbox, Audio, Button, Dropdown
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
from scraper import scrape_url
|
| 7 |
from podcast_generator import PodcastGenerator
|
| 8 |
from tts import text_to_speech
|
| 9 |
+
from api_clients import OpenRouterClient, ElevenLabsClient
|
| 10 |
+
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# Default choices for dropdowns
|
| 14 |
+
default_voices = [("", "Enter API key to load voices")]
|
| 15 |
+
default_models = [("", "Enter API key to load models")]
|
| 16 |
+
|
| 17 |
+
async def generate_podcast(
|
| 18 |
+
url: str,
|
| 19 |
+
openrouter_key: str,
|
| 20 |
+
model_id: str,
|
| 21 |
+
elevenlabs_key: str,
|
| 22 |
+
voice_id: str
|
| 23 |
+
) -> Optional[str]:
|
| 24 |
+
try:
|
| 25 |
+
content = scrape_url(url)
|
| 26 |
+
|
| 27 |
+
# Initialize API clients
|
| 28 |
+
openrouter = OpenRouterClient(openrouter_key)
|
| 29 |
+
elevenlabs = ElevenLabsClient(elevenlabs_key)
|
| 30 |
+
|
| 31 |
+
# Generate script using OpenRouter
|
| 32 |
+
script = await openrouter.generate_script(content, model_id)
|
| 33 |
+
|
| 34 |
+
# Convert to audio using ElevenLabs
|
| 35 |
+
audio_file = elevenlabs.generate_audio(script, voice_id)
|
| 36 |
+
return audio_file
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"Error: {str(e)}"
|
| 39 |
+
|
| 40 |
+
def create_ui():
|
| 41 |
+
with Blocks(title='URL to Podcast Generator', theme='huggingface') as interface:
|
| 42 |
+
Markdown('# URL to Podcast Generator')
|
| 43 |
+
Markdown('Enter a URL to generate a podcast episode based on its content.')
|
| 44 |
+
|
| 45 |
+
with Row():
|
| 46 |
+
with Column(scale=2):
|
| 47 |
+
url_input = Textbox(
|
| 48 |
+
label="Website URL",
|
| 49 |
+
placeholder="Enter the URL of the website you want to convert to a podcast"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
with Row():
|
| 53 |
+
with Column():
|
| 54 |
+
openrouter_key = Textbox(
|
| 55 |
+
label='OpenRouter API Key',
|
| 56 |
+
type='password',
|
| 57 |
+
placeholder='Enter key...'
|
| 58 |
+
)
|
| 59 |
+
openrouter_model = Dropdown(
|
| 60 |
+
label='AI Model',
|
| 61 |
+
choices=default_models,
|
| 62 |
+
value=None,
|
| 63 |
+
allow_custom_value=True
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
with Column():
|
| 67 |
+
elevenlabs_key = Textbox(
|
| 68 |
+
label='ElevenLabs API Key',
|
| 69 |
+
type='password',
|
| 70 |
+
placeholder='Enter key...'
|
| 71 |
+
)
|
| 72 |
+
voice_model = Dropdown(
|
| 73 |
+
label='Voice',
|
| 74 |
+
choices=default_voices,
|
| 75 |
+
value=None,
|
| 76 |
+
allow_custom_value=True
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
submit_btn = Button('Generate Podcast', variant='primary')
|
| 80 |
+
|
| 81 |
+
with Column(scale=1):
|
| 82 |
+
audio_output = Audio(label="Generated Podcast")
|
| 83 |
+
status = Textbox(label='Status', interactive=False)
|
| 84 |
+
|
| 85 |
+
# Event handlers for API key changes
|
| 86 |
+
async def update_openrouter_models(key):
|
| 87 |
+
if not key:
|
| 88 |
+
return gr.Dropdown(choices=default_models)
|
| 89 |
+
try:
|
| 90 |
+
client = OpenRouterClient(key)
|
| 91 |
+
models = await client.get_models()
|
| 92 |
+
return gr.Dropdown(choices=models)
|
| 93 |
+
except Exception as e:
|
| 94 |
+
return gr.Dropdown(choices=[(None, f"Error: {str(e)}")])
|
| 95 |
+
|
| 96 |
+
def update_elevenlabs_voices(key):
|
| 97 |
+
if not key:
|
| 98 |
+
return gr.Dropdown(choices=default_voices)
|
| 99 |
+
try:
|
| 100 |
+
client = ElevenLabsClient(key)
|
| 101 |
+
voices = client.get_voices()
|
| 102 |
+
return gr.Dropdown(choices=voices)
|
| 103 |
+
except Exception as e:
|
| 104 |
+
return gr.Dropdown(choices=[(None, f"Error: {str(e)}")])
|
| 105 |
+
|
| 106 |
+
# Set up event handlers
|
| 107 |
+
openrouter_key.change(fn=update_openrouter_models, inputs=openrouter_key, outputs=openrouter_model)
|
| 108 |
+
elevenlabs_key.change(fn=update_elevenlabs_voices, inputs=elevenlabs_key, outputs=voice_model)
|
| 109 |
+
|
| 110 |
+
submit_btn.click(
|
| 111 |
+
fn=lambda *args: asyncio.run(generate_podcast(*args)),
|
| 112 |
+
inputs=[url_input, openrouter_key, openrouter_model, elevenlabs_key, voice_model],
|
| 113 |
+
outputs=[audio_output]
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
return interface
|
| 117 |
+
|
| 118 |
+
if __name__ == '__main__':
|
| 119 |
+
demo = create_ui()
|
| 120 |
+
demo.launch(
|
| 121 |
+
server_name="0.0.0.0",
|
| 122 |
+
server_port=7860,
|
| 123 |
+
share=True
|
| 124 |
)
|