Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +59 -56
- thexton_logo_resized.png +0 -0
app.py
CHANGED
|
@@ -3,76 +3,79 @@ import gradio as gr
|
|
| 3 |
import openai
|
| 4 |
import speech_recognition as sr
|
| 5 |
import tempfile
|
| 6 |
-
from pydub import AudioSegment
|
| 7 |
import os
|
| 8 |
|
|
|
|
| 9 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
|
| 11 |
-
# Spraak naar tekst
|
| 12 |
-
def
|
| 13 |
recognizer = sr.Recognizer()
|
| 14 |
-
with sr.AudioFile(
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
return text
|
| 19 |
-
except sr.UnknownValueError:
|
| 20 |
-
return "Sorry, ik kon je spraak niet begrijpen."
|
| 21 |
-
except sr.RequestError:
|
| 22 |
-
return "Er is een probleem met de spraakservice."
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
def
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
response = openai.ChatCompletion.create(
|
| 42 |
-
model="gpt-3.5-turbo",
|
| 43 |
-
messages=[
|
| 44 |
-
{"role": "system", "content": "Je bent een ervaren Trusted Advisor trainer. Geef feedback en tips op het antwoord vanuit het perspectief van de Trusted Advisor-principes."},
|
| 45 |
-
{"role": "user", "content": tekst},
|
| 46 |
-
],
|
| 47 |
-
temperature=0.7,
|
| 48 |
-
max_tokens=300,
|
| 49 |
-
)
|
| 50 |
-
return tekst, response.choices[0].message.content
|
| 51 |
except Exception as e:
|
| 52 |
-
return "Fout bij spraakherkenning of verwerking.", "Fout bij AI-aanroep:
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
body {
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
-
audioblok = gr.Audio(label="Spreek in via microfoon of upload een bestand", type="filepath")
|
| 71 |
-
|
| 72 |
with gr.Column():
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
|
| 78 |
demo.launch()
|
|
|
|
| 3 |
import openai
|
| 4 |
import speech_recognition as sr
|
| 5 |
import tempfile
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
# API-key ophalen uit de omgeving
|
| 9 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
|
| 11 |
+
# Spraak naar tekst
|
| 12 |
+
def transcribe(audio):
|
| 13 |
recognizer = sr.Recognizer()
|
| 14 |
+
with sr.AudioFile(audio) as source:
|
| 15 |
+
audio_data = recognizer.record(source)
|
| 16 |
+
text = recognizer.recognize_google(audio_data, language="nl-NL")
|
| 17 |
+
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# AI-antwoord genereren
|
| 20 |
+
def get_ai_response(prompt):
|
| 21 |
+
response = openai.ChatCompletion.create(
|
| 22 |
+
model="gpt-3.5-turbo",
|
| 23 |
+
messages=[
|
| 24 |
+
{"role": "system", "content": "Je bent een AI-gesprekscoach die werkt volgens de Trusted Advisor-principes."},
|
| 25 |
+
{"role": "user", "content": prompt}
|
| 26 |
+
]
|
| 27 |
+
)
|
| 28 |
+
return response.choices[0].message.content.strip()
|
| 29 |
|
| 30 |
+
# Hoofdfunctie
|
| 31 |
+
def gesprek_met_ai(audio):
|
| 32 |
+
try:
|
| 33 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
|
| 34 |
+
tmp.write(audio.read())
|
| 35 |
+
tmp_path = tmp.name
|
| 36 |
|
| 37 |
+
transcript = transcribe(tmp_path)
|
| 38 |
+
ai_response = get_ai_response(transcript)
|
| 39 |
+
return transcript, ai_response
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
+
return "Fout bij spraakherkenning of verwerking.", f"Fout bij AI-aanroep: {str(e)}"
|
| 43 |
|
| 44 |
+
# Interface opbouw
|
| 45 |
+
with gr.Blocks(css="""
|
| 46 |
+
body {
|
| 47 |
+
font-family: 'Poppins', sans-serif;
|
| 48 |
+
background-color: #c7e5f3;
|
| 49 |
+
}
|
| 50 |
+
.instruction-box {
|
| 51 |
+
background-color: #008bce;
|
| 52 |
+
color: white;
|
| 53 |
+
font-size: 16px;
|
| 54 |
+
padding: 16px;
|
| 55 |
+
margin-bottom: 10px;
|
| 56 |
+
border-radius: 8px;
|
| 57 |
+
font-weight: 400;
|
| 58 |
+
}
|
| 59 |
+
.gradio-container {
|
| 60 |
+
max-width: 1000px !important;
|
| 61 |
+
margin: auto;
|
| 62 |
+
}
|
| 63 |
+
h1, h2 {
|
| 64 |
+
font-family: 'Poppins', sans-serif;
|
| 65 |
+
color: #002b49;
|
| 66 |
+
}
|
| 67 |
+
""") as demo:
|
| 68 |
+
gr.HTML("<link href='https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap' rel='stylesheet'>")
|
| 69 |
+
gr.Image(value="file/thexton_logo_resized.png", width=200, show_label=False, container=False)
|
| 70 |
+
gr.Markdown("""<div class='instruction-box'><strong>Begin het gesprek door op de microfoon te klikken.</strong> Vertel kort iets over je klant, zodat de AI weet met wie je in gesprek gaat.</div>""")
|
| 71 |
|
| 72 |
with gr.Row():
|
|
|
|
|
|
|
| 73 |
with gr.Column():
|
| 74 |
+
audio_input = gr.Audio(source="upload", type="filepath", label="Spreek in via microfoon of upload een bestand")
|
| 75 |
+
with gr.Column():
|
| 76 |
+
transcript_output = gr.Textbox(label="Herkenning van jouw spraak")
|
| 77 |
+
ai_output = gr.Textbox(label="AI-reactie")
|
| 78 |
|
| 79 |
+
audio_input.change(fn=gesprek_met_ai, inputs=audio_input, outputs=[transcript_output, ai_output])
|
| 80 |
|
| 81 |
demo.launch()
|
thexton_logo_resized.png
ADDED
|