VSPAN commited on
Commit
d645fd7
·
verified ·
1 Parent(s): 490eb18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -128
app.py CHANGED
@@ -1,170 +1,161 @@
1
- import os
2
  import gradio as gr
3
  import edge_tts
4
  import asyncio
5
  import tempfile
6
  import re
7
- import logging
8
- from pathlib import Path
9
  from huggingface_hub import InferenceClient
10
 
11
- # Конфигурация
12
- logging.basicConfig(level=logging.INFO)
13
- logger = logging.getLogger(__name__)
14
- MAX_TEXT_LENGTH = 5000
15
- TEMP_FILES = []
16
 
17
- # Инициализация клиентов
18
- image_client = None
19
- chat_client = None
20
- tts_voices = []
21
 
22
- # Стиль интерфейса
23
- theme = gr.themes.Soft()
 
 
 
 
 
 
 
 
24
 
25
- def initialize_clients(api_key):
26
- global image_client, chat_client, tts_voices
27
- try:
28
- # Инициализация клиентов API
29
- image_client = InferenceClient(token=api_key)
30
- chat_client = InferenceClient(provider="together", api_key=api_key)
31
-
32
- # Получение списка голосов для TTS
33
- asyncio.run(load_tts_voices())
34
- return True
35
- except Exception:
36
- return False
37
-
38
- async def load_tts_voices():
39
- global tts_voices
40
  voices = await edge_tts.list_voices()
41
- tts_voices = [f"{v['ShortName']} ({v['Gender']})" for v in voices]
 
 
 
42
 
43
- # Функция очистки текста
44
- def clean_text(text):
45
- text = re.sub(r'[*_~><`#%$^]', '', text)
46
- return text.strip()
47
-
48
- # Генерация аудио
49
- async def generate_audio(text, voice):
50
  if not text.strip():
51
- return None
52
-
53
  try:
54
- text = clean_text(text)
55
- communicate = edge_tts.Communicate(text, voice.split()[0])
56
-
 
 
 
 
57
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
58
- tmp_path = tmp_file.name
59
- await communicate.save(tmp_path)
60
- TEMP_FILES.append(tmp_path)
61
- return tmp_path
62
-
63
  except Exception:
64
- return None
65
 
66
- # Генерация изображений
67
- def generate_image(prompt):
68
- if not prompt.strip():
69
- return None
70
-
71
- try:
72
- response = image_client.text_to_image(
73
- prompt.strip(),
74
- model="nerijs/dark-fantasy-illustration-flux",
75
- negative_prompt="text, watermark, low quality",
76
- guidance_scale=9,
77
- height=512,
78
- width=512
79
- )
80
-
81
- with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
82
- tmp_file.write(response)
83
- TEMP_FILES.append(tmp_file.name)
84
- return tmp_file.name
85
-
86
- except Exception:
87
- return None
88
 
89
- # Генерация текста
90
- async def generate_text(input_text):
91
- if not input_text.strip():
92
- return None
93
-
94
  try:
95
- completion = chat_client.chat.completions.create(
 
 
 
 
 
 
96
  model="deepseek-ai/DeepSeek-R1",
97
- messages=[{"role": "user", "content": input_text}],
98
  max_tokens=500
99
  )
100
  return completion.choices[0].message.content
101
  except Exception:
102
- return None
103
 
104
- # Интерфейс
105
- def create_interface():
 
 
 
 
106
  with gr.Blocks(theme=theme, title="AI Studio Pro") as demo:
107
- gr.Markdown("# 🎙️ Full AI Studio")
108
-
109
- # Секция API ключа (скрытая)
110
- api_key = gr.Textbox(
111
- value=os.environ.get("HF_API_KEY", ""),
112
- visible=False
113
- )
114
 
 
115
  with gr.Tabs():
116
  # Вкладка TTS
117
- with gr.Tab("🔊 Text-to-Speech"):
118
  with gr.Row():
119
  with gr.Column():
