Freddy Boulton commited on
Commit
a72ef71
·
1 Parent(s): 382bb64
Files changed (2) hide show
  1. app.py +61 -18
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,33 +1,55 @@
1
- import httpx
2
-
3
  import gradio as gr
 
4
  from gradio_dialogue import Dialogue
5
 
6
- emotions = ["(laughs)", "(clears throat)", "(sighs)", "(gasps)", "(coughs)", "(singing)", "(sings)", "(mumbles)", "(beep)", "(groans)", "(sniffs)", "(claps)", "(screams)", "(inhales)", "(exhales)", "(applause)", "(burps)", "(humming)", "(sneezes)", "(chuckle)", "(whistles)"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  speakers = ["Speaker 1", "Speaker 2"]
8
 
9
- client = httpx.AsyncClient(timeout=180)
10
 
11
 
12
  async def query(dialogue: str, token: gr.OAuthToken | None):
13
  if token is None:
14
- raise gr.Error("No token provided. Use Sign in with Hugging Face to get a token.")
 
 
15
  API_URL = "https://router.huggingface.co/fal-ai/fal-ai/dia-tts"
16
  headers = {
17
  "Authorization": f"Bearer {token.token}",
18
  }
19
- print("Dialogue: ", dialogue)
20
- dialogue = dialogue.replace("\n", " ")
21
- print("Dialogue After: ", dialogue)
22
  response = await client.post(API_URL, headers=headers, json={"text": dialogue})
23
  url = response.json()["audio"]["url"]
24
  print("URL: ", url)
25
  return url
26
 
 
27
  def formatter(speaker, text):
28
  speaker = speaker.split(" ")[1]
29
  return f"[S{speaker}]: {text}"
30
 
 
31
  with gr.Blocks() as demo:
32
  with gr.Sidebar():
33
  login_button = gr.LoginButton()
@@ -42,23 +64,44 @@ with gr.Blocks() as demo:
42
  )
43
  with gr.Row():
44
  with gr.Column():
45
- dialogue = Dialogue(speakers=speakers, emotions=emotions,
46
- formatter=formatter)
 
47
  with gr.Column():
48
  with gr.Row():
49
  audio = gr.Audio(label="Audio")
50
  with gr.Row():
51
  gr.DeepLinkButton(value="Share Audio via Link")
52
  with gr.Row():
53
- gr.Examples(examples=[
54
- [[{"speaker": "Speaker 1", "text": "Why did the chicken cross the road?"},
55
- {"speaker": "Speaker 2", "text": "I don't know!"},
56
- {"speaker": "Speaker 1", "text": "to get to the other side! (laughs)"}]],
57
- [[{"speaker": "Speaker 1", "text": "(sighs) I am a little tired today."},
58
- {"speaker": "Speaker 2", "text": "Hang in there!"},
59
- ]]], inputs=[dialogue], cache_examples=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  dialogue.submit(query, [dialogue], audio)
62
 
63
  demo.launch(ssr_mode=False)
64
-
 
 
 
1
  import gradio as gr
2
+ import httpx
3
  from gradio_dialogue import Dialogue
4
 
5
+ emotions = [
6
+ "(laughs)",
7
+ "(clears throat)",
8
+ "(sighs)",
9
+ "(gasps)",
10
+ "(coughs)",
11
+ "(singing)",
12
+ "(sings)",
13
+ "(mumbles)",
14
+ "(beep)",
15
+ "(groans)",
16
+ "(sniffs)",
17
+ "(claps)",
18
+ "(screams)",
19
+ "(inhales)",
20
+ "(exhales)",
21
+ "(applause)",
22
+ "(burps)",
23
+ "(humming)",
24
+ "(sneezes)",
25
+ "(chuckle)",
26
+ "(whistles)",
27
+ ]
28
  speakers = ["Speaker 1", "Speaker 2"]
29
 
30
+ client = httpx.AsyncClient(timeout=180)
31
 
32
 
33
  async def query(dialogue: str, token: gr.OAuthToken | None):
34
  if token is None:
35
+ raise gr.Error(
36
+ "No token provided. Use Sign in with Hugging Face to get a token."
37
+ )
38
  API_URL = "https://router.huggingface.co/fal-ai/fal-ai/dia-tts"
39
  headers = {
40
  "Authorization": f"Bearer {token.token}",
41
  }
 
 
 
42
  response = await client.post(API_URL, headers=headers, json={"text": dialogue})
43
  url = response.json()["audio"]["url"]
44
  print("URL: ", url)
45
  return url
46
 
47
+
48
  def formatter(speaker, text):
49
  speaker = speaker.split(" ")[1]
50
  return f"[S{speaker}]: {text}"
51
 
52
+
53
  with gr.Blocks() as demo:
54
  with gr.Sidebar():
55
  login_button = gr.LoginButton()
 
64
  )
65
  with gr.Row():
66
  with gr.Column():
67
+ dialogue = Dialogue(
68
+ speakers=speakers, emotions=emotions, formatter=formatter
69
+ )
70
  with gr.Column():
71
  with gr.Row():
72
  audio = gr.Audio(label="Audio")
73
  with gr.Row():
74
  gr.DeepLinkButton(value="Share Audio via Link")
75
  with gr.Row():
76
+ gr.Examples(
77
+ examples=[
78
+ [
79
+ [
80
+ {
81
+ "speaker": "Speaker 1",
82
+ "text": "Why did the chicken cross the road?",
83
+ },
84
+ {"speaker": "Speaker 2", "text": "I don't know!"},
85
+ {
86
+ "speaker": "Speaker 1",
87
+ "text": "to get to the other side! (laughs)",
88
+ },
89
+ ]
90
+ ],
91
+ [
92
+ [
93
+ {
94
+ "speaker": "Speaker 1",
95
+ "text": "(sighs) I am a little tired today.",
96
+ },
97
+ {"speaker": "Speaker 2", "text": "Hang in there!"},
98
+ ]
99
+ ],
100
+ ],
101
+ inputs=[dialogue],
102
+ cache_examples=False,
103
+ )
104
 
105
  dialogue.submit(query, [dialogue], audio)
106
 
107
  demo.launch(ssr_mode=False)
 
requirements.txt CHANGED
@@ -1 +1 @@
1
- gradio-dialogue>=0.0.3
 
1
+ gradio-dialogue>=0.0.4