Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -26,7 +26,7 @@ ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
|
|
26 |
set_api_key(ELEVENLABS_API_KEY)
|
27 |
|
28 |
# Role configurations
|
29 |
-
ALLOWED_ROLES = ["+mechanic", "+trusted"
|
30 |
CHAR_LIMITS = {
|
31 |
"+mechanic": 500,
|
32 |
"+trusted": 3000,
|
@@ -47,18 +47,6 @@ ACCENT_PROMPTS = {
|
|
47 |
Example: "Hello there" → "Howdy there, partner" """
|
48 |
}
|
49 |
|
50 |
-
# Discord bot setup
|
51 |
-
intents = discord.Intents.default()
|
52 |
-
intents.message_content = True
|
53 |
-
client = discord.Client(intents=intents)
|
54 |
-
tree = app_commands.CommandTree(client)
|
55 |
-
|
56 |
-
# Define your server ID (replace this with your actual server ID)
|
57 |
-
MY_GUILD_ID = discord.Object(id=int(os.getenv('DISCORD_GUILD_ID', '0')))
|
58 |
-
|
59 |
-
# Get available voices - moved here to be available for both Discord and Gradio
|
60 |
-
VOICE_LIST = get_available_voices()
|
61 |
-
|
62 |
def get_available_voices():
|
63 |
"""Fetch only custom voices from ElevenLabs account"""
|
64 |
all_voices = voices()
|
@@ -91,26 +79,41 @@ def has_permission(member: discord.Member) -> tuple[bool, int]:
|
|
91 |
|
92 |
async def modify_accent(text: str, accent: str, enhance: bool = False) -> str:
|
93 |
"""Modify text based on selected accent using Gemini AI"""
|
94 |
-
if
|
95 |
return text
|
96 |
|
97 |
prompt = ACCENT_PROMPTS[accent]
|
98 |
if enhance:
|
99 |
-
prompt += "\
|
|
|
|
|
|
|
|
|
100 |
|
101 |
prompt += f"\n\nText to modify: {text}"
|
102 |
|
103 |
try:
|
104 |
response = await model.generate_content_async(prompt)
|
105 |
-
|
|
|
106 |
except Exception as e:
|
107 |
logger.error(f"Error modifying accent: {str(e)}")
|
108 |
return text
|
109 |
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
async def voice_list(interaction: discord.Interaction):
|
112 |
await interaction.response.defer()
|
113 |
-
|
|
|
114 |
credits_info = get_remaining_credits()
|
115 |
credits_msg = format_credits_message(credits_info)
|
116 |
|
@@ -120,8 +123,15 @@ async def voice_list(interaction: discord.Interaction):
|
|
120 |
color=0x2B2D31
|
121 |
)
|
122 |
await interaction.followup.send(embed=embed)
|
123 |
-
|
124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
@app_commands.describe(
|
126 |
text="Text to convert to speech",
|
127 |
voice_name="Select a voice to use",
|
@@ -152,7 +162,7 @@ async def voice_create(
|
|
152 |
if not has_perm:
|
153 |
embed = discord.Embed(
|
154 |
title="Permission Denied",
|
155 |
-
description="You need to be an administrator or have
|
156 |
color=0xFF0000
|
157 |
)
|
158 |
await interaction.followup.send(embed=embed)
|
@@ -171,6 +181,16 @@ async def voice_create(
|
|
171 |
# Process accent if specified
|
172 |
if accent:
|
173 |
text = await modify_accent(text, accent, accent_enhance)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
try:
|
176 |
voice_settings = VoiceSettings(
|
@@ -180,47 +200,36 @@ async def voice_create(
|
|
180 |
use_speaker_boost=True
|
181 |
)
|
182 |
|
183 |
-
if voice_name not in VOICE_LIST:
|
184 |
-
embed = discord.Embed(
|
185 |
-
title="Voice Not Found",
|
186 |
-
description=f"The voice '{voice_name}' was not found. Use `/list` to see available voices.",
|
187 |
-
color=0x2B2D31
|
188 |
-
)
|
189 |
-
await interaction.followup.send(embed=embed)
|
190 |
-
return
|
191 |
-
|
192 |
audio = generate(
|
193 |
text=text,
|
194 |
voice=Voice(
|
195 |
-
voice_id=
|
196 |
settings=voice_settings
|
197 |
)
|
198 |
)
|
199 |
|
200 |
-
# Save audio to temporary WAV file with correct format
|
201 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
|
202 |
-
# Convert audio to proper WAV format using pydub
|
203 |
audio_segment = AudioSegment.from_file(io.BytesIO(audio), format="mp3")
|
204 |
-
# Convert to mono, 16-bit PCM at 22.050 kHz
|
205 |
audio_segment = audio_segment.set_frame_rate(22050).set_channels(1).set_sample_width(2)
|
206 |
audio_segment.export(temp_file.name, format='wav')
|
207 |
temp_path = temp_file.name
|
208 |
|
209 |
-
# Get updated credits
|
210 |
credits_info = get_remaining_credits()
|
211 |
credits_msg = format_credits_message(credits_info)
|
212 |
|
|
|
|
|
|
|
213 |
embed = discord.Embed(
|
214 |
title="Voice Generated",
|
215 |
-
description=f"Prompt: {text}\nVoice: {voice_name}\nStability: {stability}\nClarity: {clarity}\nStyle: {style}\n\n{credits_msg}",
|
216 |
-
color=0x57F287
|
217 |
)
|
218 |
await interaction.followup.send(
|
219 |
embed=embed,
|
220 |
file=discord.File(temp_path)
|
221 |
)
|
222 |
|
223 |
-
# Clean up
|
224 |
os.unlink(temp_path)
|
225 |
|
226 |
except Exception as e:
|
|
|
26 |
set_api_key(ELEVENLABS_API_KEY)
|
27 |
|
28 |
# Role configurations
|
29 |
+
ALLOWED_ROLES = ["+mechanic", "+trusted"]
|
30 |
CHAR_LIMITS = {
|
31 |
"+mechanic": 500,
|
32 |
"+trusted": 3000,
|
|
|
47 |
Example: "Hello there" → "Howdy there, partner" """
|
48 |
}
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
def get_available_voices():
|
51 |
"""Fetch only custom voices from ElevenLabs account"""
|
52 |
all_voices = voices()
|
|
|
79 |
|
80 |
async def modify_accent(text: str, accent: str, enhance: bool = False) -> str:
|
81 |
"""Modify text based on selected accent using Gemini AI"""
|
82 |
+
if not accent or accent == "American":
|
83 |
return text
|
84 |
|
85 |
prompt = ACCENT_PROMPTS[accent]
|
86 |
if enhance:
|
87 |
+
prompt += """\nEnhance the text by:
|
88 |
+
1. Adding more cultural expressions
|
89 |
+
2. Including occasional words in the native language
|
90 |
+
3. Making speech patterns more authentic
|
91 |
+
4. Adding cultural references where appropriate"""
|
92 |
|
93 |
prompt += f"\n\nText to modify: {text}"
|
94 |
|
95 |
try:
|
96 |
response = await model.generate_content_async(prompt)
|
97 |
+
modified_text = response.text.strip()
|
98 |
+
return modified_text if modified_text else text
|
99 |
except Exception as e:
|
100 |
logger.error(f"Error modifying accent: {str(e)}")
|
101 |
return text
|
102 |
|
103 |
+
# Discord bot setup
|
104 |
+
intents = discord.Intents.default()
|
105 |
+
intents.message_content = True
|
106 |
+
client = discord.Client(intents=intents)
|
107 |
+
tree = app_commands.CommandTree(client)
|
108 |
+
|
109 |
+
# Define your server ID
|
110 |
+
MY_GUILD_ID = discord.Object(id=int(os.getenv('DISCORD_GUILD_ID', '0')))
|
111 |
+
|
112 |
+
@tree.command(name="list", description="List all available voices")
|
113 |
async def voice_list(interaction: discord.Interaction):
|
114 |
await interaction.response.defer()
|
115 |
+
available_voices = get_available_voices()
|
116 |
+
voice_list = "\n".join([f"• {name}" for name in available_voices.keys()])
|
117 |
credits_info = get_remaining_credits()
|
118 |
credits_msg = format_credits_message(credits_info)
|
119 |
|
|
|
123 |
color=0x2B2D31
|
124 |
)
|
125 |
await interaction.followup.send(embed=embed)
|
126 |
+
|
127 |
+
async def voice_autocomplete(interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]:
|
128 |
+
voices = list(get_available_voices().keys())
|
129 |
+
return [
|
130 |
+
app_commands.Choice(name=voice, value=voice)
|
131 |
+
for voice in voices if current.lower() in voice.lower()
|
132 |
+
][:25]
|
133 |
+
|
134 |
+
@tree.command(name="create", description="Create a voice message")
|
135 |
@app_commands.describe(
|
136 |
text="Text to convert to speech",
|
137 |
voice_name="Select a voice to use",
|
|
|
162 |
if not has_perm:
|
163 |
embed = discord.Embed(
|
164 |
title="Permission Denied",
|
165 |
+
description="You need to be an administrator or have the +mechanic/+trusted role to use this command.",
|
166 |
color=0xFF0000
|
167 |
)
|
168 |
await interaction.followup.send(embed=embed)
|
|
|
181 |
# Process accent if specified
|
182 |
if accent:
|
183 |
text = await modify_accent(text, accent, accent_enhance)
|
184 |
+
|
185 |
+
available_voices = get_available_voices()
|
186 |
+
if voice_name not in available_voices:
|
187 |
+
embed = discord.Embed(
|
188 |
+
title="Voice Not Found",
|
189 |
+
description=f"The voice '{voice_name}' was not found. Use `/list` to see available voices.",
|
190 |
+
color=0x2B2D31
|
191 |
+
)
|
192 |
+
await interaction.followup.send(embed=embed)
|
193 |
+
return
|
194 |
|
195 |
try:
|
196 |
voice_settings = VoiceSettings(
|
|
|
200 |
use_speaker_boost=True
|
201 |
)
|
202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
audio = generate(
|
204 |
text=text,
|
205 |
voice=Voice(
|
206 |
+
voice_id=available_voices[voice_name],
|
207 |
settings=voice_settings
|
208 |
)
|
209 |
)
|
210 |
|
|
|
211 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
|
|
|
212 |
audio_segment = AudioSegment.from_file(io.BytesIO(audio), format="mp3")
|
|
|
213 |
audio_segment = audio_segment.set_frame_rate(22050).set_channels(1).set_sample_width(2)
|
214 |
audio_segment.export(temp_file.name, format='wav')
|
215 |
temp_path = temp_file.name
|
216 |
|
|
|
217 |
credits_info = get_remaining_credits()
|
218 |
credits_msg = format_credits_message(credits_info)
|
219 |
|
220 |
+
accent_info = f"\nAccent: {accent}" if accent else ""
|
221 |
+
accent_enhance_info = f"\nAccent Enhancement: {'On' if accent_enhance else 'Off'}" if accent else ""
|
222 |
+
|
223 |
embed = discord.Embed(
|
224 |
title="Voice Generated",
|
225 |
+
description=f"Prompt: {text}\nVoice: {voice_name}\nStability: {stability}\nClarity: {clarity}\nStyle: {style}{accent_info}{accent_enhance_info}\n\n{credits_msg}",
|
226 |
+
color=0x57F287
|
227 |
)
|
228 |
await interaction.followup.send(
|
229 |
embed=embed,
|
230 |
file=discord.File(temp_path)
|
231 |
)
|
232 |
|
|
|
233 |
os.unlink(temp_path)
|
234 |
|
235 |
except Exception as e:
|