K3NW48 commited on
Commit
62b117d
1 Parent(s): dc2f095
Files changed (5) hide show
  1. README.md +3 -3
  2. app.py +133 -0
  3. input_audio.wav +0 -0
  4. requirements.txt +143 -0
  5. response.mp3 +0 -0
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: BoozhooBot
3
- emoji: 🦀
4
- colorFrom: green
5
- colorTo: red
6
  sdk: gradio
7
  sdk_version: 3.23.0
8
  app_file: app.py
 
1
  ---
2
  title: BoozhooBot
3
+ emoji: 😻
4
+ colorFrom: indigo
5
+ colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 3.23.0
8
  app_file: app.py
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import the required libraries
2
+ import gradio as gr
3
+ import openai
4
+ from gtts import gTTS
5
+ from pydub import AudioSegment
6
+ import os
7
+
8
+
9
+ messages = [{"role": "system", "content": 'You are the Anishinaabe hero Nanaboozhoo. Not only do you answer with profound wisdom but you will continue the conversation by answering like this, Boozhoo: (your answer)'}]
10
+ full_transcript = []
11
+ openai.api_key = ""
12
+ audio_file = 'response.mp3'
13
+
14
+
15
+ def set_api(my_key):
16
+ openai.api_key = my_key
17
+
18
+
19
+ def create_image(response):
20
+ # Send text to be summarized
21
+ dalle_prompt = openai.ChatCompletion.create(
22
+ model="gpt-3.5-turbo",
23
+ messages=[
24
+ {"role": "user", "content": f'Summarize this text "{response["choices"][0]["message"]["content"]}" into a short and concise Dall-E 2 prompt starting with "A Professional photograph of an Anishinaabe person saying :(summarization)".'}
25
+ ]
26
+ )
27
+ # Use summary as prompt for pic
28
+ dalle_summary = openai.Image.create(
29
+ prompt = dalle_prompt["choices"][0]["message"]["content"],
30
+ size="512x512"
31
+ )
32
+ image_url = dalle_summary['data'][0]['url']
33
+ return image_url
34
+
35
+
36
+ def speak(system_message):
37
+ global audio_file
38
+ content = system_message['content']
39
+ tts = gTTS(content, lang='en', slow=False)
40
+ tts.save("response.mp3")
41
+ return "response.mp3"
42
+
43
+
44
+
45
+ def transcribe(gradio_input, api_key):
46
+ global messages
47
+ global full_transcript
48
+ global audio_file
49
+ set_api(api_key)
50
+
51
+ #Transcribe audio
52
+ input_audio = AudioSegment.from_file(gradio_input)
53
+ input_audio.export("input_audio.wav", format="wav")
54
+ with open("input_audio.wav", "rb") as audio_file:
55
+ print(f"Audio file format: {os.path.splitext(audio_file.name)[1]}\n")
56
+ transcript = openai.Audio.transcribe("whisper-1", audio_file)
57
+
58
+
59
+ #Append content to messages
60
+ full_transcript.append(transcript["text"])
61
+ messages.append({"role": "user", "content": transcript["text"]})
62
+
63
+
64
+ #Send the latest set of messages to OpenAI to get a response
65
+ response = openai.ChatCompletion.create(
66
+ model="gpt-3.5-turbo",
67
+ messages=messages
68
+ )
69
+ # Extract the latest system message from the response and add it as a new message to the messages list
70
+ system_message = response["choices"][0]["message"]
71
+ messages.append(system_message)
72
+
73
+
74
+ pic_url = create_image(response)
75
+ speech = speak(system_message)
76
+
77
+
78
+ # Combine all messages in the messages list to create a chat transcript
79
+ chat_transcript = ""
80
+ for message in messages:
81
+ if message['role'] != 'system':
82
+ chat_transcript += message['role'] + ": " + message['content'] + "\n\n"
83
+
84
+
85
+ return speech, chat_transcript, pic_url
86
+
87
+
88
+
89
+ MY_INFO = '\nSupport me at my [Linktree](https://linktr.ee/Nbiish).'
90
+ API_INFO = 'Get your api key at [platform.openai.com/account/api-keys](https://platform.openai.com/account/api-keys)'
91
+
92
+
93
+ # Create a Gradio interface
94
+ demo = gr.Interface(
95
+ fn=transcribe,
96
+ inputs=[
97
+ gr.Audio(source="microphone", type="filepath", show_label=False),
98
+ gr.Textbox(
99
+ label="OpenAI API Key",
100
+ lines=1,
101
+ placeholder="Enter your OpenAI API key",
102
+ default=None,
103
+ type="password",
104
+ fn=set_api,
105
+ ),
106
+ ],
107
+ outputs=[
108
+ gr.Audio(show_label=False),
109
+ gr.Textbox(label="Transcript:"),
110
+ gr.Image(show_label=False),
111
+ ],
112
+ title="Boozhoo Bot",
113
+ description=f"""
114
+ Anishinaabe Chatbot
115
+
116
+ Applies OpenAI's Whisper to transcribe audio input.
117
+ GPT-3.5 Turbo to generate a response.
118
+ Dall-E 2.0 to generate an image.
119
+ gTTS to generate audio response.
120
+
121
+ 1) Record to get started
122
+ 2) Press X near recording to keep going
123
+ 3) Refresh page to restart
124
+
125
+ {MY_INFO}
126
+ {API_INFO}
127
+
128
+ """,
129
+ )
130
+
131
+
132
+ if __name__ == "__main__":
133
+ demo.queue().launch()
input_audio.wav ADDED
Binary file (121 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.1.0
2
+ aiohttp==3.8.4
3
+ aiosignal==1.3.1
4
+ altair==4.2.2
5
+ anyio==3.6.2
6
+ async-timeout==4.0.2
7
+ attrs==21.2.0
8
+ Automat==20.2.0
9
+ Babel==2.8.0
10
+ bcrypt==3.2.0
11
+ black==21.10b0
12
+ blinker==1.4
13
+ certifi==2020.6.20
14
+ cffi==1.15.1
15
+ chardet==4.0.0
16
+ charset-normalizer==3.0.1
17
+ click==8.0.3
18
+ cloud-init==22.4.2
19
+ colorama==0.4.4
20
+ command-not-found==0.3
21
+ config==0.5.1
22
+ configobj==5.0.6
23
+ constantly==15.1.0
24
+ contourpy==1.0.7
25
+ cryptography==3.4.8
26
+ cycler==0.11.0
27
+ dbus-python==1.2.18
28
+ distlib==0.3.4
29
+ distro==1.7.0
30
+ distro-info===1.1build1
31
+ entrypoints==0.4
32
+ fastapi==0.92.0
33
+ ffmpy==0.3.0
34
+ filelock==3.6.0
35
+ Flask==2.2.3
36
+ fonttools==4.38.0
37
+ frozenlist==1.3.3
38
+ fsspec==2023.1.0
39
+ gradio==3.23.0
40
+ gTTS==2.3.1
41
+ h11==0.14.0
42
+ httpcore==0.16.3
43
+ httplib2==0.20.2
44
+ httpx==0.23.3
45
+ huggingface-hub==0.13.3
46
+ hyperlink==21.0.0
47
+ idna==3.3
48
+ importlib-metadata==4.6.4
49
+ incremental==21.3.0
50
+ ipython_genutils==0.2.0
51
+ itsdangerous==2.1.2
52
+ jeepney==0.7.1
53
+ Jinja2==3.0.3
54
+ jsonpatch==1.32
55
+ jsonpointer==2.0
56
+ jsonschema==3.2.0
57
+ jupyter-core==4.9.1
58
+ keyring==23.5.0
59
+ kiwisolver==1.4.4
60
+ launchpadlib==1.10.16
61
+ lazr.restfulclient==0.14.4
62
+ lazr.uri==1.0.6
63
+ linkify-it-py==2.0.0
64
+ markdown-it-py==2.2.0
65
+ MarkupSafe==2.1.2
66
+ matplotlib==3.7.0
67
+ mdit-py-plugins==0.3.3
68
+ mdurl==0.1.2
69
+ more-itertools==8.10.0
70
+ multidict==6.0.4
71
+ mypy-extensions==0.4.3
72
+ netifaces==0.11.0
73
+ numpy==1.24.2
74
+ oauthlib==3.2.0
75
+ openai==0.27.2
76
+ orjson==3.8.7
77
+ packaging==23.0
78
+ pandas==1.5.3
79
+ pathspec==0.9.0
80
+ pexpect==4.8.0
81
+ Pillow==9.4.0
82
+ pipenv==11.9.0
83
+ platformdirs==2.5.1
84
+ ptyprocess==0.7.0
85
+ pyasn1==0.4.8
86
+ pyasn1-modules==0.2.1
87
+ PyAudio==0.2.13
88
+ pycparser==2.21
89
+ pycryptodome==3.17
90
+ pydantic==1.10.5
91
+ pydub==0.25.1
92
+ Pygments==2.11.2
93
+ PyGObject==3.42.1
94
+ PyHamcrest==2.0.2
95
+ PyJWT==2.3.0
96
+ pymacaroons==0.13.0
97
+ PyNaCl==1.5.0
98
+ pyOpenSSL==21.0.0
99
+ pyparsing==2.4.7
100
+ pypng==0.20220715.0
101
+ pyrsistent==0.18.1
102
+ pyserial==3.5
103
+ python-apt==2.4.0+ubuntu1
104
+ python-dateutil==2.8.2
105
+ python-debian===0.1.43ubuntu1
106
+ python-dotenv==0.21.1
107
+ python-magic==0.4.24
108
+ python-multipart==0.0.6
109
+ pyttsx3==2.90
110
+ pytz==2022.1
111
+ PyYAML==5.4.1
112
+ requests==2.28.2
113
+ rfc3986==1.5.0
114
+ SecretStorage==3.3.1
115
+ semantic-version==2.10.0
116
+ service-identity==18.1.0
117
+ six==1.16.0
118
+ sniffio==1.3.0
119
+ sos==4.4
120
+ SpeechRecognition==3.9.0
121
+ ssh-import-id==5.11
122
+ starlette==0.25.0
123
+ systemd-python==234
124
+ tomli==1.2.2
125
+ toolz==0.12.0
126
+ tqdm==4.64.1
127
+ traitlets==5.1.1
128
+ Twisted==22.1.0
129
+ typing_extensions==4.5.0
130
+ ubuntu-advantage-tools==8001
131
+ uc-micro-py==1.0.1
132
+ ufw==0.36.1
133
+ unattended-upgrades==0.1
134
+ urllib3==1.26.5
135
+ uvicorn==0.20.0
136
+ virtualenv==20.13.0+ds
137
+ virtualenv-clone==0.3.0
138
+ wadllib==1.3.6
139
+ websockets==10.4
140
+ Werkzeug==2.2.3
141
+ yarl==1.8.2
142
+ zipp==1.0.0
143
+ zope.interface==5.4.0
response.mp3 ADDED
Binary file (53 kB). View file