SwikarG commited on
Commit
127ee72
·
verified ·
1 Parent(s): 09378f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -9
app.py CHANGED
@@ -12,8 +12,12 @@ import tempfile
12
  import os
13
  import subprocess
14
  import platform
 
15
  import os
16
  import tempfile
 
 
 
17
 
18
  # MCP imports - adjusted for actual library structure
19
  try:
@@ -165,6 +169,13 @@ class NewsAggregatorMCP:
165
  """
166
  Convert text to audio using AI text-to-speech API with optional background music.
167
 
 
 
 
 
 
 
 
168
  Args:
169
  text (str): Text to convert to audio
170
  use_music (bool, optional): Whether to include background music. Defaults to False.
@@ -183,7 +194,7 @@ class NewsAggregatorMCP:
183
 
184
  try:
185
  # Prepare API request
186
- api_url = "https://sujankoirala07--news-broadcaster-with-bg-chatterboxwithm-3add60.modal.run"
187
  payload = {
188
  "prompt": text,
189
  "use_music": str(use_music).lower()
@@ -336,12 +347,15 @@ def search_news_interface(keywords, limit, date_from_input):
336
 
337
  Args:
338
  keywords (str): Comma-separated keywords to search for in news articles.
339
- limit (int): Maximum number of articles to return.
340
  date_from_input (int): Number of days in the past to consider for articles (e.g., 7 for last week).
341
 
342
  Returns:
343
  str: A JSON-formatted string containing the total number of results and a list of matched articles.
344
  """
 
 
 
345
  if not keywords:
346
  return "Please enter keywords to search for news."
347
 
@@ -357,14 +371,14 @@ def search_news_interface(keywords, limit, date_from_input):
357
 
358
 
359
  # Now update the text_to_audio_interface function to optionally play the audio
360
- def text_to_audio_interface(text, use_music):
361
  """
362
  Convert a short piece of text to speech using an AI text-to-audio model. Provide all the news in one call instead of making multiple separate calls. This helps improve the quality and flow of the speech output
363
 
364
  Args:
365
  text (str): The input text to convert into speech. Max length: 1000 characters.
366
  use_music (bool): Whether to add soft background music to the audio output. Default value is True.
367
-
368
  Returns:
369
  tuple:
370
  - str: A JSON-formatted string with metadata about the generated audio.
@@ -380,10 +394,10 @@ def text_to_audio_interface(text, use_music):
380
  asyncio.set_event_loop(loop)
381
  result = loop.run_until_complete(news_mcp.text_to_audio(args))
382
  if result.get("success"):
 
383
  # Optionally play the audio immediately
384
- play_args = {"filepath": result.get("audio_file")}
385
- play_result = loop.run_until_complete(news_mcp.play_audio(play_args))
386
-
387
  return json.dumps(result, indent=2), result.get("audio_file")
388
  else:
389
  return json.dumps(result, indent=2), None
@@ -398,7 +412,9 @@ with gr.Blocks(title="News Aggregator with Text to audio - MCP Server", theme=gr
398
 
399
  ### Available MCP Tools:
400
  - `search_news`: Search for news articles based on keywords
401
- - `text_to_audio`: Convert text to audio with optional background music (convert a single text snippet, not every news article)
 
 
402
  """)
403
 
404
  with gr.Tabs():
@@ -451,6 +467,11 @@ with gr.Blocks(title="News Aggregator with Text to audio - MCP Server", theme=gr
451
  value=False,
452
  info="Include background music in the generated audio"
453
  )
 
 
 
 
 
454
  convert_btn = gr.Button("Convert to Audio", variant="primary")
455
 
456
  with gr.Column():
