HardbanRecordsLab commited on
Commit
5703e10
verified
1 Parent(s): 8561786

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -58
app.py CHANGED
@@ -1,5 +1,5 @@
1
  # ==============================================================================
2
- # KROK NAPRAWCZY: Wymuszona instalacja brakuj膮cych bibliotek
3
  # ==============================================================================
4
  import os
5
  import subprocess
@@ -25,7 +25,7 @@ if 'PACKAGES_INSTALLED' not in os.environ:
25
  os.environ['PACKAGES_INSTALLED'] = 'TRUE'
26
 
27
  # ==============================================================================
28
- # Reszta kodu aplikacji
29
  # ==============================================================================
30
  import gradio as gr
31
  from transformers import pipeline as text_pipeline
@@ -34,7 +34,6 @@ import torch
34
  import re
35
  from fpdf import FPDF
36
  from PIL import Image
37
- import io
38
  import time
39
  from datasets import Dataset, Features, Value, Sequence, Image as HFImage
40
  from gradio_client import Client
@@ -44,7 +43,7 @@ import numpy as np
44
  from moviepy.editor import ImageClip, concatenate_videoclips, AudioFileClip, CompositeAudioClip
45
  from speechbrain.pretrained import EncoderClassifier
46
 
47
- # --- Konfiguracja Modeli i Danych ---
48
  LLM_MODEL = "HuggingFaceH4/zephyr-7b-beta"
49
  IMAGE_MODEL = "stabilityai/stable-diffusion-2-1-base"
50
  TTS_MODEL = "microsoft/speecht5_tts"
@@ -52,53 +51,46 @@ VOCODER_MODEL = "microsoft/speecht5_hifigan"
52
  SPEAKER_EMBEDDING_MODEL = "speechbrain/spkrec-xvect-voxceleb"
53
  DATASET_PATH = "saved_courses"
54
  TEXT_TO_3D_MODEL_SPACE = "stabilityai/TripoSR"
55
-
56
- # --- 艁adowanie Modeli i Klient贸w ---
57
  device = "cuda" if torch.cuda.is_available() else "cpu"
58
 
59
- try:
60
- print("艁adowanie modelu j臋zykowego..."); text_generator = text_pipeline("text-generation", model=LLM_MODEL, torch_dtype=torch.bfloat16, device_map="auto"); LLM_LOADED = True; print("Model j臋zykowy za艂adowany.")
61
- except Exception as e: print(f"B艂膮d 艂adowania LLM: {e}"); text_generator = None; LLM_LOADED = False
62
-
63
- try:
64
- print("艁adowanie modelu obrazkowego...")
65
- image_generator = DiffusionPipeline.from_pretrained(IMAGE_MODEL)
66
- image_generator.to(device)
67
- IMAGE_MODEL_LOADED = True
68
- print("Model obrazkowy za艂adowany.")
69
- except Exception as e:
70
- print(f"B艂膮d 艂adowania Image Model: {e}"); image_generator = None; IMAGE_MODEL_LOADED = False
71
-
72
- try:
73
- print("Inicjalizacja klienta 3D..."); client_3d = Client(TEXT_TO_3D_MODEL_SPACE); CLIENT_3D_LOADED = True; print("Klient 3D gotowy.")
74
- except Exception as e: print(f"B艂膮d inicjalizacji klienta 3D: {e}"); client_3d = None; CLIENT_3D_LOADED = False
75
-
76
- try:
77
- print("艁adowanie modeli TTS...")
78
- tts_processor = SpeechT5Processor.from_pretrained(TTS_MODEL)
79
- tts_model = SpeechT5ForTextToSpeech.from_pretrained(TTS_MODEL).to(device)
80
- vocoder = SpeechT5HifiGan.from_pretrained(VOCODER_MODEL).to(device)
81
- spk_model_source = SPEAKER_EMBEDDING_MODEL
82
- speaker_model = EncoderClassifier.from_hparams(source=spk_model_source, savedir=os.path.join('/tmp', spk_model_source))
83
- dummy_waveform = torch.randn(1, 16000).to(device)
84
- speaker_embeddings = speaker_model.encode_batch(dummy_waveform).squeeze().to(device)
85
- TTS_LOADED = True
86
- print("Modele TTS za艂adowane.")
87
- except Exception as e:
88
- print(f"B艂膮d 艂adowania modeli TTS: {e}"); TTS_LOADED = False
89
-
90
-
91
- # --- Funkcje Aplikacji ---
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  def list_saved_projects():
94
- if not os.path.exists(DATASET_PATH):
95
- return []
96
  try:
