siddhartharya commited on
Commit
f0db570
1 Parent(s): 652d9d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -74
app.py CHANGED
@@ -1,89 +1,49 @@
1
  import gradio as gr
2
- import PyPDF2
3
- import docx
4
- import requests
5
- from bs4 import BeautifulSoup
6
- from groq import Groq
7
- from gtts import gTTS
8
  from pydub import AudioSegment
9
  import os
10
- import io
11
 
12
- # Initialize Groq client
13
- groq_client = Groq()
14
-
15
- def extract_text(file_or_url):
16
- if isinstance(file_or_url, str): # URL
17
- response = requests.get(file_or_url)
18
- soup = BeautifulSoup(response.text, 'html.parser')
19
- return soup.get_text()
20
- elif file_or_url is not None:
21
- if file_or_url.name.endswith('.pdf'):
22
- reader = PyPDF2.PdfReader(file_or_url.file)
23
- return ' '.join([page.extract_text() for page in reader.pages])
24
- elif file_or_url.name.endswith('.docx'):
25
- doc = docx.Document(file_or_url.file)
26
- return ' '.join([para.text for para in doc.paragraphs])
27
- return ""
28
-
29
- def generate_podcast_script(text):
30
- prompt = f"""Generate a podcast script between a man and a woman discussing the following text:
31
- {text}
32
- The podcast should be informative and engaging, with a natural conversation flow.
33
- Limit the script to approximately 750 words to fit within a 5-minute podcast."""
34
-
35
- response = groq_client.chat.completions.create(
36
- messages=[
37
- {"role": "system", "content": "You are an AI assistant that generates podcast scripts based on given text."},
38
- {"role": "user", "content": prompt}
39
- ],
40
- model="llama-3.1-70b-versatile", # Using LLaMa 3.1 70B model
41
- max_tokens=1000,
42
- temperature=0.7
43
- )
44
- return response.choices[0].message.content
45
-
46
- def text_to_speech(script):
47
- lines = script.split('\n')
48
- audio_segments = []
49
- for line in lines:
50
- if line.startswith("Man:"):
51
- tts = gTTS(line[4:], lang='en', tld='co.uk')
52
- elif line.startswith("Woman:"):
53
- tts = gTTS(line[6:], lang='en', tld='com.au')
54
- else:
55
- continue
56
- buffer = io.BytesIO()
57
- tts.write_to_fp(buffer)
58
- buffer.seek(0)
59
- audio_segments.append(AudioSegment.from_mp3(buffer))
60
-
61
- final_audio = sum(audio_segments)
62
- final_audio = final_audio[:300000] # Trim to 5 minutes (300,000 ms)
63
- buffer = io.BytesIO()
64
- final_audio.export(buffer, format="mp3")
65
- buffer.seek(0)
66
- return buffer
67
-
68
- def generate_podcast(file_or_url):
69
- text = extract_text(file_or_url)
70
- if not text:
71
- return None, "Failed to extract text. Please check your input."
72
- script = generate_podcast_script(text)
73
- audio_file = text_to_speech(script)
74
- return audio_file, script
75
 
76
  iface = gr.Interface(
77
  fn=generate_podcast,
78
  inputs=[
79
- gr.File(label="Upload PDF/DOC file"),
80
- gr.Textbox(label="Or enter URL")
 
81
  ],
82
  outputs=[
83
  gr.Audio(label="Generated Podcast"),
84
- gr.Textbox(label="Podcast Script")
85
  ],
86
- title="Custom NotebookLM-type Podcast Generator (LLaMa 3.1 70B)"
 
87
  )
88
 
89
  iface.launch()
 
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
  ],
41
  outputs=[
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()