@@ -463,7 +484,7 @@ with gr.Blocks(title="News Aggregator with Text to audio - MCP Server", theme=gr
463
 
464
  convert_btn.click(
465
  text_to_audio_interface,
466
- inputs=[text_input, music_checkbox],
467
  outputs=[audio_output_json, audio_player]
468
  )
469
 
 
12
  import os
13
  import subprocess
14
  import platform
15
+ import dotenv
16
  import os
17
  import tempfile
18
+ from pathlib import Path
19
+ dotenv.load_dotenv(Path(__file__).parent / ".env")
20
+ TEXT_TO_SPEECH_API_URL = os.environ.get("TEXT_TO_SPEECH_BASE_URL")
21
 
22
  # MCP imports - adjusted for actual library structure
23
  try:
 
169
  """
170
  Convert text to audio using AI text-to-speech API with optional background music.
171
 
172
+ IMPORTANT:
173
+ - This tool should be used ONLY ONCE for summarized news content.
174
+ - DO NOT call this function separately for each news article.
175
+ - Instead, first summarize the key points from all news articles.
176
+ - Then call this function ONCE with the compiled summary.
177
+ - Maximum allowed text length: 1000 characters. (about 125 words)
178
+
179
  Args:
180
  text (str): Text to convert to audio
181
  use_music (bool, optional): Whether to include background music. Defaults to False.
 
194
 
195
  try:
196
  # Prepare API request
197
+ api_url = TEXT_TO_SPEECH_API_URL
198
  payload = {
199
  "prompt": text,
200
  "use_music": str(use_music).lower()
 
347
 
348
  Args:
349
  keywords (str): Comma-separated keywords to search for in news articles.
350
+ limit (int): Maximum number of articles to return. The maximum value must be less than 15.
351
  date_from_input (int): Number of days in the past to consider for articles (e.g., 7 for last week).
352
 
353
  Returns:
354
  str: A JSON-formatted string containing the total number of results and a list of matched articles.
355
  """
356
+ if limit>15:
357
+ limit=15
358
+
359
  if not keywords:
360
  return "Please enter keywords to search for news."
361
 
 
371
 
372
 
373
  # Now update the text_to_audio_interface function to optionally play the audio
374
+ def text_to_audio_interface(text, use_music, auto_play):
375
  """
376
  Convert a short piece of text to speech using an AI text-to-audio model. Provide all the news in one call instead of making multiple separate calls. This helps improve the quality and flow of the speech output
377
 
378
  Args:
379
  text (str): The input text to convert into speech. Max length: 1000 characters.
380
  use_music (bool): Whether to add soft background music to the audio output. Default value is True.
381
+ auto_play (bool): Whether to automatically play the generated audio after conversion. Must be True if called from Claude or other MCP clients which doesn't support audio output play in UI.
382
  Returns:
383
  tuple:
384
  - str: A JSON-formatted string with metadata about the generated audio.
 
394
  asyncio.set_event_loop(loop)
395
  result = loop.run_until_complete(news_mcp.text_to_audio(args))
396
  if result.get("success"):
397
+ if(auto_play):
398
  # Optionally play the audio immediately
399
+ play_args = {"filepath": result.get("audio_file")}
400
+ play_result = loop.run_until_complete(news_mcp.play_audio(play_args))
 
401
  return json.dumps(result, indent=2), result.get("audio_file")
402
  else:
403
  return json.dumps(result, indent=2), None
 
412
 
413
  ### Available MCP Tools:
414
  - `search_news`: Search for news articles based on keywords
415
+ - `text_to_audio`: Convert text to audio with optional background music.
416
+
417
+ **Note:** Due to the application's continuous inactivity, the text-to-speech feature may not work on the first run. If it doesn't respond initially, please try running it again
418
  """)
419
 
420
  with gr.Tabs():
 
467
  value=False,
468
  info="Include background music in the generated audio"
469
  )
470
+ auto_play = gr.Checkbox(
471
+ label="Auto play",
472
+ value=False,
473
+ info="Auto play the generated audio using local audio player after conversion."
474
+ )
475
  convert_btn = gr.Button("Convert to Audio", variant="primary")
476
 
477
  with gr.Column():
 
484
 
485
  convert_btn.click(
486
  text_to_audio_interface,
487
+ inputs=[text_input, music_checkbox,auto_play ],
488
  outputs=[audio_output_json, audio_player]
489
  )
490