Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
-
import sys
|
| 2 |
import torch
|
| 3 |
from audiocraft.models import MusicGen
|
| 4 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 5 |
-
import
|
| 6 |
import gradio as gr
|
| 7 |
from tempfile import NamedTemporaryFile
|
| 8 |
import numpy as np
|
|
@@ -12,13 +12,12 @@ import matplotlib.pyplot as plt
|
|
| 12 |
import librosa.display
|
| 13 |
import librosa
|
| 14 |
from PIL import Image
|
| 15 |
-
import os
|
| 16 |
|
| 17 |
# 1) Startup logs
|
| 18 |
print("=== STARTUP ===")
|
| 19 |
-
print("Python:", sys.version.replace(
|
| 20 |
print("Torch:", torch.__version__)
|
| 21 |
-
print("Device:
|
| 22 |
|
| 23 |
# 2) Force CPU
|
| 24 |
device = torch.device("cpu")
|
|
@@ -37,17 +36,11 @@ print("GPT-2 loaded.")
|
|
| 37 |
# 5) Load Stable Diffusion (CPU-safe)
|
| 38 |
print("Loading Stable Diffusion…")
|
| 39 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 40 |
-
"runwayml/stable-diffusion-v1-5",
|
|
|
|
| 41 |
).to(device)
|
| 42 |
print("Stable Diffusion loaded.")
|
| 43 |
|
| 44 |
-
# 6) Init pyttsx3
|
| 45 |
-
print("Initializing TTS engine…")
|
| 46 |
-
tts_engine = pyttsx3.init()
|
| 47 |
-
tts_engine.setProperty("rate", 150)
|
| 48 |
-
tts_engine.setProperty("volume", 0.8)
|
| 49 |
-
print("TTS engine ready.")
|
| 50 |
-
|
| 51 |
# Emotion helper
|
| 52 |
def get_emotion_tone(text):
|
| 53 |
txt = text.lower()
|
|
@@ -68,22 +61,21 @@ def generate_image(prompt, style="realistic"):
|
|
| 68 |
print("Image error:", e)
|
| 69 |
return None
|
| 70 |
|
| 71 |
-
# Text-to-audio
|
| 72 |
def text_to_audio(text):
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
return tmp.name
|
| 82 |
|
| 83 |
# Music generation
|
| 84 |
def generate_music(prompt):
|
| 85 |
try:
|
| 86 |
-
wav = music_model.generate([prompt])
|
| 87 |
data = wav.cpu().numpy()[0,0]
|
| 88 |
tmp = NamedTemporaryFile(delete=False, suffix=".wav")
|
| 89 |
wavfile.write(tmp.name, music_model.sample_rate, data)
|
|
|
|
| 1 |
+
import sys, os
|
| 2 |
import torch
|
| 3 |
from audiocraft.models import MusicGen
|
| 4 |
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
| 5 |
+
from gtts import gTTS
|
| 6 |
import gradio as gr
|
| 7 |
from tempfile import NamedTemporaryFile
|
| 8 |
import numpy as np
|
|
|
|
| 12 |
import librosa.display
|
| 13 |
import librosa
|
| 14 |
from PIL import Image
|
|
|
|
| 15 |
|
| 16 |
# 1) Startup logs
|
| 17 |
print("=== STARTUP ===")
|
| 18 |
+
print("Python:", sys.version.replace('\n',' '))
|
| 19 |
print("Torch:", torch.__version__)
|
| 20 |
+
print("Device: CPU")
|
| 21 |
|
| 22 |
# 2) Force CPU
|
| 23 |
device = torch.device("cpu")
|
|
|
|
| 36 |
# 5) Load Stable Diffusion (CPU-safe)
|
| 37 |
print("Loading Stable Diffusion…")
|
| 38 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 39 |
+
"runwayml/stable-diffusion-v1-5",
|
| 40 |
+
torch_dtype=torch.float32
|
| 41 |
).to(device)
|
| 42 |
print("Stable Diffusion loaded.")
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
# Emotion helper
|
| 45 |
def get_emotion_tone(text):
|
| 46 |
txt = text.lower()
|
|
|
|
| 61 |
print("Image error:", e)
|
| 62 |
return None
|
| 63 |
|
| 64 |
+
# Text-to-audio via gTTS
|
| 65 |
def text_to_audio(text):
|
| 66 |
+
try:
|
| 67 |
+
tts = gTTS(text=text, lang="en")
|
| 68 |
+
tmp = NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 69 |
+
tts.save(tmp.name)
|
| 70 |
+
return tmp.name
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print("TTS error:", e)
|
| 73 |
+
return None
|
|
|
|
| 74 |
|
| 75 |
# Music generation
|
| 76 |
def generate_music(prompt):
|
| 77 |
try:
|
| 78 |
+
wav = music_model.generate([prompt]) # [1,1,T]
|
| 79 |
data = wav.cpu().numpy()[0,0]
|
| 80 |
tmp = NamedTemporaryFile(delete=False, suffix=".wav")
|
| 81 |
wavfile.write(tmp.name, music_model.sample_rate, data)
|