File size: 5,765 Bytes
d8ad8f9 f0f4b7b d8ad8f9 93f614e d8ad8f9 45cc558 d8ad8f9 f55597f d8ad8f9 f55597f 043cce0 d8ad8f9 f55597f 043cce0 d8ad8f9 f55597f d8ad8f9 f55597f d8ad8f9 f55597f d8ad8f9 02e42c9 d8ad8f9 02e42c9 d8ad8f9 f55597f 02e42c9 f55597f d8ad8f9 f55597f 02e42c9 d8ad8f9 02e42c9 d8ad8f9 02e42c9 d8ad8f9 |
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
#Codes from killerz3/PodGen & eswardivi/Podcastify
import json
import spaces
import httpx
import os
import asyncio
import edge_tts
import torch
import tempfile
import gradio as gr
import gradio_client
from pydub import AudioSegment
from transformers import AutoModelForCausalLM, AutoTokenizer
from moviepy.editor import AudioFileClip, concatenate_audioclips
system_prompt = '''
You are an educational podcast generator. You have to create short conversations between Alice and Bob that gives an overview of the News given by the user.
Please provide the script in the following JSON format directly:
{
"title": "Strings",
"content": {
"Alice_0": "Strings",
"BOB_0": "Strings",
...
}
}
Please note that the Strings you generate now must be based on the tone of people's daily life, and the punctuation marks only include commas and periods.
No more than five rounds of conversation.
'''
DESCRIPTION = '''
<div>
<h1 style="text-align: center;">Musen</h1>
<p>A podcast talking about the link's content you provided.</p>
<p>π Paste a website link with http/https.</p>
<p>π¦ Generate podcast. </p>
</div>
'''
css = """
h1 {
text-align: center;
display: block;
}
footer {
display:none !important
}
"""
MODEL_ID = "01-ai/Yi-1.5-6B-Chat"
model = AutoModelForCausalLM.from_pretrained(
MODEL_ID,
torch_dtype=torch.float16,
device_map="auto"
).eval()
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
def validate_url(url):
try:
response = httpx.get(url, timeout=60.0)
response.raise_for_status()
return response.text
except httpx.RequestError as e:
return f"An error occurred while requesting {url}: {str(e)}"
except httpx.HTTPStatusError as e:
return f"Error response {e.response.status_code} while requesting {url}"
except Exception as e:
return f"An unexpected error occurred: {str(e)}"
def fetch_text(url):
print("Entered Webpage Extraction")
prefix_url = "https://r.jina.ai/"
full_url = prefix_url + url
print(full_url)
print("Exited Webpage Extraction")
return validate_url(full_url)
async def text_to_speech(text, voice, filename):
communicate = edge_tts.Communicate(text, voice)
await communicate.save(filename)
async def gen_show(script):
title = script['title']
content = script['content']
temp_files = []
tasks = []
for key, text in content.items():
speaker = key.split('_')[0] # Extract the speaker name
index = key.split('_')[1] # Extract the dialogue index
voice = "en-US-JennyNeural" if speaker == "Alice" else "en-US-GuyNeural"
# Create temporary file for each speaker's dialogue
temp_file = tempfile.NamedTemporaryFile(suffix='.mp3', delete=False)
temp_files.append(temp_file.name)
filename = temp_file.name
tasks.append(text_to_speech(text, voice, filename))
print(f"Generated audio for {speaker}_{index}: {filename}")
await asyncio.gather(*tasks)
# Combine the audio files using moviepy
audio_clips = [AudioFileClip(temp_file) for temp_file in temp_files]
combined = concatenate_audioclips(audio_clips)
# Create temporary file for the combined output
output_filename = tempfile.NamedTemporaryFile(suffix='.mp3', delete=False).name
# Save the combined file
combined.write_audiofile(output_filename)
print(f"Combined audio saved as: {output_filename}")
# Clean up temporary files
for temp_file in temp_files:
os.remove(temp_file)
print(f"Deleted temporary file: {temp_file}")
return output_filename
@spaces.GPU
def generator(messages):
input_ids = tokenizer.apply_chat_template(
conversation=messages,
tokenize=True,
return_tensors='pt'
)
output_ids = model.generate(input_ids.to('cuda'), eos_token_id=tokenizer.eos_token_id)
results = tokenizer.decode(output_ids[0][input_ids.shape[1]:], skip_special_tokens=True)
return results
async def main(link):
if not link.startswith("http://") and not link.startswith("https://"):
return "URL must start with 'http://' or 'https://'",None
text = fetch_text(link)
if "Error" in text:
return text, None
prompt = f"News: {text}, json:"
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt},
]
generated_script = generator(messages)
print("Generated Script:"+generated_script)
# Check if the generated_script is empty or not valid JSON
if not generated_script or not generated_script.strip().startswith('{'):
raise ValueError("Failed to generate a valid script.")
script_json = json.loads(generated_script) # Use the generated script as input
output_filename = await gen_show(script_json)
print("Output File:"+output_filename)
# Read the generated audio file
return output_filename
with gr.Blocks(theme='soft', css=css, title="Musen") as iface:
with gr.Accordion(""):
gr.Markdown(DESCRIPTION)
with gr.Row():
output_box = gr.Audio(label="Podcast", type="filepath", interactive=False, autoplay=True, elem_classes="audio") # Create an output textbox
with gr.Row():
input_box = gr.Textbox(label="Link", placeholder="Enter a http link")
with gr.Row():
submit_btn = gr.Button("π Send") # Create a submit button
clear_btn = gr.ClearButton(output_box, value="ποΈ Clear") # Create a clear button
# Set up the event listeners
submit_btn.click(main, inputs=input_box, outputs=output_box)
#gr.close_all()
iface.queue().launch(show_api=False) # Launch the Gradio interface |