fdaudens HF Staff commited on
Commit
c301481
·
1 Parent(s): e211da0

mp3 gradio, rss logic

Browse files
Files changed (4) hide show
  1. app.py +63 -3
  2. requirements.txt +3 -1
  3. rss.xml +2 -2
  4. update-rss.py +42 -0
app.py CHANGED
@@ -10,10 +10,19 @@ import time
10
  import pymupdf
11
  import requests
12
  from pathlib import Path
 
 
13
 
14
  import torch
15
  from huggingface_hub import InferenceClient
16
  from kokoro import KModel, KPipeline
 
 
 
 
 
 
 
17
  # -----------------------------------------------------------------------------
18
  # Get default podcast materials, from Daily papers and one download
19
  # -----------------------------------------------------------------------------
@@ -60,6 +69,34 @@ def generate_podcast_script(subject: str, steering_question: str | None = None)
60
  podcast_text = full_text[dialogue_start_index:]
61
  return podcast_text
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  # -----------------------------------------------------------------------------
64
  # Kokoro TTS
65
  # -----------------------------------------------------------------------------
@@ -109,9 +146,32 @@ def generate_podcast(topic: str):
109
  t0 = time.time()
110
  ref_s = pipeline_voice[len(ps) - 1]
111
  audio_numpy = kmodel(ps, ref_s, speed).numpy()
112
- yield (sr, audio_numpy)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  t1 = time.time()
114
- print(f"PROCESSED '{utterance}' in {int(t1-t0)} seconds. {audio_numpy.shape}")
 
115
 
