Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,89 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
import PyPDF2
|
| 4 |
import docx
|
| 5 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def
|
| 11 |
-
if
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
return ""
|
| 23 |
|
| 24 |
-
def
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
iface = gr.Interface(
|
| 41 |
fn=generate_podcast,
|
| 42 |
inputs=[
|
| 43 |
-
gr.
|
| 44 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
],
|
| 46 |
-
|
| 47 |
)
|
| 48 |
|
| 49 |
iface.launch()
|
|
|
|
| 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(api_key=os.environ["GROQ_API_KEY"])
|
| 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()
|