-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcopilot.py
55 lines (37 loc) · 1.14 KB
/
copilot.py
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
import os
import openai
import json
from text_to_speech import TextToSpeech
from dotenv import load_dotenv
class Copilot:
GET_IN_UZBEK = " provide answer in Uzbek language"
def clear_text(self, text):
a = text.replace("\n", " ")
b = a.split()
c = " ".join(b)
return c
def get_answer(self, question):
prompt = question + self.GET_IN_UZBEK
load_dotenv()
openai.api_key = "YOUR API KEY"
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=512,
temperature=0.5,
)
json_object = response
# Convert the JSON object to a JSON string
json_string = json.dumps(json_object)
# Parse the JSON string using json.loads()
parsed_json = json.loads(json_string)
text = parsed_json['choices'][0]['text']
cleared_text = self.clear_text(text)
return cleared_text
# Usage:
# copilot = Copilot()
# a = copilot.get_answer("Birinchi Jahon urishini kim boshlagan?")
# print(a)
# Usage:
# tts = TextToSpeech()
# tts.to_speech(a)