116
  EXAMPLES = [
117
  ["https://huggingface.co/blog/inference-providers-cohere", None, "How does using this compare with other inference solutions?"],
@@ -132,7 +192,7 @@ Based on [Kokoro TTS](https://huggingface.co/hexgrad/Kokoro-82M) and [Llama-3.3-
132
  outputs=[
133
  gr.Audio(
134
  label="Listen to your podcast! 🔊",
135
- format="wav",
136
  streaming=True,
137
  ),
138
  ],
 
10
  import pymupdf
11
  import requests
12
  from pathlib import Path
13
+ from pydub import AudioSegment # Add this import
14
+ import tempfile
15
 
16
  import torch
17
  from huggingface_hub import InferenceClient
18
  from kokoro import KModel, KPipeline
19
+
20
+ # -----------------------------------------------------------------------------
21
+ # to-do
22
+ # - Add field for the podcast title and description
23
+ # - add field for the script
24
+ # -----------------------------------------------------------------------------
25
+
26
  # -----------------------------------------------------------------------------
27
  # Get default podcast materials, from Daily papers and one download
28
  # -----------------------------------------------------------------------------
 
69
  podcast_text = full_text[dialogue_start_index:]
70
  return podcast_text
71
 
72
+ def generate_headline_and_description(subject: str, steering_question: str | None = None) -> tuple[str, str]:
73
+ """Ask the LLM for a headline and a short description for the podcast episode."""
74
+ prompt = f"""You are a world-class podcast producer. Given the following paper or topic, generate:
75
+ 1. A catchy, informative headline for a podcast episode about it (max 15 words).
76
+ 2. A short, engaging description (2-3 sentences, max 60 words) that summarizes what listeners will learn or why the topic is exciting.
77
+
78
+ Here is the topic:
79
+ {subject[:10000]}
80
+ """
81
+ messages = [
82
+ {"role": "system", "content": "You are a world-class podcast producer."},
83
+ {"role": "user", "content": prompt},
84
+ ]
85
+ response = client.chat_completion(
86
+ messages,
87
+ max_tokens=512,
88
+ )
89
+ full_text = response.choices[0].message.content.strip()
90
+ # Try to split headline and description
91
+ lines = [l.strip() for l in full_text.splitlines() if l.strip()]
92
+ if len(lines) >= 2:
93
+ headline = lines[0]
94
+ description = " ".join(lines[1:])
95
+ else:
96
+ headline = full_text[:80]
97
+ description = full_text
98
+ return headline, description
99
+
100
  # -----------------------------------------------------------------------------
101
  # Kokoro TTS
102
  # -----------------------------------------------------------------------------
 
146
  t0 = time.time()
147
  ref_s = pipeline_voice[len(ps) - 1]
148
  audio_numpy = kmodel(ps, ref_s, speed).numpy()
149
+
150
+ # Convert numpy array to MP3
151
+ with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as temp_wav:
152
+ sf.write(temp_wav.name, audio_numpy, sr)
153
+ temp_wav_path = temp_wav.name
154
+
155
+ # Use pydub to convert WAV to MP3
156
+ audio_segment = AudioSegment.from_wav(temp_wav_path)
157
+ with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as temp_mp3:
158
+ audio_segment.export(temp_mp3.name, format="mp3")
159
+ temp_mp3_path = temp_mp3.name
160
+
161
+ # Read the MP3 data
162
+ with open(temp_mp3_path, 'rb') as mp3_file:
163
+ mp3_data = mp3_file.read()
164
+
165
+ # Clean up temporary files
166
+ os.unlink(temp_wav_path)
167
+ os.unlink(temp_mp3_path)
168
+
169
+ # Yield MP3 data instead of numpy array
170
+ yield (sr, mp3_data)
171
+
172
  t1 = time.time()
173
+ print(f"PROCESSED '{utterance}' in {int(t1-t0)} seconds. MP3 conversion completed.")
174
+
175
 
176
  EXAMPLES = [
177
  ["https://huggingface.co/blog/inference-providers-cohere", None, "How does using this compare with other inference solutions?"],
 
192
  outputs=[
193
  gr.Audio(
194
  label="Listen to your podcast! 🔊",
195
+ format="mp3",
196
  streaming=True,
197
  ),
198
  ],
requirements.txt CHANGED
@@ -4,4 +4,6 @@ transformers
4
  PyMuPDF
5
  soundfile
6
  numpy
7
- requests
 
 
 
4
  PyMuPDF
5
  soundfile
6
  numpy
7
+ requests
8
+ pydub
9
+ ffmpeg-python
rss.xml CHANGED
@@ -26,8 +26,8 @@
26
 
27
  <!-- Example Episode -->
28
  <item>
29
- <title>Ep 1“Title of the Most Upvoted Paper”</title>
30
- <description>Today’s top paper on Hugging Face is: “XYZ.” Listen to an AI discuss the ideas, findings, and questions it raises. (Warning: AI may hallucinate)</description>
31
  <pubDate>Tue, 13 May 2025 10:00:00 +0000</pubDate>
32
  <enclosure url="https://yourpodcastwebsite.com/audio/episode1.mp3" length="12345678" type="audio/mpeg"/>
33
  <guid>https://yourpodcastwebsite.com/audio/episode1.mp3</guid>
 
26
 
27
  <!-- Example Episode -->
28
  <item>
29
+ <title>Step 1x3DFrom Scrap Models to Masterpieces</title>
30
+ <description>Today’s episode dives into Step 1x3D, a new open-source method that cleans noisy 3D data, bridges 2D–3D generation, and rivals top proprietary tools. From mesh repair to texture-perfect diffusion, it’s a major leap for 3D AI.</description>
31
  <pubDate>Tue, 13 May 2025 10:00:00 +0000</pubDate>
32
  <enclosure url="https://yourpodcastwebsite.com/audio/episode1.mp3" length="12345678" type="audio/mpeg"/>
33
  <guid>https://yourpodcastwebsite.com/audio/episode1.mp3</guid>
update-rss.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import xml.etree.ElementTree as ET
2
+ from datetime import datetime
3
+ import os
4
+ from app import generate_headline_and_description
5
+
6
+ def get_next_episode_number(podcast_dir="podcasts"):
7
+ files = [f for f in os.listdir(podcast_dir) if f.endswith(".wav")]
8
+ return len(files) + 1
9
+
10
+ def update_rss(subject, audio_url, audio_length, rss_path="rss.xml"):
11
+ # Generate headline and description automatically
12
+ title, description = generate_headline_and_description(subject)
13
+
14
+ tree = ET.parse(rss_path)
15
+ root = tree.getroot()
16
+ channel = root.find("channel")
17
+
18
+ # Update lastBuildDate
19
+ last_build_date = channel.find("lastBuildDate")
20
+ now_rfc2822 = datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S +0000")
21
+ if last_build_date is not None:
22
+ last_build_date.text = now_rfc2822
23
+
24
+ # Create new item
25
+ item = ET.Element("item")
26
+ ET.SubElement(item, "title").text = title
27
+ ET.SubElement(item, "description").text = description
28
+ ET.SubElement(item, "pubDate").text = now_rfc2822
29
+ ET.SubElement(item, "enclosure", url=audio_url, length=str(audio_length), type="audio/mpeg")
30
+ ET.SubElement(item, "guid").text = audio_url
31
+ ET.SubElement(item, "itunes:explicit").text = "false"
32
+
33
+ # Insert new item after lastBuildDate (i.e., as the first item)
34
+ # Find the first <item> and insert before it, or append if none exist
35
+ items = channel.findall("item")
36
+ if items:
37
+ channel.insert(list(channel).index(items[0]), item)
38
+ else:
39
+ channel.append(item)
40
+
41
+ # Write back to file
42
+ tree.write(rss_path, encoding="utf-8", xml_declaration=True)