Download app.py from jibay/test: direct link, hf CLI and curl.
- Browser
- Download file 3.2 kB
-
https://huggingface.co/spaces/jibay/test/resolve/main/app.py
- Command line
-
hf download hf://spaces/jibay/test/app.py
-
curl -L -o app.py https://huggingface.co/spaces/jibay/test/resolve/main/app.py
3.2 kB
| import audioop | |
| import base64 | |
| import json | |
| import os | |
| from flask import Flask, request | |
| from flask_sock import Sock, ConnectionClosed | |
| from twilio.twiml.voice_response import VoiceResponse, Start, Stop, Gather | |
| from twilio.rest import Client | |
| import vosk | |
| import openai | |
| app = Flask(__name__) | |
| sock = Sock(app) | |
| twilio_client = Client() | |
| #model = vosk.Model('/tmp/model/vosk-model-small-fr-0.22') | |
| model = vosk.Model('/tmp/model/vosk-model-fr-0.22') | |
| CL = '\x1b[0K' | |
| BS = '\x08' | |
| openai.api_key = os.environ.get("OPEN_API_KEY") | |
| messages = [ {"role": "system", "content": | |
| "You are a intelligent assistant."} ] | |
| def call(): | |
| """Accept a phone call.""" | |
| response = VoiceResponse() | |
| start = Start() | |
| start.stream(url=f'wss://jibay-test.hf.space/stream') | |
| response.append(start) | |
| gather = Gather(input='speech', action='/completed') | |
| gather.say('Bonjour je suis là pour vous aider. Quel est votre problème?', language='fr-FR') | |
| response.append(gather) | |
| #response.say('Bonjour je suis là pour vous aider. Quel est votre problème?', language='fr-FR') | |
| #response.pause(length=60) | |
| print(f'Incoming call from {request.form["From"]}') | |
| return str(response), 200, {'Content-Type': 'text/xml'} | |
| def stream(ws): | |
| """Receive and transcribe audio stream.""" | |
| rec = vosk.KaldiRecognizer(model, 16000) | |
| while True: | |
| message = ws.receive() | |
| packet = json.loads(message) | |
| if packet['event'] == 'start': | |
| print('Streaming is starting') | |
| elif packet['event'] == 'stop': | |
| print('\nStreaming has stopped') | |
| elif packet['event'] == 'media': | |
| audio = base64.b64decode(packet['media']['payload']) | |
| audio = audioop.ulaw2lin(audio, 2) | |
| audio = audioop.ratecv(audio, 2, 1, 8000, 16000, None)[0] | |
| if rec.AcceptWaveform(audio): | |
| r = json.loads(rec.Result()) | |
| #print(CL + r['text'] + ' ', end='', flush=True) | |
| print('FINAL:'+r['text'] + '\n ', end='', flush=True) | |
| messageChatGpt = "peux-tu me dire s'il faut appeler les secours, quelqu'un m'a ecrit ceci: \""+r['text']+"\ ?" | |
| messages.append( | |
| {"role": "user", "content": messageChatGpt} | |
| ) | |
| chat = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", messages=messages | |
| ) | |
| reply = chat.choices[0].message.content | |
| print(reply) | |
| else: | |
| r = json.loads(rec.PartialResult()) | |
| #print('PARTIAL:' + r['partial']+'\n', end='', flush=True) | |
| #print(CL + r['partial'] + BS * len(r['partial']), end='', flush=True) | |
| #print(CL + r['partial'] + BS * len(r['partial']), end='', flush=True) | |
| if __name__ == '__main__': | |
| print("here") | |
| port = 7860 | |
| public_url = "https://jibay-test.hf.space/call" | |
| number = twilio_client.incoming_phone_numbers.list()[1] | |
| number.update(voice_url=public_url) | |
| print(f'Waiting for calls on {number.phone_number}') | |
| app.run(port=port, debug=True) |