Spaces:
Sleeping
Sleeping
Download app.py from ta1333/Trusted_Advisor: direct link, hf CLI and curl.
- Browser
- Download file 7.98 kB
-
https://huggingface.co/spaces/ta1333/Trusted_Advisor/resolve/main/app.py
- Command line
-
hf download hf://spaces/ta1333/Trusted_Advisor/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ta1333/Trusted_Advisor/resolve/main/app.py
7.98 kB
| 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() |