KarthikaRajagopal commited on
Commit
9af4092
·
verified ·
1 Parent(s): 5f0bcb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -55
app.py CHANGED
@@ -1,64 +1,78 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
 
1
  import gradio as gr
2
+ import edge_tts
3
+ import asyncio
4
+ import tempfile
5
+ import os
6
 
7
+ # Get all available voices
8
+ async def get_voices():
9
+ voices = await edge_tts.list_voices()
10
+ return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
11
 
12
+ #text to speech fun
13
+ async def text_to_speech(text, voice, rate, pitch):
14
+ text = text.strip()
15
+ if not text:
16
+ return None, gr.Warning("Please enter text to convert.")
17
+ if not voice:
18
+ return None, gr.Warning("Please select a voice.")
19
+
20
+ voice_short_name = voice.split(" - ")[0]
21
+ rate_str = f"{rate:+d}%"
22
+ pitch_str = f"{pitch:+d}Hz"
23
+
24
+ communicate = edge_tts.Communicate(
25
+ text=text,
26
+ voice=voice_short_name,
27
+ rate=rate_str,
28
+ pitch=pitch_str
29
+ )
30
+
31
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
32
+ await communicate.save(tmp_file.name)
33
+ return tmp_file.name, None
34
 
 
 
 
 
 
 
 
 
 
35
 
36
+ # Gradio interface function
37
+ def tts_interface(text, voice, rate, pitch):
38
+ audio, warning = asyncio.run(text_to_speech(text, voice, rate, pitch))
39
+ return audio, warning
 
40
 
41
+ # Create Gradio application
42
+ import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
+ async def create_demo():
45
+ voices = await get_voices()
46
+
47
+ description = """
48
+ Convert text to speech using Microsoft Edge TTS. Adjust speech rate and pitch: 0 is default, positive values increase, negative values decrease.
49
+
50
+
51
+
52
+
53
+ """
54
+
55
+ demo = gr.Interface(
56
+ fn=tts_interface,
57
+ inputs=[
58
+ gr.Textbox(label="Input Text", lines=5),
59
+ gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value=""),
60
+ gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Rate Adjustment (%)", step=1),
61
+ gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1)
62
+ ],
63
+ outputs=[
64
+ gr.Audio(label="Generated Audio", type="filepath"),
65
+ gr.Markdown(label="Warning", visible=False)
66
+ ],
67
+ title="Edge TTS Text-to-Speech",
68
+ description=description,
69
+
70
+ analytics_enabled=False,
71
+ allow_flagging="manual"
72
+ )
73
+ return demo
74
 
75
+ # Run the application
76
  if __name__ == "__main__":
77
+ demo = asyncio.run(create_demo())
78
+ demo.launch(server_name="0.0.0.0", server_port=7860, share=True)