120
- tts_text = gr.Textbox(label="Text to speak", lines=5)
121
- tts_voice = gr.Dropdown(
122
- label="Voice",
123
- choices=tts_voices,
124
- value="en-US-ChristopherNeural (Male)" if tts_voices else None
125
  )
126
- tts_btn = gr.Button("Generate Speech", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
  with gr.Column():
129
- tts_audio = gr.Audio(label="Generated Audio")
130
-
131
- tts_btn.click(
132
- generate_audio,
133
- [tts_text, tts_voice],
134
- tts_audio
135
- )
136
-
137
- # Вкладка генерации текста и изображений
138
- with gr.Tab("🖼️ Text & Image Generation"):
139
  with gr.Row():
140
  with gr.Column():
141
- input_text = gr.Textbox(
142
- label="Input Text",
143
- placeholder="Enter text with <image>prompt</image> tags...",
144
- lines=5
 
145
  )
146
- gen_btn = gr.Button("Generate Content", variant="primary")
 
 
147
 
148
  with gr.Column():
149
- output_text = gr.Textbox(label="Generated Text", interactive=False)
150
- gallery = gr.Gallery(label="Generated Images", columns=2)
151
-
152
- gen_btn.click(
153
- fn=lambda x: [generate_text(x)] + [generate_image(p) for p in parse_image_prompts(x)],
154
- inputs=input_text,
155
- outputs=[output_text, gallery]
156
- )
 
 
157
 
158
- return demo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
 
160
- # Запуск
161
  if __name__ == "__main__":
162
- try:
163
- demo = create_interface()
164
- demo.launch(server_port=7860)
165
- finally:
166
- for file in TEMP_FILES:
167
- try:
168
- Path(file).unlink(missing_ok=True)
169
- except Exception:
170
- pass
 
 
1
  import gradio as gr
2
  import edge_tts
3
  import asyncio
4
  import tempfile
5
  import re
6
+ from typing import Optional, Tuple, Dict
 
7
  from huggingface_hub import InferenceClient
8
 
9
+ # Константы
10
+ DEFAULT_RATE = 0
11
+ DEFAULT_PITCH = 0
12
+ HF_API_KEY = "YOUR_API_KEY" # Замените на ваш API ключ
 
13
 
14
+ # Инициализация клиента для генерации текста
15
+ text_client = InferenceClient(provider="together", api_key=HF_API_KEY)
 
 
16
 
17
+ # Кастомная цветовая схема
18
+ theme = gr.themes.Default(
19
+ primary_hue="orange",
20
+ secondary_hue="yellow",
21
+ ).set(
22
+ button_primary_background="linear-gradient(90deg, #ff9a00, #ffd700)",
23
+ button_primary_background_hover="linear-gradient(90deg, #ff8c00, #ffcc00)",
24
+ slider_color="#ff9a00",
25
+ block_background="#fff5e6"
26
+ )
27
 
28
+ async def get_voices() -> Dict[str, str]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  voices = await edge_tts.list_voices()
30
+ return {
31
+ f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName']
32
+ for v in voices
33
+ }
34
 
35
+ async def text_to_speech(text: str, voice: str, rate: int, pitch: int) -> Tuple[Optional[str], Optional[str]]:
 
 
 
 
 
 
36
  if not text.strip():
37
+ return None, None
 
38
  try:
39
+ voice_short_name = voice.split(" - ")[0]
40
+ communicate = edge_tts.Communicate(
41
+ text,
42
+ voice_short_name,
43
+ rate=f"{rate:+d}%",
44
+ pitch=f"{pitch:+d}Hz"
45
+ )
46
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
47
+ await communicate.save(tmp_file.name)
48
+ return tmp_file.name, None
 
 
 
49
  except Exception:
50
+ return None, None
51
 
52
+ # ADD PROMPT HERE - Вставьте ваш промпт для генерации текста
53
+ SYSTEM_PROMPT = """
54
+ [Здесь будет ваш системный промпт]
55
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
+ async def generate_detailed_description(input_text: str) -> str:
 
 
 
 
58
  try:
59
+ # Добавьте вашу логику обработки промпта
60
+ messages = [
61
+ {"role": "system", "content": SYSTEM_PROMPT},
62
+ {"role": "user", "content": input_text}
63
+ ]
64
+
65
+ completion = text_client.chat.completions.create(
66
  model="deepseek-ai/DeepSeek-R1",
67
+ messages=messages,
68
  max_tokens=500
69
  )
70
  return completion.choices[0].message.content
71
  except Exception:
72
+ return ""
73
 
74
+ async def tts_interface(text: str, voice: str, rate: int, pitch: int):
75
+ return await text_to_speech(text, voice, rate, pitch)
76
+
77
+ async def create_demo():
78
+ voices = await get_voices()
79
+
80
  with gr.Blocks(theme=theme, title="AI Studio Pro") as demo:
81
+ gr.Markdown("# 🧡💛 AI Creative Studio")
 
 
 
 
 
 
82
 
83
+ # Вкладки
84
  with gr.Tabs():
85
  # Вкладка TTS
86
+ with gr.Tab("🔊 Генерация речи"):
87
  with gr.Row():
88
  with gr.Column():
89
+ text_input = gr.Textbox(
90
+ label="Входной текст",
91
+ lines=5,
92
+ placeholder="Введите текст для озвучки...",
93
+ elem_classes="orange-border"
94
  )
95
+ with gr.Row():
96
+ lang_dropdown = gr.Dropdown(
97
+ choices=["Все языки", "en", "ru", "es", "fr", "de"],
98
+ value="Все языки",
99
+ label="Выберите язык"
100
+ )
101
+ voice_dropdown = gr.Dropdown(
102
+ choices=list(voices.keys()),
103
+ label="Выберите голос",
104
+ interactive=True
105
+ )
106
+ with gr.Row():
107
+ rate_slider = gr.Slider(-50, 50, 0, label="Скорость")
108
+ pitch_slider = gr.Slider(-20, 20, 0, label="Тон")
109
+ generate_btn = gr.Button("Сгенерировать речь", variant="primary")
110
 
111
  with gr.Column():
112
+ audio_output = gr.Audio(label="Результат", elem_classes="orange-border")
113
+
114
+ # Вкладка генерации контента
115
+ with gr.Tab("✨ Генератор контента"):
 
 
 
 
 
 
116
  with gr.Row():
117
  with gr.Column():
118
+ prompt_input = gr.Textbox(
119
+ label="Краткое описание",
120
+ lines=3,
121
+ placeholder="Опишите идею для генерации...",
122
+ elem_classes="orange-border"
123
  )
124
+ with gr.Accordion("Дополнительные настройки", open=False):
125
+ gr.Markdown("Здесь будут дополнительные параметры")
126
+ generate_content_btn = gr.Button("Сгенерировать контент", variant="primary")
127
 
128
  with gr.Column():
129
+ text_output = gr.Textbox(
130
+ label="Сгенерированный текст",
131
+ interactive=False,
132
+ elem_classes="orange-border"
133
+ )
134
+ image_output = gr.Gallery(
135
+ label="Сгенерированные изображения",
136
+ columns=2,
137
+ elem_classes="orange-border"
138
+ )
139
 
140
+ # Обработчики событий
141
+ generate_btn.click(
142
+ tts_interface,
143
+ [text_input, voice_dropdown, rate_slider, pitch_slider],
144
+ audio_output
145
+ )
146
+
147
+ generate_content_btn.click(
148
+ generate_detailed_description,
149
+ prompt_input,
150
+ text_output
151
+ )
152
+
153
+ return demo
154
+
155
+ async def main():
156
+ demo = await create_demo()
157
+ demo.queue()
158
+ demo.launch()
159
 
 
160
  if __name__ == "__main__":
161
+ asyncio.run(main())