tsi-org commited on
Commit
453bb82
·
1 Parent(s): ab98968

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -163
app.py CHANGED
@@ -1,168 +1,55 @@
1
- import random
2
- import gradio as gr
3
- import numpy as np
4
- from elevenlabs import voices, generate, set_api_key, UnauthenticatedRateLimitError
 
 
 
 
 
 
 
5
 
6
- def pad_buffer(audio):
7
- # Pad buffer to multiple of 2 bytes
8
- buffer_size = len(audio)
9
- element_size = np.dtype(np.int16).itemsize
10
- if buffer_size % element_size != 0:
11
- audio = audio + b'\0' * (element_size - (buffer_size % element_size))
12
- return audio
13
-
14
- def generate_voice(text, voice_name):
15
- try:
16
- audio = generate(
17
- text[:250], # Limit to 250 characters
18
- voice=voice_name,
19
- model="eleven_multilingual_v2"
20
- )
21
- return (44100, np.frombuffer(pad_buffer(audio), dtype=np.int16))
22
- except UnauthenticatedRateLimitError as e:
23
- raise gr.Error("Thanks for trying out ElevenLabs TTS! You've reached the free tier limit. Please provide an API key to continue.")
24
- except Exception as e:
25
- raise gr.Error(e)
26
-
27
  # Login function for authentication
28
  def custom_auth(username, password):
29
  return password == "aitutor"
30
 
31
-
32
- badges = """
33
- """
34
- menu = """
35
- <style>
36
- /* Existing styles */
37
- .gradio-container-3-40-1 .prose a {
38
- color: #ffffff!important;
39
- text-decoration: auto!important;
40
- }
41
- .menu-bar {
42
- display: flex;
43
- justify-content: space-between;
44
- align-items: center;
45
- background-color: #333;
46
- padding: 10px;
47
- color: white;
48
- font-family: 'sans-serif';
49
- }
50
- .menu-bar a, .menu-bar a:visited {
51
- color: white;
52
- text-decoration: none;
53
- }
54
- .menu-icon {
55
- font-size: 24px;
56
- background-color: #ffffff;
57
- border-radius: 50%;
58
- padding: 5px;
59
- }
60
- .menu-items {
61
- display: flex;
62
- gap: 15px;
63
- color: white;
64
- }
65
- .menu-item {
66
- padding: 8px 16px;
67
- background-color: #555;
68
- border-radius: 4px;
69
- transition: background-color 0.3s;
70
- font-weight: bold;
71
- font-size: 12px;
72
- }
73
- .menu-item:hover {
74
- background-color: #777;
75
- }
76
-
77
- /* Responsive styles for mobile */
78
- @media (max-width: 768px) {
79
- .menu-item {
80
- font-size: 12px; /* Shrink text size */
81
- }
82
- .menu-icon {
83
- font-size: 18px; /* Shrink icon size */
84
- }
85
- }
86
- </style>
87
-
88
- <div class="menu-bar">
89
- <a href="#" class="menu-icon">🎵</a>
90
- <div class="menu-items">
91
- <span class="menu-item"><a href="#">Dashboard</a></span>
92
- <span class="menu-item"><a href="#">Premium</a></span>
93
- <span class="menu-item"><a href="#">Account</a></span>
94
- <span class="menu-item"><a href="#">Voices</a></span>
95
- </div>
96
- </div>
97
-
98
- """
99
-
100
- description = """
101
- <style>
102
- .notification {
103
- text-align: left; /* Left-align the text */
104
- background-color: #ffffff; /* White Background */
105
- color: #000; /* Black text */
106
- padding: 20px; /* Padding */
107
- margin: 20px; /* Margin */
108
- margin-bottom: 40px; /* Additional space below the notification */
109
- border: 2px solid #000000; /* Black border */
110
- border-radius: 20px; /* Rounded corners */
111
- font-size: 18px; /* Font size */
112
- font-family: 'Arial', sans-serif; /* Font family */
113
- box-shadow: 0 4px 8px 0 rgba(0,0,0,0.1); /* Subtle box shadow for depth */
114
- }
115
- </style>
116
-
117
- <div class="notification">
118
- Welcome to Text to Voice, single foundational model supporting 28 languages including: English, Chinese, Spanish, Hindi, Portuguese, French, German, Japanese, Arabic, Korean, Indonesian, Italian, Dutch, Turkish, Polish, Swedish, Filipino, Malay, Romanian, Ukrainian, Greek, Czech, Danish, Finnish, Bulgarian, Croatian, Slovak, and Tamil.
119
- </div>
120
-
121
- """
122
-
123
-
124
-
125
- with gr.Blocks() as block:
126
- gr.Markdown('')
127
- gr.Markdown(menu)
128
- gr.Markdown(badges)
129
- gr.Markdown(description)
130
-
131
- input_text = gr.Textbox(
132
- label="Input Text (250 characters max)",
133
- lines=2,
134
- value="Hello! 你好! Hola! नमस्ते! Bonjour! こんにちは! مرحبا! 안녕하세요! Ciao! Cześć! Привіт! Γειά σας! Здравей! வணக்கம்!",
135
- elem_id="input_text"
136
- )
137
-
138
- all_voices = voices()
139
- input_voice = gr.Dropdown(
140
- [voice.name for voice in all_voices],
141
- value="Bella",
142
- label="Voice",
143
- elem_id="input_voice"
144
- )
145
-
146
- run_button = gr.Button(
147
- text="Generate Voice",
148
- type="button"
149
- )
150
-
151
- out_audio = gr.Audio(
152
- label="Generated Voice",
153
- type="numpy",
154
- elem_id="out_audio",
155
- format="mp3"
156
- )
157
-
158
- inputs = [input_text, input_voice]
159
- outputs = [out_audio]
160
-
161
- run_button.click(
162
- fn=generate_voice,
163
- inputs=inputs,
164
- outputs=outputs,
165
- queue=True
166
- )
167
-
168
- block.queue(concurrency_count=5).launch(debug=True, auth=custom_auth)
 