97
  dataset = Dataset.load_from_disk(DATASET_PATH)
98
  return dataset["project_name"]
99
  except Exception as e:
100
- print(f"B艂膮d odczytu zapisanych projekt贸w: {e}")
101
- return []
102
 
103
  def parse_course_to_structure(markdown_text):
104
  course_data = {'title': "Nowy Kurs", 'steps': []}
@@ -112,10 +104,11 @@ def parse_course_to_structure(markdown_text):
112
 
113
  def generate_course_structure_and_images(topic, progress=gr.Progress(track_tqdm=True)):
114
  if not LLM_LOADED or not IMAGE_MODEL_LOADED:
 
115
  return [None, gr.State([])] + [gr.update(visible=False)]*2 + [gr.update(interactive=False)]*3 + [gr.update()]*11
116
 
117
  progress(0, desc="Generowanie tekstu kursu...")
118
- prompt = f"<|system|>\nJeste艣 ekspertem w tworzeniu kurs贸w online. Twoim zadaniem jest stworzenie zwi臋z艂ego, 5-etapowego planu kursu DIY na podany temat. Wygeneruj odpowied藕 w formacie Markdown, kt贸ra zawiera: 1. Chwytliwy tytu艂 kursu (jako nag艂贸wek H1). 2. Pi臋膰 ponumerowanych krok贸w kursu. Ka偶dy krok powinien mie膰 tytu艂 (pogrubiony) i kr贸tki, 2-3 zdaniowy opis. Nie dodawaj 偶adnych wst臋p贸w, podsumowa艅 ani dodatkowych komentarzy.</s>\n<|user|>\nTemat kursu: \"{topic}\"</s>\n<|assistant|>"
119
  response = text_generator(prompt, max_new_tokens=1024, do_sample=True, temperature=0.7, top_p=0.95)
120
  course_text = response[0]['generated_text'].split('<|assistant|>')[-1].strip()
121
  course_data = parse_course_to_structure(course_text)
@@ -134,10 +127,11 @@ def generate_course_structure_and_images(topic, progress=gr.Progress(track_tqdm=
134
  if i < len(course_data['steps']):
135
  updates.extend([gr.update(value=course_data['steps'][i]['title']), gr.update(value=course_data['steps'][i]['description'])])
136
  else:
137
- updates.extend([gr.update(value=""), gr.update(value="")])
138
 
139
  return generated_images, gr.State(generated_images), gr.update(visible=True), gr.update(visible=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), *updates
140
 
 
141
  def load_project(project_name):
142
  if not project_name:
143
  gr.Warning("Wybierz projekt do wczytania.")
@@ -154,7 +148,7 @@ def load_project(project_name):
154
  step = project_data['steps'][i]
155
  updates.extend([gr.update(value=step['title']), gr.update(value=step['description'])])
156
  else:
157
- updates.extend([gr.update(value=""), gr.update(value="")])
158
 
159
  return loaded_images, gr.State(loaded_images), gr.update(visible=True), gr.update(visible=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), *updates
160
 
@@ -288,16 +282,7 @@ def generate_pdf_from_ui(images_state, course_title, *args):
288
  return pdf_output_path
289
 
290
  # --- Budowa Interfejsu Gradio ---
291
- custom_css = """
292
- body, #root { height: 100vh; margin: 0; padding: 0; }
293
- .gradio-container { max-width: 100% !important; }
294
- .main, .wrap { height: 100%; }
295
- .gap { height: 100%; }
296
- .app-title { text-align: center; font-size: 2.5em; color: #2c3e50; font-weight: bold; margin-bottom: 0px !important; }
297
- .app-subtitle { text-align: center; font-size: 1.1em; color: #576574; margin-top: 5px; margin-bottom: 20px; }
298
- .section-title { font-size: 1.5em; font-weight: bold; color: #2980b9; border-bottom: 2px solid #3498db; padding-bottom: 5px; margin-top: 10px; }
299
- footer { display: none !important; }
300
- """
301
 
302
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="sky", secondary_hue="blue"), title="Kreator Kurs贸w AI", css=custom_css) as demo:
303
 
