Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import PyPDF2
|
| 4 |
+
import docx
|
| 5 |
+
import requests
|
| 6 |
+
|
| 7 |
+
# Load the Llama model from Groq Cloud
|
| 8 |
+
model = pipeline("text-to-speech", model="groq/llama-3.1-70b")
|
| 9 |
+
|
| 10 |
+
def extract_text_from_file(file_path):
|
| 11 |
+
if file_path.endswith('.pdf'):
|
| 12 |
+
with open(file_path, 'rb') as file:
|
| 13 |
+
reader = PyPDF2.PdfReader(file)
|
| 14 |
+
text = ""
|
| 15 |
+
for page in reader.pages:
|
| 16 |
+
text += page.extract_text()
|
| 17 |
+
return text
|
| 18 |
+
elif file_path.endswith('.docx'):
|
| 19 |
+
doc = docx.Document(file_path)
|
| 20 |
+
text = "\n".join([para.text for para in doc.paragraphs])
|
| 21 |
+
return text
|
| 22 |
+
return ""
|
| 23 |
+
|
| 24 |
+
def fetch_text_from_url(url):
|
| 25 |
+
response = requests.get(url)
|
| 26 |
+
return response.text if response.status_code == 200 else ""
|
| 27 |
+
|
| 28 |
+
def generate_podcast(uploaded_file=None, url=None):
|
| 29 |
+
if uploaded_file:
|
| 30 |
+
input_text = extract_text_from_file(uploaded_file.name)
|
| 31 |
+
elif url:
|
| 32 |
+
input_text = fetch_text_from_url(url)
|
| 33 |
+
else:
|
| 34 |
+
return "No input provided."
|
| 35 |
+
|
| 36 |
+
dialogue = f"Man: {input_text}\nWoman: {input_text} (response)"
|
| 37 |
+
audio = model(dialogue)
|
| 38 |
+
return audio
|
| 39 |
+
|
| 40 |
+
iface = gr.Interface(
|
| 41 |
+
fn=generate_podcast,
|
| 42 |
+
inputs=[
|
| 43 |
+
gr.inputs.File(label="Upload PDF or DOC"),
|
| 44 |
+
gr.inputs.Textbox(label="Or enter URL"),
|
| 45 |
+
],
|
| 46 |
+
outputs=gr.outputs.Audio(label="Generated Podcast"),
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
iface.launch()
|