1
+ import gradio as gr
2
+ import subprocess
3
+ import shutil
4
+ import os
5
+ from PIL import Image
6
+ import numpy as np
7
+
8
+ # Ensure output directory exists
9
+ output_dir = "output"
10
+ if not os.path.exists(output_dir):
11
+ os.makedirs(output_dir)
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # Login function for authentication
14
  def custom_auth(username, password):
15
  return password == "aitutor"
16
 
17
+ def run_scripts(target, source, use_face_enhancer):
18
+ if target is None or (not use_face_enhancer and source is None):
19
+ return None
20
+
21
+ target_extension = os.path.splitext(target.name)[-1]
22
+ output_path1 = os.path.join(output_dir, "output1" + target_extension)
23
+ output_path2 = os.path.join(output_dir, "output2" + target_extension)
24
+
25
+ if not use_face_enhancer:
26
+ cmd1 = ["python3", "run.py", "-s", source.name, "-t", target.name, "-o", output_path1, "--frame-processor", "face_swapper"]
27
+ subprocess.run(cmd1)
28
+
29
+ cmd2 = ["python3", "run.py", "-t", target.name if use_face_enhancer else output_path1, "-o", output_path2, "--frame-processor", "face_enhancer"]
30
+ subprocess.run(cmd2)
31
+
32
+ if not use_face_enhancer:
33
+ os.remove(source.name)
34
+ os.remove(target.name)
35
+
36
+ # Open the image with PIL and convert to NumPy array
37
+ with Image.open(output_path2) as img:
38
+ img_array = np.array(img)
39
+
40
+ return img_array
41
+
42
+ iface = gr.Interface(
43
+ fn=run_scripts,
44
+ inputs=[
45
+ "file",
46
+ "file",
47
+ gr.inputs.Checkbox(default=False, label="Use only Face Enhancer")
48
+ ],
49
+ outputs="image",
50
+ title="Pixio Swap",
51
+ description="Upload a target image/video and a source image to swap faces.",
52
+ live=True
53
+ )
54
+
55
+ iface.launch(debug=True, auth=custom_auth)