NewsMuse_Radio / app.py
eswardivi's picture
Update app.py
476a003 verified
import gradio as gr
import requests
import tempfile
# Audio URLs
AUDIO_URLS = {
"Money": "https://pub-6dc43089a07d4218adb4b0579b3be0b9.r2.dev/News/News_Money.wav",
"Geopolitical": "https://pub-6dc43089a07d4218adb4b0579b3be0b9.r2.dev/News/News_Geopolitical.wav",
"World": "https://pub-6dc43089a07d4218adb4b0579b3be0b9.r2.dev/News/News_World.wav",
"Entertainment": "https://pub-6dc43089a07d4218adb4b0579b3be0b9.r2.dev/News/News_Entertainment.wav",
"Health": "https://pub-6dc43089a07d4218adb4b0579b3be0b9.r2.dev/News/News_Health.wav",
"Sports": "https://pub-6dc43089a07d4218adb4b0579b3be0b9.r2.dev/News/News_Sports.wav"
}
def fetch_audio(category):
# Fetch audio from URL and save it to a temporary file
url = AUDIO_URLS[category]
response = requests.get(url)
response.raise_for_status() # Ensure the request was successful
# Save to a temporary file
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
temp_file.write(response.content)
temp_file.close()
return temp_file.name # Return the path to the temporary file
def create_news_dashboard():
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# πŸ“» Radio News Dashboard")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### πŸ’° Money News")
money_audio = gr.Audio(interactive=False, type="filepath")
money_btn = gr.Button("Play Money News")
money_state = gr.State("Money") # Use gr.State for category
money_btn.click(fn=fetch_audio, inputs=money_state, outputs=money_audio)
with gr.Column(scale=1):
gr.Markdown("### 🌍 Geopolitical News")
geo_audio = gr.Audio(interactive=False, type="filepath")
geo_btn = gr.Button("Play Geopolitical News")
geo_state = gr.State("Geopolitical") # Use gr.State for category
geo_btn.click(fn=fetch_audio, inputs=geo_state, outputs=geo_audio)
with gr.Column(scale=1):
gr.Markdown("### 🌐 World News")
world_audio = gr.Audio(interactive=False, type="filepath")
world_btn = gr.Button("Play World News")
world_state = gr.State("World") # Use gr.State for category
world_btn.click(fn=fetch_audio, inputs=world_state, outputs=world_audio)
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### 🎭 Entertainment")
ent_audio = gr.Audio(interactive=False, type="filepath")
ent_btn = gr.Button("Play Entertainment News")
ent_state = gr.State("Entertainment") # Use gr.State for category
ent_btn.click(fn=fetch_audio, inputs=ent_state, outputs=ent_audio)
with gr.Column(scale=1):
gr.Markdown("### πŸ₯ Health News")
health_audio = gr.Audio(interactive=False, type="filepath")
health_btn = gr.Button("Play Health News")
health_state = gr.State("Health") # Use gr.State for category
health_btn.click(fn=fetch_audio, inputs=health_state, outputs=health_audio)
with gr.Column(scale=1):
gr.Markdown("### πŸ† Sports News")
sports_audio = gr.Audio(interactive=False, type="filepath")
sports_btn = gr.Button("Play Sports News")
sports_state = gr.State("Sports") # Use gr.State for category
sports_btn.click(fn=fetch_audio, inputs=sports_state, outputs=sports_audio)
return demo
if __name__ == "__main__":
demo = create_news_dashboard()
demo.launch()