@@ -408,5 +393,8 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="sky", secondary_hue="blue"), ti
408
  inputs=[images_state] + editor_text_fields,
409
  outputs=[video_output]
410
  )
411
-
412
- demo.launch(show_api=False)
 
 
 
 
1
  # ==============================================================================
2
+ # Wymuszona instalacja bibliotek
3
  # ==============================================================================
4
  import os
5
  import subprocess
 
25
  os.environ['PACKAGES_INSTALLED'] = 'TRUE'
26
 
27
  # ==============================================================================
28
+ # G艂贸wny kod aplikacji
29
  # ==============================================================================
30
  import gradio as gr
31
  from transformers import pipeline as text_pipeline
 
34
  import re
35
  from fpdf import FPDF
36
  from PIL import Image
 
37
  import time
38
  from datasets import Dataset, Features, Value, Sequence, Image as HFImage
39
  from gradio_client import Client
 
43
  from moviepy.editor import ImageClip, concatenate_videoclips, AudioFileClip, CompositeAudioClip
44
  from speechbrain.pretrained import EncoderClassifier
45
 
46
+ # --- Konfiguracja ---
47
  LLM_MODEL = "HuggingFaceH4/zephyr-7b-beta"
48
  IMAGE_MODEL = "stabilityai/stable-diffusion-2-1-base"
49
  TTS_MODEL = "microsoft/speecht5_tts"
 
51
  SPEAKER_EMBEDDING_MODEL = "speechbrain/spkrec-xvect-voxceleb"
52
  DATASET_PATH = "saved_courses"
53
  TEXT_TO_3D_MODEL_SPACE = "stabilityai/TripoSR"
 
 
54
  device = "cuda" if torch.cuda.is_available() else "cpu"
55
 
56
+ # --- Globalne zmienne dla modeli ---
57
+ text_generator, image_generator, client_3d, tts_processor, tts_model, vocoder, speaker_embeddings = (None,) * 7
58
+ LLM_LOADED, IMAGE_MODEL_LOADED, CLIENT_3D_LOADED, TTS_LOADED = (False,) * 4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
+ # --- Funkcje 艂adowania modeli ---
61
+ def load_models():
62
+ global text_generator, image_generator, client_3d, tts_processor, tts_model, vocoder, speaker_embeddings
63
+ global LLM_LOADED, IMAGE_MODEL_LOADED, CLIENT_3D_LOADED, TTS_LOADED
64
+
65
+ try:
66
+ print("艁adowanie modelu j臋zykowego..."); text_generator = text_pipeline("text-generation", model=LLM_MODEL, torch_dtype=torch.bfloat16, device_map="auto"); LLM_LOADED = True; print("Model j臋zykowy za艂adowany.")
67
+ except Exception as e: print(f"B艂膮d 艂adowania LLM: {e}")
68
+ try:
69
+ print("艁adowanie modelu obrazkowego..."); image_generator = DiffusionPipeline.from_pretrained(IMAGE_MODEL); image_generator.to(device); IMAGE_MODEL_LOADED = True; print("Model obrazkowy za艂adowany.")
70
+ except Exception as e: print(f"B艂膮d 艂adowania Image Model: {e}")
71
+ try:
72
+ print("Inicjalizacja klienta 3D..."); client_3d = Client(TEXT_TO_3D_MODEL_SPACE); CLIENT_3D_LOADED = True; print("Klient 3D gotowy.")
73
+ except Exception as e: print(f"B艂膮d inicjalizacji klienta 3D: {e}")
74
+ try:
75
+ print("艁adowanie modeli TTS...")
76
+ tts_processor = SpeechT5Processor.from_pretrained(TTS_MODEL)
77
+ tts_model = SpeechT5ForTextToSpeech.from_pretrained(TTS_MODEL).to(device)
78
+ vocoder = SpeechT5HifiGan.from_pretrained(VOCODER_MODEL).to(device)
79
+ speaker_model = EncoderClassifier.from_hparams(source=SPEAKER_EMBEDDING_MODEL, savedir=os.path.join('/tmp', SPEAKER_EMBEDDING_MODEL))
80
+ dummy_waveform = torch.randn(1, 16000).to(device)
81
+ speaker_embeddings = speaker_model.encode_batch(dummy_waveform).squeeze().to(device)
82
+ TTS_LOADED = True
83
+ print("Modele TTS za艂adowane.")
84
+ except Exception as e: print(f"B艂膮d 艂adowania modeli TTS: {e}")
85
+
86
+ # --- Funkcje pomocnicze i g艂贸wne ---
87
  def list_saved_projects():
