Steveeeeeeen HF staff commited on
Commit
2cdcbd0
·
verified ·
1 Parent(s): 77dbc9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -20
app.py CHANGED
@@ -21,9 +21,7 @@ auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
21
 
22
  if account_sid and auth_token:
23
  client = Client(account_sid, auth_token)
24
-
25
  token = client.tokens.create()
26
-
27
  rtc_configuration = {
28
  "iceServers": token.ice_servers,
29
  "iceTransportPolicy": "relay",
@@ -32,11 +30,7 @@ else:
32
  rtc_configuration = None
33
 
34
 
35
- def transcribe(
36
- audio: tuple[int, np.ndarray],
37
- transformers_chat: list[dict],
38
- conversation: list[dict],
39
- ):
40
  original_sr = audio[0]
41
  target_sr = 16000
42
 
@@ -46,7 +40,7 @@ def transcribe(
46
 
47
  tf_input = [d for d in transformers_chat]
48
 
49
- # Generate response from the pipeline using the audio input
50
  output = pipe(
51
  {"audio": audio_sr, "turns": tf_input, "sampling_rate": target_sr},
52
  max_new_tokens=512,
@@ -63,21 +57,15 @@ def transcribe(
63
  yield AdditionalOutputs(transformers_chat, conversation)
64
 
65
 
66
- def respond_text(
67
- user_text: str,
68
- transformers_chat: list[dict],
69
- conversation: list[dict],
70
- ):
71
  if not user_text.strip():
72
- # Do nothing if the textbox is empty
73
  return transformers_chat, conversation
74
 
75
  # Append the user message from the textbox
76
  conversation.append({"role": "user", "content": user_text})
77
  transformers_chat.append({"role": "user", "content": user_text})
78
 
79
- # Generate a response using the pipeline.
80
- # Here we assume the pipeline can also process text input via the "text" key.
81
  output = pipe({"text": user_text, "turns": transformers_chat}, max_new_tokens=512)
82
 
83
  conversation.append({"role": "assistant", "content": output})
@@ -100,6 +88,7 @@ with gr.Blocks() as demo:
100
  </p>
101
  """
102
  )
 
103
  # Shared conversation state
104
  transformers_chat = gr.State(
105
  value=[
@@ -110,9 +99,12 @@ with gr.Blocks() as demo:
110
  ]
111
  )
112
 
 
 
 
 
113
  with gr.Row():
114
  with gr.Column(scale=1):
115
- transcript = gr.Chatbot(label="Transcript", type="messages")
116
  text_input = gr.Textbox(
117
  placeholder="Type your message here...", label="Your Message"
118
  )
@@ -125,7 +117,7 @@ with gr.Blocks() as demo:
125
  modality="audio",
126
  )
127
 
128
- # Audio stream: when you stop speaking, process the audio input.
129
  audio.stream(
130
  ReplyOnPause(transcribe),
131
  inputs=[audio, transformers_chat, transcript],
@@ -139,13 +131,13 @@ with gr.Blocks() as demo:
139
  show_progress="hidden",
140
  )
141
 
142
- # Text input: when you click "Send", process the typed message.
143
  send_button.click(
144
  respond_text,
145
  inputs=[text_input, transformers_chat, transcript],
146
  outputs=[transformers_chat, transcript],
147
  )
148
- # Optionally clear the text box after sending:
149
  send_button.click(lambda: "", inputs=[], outputs=[text_input])
150
 
151
  if __name__ == "__main__":
 
21
 
22
  if account_sid and auth_token:
23
  client = Client(account_sid, auth_token)
 
24
  token = client.tokens.create()
 
25
  rtc_configuration = {
26
  "iceServers": token.ice_servers,
27
  "iceTransportPolicy": "relay",
 
30
  rtc_configuration = None
31
 
32
 
33
+ def transcribe(audio: tuple[int, np.ndarray], transformers_chat: list[dict], conversation: list[dict]):
 
 
 
 
34
  original_sr = audio[0]
35
  target_sr = 16000
36
 
 
40
 
41
  tf_input = [d for d in transformers_chat]
42
 
43
+ # Generate a response from the pipeline using the audio input
44
  output = pipe(
45
  {"audio": audio_sr, "turns": tf_input, "sampling_rate": target_sr},
46
  max_new_tokens=512,
 
57
  yield AdditionalOutputs(transformers_chat, conversation)
58
 
59
 
60
+ def respond_text(user_text: str, transformers_chat: list[dict], conversation: list[dict]):
 
 
 
 
61
  if not user_text.strip():
 
62
  return transformers_chat, conversation
63
 
64
  # Append the user message from the textbox
65
  conversation.append({"role": "user", "content": user_text})
66
  transformers_chat.append({"role": "user", "content": user_text})
67
 
68
+ # Generate a response using the pipeline. We assume it can process text input via "text"
 
69
  output = pipe({"text": user_text, "turns": transformers_chat}, max_new_tokens=512)
70
 
71
  conversation.append({"role": "assistant", "content": output})
 
88
  </p>
89
  """
90
  )
91
+
92
  # Shared conversation state
93
  transformers_chat = gr.State(
94
  value=[
 
99
  ]
100
  )
101
 
102
+ # Chat transcript on top
103
+ transcript = gr.Chatbot(label="Transcript", type="messages")
104
+
105
+ # Lower row with text input and audio input side by side
106
  with gr.Row():
107
  with gr.Column(scale=1):
 
108
  text_input = gr.Textbox(
109
  placeholder="Type your message here...", label="Your Message"
110
  )
 
117
  modality="audio",
118
  )
119
 
120
+ # Audio stream: process audio when speaking stops.
121
  audio.stream(
122
  ReplyOnPause(transcribe),
123
  inputs=[audio, transformers_chat, transcript],
 
131
  show_progress="hidden",
132
  )
133
 
134
+ # Text input: process the typed message when "Send" is clicked.
135
  send_button.click(
136
  respond_text,
137
  inputs=[text_input, transformers_chat, transcript],
138
  outputs=[transformers_chat, transcript],
139
  )
140
+ # Clear text input after sending.
141
  send_button.click(lambda: "", inputs=[], outputs=[text_input])
142
 
143
  if __name__ == "__main__":