Upload folder using huggingface_hub
Browse files
chat.py
CHANGED
@@ -4,14 +4,8 @@ from openai import OpenAI
|
|
4 |
import gradio as gr
|
5 |
import codecs
|
6 |
import base64
|
7 |
-
import json
|
8 |
-
|
9 |
-
def login(username, password):
|
10 |
-
users = json.loads(os.environ.get('users'))
|
11 |
-
return users[username] == password
|
12 |
|
13 |
print("Start")
|
14 |
-
|
15 |
client = OpenAI(api_key=api_key)
|
16 |
assistants = client.beta.assistants.list()
|
17 |
for a in assistants:
|
@@ -21,19 +15,16 @@ for a in assistants:
|
|
21 |
instruction = codecs.open("instruction.txt", "r", "utf-8").read()
|
22 |
#instruction = "You are helpful assistant. Keep your responses clear and concise."
|
23 |
model = "gpt-4o"
|
24 |
-
|
25 |
assistant = client.beta.assistants.create(name="Patient Sim", instructions=instruction, model=model)
|
26 |
toggle_js = open("toggle_speech.js").read()
|
27 |
-
play_js = open("play.js").read()
|
28 |
|
29 |
def start_thread():
|
30 |
-
print("start_thread")
|
31 |
return client.beta.threads.create()
|
32 |
|
33 |
def user(text, audio, history, thread):
|
34 |
if audio:
|
35 |
text = transcribe(audio)
|
36 |
-
print("User:", text)
|
37 |
message = client.beta.threads.messages.create(thread_id=thread.id, role="user", content=text)
|
38 |
return "", history + [[text, None]]
|
39 |
|
@@ -48,7 +39,6 @@ def bot(history, thread):
|
|
48 |
except: pass
|
49 |
|
50 |
def transcribe(file):
|
51 |
-
print(f"Transcribe: {file}")
|
52 |
file = open(file, "rb")
|
53 |
response = client.audio.transcriptions.create(
|
54 |
file=file,
|
@@ -59,14 +49,12 @@ def transcribe(file):
|
|
59 |
return response
|
60 |
|
61 |
def speak(history):
|
62 |
-
print(history)
|
63 |
text = history[-1][1]
|
64 |
-
|
65 |
-
|
66 |
-
audio =
|
67 |
-
audio_element
|
68 |
-
|
69 |
-
|
70 |
def vote(data: gr.LikeData):
|
71 |
if data.liked:
|
72 |
print("You upvoted this response: " + data.value)
|
@@ -82,34 +70,31 @@ css = """
|
|
82 |
footer{display:none !important}
|
83 |
"""
|
84 |
|
85 |
-
|
86 |
-
with gr.
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
)
|
109 |
-
|
110 |
-
demo.queue()
|
111 |
-
demo.launch(auth=login)
|
112 |
|
113 |
-
|
114 |
-
|
115 |
|
|
|
4 |
import gradio as gr
|
5 |
import codecs
|
6 |
import base64
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
print("Start")
|
|
|
9 |
client = OpenAI(api_key=api_key)
|
10 |
assistants = client.beta.assistants.list()
|
11 |
for a in assistants:
|
|
|
15 |
instruction = codecs.open("instruction.txt", "r", "utf-8").read()
|
16 |
#instruction = "You are helpful assistant. Keep your responses clear and concise."
|
17 |
model = "gpt-4o"
|
18 |
+
model = "gpt-3.5-turbo"
|
19 |
assistant = client.beta.assistants.create(name="Patient Sim", instructions=instruction, model=model)
|
20 |
toggle_js = open("toggle_speech.js").read()
|
|
|
21 |
|
22 |
def start_thread():
|
|
|
23 |
return client.beta.threads.create()
|
24 |
|
25 |
def user(text, audio, history, thread):
|
26 |
if audio:
|
27 |
text = transcribe(audio)
|
|
|
28 |
message = client.beta.threads.messages.create(thread_id=thread.id, role="user", content=text)
|
29 |
return "", history + [[text, None]]
|
30 |
|
|
|
39 |
except: pass
|
40 |
|
41 |
def transcribe(file):
|
|
|
42 |
file = open(file, "rb")
|
43 |
response = client.audio.transcriptions.create(
|
44 |
file=file,
|
|
|
49 |
return response
|
50 |
|
51 |
def speak(history):
|
|
|
52 |
text = history[-1][1]
|
53 |
+
speech = client.audio.speech.create(model="tts-1", voice="alloy", input=text)
|
54 |
+
audio = base64.b64encode(speech.read()).decode("utf-8")
|
55 |
+
audio_element = f'<audio src="data:audio/mpeg;base64,{audio}" autoplay></audio>'
|
56 |
+
return audio_element
|
57 |
+
|
|
|
58 |
def vote(data: gr.LikeData):
|
59 |
if data.liked:
|
60 |
print("You upvoted this response: " + data.value)
|
|
|
70 |
footer{display:none !important}
|
71 |
"""
|
72 |
|
73 |
+
with gr.Blocks(title="Paitient Sim", css=css) as demo:
|
74 |
+
with gr.Column(elem_classes=["container"]):
|
75 |
+
gr.Markdown("""
|
76 |
+
# Patient Sim
|
77 |
+
Say goodbye to end the session and receive a feedback.
|
78 |
+
""", elem_id="title_markdown")
|
79 |
+
chatbot = gr.Chatbot(label="Messages", elem_id="chatbot")
|
80 |
+
chatbot.like(vote, None, None)
|
81 |
+
speech = gr.Button("Record", size="sm", elem_id="speech")
|
82 |
+
speech.click(None, js=toggle_js)
|
83 |
+
msg = gr.Textbox(label="Say something.", elem_id="textbox")
|
84 |
+
mic = gr.Microphone(type="filepath", format="mp3", editable=False, waveform_options={"show_controls": False}, visible=False)
|
85 |
+
thread = gr.State(start_thread)
|
86 |
+
audio_html = gr.HTML()
|
87 |
+
audio_html.visible = False
|
88 |
+
msg.submit(user, [msg, mic, chatbot, thread], [msg, chatbot], queue=False).then(
|
89 |
+
bot, [chatbot, thread], chatbot
|
90 |
+
).then(
|
91 |
+
speak, chatbot, audio_html
|
92 |
+
)
|
93 |
+
mic.stop_recording(user, [msg, mic, chatbot, thread], [msg, chatbot], queue=False).then(
|
94 |
+
lambda:None, None, mic, queue=False).then(
|
95 |
+
bot, [chatbot, thread], chatbot).then(
|
96 |
+
speak, chatbot, audio_html)
|
|
|
|
|
|
|
97 |
|
98 |
+
demo.queue()
|
99 |
+
demo.launch(server_name="0.0.0.0", share=True)
|
100 |
|