Spaces:
Paused
Paused
Commit
•
9569a05
1
Parent(s):
624e4ac
modularization
Browse files- config.py +0 -3
- discourse.py +5 -30
- gradio-ui.py +3 -2
- memory.py +26 -0
config.py
DELETED
@@ -1,3 +0,0 @@
|
|
1 |
-
TARS_LOGO = "images/TARS-Logo.png"
|
2 |
-
LOGO_IMAGE_WIDTH = 360
|
3 |
-
LOGO_IMAGE_HEIGHT = 360
|
|
|
|
|
|
|
|
discourse.py
CHANGED
@@ -2,31 +2,15 @@ import openai
|
|
2 |
import os
|
3 |
import azure.cognitiveservices.speech as speechsdk
|
4 |
|
5 |
-
import csv
|
6 |
-
from huggingface_hub import Repository
|
7 |
-
|
8 |
-
DATASET_REPO_URL = "https://huggingface.co/datasets/FranklinWillemen/persistent-space-dataset"
|
9 |
-
DATA_FILENAME = "data.csv"
|
10 |
-
DATA_FILE = os.path.join("data", DATA_FILENAME)
|
11 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
12 |
-
|
13 |
LANGUAGE = "nl"
|
14 |
AUDIO_FILE_NAME = "audio_response.wav"
|
15 |
|
16 |
-
repo = Repository(
|
17 |
-
local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
18 |
-
)
|
19 |
-
|
20 |
openai.api_key = os.environ['OPEN_AI_KEY']
|
21 |
|
22 |
speech_config = speechsdk.SpeechConfig(subscription=os.environ['AZURE_SPEECH_KEY'], region="westeurope")
|
23 |
speech_config.speech_synthesis_voice_name = "nl-NL-ColetteNeural"
|
24 |
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=None)
|
25 |
|
26 |
-
context = [{"role": "system", "content": 'Je bent een slimme en behulpzame gesprekspartner. \
|
27 |
-
Antwoord beknopt en ter zake.\
|
28 |
-
Vermeld niet dat je een AI of een soort service bent.'}]
|
29 |
-
|
30 |
def transcribe(model: str, audio: str):
|
31 |
audio_file = open(audio, "rb")
|
32 |
transcript = openai.Audio.transcribe(model, audio_file, language=LANGUAGE)
|
@@ -40,18 +24,8 @@ def gen_voice(response, response_filename):
|
|
40 |
reponse_audio = speech_synthesizer.speak_text_async(response['content']).get()
|
41 |
stream = speechsdk.AudioDataStream(reponse_audio)
|
42 |
stream.save_to_wav_file(response_filename)
|
43 |
-
|
44 |
-
def memory():
|
45 |
-
with open(DATA_FILE, "a") as csvfile:
|
46 |
-
for message in context:
|
47 |
-
writer = csv.DictWriter(csvfile, fieldnames=["name", "message"])
|
48 |
-
writer.writerow(
|
49 |
-
{"name": message['role'], "message": message['content']}
|
50 |
-
)
|
51 |
-
commit_url = repo.push_to_hub()
|
52 |
-
print(commit_url)
|
53 |
|
54 |
-
def respond(audio:str):
|
55 |
transcript = transcribe("whisper-1", audio)
|
56 |
context.append({"role": "user", "content": transcript['text']})
|
57 |
|
@@ -60,9 +34,10 @@ def respond(audio:str):
|
|
60 |
|
61 |
gen_voice(response, AUDIO_FILE_NAME)
|
62 |
|
|
|
|
|
|
|
63 |
transcript = ""
|
64 |
for m in context:
|
65 |
if m["role"] != "system":
|
66 |
-
transcript += m["role"] + " : " + m["content"] + "\n\n"
|
67 |
-
|
68 |
-
return AUDIO_FILE_NAME, transcript
|
|
|
2 |
import os
|
3 |
import azure.cognitiveservices.speech as speechsdk
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
LANGUAGE = "nl"
|
6 |
AUDIO_FILE_NAME = "audio_response.wav"
|
7 |
|
|
|
|
|
|
|
|
|
8 |
openai.api_key = os.environ['OPEN_AI_KEY']
|
9 |
|
10 |
speech_config = speechsdk.SpeechConfig(subscription=os.environ['AZURE_SPEECH_KEY'], region="westeurope")
|
11 |
speech_config.speech_synthesis_voice_name = "nl-NL-ColetteNeural"
|
12 |
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=None)
|
13 |
|
|
|
|
|
|
|
|
|
14 |
def transcribe(model: str, audio: str):
|
15 |
audio_file = open(audio, "rb")
|
16 |
transcript = openai.Audio.transcribe(model, audio_file, language=LANGUAGE)
|
|
|
24 |
reponse_audio = speech_synthesizer.speak_text_async(response['content']).get()
|
25 |
stream = speechsdk.AudioDataStream(reponse_audio)
|
26 |
stream.save_to_wav_file(response_filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
def respond(audio:str, context: list):
|
29 |
transcript = transcribe("whisper-1", audio)
|
30 |
context.append({"role": "user", "content": transcript['text']})
|
31 |
|
|
|
34 |
|
35 |
gen_voice(response, AUDIO_FILE_NAME)
|
36 |
|
37 |
+
return AUDIO_FILE_NAME
|
38 |
+
|
39 |
+
def transcript(context: list):
|
40 |
transcript = ""
|
41 |
for m in context:
|
42 |
if m["role"] != "system":
|
43 |
+
transcript += m["role"] + " : " + m["content"] + "\n\n"
|
|
|
|
gradio-ui.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import discourse as d
|
|
|
3 |
|
4 |
# set a custom theme
|
5 |
theme = gr.themes.Default().set(
|
@@ -20,7 +21,7 @@ with gr.Blocks(theme=theme) as ui:
|
|
20 |
with gr.Row():
|
21 |
btn2 = gr.Button("Save Conversation")
|
22 |
|
23 |
-
btn1.click(fn=d.respond, inputs=message, outputs=[audio_response, text_response])
|
24 |
-
btn2.click(fn=
|
25 |
|
26 |
ui.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import discourse as d
|
3 |
+
import memory as m
|
4 |
|
5 |
# set a custom theme
|
6 |
theme = gr.themes.Default().set(
|
|
|
21 |
with gr.Row():
|
22 |
btn2 = gr.Button("Save Conversation")
|
23 |
|
24 |
+
btn1.click(fn=d.respond, inputs=[message, m.context], outputs=[audio_response, text_response])
|
25 |
+
btn2.click(fn=m.save_as_hf_dataset)
|
26 |
|
27 |
ui.launch()
|
memory.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import csv
|
3 |
+
from huggingface_hub import Repository
|
4 |
+
|
5 |
+
DATASET_REPO_URL = "https://huggingface.co/datasets/FranklinWillemen/persistent-space-dataset"
|
6 |
+
DATA_FILENAME = "data.csv"
|
7 |
+
DATA_FILE = os.path.join("data", DATA_FILENAME)
|
8 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
9 |
+
|
10 |
+
repo = Repository(
|
11 |
+
local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN
|
12 |
+
)
|
13 |
+
|
14 |
+
context = [{"role": "system", "content": 'Je bent een slimme en behulpzame gesprekspartner. \
|
15 |
+
Antwoord beknopt en ter zake.\
|
16 |
+
Vermeld niet dat je een AI of een soort service bent.'}]
|
17 |
+
|
18 |
+
def save_as_hf_dataset():
|
19 |
+
with open(DATA_FILE, "a") as csvfile:
|
20 |
+
for message in context:
|
21 |
+
writer = csv.DictWriter(csvfile, fieldnames=["name", "message"])
|
22 |
+
writer.writerow(
|
23 |
+
{"name": message['role'], "message": message['content']}
|
24 |
+
)
|
25 |
+
commit_url = repo.push_to_hub()
|
26 |
+
print(commit_url)
|