Spaces:
Running
Running
Commit
·
ef926c5
1
Parent(s):
93606a3
init
Browse files- __pycache__/app.cpython-313.pyc +0 -0
- __pycache__/util.cpython-313.pyc +0 -0
- app.py +109 -9
- util.py +4 -3
__pycache__/app.cpython-313.pyc
ADDED
|
Binary file (26.8 kB). View file
|
|
|
__pycache__/util.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/util.cpython-313.pyc and b/__pycache__/util.cpython-313.pyc differ
|
|
|
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import time
|
| 3 |
import uuid
|
|
|
|
| 4 |
from util import (
|
| 5 |
create_task_v3,
|
| 6 |
get_task_result,
|
|
@@ -9,10 +10,95 @@ from util import (
|
|
| 9 |
|
| 10 |
IP_Dict = {}
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
"""
|
| 14 |
Trump AI voice generation function with real-time status updates
|
| 15 |
"""
|
|
|
|
|
|
|
|
|
|
| 16 |
client_ip = request.client.host
|
| 17 |
x_forwarded_for = dict(request.headers).get('x-forwarded-for')
|
| 18 |
if x_forwarded_for:
|
|
@@ -53,9 +139,10 @@ def generate_trump_voice_with_realtime_updates(text, word_num, request: gr.Reque
|
|
| 53 |
|
| 54 |
try:
|
| 55 |
task_type = "voice"
|
|
|
|
| 56 |
|
| 57 |
# Create task
|
| 58 |
-
task_result = create_task_v3(task_type, text.strip(), word_num, is_rewrite=False)
|
| 59 |
if not task_result:
|
| 60 |
return "Failed to create task", None, gr.update(visible=False), ""
|
| 61 |
else:
|
|
@@ -183,9 +270,15 @@ with gr.Blocks(title="Donald Trump AI Voice", theme=gr.themes.Soft()) as demo:
|
|
| 183 |
)
|
| 184 |
|
| 185 |
with gr.Column(scale=1):
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
)
|
| 190 |
|
| 191 |
submit_btn = gr.Button(
|
|
@@ -229,7 +322,7 @@ with gr.Blocks(title="Donald Trump AI Voice", theme=gr.themes.Soft()) as demo:
|
|
| 229 |
<div style="background: rgba(255,255,255,0.8); padding: 20px; border-radius: 15px; box-shadow: 0 4px 15px rgba(0,0,0,0.1);">
|
| 230 |
<h4 style="color: #2c3e50; margin: 0 0 15px 0; font-size: 1.1em;">🌺 Garden Interview</h4>
|
| 231 |
<video id="video-garden" style="width: 100%; height: 180px; border-radius: 10px; object-fit: cover;" controls preload="metadata">
|
| 232 |
-
<source src="https://www.trumpaivoice.net/
|
| 233 |
Your browser does not support the video tag.
|
| 234 |
</video>
|
| 235 |
</div>
|
|
@@ -237,7 +330,7 @@ with gr.Blocks(title="Donald Trump AI Voice", theme=gr.themes.Soft()) as demo:
|
|
| 237 |
<div style="background: rgba(255,255,255,0.8); padding: 20px; border-radius: 15px; box-shadow: 0 4px 15px rgba(0,0,0,0.1);">
|
| 238 |
<h4 style="color: #2c3e50; margin: 0 0 15px 0; font-size: 1.1em;">🏛️ Presidential Office</h4>
|
| 239 |
<video id="video-office" style="width: 100%; height: 180px; border-radius: 10px; object-fit: cover;" controls preload="metadata">
|
| 240 |
-
<source src="https://www.trumpaivoice.net/
|
| 241 |
Your browser does not support the video tag.
|
| 242 |
</video>
|
| 243 |
</div>
|
|
@@ -245,7 +338,7 @@ with gr.Blocks(title="Donald Trump AI Voice", theme=gr.themes.Soft()) as demo:
|
|
| 245 |
<div style="background: rgba(255,255,255,0.8); padding: 20px; border-radius: 15px; box-shadow: 0 4px 15px rgba(0,0,0,0.1);">
|
| 246 |
<h4 style="color: #2c3e50; margin: 0 0 15px 0; font-size: 1.1em;">🇺🇸 Flag Background</h4>
|
| 247 |
<video id="video-flag" style="width: 100%; height: 180px; border-radius: 10px; object-fit: cover;" controls preload="metadata">
|
| 248 |
-
<source src="https://www.trumpaivoice.net/
|
| 249 |
Your browser does not support the video tag.
|
| 250 |
</video>
|
| 251 |
</div>
|
|
@@ -449,10 +542,17 @@ with gr.Blocks(title="Donald Trump AI Voice", theme=gr.themes.Soft()) as demo:
|
|
| 449 |
# Hidden state to store task_url
|
| 450 |
task_url_state = gr.State("")
|
| 451 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
# Bind event
|
| 453 |
submit_btn.click(
|
| 454 |
generate_trump_voice_with_realtime_updates,
|
| 455 |
-
inputs=[text_input,
|
| 456 |
outputs=[status_output, audio_output, action_links, task_url_state]
|
| 457 |
)
|
| 458 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import time
|
| 3 |
import uuid
|
| 4 |
+
import re
|
| 5 |
from util import (
|
| 6 |
create_task_v3,
|
| 7 |
get_task_result,
|
|
|
|
| 10 |
|
| 11 |
IP_Dict = {}
|
| 12 |
|
| 13 |
+
# 支持的语言列表
|
| 14 |
+
SUPPORTED_LANGUAGES = [
|
| 15 |
+
{"flag": "🇺🇸", "name": "English", "code": "en"},
|
| 16 |
+
{"flag": "🇨🇳", "name": "中文", "code": "zh"},
|
| 17 |
+
{"flag": "🇪🇸", "name": "Español", "code": "es"},
|
| 18 |
+
{"flag": "🇫🇷", "name": "Français", "code": "fr"},
|
| 19 |
+
{"flag": "🇩🇪", "name": "Deutsch", "code": "de"},
|
| 20 |
+
{"flag": "🇮🇹", "name": "Italiano", "code": "it"},
|
| 21 |
+
{"flag": "🇯🇵", "name": "日本語", "code": "ja"},
|
| 22 |
+
{"flag": "🇰🇷", "name": "한국어", "code": "ko"},
|
| 23 |
+
{"flag": "🇷🇺", "name": "Русский", "code": "ru"}
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
def detect_language(text):
|
| 27 |
+
"""
|
| 28 |
+
语言检测函数 - 只检测容易区分的语言
|
| 29 |
+
"""
|
| 30 |
+
if len(text) < 5:
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
# 语言特征字符
|
| 34 |
+
language_features = {
|
| 35 |
+
"zh": re.compile(r'[\u4e00-\u9fff\u3400-\u4dbf\uff00-\uffef\u3000-\u303f]'),
|
| 36 |
+
"ja": re.compile(r'[\u3040-\u309f\u30a0-\u30ff]'),
|
| 37 |
+
"ko": re.compile(r'[\uac00-\ud7af]'),
|
| 38 |
+
"ru": re.compile(r'[\u0400-\u04ff]')
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
scores = {lang: 0 for lang in language_features.keys()}
|
| 42 |
+
|
| 43 |
+
# 字符特征检测 - 只检测明显的非拉丁字符
|
| 44 |
+
for char in text:
|
| 45 |
+
for lang, pattern in language_features.items():
|
| 46 |
+
if pattern.search(char):
|
| 47 |
+
scores[lang] += 1
|
| 48 |
+
|
| 49 |
+
# 找到得分最高的语言
|
| 50 |
+
max_score = 0
|
| 51 |
+
detected_lang = None
|
| 52 |
+
|
| 53 |
+
for lang, score in scores.items():
|
| 54 |
+
if score > max_score and score > 0:
|
| 55 |
+
max_score = score
|
| 56 |
+
detected_lang = lang
|
| 57 |
+
|
| 58 |
+
# 检查是否满足阈值 - 如果有明显的非拉丁字符就切换
|
| 59 |
+
if detected_lang and max_score >= 2:
|
| 60 |
+
return detected_lang
|
| 61 |
+
|
| 62 |
+
# 没有检测到其他语言,默认保持英语
|
| 63 |
+
return None
|
| 64 |
+
|
| 65 |
+
def get_language_display_text(language_code):
|
| 66 |
+
"""获取语言的显示文本(国旗 + 名称)"""
|
| 67 |
+
for lang in SUPPORTED_LANGUAGES:
|
| 68 |
+
if lang["code"] == language_code:
|
| 69 |
+
return f"{lang['flag']} {lang['name']}"
|
| 70 |
+
return "🇺🇸 English"
|
| 71 |
+
|
| 72 |
+
def get_language_code_from_display(display_text):
|
| 73 |
+
"""从显示文本中提取语言代码"""
|
| 74 |
+
for lang in SUPPORTED_LANGUAGES:
|
| 75 |
+
if f"{lang['flag']} {lang['name']}" == display_text:
|
| 76 |
+
return lang['code']
|
| 77 |
+
return "en"
|
| 78 |
+
|
| 79 |
+
def auto_detect_language(text, current_language_display):
|
| 80 |
+
"""
|
| 81 |
+
自动检测语言并返回更新后的语言选择
|
| 82 |
+
"""
|
| 83 |
+
if not text or len(text.strip()) < 5:
|
| 84 |
+
return current_language_display
|
| 85 |
+
|
| 86 |
+
current_code = get_language_code_from_display(current_language_display)
|
| 87 |
+
detected_lang = detect_language(text)
|
| 88 |
+
|
| 89 |
+
if detected_lang and detected_lang != current_code:
|
| 90 |
+
print(f"Language auto-detected: {current_code} -> {detected_lang}")
|
| 91 |
+
return get_language_display_text(detected_lang)
|
| 92 |
+
|
| 93 |
+
return current_language_display
|
| 94 |
+
|
| 95 |
+
def generate_trump_voice_with_realtime_updates(text, language_display, request: gr.Request):
|
| 96 |
"""
|
| 97 |
Trump AI voice generation function with real-time status updates
|
| 98 |
"""
|
| 99 |
+
# 从显示文本中提取语言代码
|
| 100 |
+
language = get_language_code_from_display(language_display)
|
| 101 |
+
|
| 102 |
client_ip = request.client.host
|
| 103 |
x_forwarded_for = dict(request.headers).get('x-forwarded-for')
|
| 104 |
if x_forwarded_for:
|
|
|
|
| 139 |
|
| 140 |
try:
|
| 141 |
task_type = "voice"
|
| 142 |
+
word_num = len(text.strip().split()) # 自动计算单词数
|
| 143 |
|
| 144 |
# Create task
|
| 145 |
+
task_result = create_task_v3(task_type, text.strip(), word_num, is_rewrite=False, language=language)
|
| 146 |
if not task_result:
|
| 147 |
return "Failed to create task", None, gr.update(visible=False), ""
|
| 148 |
else:
|
|
|
|
| 270 |
)
|
| 271 |
|
| 272 |
with gr.Column(scale=1):
|
| 273 |
+
# 创建语言选择器的选项
|
| 274 |
+
language_choices = [f"{lang['flag']} {lang['name']}" for lang in SUPPORTED_LANGUAGES]
|
| 275 |
+
language_values = [lang['code'] for lang in SUPPORTED_LANGUAGES]
|
| 276 |
+
|
| 277 |
+
language_dropdown = gr.Dropdown(
|
| 278 |
+
choices=language_choices,
|
| 279 |
+
value="🇺🇸 English", # 默认值
|
| 280 |
+
label="🌍 Language",
|
| 281 |
+
interactive=True
|
| 282 |
)
|
| 283 |
|
| 284 |
submit_btn = gr.Button(
|
|
|
|
| 322 |
<div style="background: rgba(255,255,255,0.8); padding: 20px; border-radius: 15px; box-shadow: 0 4px 15px rgba(0,0,0,0.1);">
|
| 323 |
<h4 style="color: #2c3e50; margin: 0 0 15px 0; font-size: 1.1em;">🌺 Garden Interview</h4>
|
| 324 |
<video id="video-garden" style="width: 100%; height: 180px; border-radius: 10px; object-fit: cover;" controls preload="metadata">
|
| 325 |
+
<source src="https://www.trumpaivoice.net/SelfitAssert/Heygem/Trump/trump_showcases/musk0710.mp4" type="video/mp4">
|
| 326 |
Your browser does not support the video tag.
|
| 327 |
</video>
|
| 328 |
</div>
|
|
|
|
| 330 |
<div style="background: rgba(255,255,255,0.8); padding: 20px; border-radius: 15px; box-shadow: 0 4px 15px rgba(0,0,0,0.1);">
|
| 331 |
<h4 style="color: #2c3e50; margin: 0 0 15px 0; font-size: 1.1em;">🏛️ Presidential Office</h4>
|
| 332 |
<video id="video-office" style="width: 100%; height: 180px; border-radius: 10px; object-fit: cover;" controls preload="metadata">
|
| 333 |
+
<source src="https://www.trumpaivoice.net/SelfitAssert/Heygem/Trump/trump_showcases/62_1757764805_9klRXL24-r.mp4" type="video/mp4">
|
| 334 |
Your browser does not support the video tag.
|
| 335 |
</video>
|
| 336 |
</div>
|
|
|
|
| 338 |
<div style="background: rgba(255,255,255,0.8); padding: 20px; border-radius: 15px; box-shadow: 0 4px 15px rgba(0,0,0,0.1);">
|
| 339 |
<h4 style="color: #2c3e50; margin: 0 0 15px 0; font-size: 1.1em;">🇺🇸 Flag Background</h4>
|
| 340 |
<video id="video-flag" style="width: 100%; height: 180px; border-radius: 10px; object-fit: cover;" controls preload="metadata">
|
| 341 |
+
<source src="https://www.trumpaivoice.net/SelfitAssert/Heygem/Trump/trump_showcases/puting0710.mp4" type="video/mp4">
|
| 342 |
Your browser does not support the video tag.
|
| 343 |
</video>
|
| 344 |
</div>
|
|
|
|
| 542 |
# Hidden state to store task_url
|
| 543 |
task_url_state = gr.State("")
|
| 544 |
|
| 545 |
+
# 自动语言检测事件
|
| 546 |
+
text_input.change(
|
| 547 |
+
auto_detect_language,
|
| 548 |
+
inputs=[text_input, language_dropdown],
|
| 549 |
+
outputs=[language_dropdown]
|
| 550 |
+
)
|
| 551 |
+
|
| 552 |
# Bind event
|
| 553 |
submit_btn.click(
|
| 554 |
generate_trump_voice_with_realtime_updates,
|
| 555 |
+
inputs=[text_input, language_dropdown],
|
| 556 |
outputs=[status_output, audio_output, action_links, task_url_state]
|
| 557 |
)
|
| 558 |
|
util.py
CHANGED
|
@@ -48,10 +48,10 @@ class VideoTemplate:
|
|
| 48 |
UsFlag = "us-flag"
|
| 49 |
WhiteHouse = "white-house"
|
| 50 |
|
| 51 |
-
def create_task_v3(task_type, text, word_num, is_rewrite):
|
| 52 |
import json
|
| 53 |
is_rewrite = False
|
| 54 |
-
url = f"{BackendUrl}/
|
| 55 |
headers = {
|
| 56 |
"Content-Type": "application/json"
|
| 57 |
}
|
|
@@ -62,6 +62,7 @@ def create_task_v3(task_type, text, word_num, is_rewrite):
|
|
| 62 |
"text": text,
|
| 63 |
"word_num": word_num,
|
| 64 |
"is_rewrite": False,
|
|
|
|
| 65 |
"watermark": True,
|
| 66 |
"type": "voice",
|
| 67 |
"cost_credits": 2,
|
|
@@ -114,5 +115,5 @@ if __name__ == "__main__":
|
|
| 114 |
text = "Hello, this is a test message for Trump AI Voice."
|
| 115 |
word_num = 10
|
| 116 |
is_rewrite = True
|
| 117 |
-
task_result =
|
| 118 |
print(f"task_result: {task_result}")
|
|
|
|
| 48 |
UsFlag = "us-flag"
|
| 49 |
WhiteHouse = "white-house"
|
| 50 |
|
| 51 |
+
def create_task_v3(task_type, text, word_num, is_rewrite, language="en"):
|
| 52 |
import json
|
| 53 |
is_rewrite = False
|
| 54 |
+
url = f"{BackendUrl}/trump_process_ctx_api_v3"
|
| 55 |
headers = {
|
| 56 |
"Content-Type": "application/json"
|
| 57 |
}
|
|
|
|
| 62 |
"text": text,
|
| 63 |
"word_num": word_num,
|
| 64 |
"is_rewrite": False,
|
| 65 |
+
"language": language,
|
| 66 |
"watermark": True,
|
| 67 |
"type": "voice",
|
| 68 |
"cost_credits": 2,
|
|
|
|
| 115 |
text = "Hello, this is a test message for Trump AI Voice."
|
| 116 |
word_num = 10
|
| 117 |
is_rewrite = True
|
| 118 |
+
task_result = create_task_v3(task_type, text, word_num, is_rewrite)
|
| 119 |
print(f"task_result: {task_result}")
|