Sandra1333 commited on
Commit
fa0fec7
·
verified ·
1 Parent(s): cea78f7

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +59 -56
  2. 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 functie
12
- def transcribe_audio(audio_file):
13
  recognizer = sr.Recognizer()
14
- with sr.AudioFile(audio_file) as source:
15
- audio = recognizer.record(source)
16
- try:
17
- text = recognizer.recognize_google(audio, language="nl-NL")
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
- # Genereer AI-reactie
25
- def gesprek_met_ai(audio_file):
26
- try:
27
- # Zet audio om naar WAV indien nodig
28
- if isinstance(audio_file, tuple):
29
- audio_path = audio_file[0]
30
- else:
31
- audio_path = audio_file
 
 
32
 
33
- if audio_path.endswith(".mp3"):
34
- sound = AudioSegment.from_mp3(audio_path)
35
- temp_wav = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
36
- sound.export(temp_wav.name, format="wav")
37
- audio_path = temp_wav.name
 
38
 
39
- tekst = transcribe_audio(audio_path)
 
 
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: " + str(e)
53
 
54
- # Styling
55
- css = """
56
- body { font-family: 'Poppins', sans-serif; }
57
- h1, h2, h3, h4, h5, h6, label { font-family: 'Poppins', sans-serif; }
58
- .gradio-container { background-color: #c7e5f3; }
59
- #logo-container { text-align: center; margin-bottom: 10px; }
60
- #instructie { background-color: #008bce; color: white; padding: 20px; border-radius: 8px; }
61
- .outputbox { background-color: #ffffff; padding: 10px; border-radius: 5px; }
62
- """
63
-
64
- with gr.Blocks(css=css) as demo:
65
- with gr.Column():
66
- gr.HTML('<div id="logo-container"><img src="file/logo.png" style="height:80px;" /></div>')
67
- gr.Markdown('<div id="instructie"><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>')
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- transcriptie = gr.Textbox(label="Herkenning van jouw spraak")
74
- reactie = gr.Textbox(label="AI-reactie")
 
 
75
 
76
- audioblok.change(fn=gesprek_met_ai, inputs=audioblok, outputs=[transcriptie, reactie])
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