import os
import openai
from transcript import text


os.remove('raspuns.txt')
os.remove('audio.mp3')


openai.api_key = "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"

# list engines
engine="text-davinci-002"

# print the first engine's id
#print(engines.data[9].id)

# create a completion
completion = response = openai.Completion.create(
  engine="text-davinci-002",
  prompt=(text),
  temperature=0,
  max_tokens=150,
  top_p=1,
  frequency_penalty=0,
  presence_penalty=0
)

# print the completion
print(completion.choices[0].text)
#deschide un fisier cu numele raspuns.txt si scrie raspunsul compltion in el
f = open("raspuns.txt", "w", encoding="utf-8")
f.write(completion.choices[0].text)
f.close()

#importa tts
#import pyttsx3
#creaza un obiect de tip TTS
#engine = pyttsx3.init()
#seteaza limba
#engine.setProperty('voice', 'ro')
#seteaza viteza
#engine.setProperty('rate', 150)
#seteaza pitch
#engine.setProperty('pitch', 1)
#seteaza volumul
#engine.setProperty('volume', 1)
#transforma audio textul din rspuns.txt
#engine.say(completion.choices[0].text)
#ruleaza audio
#engine.runAndWait()

from gtts import gTTS
import os

tx = open("raspuns.txt", "r", encoding = "utf-8")
textul = tx.read().replace("\n", " ")

limba = "ro"

output = gTTS(text=textul, lang=limba, slow=False)


output.save("audio.mp3")
tx.close()
 
os.system("start audio.mp3")
os.system("start raspuns.txt")


