Spaces:
Build error
Build error
Commit ·
da5a128
1
Parent(s): 8fca242
Upload 2 files
Browse files- main.py +50 -0
- requirements.txt +2 -0
main.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import pyttsx3
|
| 3 |
+
|
| 4 |
+
openai.api_key = "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"
|
| 5 |
+
|
| 6 |
+
# Global variable to hold the chat history, initialise with system role
|
| 7 |
+
conversation = [
|
| 8 |
+
{"role": "system", "content": "You are an intelligent professor."}
|
| 9 |
+
]
|
| 10 |
+
|
| 11 |
+
# transcribe function to record the audio input
|
| 12 |
+
|
| 13 |
+
def transcribe(audio):
|
| 14 |
+
print(audio)
|
| 15 |
+
|
| 16 |
+
# Whisper API
|
| 17 |
+
|
| 18 |
+
audio_file = open(audio, "rb")
|
| 19 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
print(transcript)
|
| 23 |
+
|
| 24 |
+
# ChatGPT API
|
| 25 |
+
|
| 26 |
+
# append user's inut to conversation
|
| 27 |
+
conversation.append({"role": "user", "content": transcript["text"]})
|
| 28 |
+
|
| 29 |
+
response = openai.ChatCompletion.create(
|
| 30 |
+
model="gpt-3.5-turbo",
|
| 31 |
+
messages=conversation
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
print(response)
|
| 35 |
+
|
| 36 |
+
# system_message is the response from ChatGPT API
|
| 37 |
+
system_message = response["choices"][0]["message"]["content"]
|
| 38 |
+
|
| 39 |
+
# append ChatGPT response (assistant role) back to conversation
|
| 40 |
+
conversation.append({"role": "assistant", "content": system_message})
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# Text to speech
|
| 44 |
+
engine = pyttsx3.init()
|
| 45 |
+
engine.setProperty("rate", 150)
|
| 46 |
+
engine.setProperty("voice", "english-us")
|
| 47 |
+
engine.save_to_file(system_message, "response.mp3")
|
| 48 |
+
engine.runAndWait()
|
| 49 |
+
|
| 50 |
+
return "response.mp3"
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai==0.27.2
|
| 2 |
+
pyttsx3
|