Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -43,9 +43,8 @@ async def generate_conversation(script):
|
|
43 |
combined = concatenate_audioclips(audio_clips)
|
44 |
|
45 |
# Create temporary file for the combined output
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
# Save the combined file
|
50 |
combined.write_audiofile(output_filename)
|
51 |
print(f"Combined audio saved as: {output_filename}")
|
@@ -58,7 +57,7 @@ async def generate_conversation(script):
|
|
58 |
return output_filename
|
59 |
|
60 |
# Function to generate podcast based on user input
|
61 |
-
def generate_podcast(topic, seed):
|
62 |
system_instructions = '''[SYSTEM] You are an educational podcast generator. You have to create a podcast between Alice and Bob that gives an overview of the topic given by the user.
|
63 |
Please provide the script in the following JSON format:
|
64 |
{
|
@@ -72,7 +71,7 @@ def generate_podcast(topic, seed):
|
|
72 |
Be concise.
|
73 |
'''
|
74 |
|
75 |
-
text = f" Topic: {topic}"
|
76 |
formatted_prompt = system_instructions + text
|
77 |
stream = Client.text_generation(formatted_prompt, max_new_tokens=1024, seed=seed, stream=True, details=True, return_full_text=False)
|
78 |
|
@@ -80,43 +79,37 @@ def generate_podcast(topic, seed):
|
|
80 |
for response in stream:
|
81 |
if not response.token.text == "</s>":
|
82 |
generated_script += response.token.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
-
# Generate the podcast
|
85 |
script_json = json.loads(generated_script) # Use the generated script as input
|
86 |
-
output_filename =
|
87 |
print("Output File:"+output_filename)
|
88 |
|
89 |
# Read the generated audio file
|
90 |
-
|
91 |
-
audio_bytes = f.read()
|
92 |
-
|
93 |
-
# Clean up the final output temporary file
|
94 |
-
os.remove(output_filename)
|
95 |
-
print(f"Deleted temporary file: {output_filename}")
|
96 |
-
|
97 |
-
return audio_bytes
|
98 |
|
|
|
99 |
DESCRIPTION = """ # <center><b>PODGEN 📻</b></center>
|
100 |
### <center>Generate a podcast on any topic</center>
|
101 |
### <center>Use the Power of llms to understand any topic better</center>
|
102 |
"""
|
103 |
-
|
104 |
with gr.Blocks(css="#important{display: none;}") as demo:
|
105 |
gr.Markdown(DESCRIPTION)
|
106 |
with gr.Row():
|
107 |
input = gr.Textbox(label="Topic", placeholder="Enter a topic")
|
108 |
-
output = gr.Audio(label="Podgen", type="filepath",
|
109 |
-
|
110 |
-
autoplay=True,
|
111 |
-
elem_classes="audio")
|
112 |
gr.Interface(
|
113 |
-
batch=True,
|
114 |
-
max_batch_size=10,
|
115 |
fn=generate_podcast,
|
116 |
inputs=[input],
|
117 |
-
outputs=[output],
|
118 |
-
|
119 |
-
|
120 |
|
121 |
if __name__ == "__main__":
|
122 |
demo.queue(max_size=200).launch()
|
|
|
43 |
combined = concatenate_audioclips(audio_clips)
|
44 |
|
45 |
# Create temporary file for the combined output
|
46 |
+
output_filename = tempfile.NamedTemporaryFile(suffix='.mp3', delete=False).name
|
47 |
+
|
|
|
48 |
# Save the combined file
|
49 |
combined.write_audiofile(output_filename)
|
50 |
print(f"Combined audio saved as: {output_filename}")
|
|
|
57 |
return output_filename
|
58 |
|
59 |
# Function to generate podcast based on user input
|
60 |
+
async def generate_podcast(topic, seed=42):
|
61 |
system_instructions = '''[SYSTEM] You are an educational podcast generator. You have to create a podcast between Alice and Bob that gives an overview of the topic given by the user.
|
62 |
Please provide the script in the following JSON format:
|
63 |
{
|
|
|
71 |
Be concise.
|
72 |
'''
|
73 |
|
74 |
+
text = f" Topic: {topic} json:"
|
75 |
formatted_prompt = system_instructions + text
|
76 |
stream = Client.text_generation(formatted_prompt, max_new_tokens=1024, seed=seed, stream=True, details=True, return_full_text=False)
|
77 |
|
|
|
79 |
for response in stream:
|
80 |
if not response.token.text == "</s>":
|
81 |
generated_script += response.token.text
|
82 |
+
|
83 |
+
print("Generated Script:"+generated_script)
|
84 |
+
|
85 |
+
# Check if the generated_script is empty or not valid JSON
|
86 |
+
if not generated_script or not generated_script.strip().startswith('{'):
|
87 |
+
raise ValueError("Failed to generate a valid script.")
|
88 |
+
|
89 |
|
|
|
90 |
script_json = json.loads(generated_script) # Use the generated script as input
|
91 |
+
output_filename = await generate_conversation(script_json)
|
92 |
print("Output File:"+output_filename)
|
93 |
|
94 |
# Read the generated audio file
|
95 |
+
return output_filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
+
# Gradio Interface
|
98 |
DESCRIPTION = """ # <center><b>PODGEN 📻</b></center>
|
99 |
### <center>Generate a podcast on any topic</center>
|
100 |
### <center>Use the Power of llms to understand any topic better</center>
|
101 |
"""
|
|
|
102 |
with gr.Blocks(css="#important{display: none;}") as demo:
|
103 |
gr.Markdown(DESCRIPTION)
|
104 |
with gr.Row():
|
105 |
input = gr.Textbox(label="Topic", placeholder="Enter a topic")
|
106 |
+
output = gr.Audio(label="Podgen", type="filepath", interactive=False, autoplay=True, elem_classes="audio")
|
107 |
+
|
|
|
|
|
108 |
gr.Interface(
|
|
|
|
|
109 |
fn=generate_podcast,
|
110 |
inputs=[input],
|
111 |
+
outputs=[output],
|
112 |
+
)
|
|
|
113 |
|
114 |
if __name__ == "__main__":
|
115 |
demo.queue(max_size=200).launch()
|