File size: 1,934 Bytes
040aff1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import openai
from io import BytesIO
from pydub import AudioSegment
import requests

# Set up OpenAI API
openai.api_key = "YOUR_OPENAI_API_KEY"

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
    api_key = "YOUR_TTS_API_KEY"
    api_url = "https://api.fpt.ai/hmi/tts/v5"
    voice = "banmai"
    speed = "0"

    # Send a request to the text-to-speech API
    headers = {
        "api-key": api_key,
        "voice": voice,
        "speed": speed
    }
    data = {"text": text}
    response = requests.post(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 ai_presentation(topic):
    presentation = generate_presentation(topic)
    audio = generate_audio(presentation)

    # Return the presentation and generated audio
    return presentation, audio.read()

# Set up Gradio interface
inputs = gr.inputs.Textbox(label="Enter the topic for your presentation:")
outputs = [
    gr.outputs.Textbox(label="Presentation"),
    gr.outputs.Audio(label="Presentation Audio", type="audio")
]

gr.Interface(fn=ai_presentation, inputs=inputs, outputs=outputs, title="AICademy", 
             icon=":books:", server_port=8080).launch()