Spaces:
Runtime error
Runtime error
waiting for the client review
Browse files- .gitignore +3 -0
- app.py +59 -0
- description.py +8 -0
- utils.py +27 -0
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
.env
|
2 |
+
env/
|
3 |
+
__pycache__/
|
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import openai
|
5 |
+
from utils import serialize
|
6 |
+
from utils import compress
|
7 |
+
|
8 |
+
from description import DESCRIPTION
|
9 |
+
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
# configuring openai package
|
13 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
14 |
+
openai.api_key = OPENAI_API_KEY
|
15 |
+
|
16 |
+
|
17 |
+
def chat(message, history):
|
18 |
+
"""
|
19 |
+
Sends a request to the OpenAi api based on the user input and the history
|
20 |
+
"""
|
21 |
+
messages = serialize(history)
|
22 |
+
messages.append({"role": "user", "content": message})
|
23 |
+
|
24 |
+
completion = openai.ChatCompletion.create(
|
25 |
+
model="gpt-3.5-turbo",
|
26 |
+
messages=messages,
|
27 |
+
)
|
28 |
+
|
29 |
+
return completion["choices"][0]["message"]["content"].strip()
|
30 |
+
|
31 |
+
|
32 |
+
def transcribe(audio_file):
|
33 |
+
audio_file = open(audio_file, "rb")
|
34 |
+
transcription = openai.Audio.transcribe("whisper-1", audio_file, language="en")
|
35 |
+
transcription = transcription["text"]
|
36 |
+
return transcription
|
37 |
+
|
38 |
+
|
39 |
+
def predict(input, history=[]):
|
40 |
+
compress(input)
|
41 |
+
transcription = transcribe(input)
|
42 |
+
|
43 |
+
answer = chat(transcription, history)
|
44 |
+
history.append((transcription, answer))
|
45 |
+
response = history
|
46 |
+
return response, history
|
47 |
+
|
48 |
+
|
49 |
+
with gr.Blocks() as demo:
|
50 |
+
gr.Markdown(DESCRIPTION)
|
51 |
+
chatbot = gr.Chatbot()
|
52 |
+
state = gr.State([])
|
53 |
+
|
54 |
+
with gr.Row():
|
55 |
+
audio_file = gr.Audio(label="Audio", source="microphone", type="filepath")
|
56 |
+
|
57 |
+
audio_file.change(predict, [audio_file, state], [chatbot, state])
|
58 |
+
|
59 |
+
demo.launch()
|
description.py
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
DESCRIPTION = """
|
2 |
+
|
3 |
+
# AssistGpt
|
4 |
+
|
5 |
+
Introducing our cutting-edge web app that utilizes the advanced capabilities of GPT-based
|
6 |
+
chatbots to create an intelligent assistant like no other.
|
7 |
+
|
8 |
+
"""
|
utils.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import librosa
|
2 |
+
from pathlib import Path, PurePath
|
3 |
+
import soundfile as sf
|
4 |
+
|
5 |
+
|
6 |
+
def serialize(messages):
|
7 |
+
"""
|
8 |
+
Converts a list of tuples where each element of the list represents a message to dictionary of
|
9 |
+
messages
|
10 |
+
"""
|
11 |
+
|
12 |
+
serialized_messages = []
|
13 |
+
for message in messages:
|
14 |
+
serialized_message = [
|
15 |
+
{"role": "system", "content": message[0]},
|
16 |
+
{"role": "user", "content": message[1]},
|
17 |
+
]
|
18 |
+
|
19 |
+
serialized_messages.extend(serialized_message)
|
20 |
+
|
21 |
+
return serialized_messages
|
22 |
+
|
23 |
+
|
24 |
+
def compress(audio_file):
|
25 |
+
y, s = librosa.load(audio_file, sr=8000) # Downsample 44.1kHz to 8kHz
|
26 |
+
sf.write(audio_file, y, s, "PCM_24")
|
27 |
+
return audio_file
|