Spaces:
Running
on
Zero
Running
on
Zero
finally
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import spaces
|
|
3 |
import os, torch, io
|
4 |
import json
|
5 |
import re
|
6 |
-
|
7 |
import httpx
|
8 |
# print("Make sure you've downloaded unidic (python -m unidic download) for this WebUI to work.")
|
9 |
from melo.api import TTS
|
@@ -13,18 +13,33 @@ from pydub import AudioSegment
|
|
13 |
from gradio_client import Client
|
14 |
|
15 |
client = Client("eswardivi/AIO_Chat")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def fetch_text(url):
|
17 |
print("Entered Webpage Extraction")
|
18 |
prefix_url = "https://r.jina.ai/"
|
19 |
-
|
20 |
-
|
21 |
-
print("Response Received")
|
22 |
-
return response.text
|
23 |
-
|
24 |
-
|
25 |
@spaces.GPU
|
26 |
-
def synthesize(article_url,
|
|
|
|
|
|
|
27 |
text = fetch_text(article_url)
|
|
|
|
|
|
|
28 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
29 |
|
30 |
template = """
|
@@ -50,38 +65,40 @@ def synthesize(article_url, progress=gr.Progress()):
|
|
50 |
else:
|
51 |
conversation = template
|
52 |
speed = 1.0
|
53 |
-
models = {
|
54 |
-
"EN": TTS(language="EN", device=device),
|
55 |
-
}
|
56 |
speakers = ["EN-Default", "EN-US"]
|
57 |
-
|
58 |
combined_audio = AudioSegment.empty()
|
59 |
-
|
60 |
-
|
|
|
61 |
bio = io.BytesIO()
|
62 |
text = turn["text"]
|
63 |
speaker = speakers[i % 2]
|
64 |
speaker_id = models["EN"].hps.data.spk2id[speaker]
|
65 |
-
models["EN"].tts_to_file(
|
66 |
-
text, speaker_id, bio, speed=speed, pbar=progress.tqdm, format="wav"
|
67 |
-
)
|
68 |
bio.seek(0)
|
69 |
audio_segment = AudioSegment.from_file(bio, format="wav")
|
70 |
combined_audio += audio_segment
|
71 |
-
|
72 |
final_audio_path = "final.mp3"
|
73 |
combined_audio.export(final_audio_path, format="mp3")
|
74 |
-
return final_audio_path
|
75 |
|
76 |
|
77 |
with gr.Blocks() as demo:
|
78 |
-
gr.Markdown("#
|
79 |
-
gr.Markdown("
|
80 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
81 |
with gr.Group():
|
82 |
text = gr.Textbox(label="Article Link")
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
86 |
|
87 |
demo.queue(api_open=True, default_concurrency_limit=10).launch(show_api=True,share=True)
|
|
|
3 |
import os, torch, io
|
4 |
import json
|
5 |
import re
|
6 |
+
os.system("python -m unidic download")
|
7 |
import httpx
|
8 |
# print("Make sure you've downloaded unidic (python -m unidic download) for this WebUI to work.")
|
9 |
from melo.api import TTS
|
|
|
13 |
from gradio_client import Client
|
14 |
|
15 |
client = Client("eswardivi/AIO_Chat")
|
16 |
+
|
17 |
+
def validate_url(url):
|
18 |
+
try:
|
19 |
+
response = httpx.get(url, timeout=60.0)
|
20 |
+
response.raise_for_status()
|
21 |
+
return response.text
|
22 |
+
except httpx.RequestError as e:
|
23 |
+
return f"An error occurred while requesting {url}: {str(e)}"
|
24 |
+
except httpx.HTTPStatusError as e:
|
25 |
+
return f"Error response {e.response.status_code} while requesting {url}"
|
26 |
+
except Exception as e:
|
27 |
+
return f"An unexpected error occurred: {str(e)}"
|
28 |
+
|
29 |
def fetch_text(url):
|
30 |
print("Entered Webpage Extraction")
|
31 |
prefix_url = "https://r.jina.ai/"
|
32 |
+
full_url = prefix_url + url
|
33 |
+
return validate_url(full_url)
|
|
|
|
|
|
|
|
|
34 |
@spaces.GPU
|
35 |
+
def synthesize(article_url,progress_audio=gr.Progress()):
|
36 |
+
if not article_url.startswith("http://") and article_url.startswith("https://"):
|
37 |
+
return "URL must start with 'http://' or 'https://'",None
|
38 |
+
|
39 |
text = fetch_text(article_url)
|
40 |
+
if "Error" in text:
|
41 |
+
return text, None
|
42 |
+
|
43 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
44 |
|
45 |
template = """
|
|
|
65 |
else:
|
66 |
conversation = template
|
67 |
speed = 1.0
|
68 |
+
models = {"EN": TTS(language="EN", device=device)}
|
|
|
|
|
69 |
speakers = ["EN-Default", "EN-US"]
|
|
|
70 |
combined_audio = AudioSegment.empty()
|
71 |
+
|
72 |
+
conversation_dict = json.loads(conversation)
|
73 |
+
for i, turn in enumerate(conversation_dict["conversation"]):
|
74 |
bio = io.BytesIO()
|
75 |
text = turn["text"]
|
76 |
speaker = speakers[i % 2]
|
77 |
speaker_id = models["EN"].hps.data.spk2id[speaker]
|
78 |
+
models["EN"].tts_to_file(text, speaker_id, bio, speed=1.0, pbar=progress_audio.tqdm, format="wav")
|
|
|
|
|
79 |
bio.seek(0)
|
80 |
audio_segment = AudioSegment.from_file(bio, format="wav")
|
81 |
combined_audio += audio_segment
|
|
|
82 |
final_audio_path = "final.mp3"
|
83 |
combined_audio.export(final_audio_path, format="mp3")
|
84 |
+
return conversation, final_audio_path
|
85 |
|
86 |
|
87 |
with gr.Blocks() as demo:
|
88 |
+
gr.Markdown("# Turn Any Article into a Podcast")
|
89 |
+
gr.Markdown("## Easily convert articles from URLs into listenable audio podcasts.")
|
90 |
+
gr.Markdown("### Instructions")
|
91 |
+
gr.Markdown("""
|
92 |
+
- **Step 1:** Paste the URL of the article you want to convert into the textbox.
|
93 |
+
- **Step 2:** Click on "Podcastify" to generate the podcast.
|
94 |
+
- **Step 3:** Listen to the podcast or view the conversation.
|
95 |
+
""")
|
96 |
with gr.Group():
|
97 |
text = gr.Textbox(label="Article Link")
|
98 |
+
btn = gr.Button("Podcastify", variant="primary")
|
99 |
+
with gr.Row():
|
100 |
+
conv_display = gr.Textbox(label="Conversation", interactive=False)
|
101 |
+
aud = gr.Audio(interactive=False)
|
102 |
+
btn.click(synthesize, inputs=[text], outputs=[conv_display, aud])
|
103 |
|
104 |
demo.queue(api_open=True, default_concurrency_limit=10).launch(show_api=True,share=True)
|