File size: 1,395 Bytes
416840c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import gradio as gr
from transformers import pipeline
import PyPDF2
import docx
import requests

# Load the Llama model from Groq Cloud
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()