File size: 3,548 Bytes
f37379e 39fed75 424d21c 47e7313 39fed75 20a13a6 39fed75 f37379e dc6cc04 f37379e 943acb7 f37379e 430cba4 f37379e 430cba4 f37379e a337b22 f37379e f22c88d f37379e 430cba4 f37379e dade810 b7ec722 f37379e dc6cc04 5418c20 dc6cc04 943acb7 da44018 a337b22 dc6cc04 93d9045 dc6cc04 a337b22 dc6cc04 943acb7 dc6cc04 f37379e dc6cc04 |
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 |
import os
api_key = os.environ.get('api_key')
from openai import OpenAI
import gradio as gr
import codecs
import base64
import json
import soundfile as sf
from io import BytesIO
import numpy as np
def login(username, password):
users = json.loads(os.environ.get('users'))
return users[username] == password
print("Start")
client = OpenAI(api_key=api_key)
assistants = client.beta.assistants.list()
for a in assistants:
if "Patient Sim" in a.name:
client.beta.assistants.delete(a.id)
instruction = codecs.open("instruction.txt", "r", "utf-8").read()
#instruction = "You are helpful assistant. Keep your responses clear and concise."
model = "gpt-4o"
#model = "gpt-3.5-turbo"
assistant = client.beta.assistants.create(name="Patient Sim", instructions=instruction, model=model)
toggle_js = open("toggle_speech.js").read()
play_js = open("play.js").read()
def start_thread():
print("start_thread")
return client.beta.threads.create()
def user(text, audio, history, thread):
if audio:
text = transcribe(audio)
print("User:", text)
message = client.beta.threads.messages.create(thread_id=thread.id, role="user", content=text)
return "", history + [[text, None]]
def bot(history, thread):
stream = client.beta.threads.runs.create(thread_id=thread.id, assistant_id=assistant.id, stream=True)
history[-1][1] = ""
for event in stream:
try:
delta = event.data.delta.content[0].text.value
history[-1][1] += delta
yield history
except: pass
def transcribe(file):
print(f"Transcribe: {file}")
file = open(file, "rb")
response = client.audio.transcriptions.create(
file=file,
model="whisper-1",
language="en",
response_format="text"
)
return response
def speak(history):
text = history[-1][1]
print("Assistant:", text)
speech = client.audio.speech.create(model="tts-1", voice="alloy", input=text)
data, sr = sf.read(BytesIO(speech.read()), dtype='int16')
return sr, data
def vote(data: gr.LikeData):
if data.liked:
print("You upvoted this response: " + data.value)
else:
print("You downvoted this response: " + data.value)
css = """
.container {height: 100vh;}
#title_markdown {height: 10%;}
#chatbot {height: 60%;}
#speech {height: 10%;}
#textbox {height: 60%;}
footer{display:none !important}
"""
def main():
with gr.Blocks(title="Paitient Sim", css=css) as demo:
with gr.Column(elem_classes=["container"]):
gr.Markdown("""
# Patient Sim
Say goodbye to end the session and receive a feedback.
""", elem_id="title_markdown")
thread = gr.State(start_thread)
chatbot = gr.Chatbot(label="Messages", elem_id="chatbot")
chatbot.like(vote, None, None)
speech = gr.Button("Record", size="sm", elem_id="speech")
speech.click(None, js=toggle_js)
msg = gr.Textbox(label="Say something.", elem_id="textbox")
mic = gr.Microphone(type="filepath", format="mp3", editable=False, waveform_options={"show_controls": False}, visible=False, elem_id="recorder")
player = gr.Audio(editable=False,elem_id="player", visible=False)
msg.submit(user, [msg, mic, chatbot, thread], [msg, chatbot]).then(
bot, [chatbot, thread], chatbot).then(
speak, chatbot, player
)
mic.stop_recording(user, [msg, mic, chatbot, thread], [msg, chatbot]).then(
lambda:None, None, mic).then(
bot, [chatbot, thread], chatbot).then(
speak, chatbot, player, js=play_js
)
demo.queue()
demo.launch(auth=login)
if __name__ == "__main__":
main()
|