Update app.py
Browse files
app.py
CHANGED
@@ -16,30 +16,27 @@ import logging
|
|
16 |
logging.basicConfig(level=logging.INFO)
|
17 |
logger = logging.getLogger(__name__)
|
18 |
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
model = None
|
22 |
tokenizer = None
|
23 |
|
24 |
-
import torch
|
25 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
26 |
-
from huggingface_hub import snapshot_download, login
|
27 |
-
import os
|
28 |
-
|
29 |
-
# Set up device
|
30 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
31 |
-
|
32 |
-
# Initialize model and tokenizer
|
33 |
-
model = None
|
34 |
-
tokenizer = None
|
35 |
-
|
36 |
def load_model():
|
37 |
global model, tokenizer
|
38 |
|
39 |
print("Loading Orpheus model...")
|
40 |
model_name = "canopylabs/orpheus-3b-0.1-ft"
|
41 |
|
42 |
-
# Get Hugging Face token from environment variable
|
43 |
hf_token = os.environ.get("HUGGINGFACE_TOKEN")
|
44 |
if not hf_token:
|
45 |
raise ValueError("HUGGINGFACE_TOKEN environment variable is not set")
|
@@ -68,26 +65,11 @@ def load_model():
|
|
68 |
]
|
69 |
)
|
70 |
|
71 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16)
|
72 |
model.to(device)
|
73 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
74 |
print(f"Orpheus model and tokenizer loaded to {device}")
|
75 |
|
76 |
-
# Load the model before creating the Gradio interface
|
77 |
-
load_model()
|
78 |
-
|
79 |
-
def text_to_speech(text, voice):
|
80 |
-
global model, tokenizer
|
81 |
-
if tokenizer is None or model is None:
|
82 |
-
raise ValueError("Model or tokenizer not initialized. Please call load_model() first.")
|
83 |
-
|
84 |
-
inputs = tokenizer(text, return_tensors="pt").to(device)
|
85 |
-
with torch.no_grad():
|
86 |
-
output = model.generate(**inputs, max_new_tokens=256)
|
87 |
-
mel = output[0].cpu().numpy()
|
88 |
-
audio = mel_to_audio(mel)
|
89 |
-
return audio
|
90 |
-
|
91 |
def generate_podcast_script(api_key, content, duration, num_hosts):
|
92 |
genai.configure(api_key=api_key)
|
93 |
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
|
|
|
16 |
logging.basicConfig(level=logging.INFO)
|
17 |
logger = logging.getLogger(__name__)
|
18 |
|
19 |
+
def get_device():
|
20 |
+
if torch.cuda.is_available():
|
21 |
+
try:
|
22 |
+
torch.cuda.init()
|
23 |
+
return torch.device("cuda")
|
24 |
+
except Exception as e:
|
25 |
+
print(f"CUDA initialization failed: {e}")
|
26 |
+
return torch.device("cpu")
|
27 |
+
|
28 |
+
device = get_device()
|
29 |
+
print(f"Using device: {device}")
|
30 |
|
31 |
model = None
|
32 |
tokenizer = None
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
def load_model():
|
35 |
global model, tokenizer
|
36 |
|
37 |
print("Loading Orpheus model...")
|
38 |
model_name = "canopylabs/orpheus-3b-0.1-ft"
|
39 |
|
|
|
40 |
hf_token = os.environ.get("HUGGINGFACE_TOKEN")
|
41 |
if not hf_token:
|
42 |
raise ValueError("HUGGINGFACE_TOKEN environment variable is not set")
|
|
|
65 |
]
|
66 |
)
|
67 |
|
68 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float32 if device.type == 'cpu' else torch.bfloat16)
|
69 |
model.to(device)
|
70 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
71 |
print(f"Orpheus model and tokenizer loaded to {device}")
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
def generate_podcast_script(api_key, content, duration, num_hosts):
|
74 |
genai.configure(api_key=api_key)
|
75 |
model = genai.GenerativeModel('gemini-2.5-pro-preview-03-25')
|