Spaces:
Sleeping
Sleeping
# app.py | |
# ============= | |
# This is a complete app.py file for a web scraping and text-to-speech app using gTTS, BeautifulSoup, langdetect, and Gradio. | |
import requests | |
from bs4 import BeautifulSoup | |
from gtts import gTTS | |
import os | |
import gradio as gr | |
from langdetect import detect | |
def get_text_from_webpage(url): | |
response = requests.get(url) | |
soup = BeautifulSoup(response.text, 'html.parser') | |
text = soup.get_text() | |
return text | |
def text_to_speech(text, lang='en', slow=False, tld='com'): | |
tts = gTTS(text=text, lang=lang, slow=slow, tld=tld) | |
tts.save("output.mp3") | |
return "output.mp3" | |
def process_url(url, lang, slow, tld): | |
text = get_text_from_webpage(url) | |
audio_file = text_to_speech(text, lang, slow, tld) | |
return audio_file, text | |
# Create Gradio interface | |
with gr.Blocks() as demo: | |
url_input = gr.Textbox(lines=1, placeholder="Enter the URL of the webpage...") | |
lang_input = gr.Dropdown(choices=["en", "fr", "de", "es", "it", "ja", "ko", "zh-CN", "zh-TW", "ru"], label="Language", value="en") | |
slow_checkbox = gr.Checkbox(label="Slow Speed") | |
tld_input = gr.Dropdown(choices=["com", "co.uk", "fr", "de", "es", "it", "co.jp", "co.kr", "com.cn", "com.tw"], label="Accent", value="com") | |
audio_output = gr.Audio(label="Generated Speech") | |
text_output = gr.Textbox(lines=10, label="Extracted Text") | |
greet_btn = gr.Button("Submit") | |
greet_btn.click(fn=process_url, inputs=[url_input, lang_input, slow_checkbox, tld_input], outputs=[audio_output, text_output]) | |
if __name__ == "__main__": | |
demo.launch() | |
# Dependencies | |
# ============= | |
# The following dependencies are required to run this app: | |
# - requests | |
# - beautifulsoup4 | |
# - gtts | |
# - langdetect | |
# - gradio | |
# | |
# You can install these dependencies using pip: | |
# pip install requests beautifulsoup4 gtts langdetect gradio | |