Spaces:
Sleeping
Sleeping
File size: 7,976 Bytes
1134aca 4317956 ea4d973 1134aca e662bfb 382fe15 e662bfb 1c2302f 382fe15 42fbaea 1134aca d2c17e6 5025a32 5290c1b b0f85a9 47bb2fd 1134aca 804cf2d d2c17e6 bf250ef 42fed54 d3059eb b5f9b5e b0f85a9 b5f9b5e fe62808 5025a32 e474bf3 1134aca e662bfb 5025a32 1c2302f e662bfb 1c2302f e662bfb bbedb0f 42fbaea 5025a32 a012388 42fbaea 5025a32 bbedb0f 5025a32 e662bfb 5025a32 1c2302f 42fbaea e662bfb ab92640 b0f85a9 fcd10da c4238d0 fcd10da e662bfb 6ac714f e662bfb 6ac714f 03c8a9b 42fbaea 382fe15 b63f0df 5025a32 b63f0df 5025a32 b63f0df 5ae43ea 42fbaea de97b5b 42fbaea 382fe15 e474bf3 b63f0df 5025a32 42fbaea 5025a32 1134aca ce798fe 99d18e5 fa0fec7 e662bfb 5025a32 bcd2d76 e662bfb fa0fec7 fe62808 d3059eb 14de2b8 fe62808 b0f85a9 f7c42ce b0f85a9 fe62808 e662bfb 42fed54 5ae43ea 42fed54 bf250ef 4cc9a9f 42fed54 e474bf3 99d18e5 5025a32 e662bfb 5025a32 14de2b8 5025a32 aebffe2 e474bf3 bcd2d76 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | import gradio as gr
from openai import OpenAI
client = OpenAI()
import speech_recognition as sr
from pydub import AudioSegment
from gtts import gTTS
import io
import os
import tempfile
import time
Log_aan = True
Klant_profiel = True
Google_TTS_gebruiken = True
AI_taal_model = "gpt-3.5-turbo"
OpenAI.api_key = os.getenv("OPENAI_API_KEY")
if Log_aan:
print("Start programma")
# def change_TTS():
# global Google_TTS_gebruiken
# Google_TTS_gebruiken = not Google_TTS_gebruiken
# print("TTS gewisseld")
def toggle_google_openai(huidige_status):
nieuwe_status = "Google" if huidige_status == "Openai" else "Openai"
Google_TTS_gebruiken = True if nieuwe_status == "Google" else False
print("TTS door middel van ", nieuwe_status)
return nieuwe_status, nieuwe_status
def toggle_taal_model(huidige_status):
nieuwe_status = "gpt-3.5-turbo" if huidige_status == "gpt-4o" else "gpt-4o"
print("Gebruikt ChatGPT model:", nieuwe_status)
return nieuwe_status, nieuwe_status
def mentor_audio_2_text(audio_file):
recognizer = sr.Recognizer()
if audio_file is None:
return "Geen audio ontvangen.", "", None
audio = AudioSegment.from_file(audio_file, format="wav")
wav_io = io.BytesIO()
audio.export(wav_io, format="wav")
wav_io.seek(0)
with sr.AudioFile(wav_io) as source:
try:
audio_data = recognizer.record(source)
text = recognizer.recognize_google(audio_data, language="nl-NL")
if Log_aan:
print("Ingesproken rol van de ondernemer :", text)
return text
except Exception as e:
return "Fout bij spraakherkenning of verwerking.", str(e), None
def transcribe_and_respond(audio_file, klant_profiel):
if Log_aan:
print("functie TTS en AI antwoord gestart")
start = time.time()
recognizer = sr.Recognizer()
if audio_file is None:
return "Geen audio ontvangen.", "", None
audio = AudioSegment.from_file(audio_file, format="wav")
wav_io = io.BytesIO()
audio.export(wav_io, format="wav")
wav_io.seek(0)
with sr.AudioFile(wav_io) as source:
try:
audio_data = recognizer.record(source)
text = recognizer.recognize_google(audio_data, language="nl-NL")
if Log_aan:
print("Ingesproken tekst :", text)
except Exception as e:
return "Fout bij spraakherkenning of verwerking.", str(e), None
if Log_aan:
print(f"Audio to text is klaar in {time.time() - start:.2f} seconden")
try:
response = client.chat.completions.create(
model= AI_taal_model,
messages=[
{"role": "system", "content": f"Je speelt de rol van een MKB ondernemer en een potentiële klant in een professioneel gesprek. {klant_profiel}. Geef korte, duidelijke antwoorden van maximaal 2 zinnen. Sluit elk antwoord volledig af"},
{"role": "user", "content": text}
],
temperature=0.7,
max_tokens = 100,
stream=True
)
# ai_reply = response.choices[0].message.content
ai_reply = ""
for chunk in response:
if chunk.choices and chunk.choices[0].delta.content:
ai_reply += chunk.choices[0].delta.content
if Log_aan:
print("AI antwoordtekst :", ai_reply)
print(f"AI antwoord tekst is klaar in {time.time() - start:.2f} seconden")
except Exception as e:
return text, "Fout bij AI-chatgeneratie.", None
try:
bestand = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
if Google_TTS_gebruiken:
tts = gTTS(text=ai_reply, lang='nl')
tts.save(bestand.name)
else:
response = client.audio.speech.create(
model="tts-1",
voice="nova", # Of bijv. alloy, echo, fable
input=ai_reply
)
with open(bestand.name, "wb") as f:
for chunk in response.iter_bytes():
f.write(chunk)
# bestand = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
# response.stream_to_file(bestand.name)
# vertraagd_pad = slow_down_audio(bestand.name)
if Log_aan:
print(f"AI tekst to audio is klaar in {time.time() - start:.2f} seconden")
return text, ai_reply, bestand.name
except Exception as e:
return "Fout bij spraakgeneratie.", str(e), None
def slow_down_audio(input_file):
if Log_aan:
print("slow_douwn_audio functie gestart")
speed = 0.3 # 0.30 = 70% trager
sound = AudioSegment.from_file(input_file)
new_frame_rate = int(sound.frame_rate * speed)
slowed_sound = sound._spawn(sound.raw_data, overrides={
"frame_rate": new_frame_rate
}).set_frame_rate(sound.frame_rate) # om pitch gelijk te houden
output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
slowed_sound.export(output_file.name, format="mp3")
return output_file.name
def ai_evaluation():
print("AI evaluatie gestart")
with gr.Blocks(css="""
body {
font-family: 'Poppins', sans-serif;
background-color: #c7e5f3;
}
#logo {
width: 120px;
margin: 10px;
}
.instructions {
background-color: #008bce;
color: white;
padding: 12px;
border-radius: 5px;
font-size: 16px;
}
""") as demo:
if Log_aan:
with gr.Row():
with gr.Column(scale=1):
status = gr.State("Google") # startwaarde
output_vak = gr.Textbox(value="Google", label="Soort Text-2-Speech", interactive=False)
toggle_knop = gr.Button("Wissel tussen Google/Openai").click(fn=toggle_google_openai, inputs=status, outputs=[output_vak, status])
with gr.Column(scale=1):
status = gr.State("gpt-3.5-turbo") # startwaarde
output_vak = gr.Textbox(value=status, label="Soort Text-2-Speech", interactive=False)
toggle_knop = gr.Button("Wissel tussen gpt-3.5-turbo en gpt-4o").click(fn=toggle_taal_model, inputs=status, outputs=[output_vak, status])
with gr.Column(scale=1):
gr.Button("optie 3")
with gr.Column(scale=1):
gr.Button("optie 4")
with gr.Row():
with gr.Column(scale=2):
gr.HTML("<div class='instructions'><b>Definieer eerst je prospect en start daarna het gesprek.</b> De AI reageert automatisch.</div>")
with gr.Column(scale=1):
gr.Button("Verander Tekst2Speech techniek")
with gr.Column(scale=2, min_width=250):
gr.Image(value="thexton_logo_resized.png", elem_id="logo", show_label=False, show_download_button=False)
with gr.Row():
with gr.Column(scale=1):
klant_input = gr.Audio(label="🎤 Beschrijf je klant", type="filepath")
with gr.Column(scale=1):
klantbeschrijving = gr.Textbox(label="Zo heb je de klant gedefinieerd")
with gr.Column(scale=2):
gr.Button("Stop gesprek en start evaluatie").click(fn=ai_evaluation)
with gr.Row():
audio_input = gr.Audio(label="🎤 Spreek hier in", type="filepath")
transcript = gr.Textbox(label="Herkenning van jouw spraak")
ai_response = gr.Textbox(label="AI-reactie")
ai_uitgesproken = gr.Audio(label="🎧 AI-reactie uitgesproken", type="filepath")
# toggle_knop.click(fn=toggle_google_openai, inputs=status, outputs=[output_vak, status])
klant_input.change(fn=mentor_audio_2_text, inputs=[klant_input], outputs=[klantbeschrijving])
audio_input.change(fn=transcribe_and_respond, inputs=[audio_input, klantbeschrijving], outputs=[transcript, ai_response, ai_uitgesproken])
demo.launch() |