|
import gradio as gr |
|
from transformers import pipeline |
|
import PyPDF2 |
|
import docx |
|
import requests |
|
|
|
|
|
model = pipeline("text-to-speech", model="groq/llama-3.1-70b") |
|
|
|
def extract_text_from_file(file_path): |
|
if file_path.endswith('.pdf'): |
|
with open(file_path, 'rb') as file: |
|
reader = PyPDF2.PdfReader(file) |
|
text = "" |
|
for page in reader.pages: |
|
text += page.extract_text() |
|
return text |
|
elif file_path.endswith('.docx'): |
|
doc = docx.Document(file_path) |
|
text = "\n".join([para.text for para in doc.paragraphs]) |
|
return text |
|
return "" |
|
|
|
def fetch_text_from_url(url): |
|
response = requests.get(url) |
|
return response.text if response.status_code == 200 else "" |
|
|
|
def generate_podcast(uploaded_file=None, url=None): |
|
if uploaded_file: |
|
input_text = extract_text_from_file(uploaded_file.name) |
|
elif url: |
|
input_text = fetch_text_from_url(url) |
|
else: |
|
return "No input provided." |
|
|
|
dialogue = f"Man: {input_text}\nWoman: {input_text} (response)" |
|
audio = model(dialogue) |
|
return audio |
|
|
|
iface = gr.Interface( |
|
fn=generate_podcast, |
|
inputs=[ |
|
gr.inputs.File(label="Upload PDF or DOC"), |
|
gr.inputs.Textbox(label="Or enter URL"), |
|
], |
|
outputs=gr.outputs.Audio(label="Generated Podcast"), |
|
) |
|
|
|
iface.launch() |