siddhartharya commited on
Commit
7263dd8
1 Parent(s): f0db570

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -10
app.py CHANGED
@@ -1,40 +1,90 @@
1
  import gradio as gr
2
- from utils import generate_script, generate_audio
3
  from prompts import SYSTEM_PROMPT
4
  from pydub import AudioSegment
 
5
  import os
 
 
6
 
7
  def generate_podcast(file, tone, length):
8
  # Extract text from PDF
9
- # Generate script using generate_script function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  # Generate audio for each dialogue item
 
 
 
 
 
 
 
 
 
 
 
 
11
  # Combine audio segments
12
- # Return audio file and transcript
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- # Gradio interface
15
  instructions = """
16
  # Podcast Generator
17
 
18
  Welcome to the Podcast Generator project! This tool allows you to create custom podcast episodes using AI-generated content.
19
 
20
  ## Features
21
- * Generate podcast scripts on any topic
22
  * Convert text to speech for a natural listening experience
23
  * Choose the tone of your podcast
24
  * Export episodes as MP3 files
25
 
26
  ## How to Use
27
- 1. Upload a PDF file (max 2048 tokens)
28
  2. Select the desired tone (humorous, casual, formal)
29
  3. Choose the podcast length
30
  4. Click "Generate" to create your podcast
31
  5. Listen to the generated audio and review the transcript
 
 
32
  """
33
 
34
  iface = gr.Interface(
35
  fn=generate_podcast,
36
  inputs=[
37
- gr.File(label="Upload PDF file"),
38
  gr.Radio(["humorous", "casual", "formal"], label="Select podcast tone", value="casual"),
39
  gr.Radio(["Short (1-2 min)", "Medium (3-5 min)"], label="Podcast length", value="Medium (3-5 min)")
40
  ],
@@ -42,8 +92,11 @@ iface = gr.Interface(
42
  gr.Audio(label="Generated Podcast"),
43
  gr.Markdown(label="Transcript")
44
  ],
45
- title="Custom NotebookLM-type Podcast Generator",
46
- description=instructions
 
 
47
  )
48
 
49
- iface.launch()
 
 
1
  import gradio as gr
2
+ from utils import generate_script, generate_audio, truncate_text
3
  from prompts import SYSTEM_PROMPT
4
  from pydub import AudioSegment
5
+ import pypdf
6
  import os
7
+ import io
8
+ import tempfile
9
 
10
  def generate_podcast(file, tone, length):
11
  # Extract text from PDF
12
+ if not file.name.lower().endswith('.pdf'):
13
+ raise gr.Error("Please upload a PDF file.")
14
+
15
+ try:
16
+ pdf_reader = pypdf.PdfReader(file.name)
17
+ text = ""
18
+ for page in pdf_reader.pages:
19
+ text += page.extract_text()
20
+ except Exception as e:
21
+ raise gr.Error(f"Error reading the PDF file: {str(e)}")
22
+
23
+ # Truncate text to 2048 tokens
24
+ truncated_text = truncate_text(text)
25
+ if len(truncated_text) < len(text):
26
+ print("Warning: The input text was truncated to fit within 2048 tokens.")
27
+
28
+ # Generate script
29
+ try:
30
+ script = generate_script(SYSTEM_PROMPT, truncated_text, tone)
31
+ except Exception as e:
32
+ raise gr.Error(f"Error generating script: {str(e)}")
33
+
34
  # Generate audio for each dialogue item
35
+ audio_segments = []
36
+ transcript = ""
37
+ try:
38
+ for item in script.dialogue:
39
+ audio_file = generate_audio(item.text, item.speaker)
40
+ audio_segment = AudioSegment.from_mp3(audio_file)
41
+ audio_segments.append(audio_segment)
42
+ transcript += f"**{item.speaker}**: {item.text}\n\n"
43
+ os.remove(audio_file) # Clean up temporary audio file
44
+ except Exception as e:
45
+ raise gr.Error(f"Error generating audio: {str(e)}")
46
+
47
  # Combine audio segments
48
+ combined_audio = sum(audio_segments)
49
+
50
+ # Adjust length if needed
51
+ if length == "Short (1-2 min)":
52
+ combined_audio = combined_audio[:120000] # 2 minutes max
53
+ else: # "Medium (3-5 min)"
54
+ combined_audio = combined_audio[:300000] # 5 minutes max
55
+
56
+ # Save combined audio to a temporary file
57
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
58
+ combined_audio.export(temp_audio.name, format="mp3")
59
+ temp_audio_path = temp_audio.name
60
+
61
+ return temp_audio_path, transcript
62
 
 
63
  instructions = """
64
  # Podcast Generator
65
 
66
  Welcome to the Podcast Generator project! This tool allows you to create custom podcast episodes using AI-generated content.
67
 
68
  ## Features
69
+ * Generate podcast scripts from PDF content
70
  * Convert text to speech for a natural listening experience
71
  * Choose the tone of your podcast
72
  * Export episodes as MP3 files
73
 
74
  ## How to Use
75
+ 1. Upload a PDF file (content will be truncated to 2048 tokens if longer)
76
  2. Select the desired tone (humorous, casual, formal)
77
  3. Choose the podcast length
78
  4. Click "Generate" to create your podcast
79
  5. Listen to the generated audio and review the transcript
80
+
81
+ Note: This tool uses the LLaMa 3.1 70B model for script generation and gTTS for text-to-speech conversion. The input is limited to 2048 tokens to ensure compatibility with the model.
82
  """
83
 
84
  iface = gr.Interface(
85
  fn=generate_podcast,
86
  inputs=[
87
+ gr.File(label="Upload PDF file", type="file"),
88
  gr.Radio(["humorous", "casual", "formal"], label="Select podcast tone", value="casual"),
89
  gr.Radio(["Short (1-2 min)", "Medium (3-5 min)"], label="Podcast length", value="Medium (3-5 min)")
90
  ],
 
92
  gr.Audio(label="Generated Podcast"),
93
  gr.Markdown(label="Transcript")
94
  ],
95
+ title="Custom NotebookLM-type Podcast Generator (2048 token limit)",
96
+ description=instructions,
97
+ allow_flagging="never",
98
+ theme=gr.themes.Soft()
99
  )
100
 
101
+ if __name__ == "__main__":
102
+ iface.launch()