marks commited on
Commit
9b11602
·
1 Parent(s): eb7a05f

Simplified model selections

Browse files
Files changed (1) hide show
  1. app.py +120 -56
app.py CHANGED
@@ -1,60 +1,124 @@
1
- from fastapi import FastAPI
2
- from fastapi.staticfiles import StaticFiles
3
- from fastapi.middleware.cors import CORSMiddleware
4
- import gradio as gr
 
5
  from scraper import scrape_url
6
  from podcast_generator import PodcastGenerator
7
  from tts import text_to_speech
8
- import uvicorn
9
-
10
- app = FastAPI()
11
-
12
- # Add CORS middleware
13
- app.add_middleware(
14
- CORSMiddleware,
15
- allow_origins=["*"],
16
- allow_credentials=True,
17
- allow_methods=["*"],
18
- allow_headers=["*"],
19
- )
20
-
21
- # Mount static files
22
- app.mount("/static", StaticFiles(directory="static"), name="static")
23
-
24
- def generate_podcast(url):
25
- content = scrape_url(url)
26
- podcast_generator = PodcastGenerator()
27
- podcast_text = podcast_generator.generate_podcast(content)
28
- audio_file = text_to_speech(podcast_text)
29
- return audio_file
30
-
31
- # Create Gradio interface with custom configuration
32
- demo = gr.Interface(
33
- fn=generate_podcast,
34
- inputs=gr.Textbox(
35
- label="Website URL",
36
- placeholder="Enter the URL of the website you want to convert to a podcast"
37
- ),
38
- outputs=gr.Audio(label="Generated Podcast"),
39
- title="URL to Podcast Generator",
40
- description="Enter a URL to generate a podcast episode based on its content.",
41
- theme="huggingface",
42
- allow_flagging="never",
43
- css="static/custom.css", # Optional: for any custom CSS
44
- analytics_enabled=False,
45
- )
46
-
47
- # Mount Gradio app with a specific base path
48
- app = gr.mount_gradio_app(app, demo, path="/")
49
-
50
- if __name__ == "__main__":
51
- import os
52
- # Create static directory if it doesn't exist
53
- os.makedirs("static", exist_ok=True)
54
-
55
- uvicorn.run(
56
- "app:app",
57
- host="0.0.0.0",
58
- port=7860,
59
- reload=True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  )