Spaces:
Runtime error
Runtime error
File size: 2,342 Bytes
4c3bb61 040aff1 03fc067 bae95ed 03fc067 4c3bb61 040aff1 2981ea3 040aff1 03fc067 040aff1 4c3bb61 040aff1 4c3bb61 040aff1 4c3bb61 040aff1 4c3bb61 |
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 |
import requests
import gradio as gr
import openai
from io import BytesIO
from pydub import AudioSegment
# Set up OpenAI API
openai.api_key = "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"
# Set up FPT API
tts = "IsZh7u8eHrjHgALqn0XS3M4vvouN331F"
tts_api_url = "https://api.fpt.ai/hmi/tts/v5"
def generate_presentation(topic):
prompt = f"Please explain {topic} in the most easy and attractive way possible."
# Set up OpenAI API parameters
model_engine = "text-davinci-002"
max_tokens = 1048
temperature = 0.7
# Generate the presentation content using OpenAI's GPT-3 API
response = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=max_tokens,
temperature=temperature
)
return response.choices[0].text
def generate_audio(text):
# Set up text-to-speech API parameters
voice = "banmai"
speed = "0"
# Send a request to the text-to-speech API
headers = {
"api-key": tts,
"voice": voice,
"speed": speed
}
data = {"text": text}
response = requests.post(tts_api_url, headers=headers, json=data)
# Convert the response audio to a playable format
audio_bytes = BytesIO(response.content)
audio_segment = AudioSegment.from_file(audio_bytes.getvalue(), format="mp3")
audio_segment.export("presentation_audio.mp3", format="mp3")
return audio_bytes
def generate_presentation_and_audio(topic):
presentation = generate_presentation(topic)
audio = generate_audio(presentation)
return presentation, audio
def main():
title = "AICademy"
description = "Generate a presentation on any topic using GPT-3 and convert it to audio using FPT's text-to-speech API."
inputs = gr.inputs.Textbox(label="Enter the topic for your presentation:")
outputs = [
gr.outputs.Audio(label="Presentation Audio", type="numpy"),
gr.outputs.Textbox(label="Presentation Text")
]
examples = [
["What is artificial intelligence?"],
["How do black holes form?"],
["Explain the concept of quantum computing."],
]
iface = gr.Interface(fn=generate_presentation_and_audio, inputs=inputs, outputs=outputs, title=title, description=description, examples=examples)
iface.launch()
if __name__ == "__main__":
main()
|