Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,8 @@ import logging
|
|
| 12 |
from dash.exceptions import PreventUpdate
|
| 13 |
import pandas as pd
|
| 14 |
import time
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Set up logging
|
| 17 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -20,8 +22,12 @@ logger = logging.getLogger(__name__)
|
|
| 20 |
# Initialize Dash app
|
| 21 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
| 22 |
|
| 23 |
-
# Initialize Gemini AI
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def generate_podcast_script(api_key, content, duration, num_hosts):
|
| 27 |
genai.configure(api_key=api_key)
|
|
@@ -40,6 +46,8 @@ def generate_podcast_script(api_key, content, duration, num_hosts):
|
|
| 40 |
Do not use any special characters or markdown. Only include the monologue with proper punctuation.
|
| 41 |
Ensure the content flows naturally and stays relevant to the topic.
|
| 42 |
Limit the script length to match the requested duration of {duration}.
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
else:
|
| 45 |
prompt = f"""
|
|
@@ -54,6 +62,8 @@ def generate_podcast_script(api_key, content, duration, num_hosts):
|
|
| 54 |
Do not use any special characters or markdown. Only include the alternating dialogue lines with proper punctuation.
|
| 55 |
Ensure the conversation flows naturally and stays relevant to the topic.
|
| 56 |
Limit the script length to match the requested duration of {duration}.
|
|
|
|
|
|
|
| 57 |
"""
|
| 58 |
|
| 59 |
response = model.generate_content(prompt)
|
|
@@ -155,7 +165,6 @@ app.layout = dbc.Container([
|
|
| 155 |
|
| 156 |
dbc.Card([
|
| 157 |
dbc.CardBody([
|
| 158 |
-
dbc.Input(id="api-key-input", type="password", placeholder="Enter your Gemini API Key"),
|
| 159 |
dbc.Textarea(id="content-input", placeholder="Paste your content or upload a document", rows=5, className="my-3"),
|
| 160 |
dcc.Upload(
|
| 161 |
id='document-upload',
|
|
@@ -246,26 +255,37 @@ def update_voice2_options(lang):
|
|
| 246 |
[Output("script-output", "value"),
|
| 247 |
Output("script-progress", "value")],
|
| 248 |
Input("generate-btn", "n_clicks"),
|
| 249 |
-
[State("
|
| 250 |
-
State("content-input", "value"),
|
| 251 |
State("duration", "value"),
|
| 252 |
State("num-hosts", "value")],
|
| 253 |
prevent_initial_call=True
|
| 254 |
)
|
| 255 |
-
def generate_script(n_clicks,
|
| 256 |
if n_clicks is None:
|
| 257 |
raise PreventUpdate
|
| 258 |
try:
|
| 259 |
for i in range(10):
|
| 260 |
time.sleep(0.5) # Simulate progress
|
| 261 |
-
# Instead of yielding, we'll just pass and update at the end
|
| 262 |
pass
|
| 263 |
-
script = generate_podcast_script(
|
|
|
|
| 264 |
return script, 100
|
| 265 |
except Exception as e:
|
| 266 |
logger.error(f"Error generating script: {str(e)}")
|
| 267 |
return f"Error: {str(e)}", 0
|
| 268 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
@app.callback(
|
| 270 |
[Output("audio-output", "children"),
|
| 271 |
Output("download-audio", "data"),
|
|
@@ -283,8 +303,7 @@ def render_and_download_podcast(n_clicks, api_key, script, voice1, voice2, num_h
|
|
| 283 |
raise PreventUpdate
|
| 284 |
try:
|
| 285 |
# Run the async function in a synchronous context
|
| 286 |
-
sample_rate, audio_data = asyncio.run(render_podcast(
|
| 287 |
-
|
| 288 |
# Convert numpy array to WAV
|
| 289 |
wav_audio = AudioSegment(
|
| 290 |
audio_data.tobytes(),
|
|
|
|
| 12 |
from dash.exceptions import PreventUpdate
|
| 13 |
import pandas as pd
|
| 14 |
import time
|
| 15 |
+
import os
|
| 16 |
+
from huggingface_hub import HfApi
|
| 17 |
|
| 18 |
# Set up logging
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 22 |
# Initialize Dash app
|
| 23 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
| 24 |
|
| 25 |
+
# Initialize Gemini AI with the API key from HF secret
|
| 26 |
+
gemini_api_key = os.environ.get("GEMINI_API_KEY")
|
| 27 |
+
if not gemini_api_key:
|
| 28 |
+
raise ValueError("GEMINI_API_KEY not found in environment variables. Please set it as a secret in your Hugging Face Space.")
|
| 29 |
+
|
| 30 |
+
genai.configure(api_key=gemini_api_key)
|
| 31 |
|
| 32 |
def generate_podcast_script(api_key, content, duration, num_hosts):
|
| 33 |
genai.configure(api_key=api_key)
|
|
|
|
| 46 |
Do not use any special characters or markdown. Only include the monologue with proper punctuation.
|
| 47 |
Ensure the content flows naturally and stays relevant to the topic.
|
| 48 |
Limit the script length to match the requested duration of {duration}.
|
| 49 |
+
Do not put an intro our outro music as I only need the dialog
|
| 50 |
+
The dialog must have proper punctuation like apostrophes
|
| 51 |
"""
|
| 52 |
else:
|
| 53 |
prompt = f"""
|
|
|
|
| 62 |
Do not use any special characters or markdown. Only include the alternating dialogue lines with proper punctuation.
|
| 63 |
Ensure the conversation flows naturally and stays relevant to the topic.
|
| 64 |
Limit the script length to match the requested duration of {duration}.
|
| 65 |
+
Do not put an intro our outro music as I only need the dialog
|
| 66 |
+
The dialog must have proper punctuation like apostrophes
|
| 67 |
"""
|
| 68 |
|
| 69 |
response = model.generate_content(prompt)
|
|
|
|
| 165 |
|
| 166 |
dbc.Card([
|
| 167 |
dbc.CardBody([
|
|
|
|
| 168 |
dbc.Textarea(id="content-input", placeholder="Paste your content or upload a document", rows=5, className="my-3"),
|
| 169 |
dcc.Upload(
|
| 170 |
id='document-upload',
|
|
|
|
| 255 |
[Output("script-output", "value"),
|
| 256 |
Output("script-progress", "value")],
|
| 257 |
Input("generate-btn", "n_clicks"),
|
| 258 |
+
[State("content-input", "value"),
|
|
|
|
| 259 |
State("duration", "value"),
|
| 260 |
State("num-hosts", "value")],
|
| 261 |
prevent_initial_call=True
|
| 262 |
)
|
| 263 |
+
def generate_script(n_clicks, content, duration, num_hosts):
|
| 264 |
if n_clicks is None:
|
| 265 |
raise PreventUpdate
|
| 266 |
try:
|
| 267 |
for i in range(10):
|
| 268 |
time.sleep(0.5) # Simulate progress
|
|
|
|
| 269 |
pass
|
| 270 |
+
script = generate_podcast_script(content, duration, num_hosts) # Remove api_key parameter
|
| 271 |
+
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
|
| 272 |
return script, 100
|
| 273 |
except Exception as e:
|
| 274 |
logger.error(f"Error generating script: {str(e)}")
|
| 275 |
return f"Error: {str(e)}", 0
|
| 276 |
|
| 277 |
+
@app.callback(
|
| 278 |
+
[Output("audio-output", "children"),
|
| 279 |
+
Output("download-audio", "data"),
|
| 280 |
+
Output("podcast-progress", "value")],
|
| 281 |
+
Input("generate-podcast-btn", "n_clicks"),
|
| 282 |
+
[State("script-output", "value"),
|
| 283 |
+
State("voice1-select", "value"),
|
| 284 |
+
State("voice2-select", "value"),
|
| 285 |
+
State("num-hosts", "value")],
|
| 286 |
+
prevent_initial_call=True
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
@app.callback(
|
| 290 |
[Output("audio-output", "children"),
|
| 291 |
Output("download-audio", "data"),
|
|
|
|
| 303 |
raise PreventUpdate
|
| 304 |
try:
|
| 305 |
# Run the async function in a synchronous context
|
| 306 |
+
sample_rate, audio_data = asyncio.run(render_podcast(script, voice1, voice2, num_hosts)) # Remove api_key parameter
|
|
|
|
| 307 |
# Convert numpy array to WAV
|
| 308 |
wav_audio = AudioSegment(
|
| 309 |
audio_data.tobytes(),
|