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()