88
+ if not os.path.exists(DATASET_PATH): return []
 
89
  try:
90
  dataset = Dataset.load_from_disk(DATASET_PATH)
91
  return dataset["project_name"]
92
  except Exception as e:
93
+ print(f"B艂膮d odczytu projekt贸w: {e}"); return []
 
94
 
95
  def parse_course_to_structure(markdown_text):
96
  course_data = {'title': "Nowy Kurs", 'steps': []}
 
104
 
105
  def generate_course_structure_and_images(topic, progress=gr.Progress(track_tqdm=True)):
106
  if not LLM_LOADED or not IMAGE_MODEL_LOADED:
107
+ gr.Error("Kluczowe modele AI nie zosta艂y za艂adowane. Sprawd藕 logi.")
108
  return [None, gr.State([])] + [gr.update(visible=False)]*2 + [gr.update(interactive=False)]*3 + [gr.update()]*11
109
 
110
  progress(0, desc="Generowanie tekstu kursu...")
111
+ prompt = f"<|system|>\nJeste艣 ekspertem w tworzeniu kurs贸w online...</s>\n<|user|>\nTemat kursu: \"{topic}\"</s>\n<|assistant|>"
112
  response = text_generator(prompt, max_new_tokens=1024, do_sample=True, temperature=0.7, top_p=0.95)
113
  course_text = response[0]['generated_text'].split('<|assistant|>')[-1].strip()
114
  course_data = parse_course_to_structure(course_text)
 
127
  if i < len(course_data['steps']):
128
  updates.extend([gr.update(value=course_data['steps'][i]['title']), gr.update(value=course_data['steps'][i]['description'])])
129
  else:
130
+ updates.extend(["", ""])
131
 
132
  return generated_images, gr.State(generated_images), gr.update(visible=True), gr.update(visible=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), *updates
133
 
134
+ # ... (pozosta艂e funkcje: load_project, update_preview, move_step, etc. bez zmian) ...
135
  def load_project(project_name):
136
  if not project_name:
137
  gr.Warning("Wybierz projekt do wczytania.")
 
148
  step = project_data['steps'][i]
149
  updates.extend([gr.update(value=step['title']), gr.update(value=step['description'])])
150
  else:
151
+ updates.extend(["", ""])
152
 
153
  return loaded_images, gr.State(loaded_images), gr.update(visible=True), gr.update(visible=True), gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True), *updates
154
 
 
282
  return pdf_output_path
283
 
284
  # --- Budowa Interfejsu Gradio ---
285
+ custom_css = "body, #root { height: 100vh; margin: 0; padding: 0; } .gradio-container { max-width: 100% !important; } .main, .wrap, .gap { height: 100%; } .app-title { text-align: center; font-size: 2.5em; color: #2c3e50; font-weight: bold; margin-bottom: 0px !important; } .app-subtitle { text-align: center; font-size: 1.1em; color: #576574; margin-top: 5px; margin-bottom: 20px; } .section-title { font-size: 1.5em; font-weight: bold; color: #2980b9; border-bottom: 2px solid #3498db; padding-bottom: 5px; margin-top: 10px; } footer { display: none !important; }"
 
 
 
 
 
 
 
 
 
286
 
287
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="sky", secondary_hue="blue"), title="Kreator Kurs贸w AI", css=custom_css) as demo:
288
 
 
393
  inputs=[images_state] + editor_text_fields,
394
  outputs=[video_output]
395
  )
396
+
397
+ # Uruchomienie aplikacji po za艂adowaniu modeli
398
+ demo.queue().launch(show_api=False)
399
+ # Wywo艂anie funkcji 艂aduj膮cej modele po zdefiniowaniu interfejsu
400
+ load_models()