Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai, config, subprocess
|
3 |
+
openai.api_key = os.environ['API_TOKEN']
|
4 |
+
|
5 |
+
messages = [{"role": "system", "content": 'You are a helpful technology assistant.'}]
|
6 |
+
|
7 |
+
def audioGPT(audio):
|
8 |
+
global messages
|
9 |
+
|
10 |
+
audio_file = open(audio, "rb")
|
11 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
12 |
+
|
13 |
+
messages.append({"role": "user", "content": transcript["text"]})
|
14 |
+
|
15 |
+
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
|
16 |
+
|
17 |
+
system_message = response["choices"][0]["message"]
|
18 |
+
messages.append(system_message)
|
19 |
+
|
20 |
+
chats = ""
|
21 |
+
for msg in messages:
|
22 |
+
if msg['role'] != 'system':
|
23 |
+
chats += msg['role'] + ": " + msg['content'] + "\n\n"
|
24 |
+
|
25 |
+
return chats
|
26 |
+
|
27 |
+
|
28 |
+
def textGPT(text):
|
29 |
+
global messages
|
30 |
+
|
31 |
+
messages.append({"role": "user", "content": text})
|
32 |
+
|
33 |
+
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
|
34 |
+
|
35 |
+
system_message = response["choices"][0]["message"]
|
36 |
+
messages.append(system_message)
|
37 |
+
|
38 |
+
chats = ""
|
39 |
+
for msg in messages:
|
40 |
+
if msg['role'] != 'system':
|
41 |
+
chats += msg['role'] + ": " + msg['content'] + "\n\n"
|
42 |
+
|
43 |
+
return chats
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
text = gr.Interface(fn=textGPT, inputs="text", outputs="text")
|
52 |
+
audio = gr.Interface(fn=audioGPT, inputs=gr.Audio(source="microphone", type="filepath"), outputs="text")
|
53 |
+
demo = gr.TabbedInterface([text, audio], [ "chatGPT", "audioGPT"])
|
54 |
+
|
55 |
+
if __name__ == "__main__":
|
56 |
+
demo.launch(share=True, auth=("os.environ['username']", "os.environ['password']"))
|
57 |
+
#demo.launch()
|