Spaces:
Build error
Build error
Download src/main.py from binaryfarmer/Chatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.27 kB
-
https://huggingface.co/spaces/binaryfarmer/Chatbot/resolve/a898669d4ab12cfd06a6ffd80b231c1ffe3bd2ac/src/main.py
- Command line
-
hf download hf://spaces/binaryfarmer/Chatbot@a898669d4ab12cfd06a6ffd80b231c1ffe3bd2ac/src/main.py
-
curl -L -o main.py https://huggingface.co/spaces/binaryfarmer/Chatbot/resolve/a898669d4ab12cfd06a6ffd80b231c1ffe3bd2ac/src/main.py
1.27 kB
| import openai | |
| import pyttsx3 | |
| openai.api_key = "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" | |
| # Global variable to hold the chat history, initialise with system role | |
| conversation = [ | |
| {"role": "system", "content": "You are an intelligent professor."} | |
| ] | |
| # transcribe function to record the audio input | |
| def transcribe(audio): | |
| print(audio) | |
| # Whisper API | |
| audio_file = open(audio, "rb") | |
| transcript = openai.Audio.transcribe("whisper-1", audio_file) | |
| print(transcript) | |
| # ChatGPT API | |
| # append user's inut to conversation | |
| conversation.append({"role": "user", "content": transcript["text"]}) | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=conversation | |
| ) | |
| print(response) | |
| # system_message is the response from ChatGPT API | |
| system_message = response["choices"][0]["message"]["content"] | |
| # append ChatGPT response (assistant role) back to conversation | |
| conversation.append({"role": "assistant", "content": system_message}) | |
| # Text to speech | |
| engine = pyttsx3.init() | |
| engine.setProperty("rate", 150) | |
| engine.setProperty("voice", "english-us") | |
| engine.save_to_file(system_message, "response.mp3") | |
| engine.runAndWait() | |
| return "response.mp3" |