Spaces:
Runtime error
Runtime error
# import numpy as np | |
import gradio | |
from openai import OpenAI | |
import tempfile | |
import os | |
from dotenv import load_dotenv | |
load_dotenv() | |
import uuid | |
client = OpenAI() | |
DEFAULT_SYSTEM_PROMPT = """ | |
You are a podcast editor that specialized to create a script out of a webpage. | |
[TASKS] | |
- You will receive a link to a webpage about some content. | |
- You will create a script out of the content. | |
- The script should be 1 minute long if you read it out loud. | |
- Start with an intro to peak the interest of the listener. | |
- Then, summarize the content in a way that is easy to understand. Ask questions about the the content and answer them. | |
- Conclude with the most intriguing part of the content. | |
- Refrain from adding section headers in the script like [INTRUCTION], [CONTENT], [CONCLUSION]. | |
- The script should be inspiring, written in colloquialism with english words and proverbs understood around the world. | |
- Write in a relax tone. | |
- Use filler words like 'um', 'ah', 'well' etc. to make it sound more natural. | |
[BREAKS] | |
- Use the <break time="1s"/> tag to add a pause in the speech. Use it after each paragraph and after a sentence when appropriate to get to a relax tone. | |
- Use <break time="3s"/> for longer pauses, to emphasize a point. | |
""" | |
def generate_episode(system_prompt, weblink): | |
# sr = 48000 | |
# a4_freq, tones_from_a4 = 440, 12 * (octave - 4) + (note - 9) | |
# frequency = a4_freq * 2 ** (tones_from_a4 / 12) | |
# duration = int(duration) | |
# audio = np.linspace(0, duration, duration * sr) | |
# audio = (20000 * np.sin(audio * (2 * np.pi * frequency))).astype(np.int16) | |
# return sr, audio | |
response = client.chat.completions.create( | |
model="gpt-4o", | |
messages=[ | |
{"role": "system", "content": system_prompt}, | |
{"role": "user", "content": weblink}, | |
] | |
) | |
script = response.choices[0].message.content | |
response = client.audio.speech.create( | |
model="tts-1", | |
voice="fable", | |
input=script, | |
) | |
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file: | |
temp_file.write(response.content) | |
# state["mp3_file"] = temp_file.name | |
return temp_file.name | |
def cleanup(state): | |
mp3_file_name = state.get("mp3_file") | |
if mp3_file_name and os.path.exists(mp3_file_name): | |
os.remove(mp3_file_name) | |
print(f"Removed file: {mp3_file_name}") | |
else: | |
print(f"No file found to delete: {mp3_file_name}") | |
demo = gradio.Interface( | |
fn=generate_episode, | |
inputs=[ | |
gradio.Textbox(value=DEFAULT_SYSTEM_PROMPT, label="System Prompt"), | |
gradio.Textbox(value="https://en.wikipedia.org/wiki/Mount_Tambora", label="Weblink"), | |
# gradio.State(), # State to track the mp3 file | |
], | |
# outputs=["audio", gradio.State()], | |
outputs="audio", | |
) | |
# demo.cleanup(cleanup) | |
if __name__ == "__main__": | |
demo.launch() |