Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ import io
|
|
5 |
import re
|
6 |
import torch
|
7 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
8 |
-
from huggingface_hub import snapshot_download
|
9 |
import torchaudio
|
10 |
from torchaudio.functional import resample
|
11 |
import threading
|
@@ -16,41 +16,47 @@ import logging
|
|
16 |
logging.basicConfig(level=logging.INFO)
|
17 |
logger = logging.getLogger(__name__)
|
18 |
|
19 |
-
# Initialize Gemini AI
|
20 |
-
genai.configure(api_key='YOUR_GEMINI_API_KEY')
|
21 |
-
|
22 |
# Set up device
|
23 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
24 |
|
25 |
-
#
|
26 |
-
print("Loading Orpheus model...")
|
27 |
model_name = "canopylabs/orpheus-3b-0.1-ft"
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
def generate_podcast_script(api_key, content, duration, num_hosts):
|
56 |
genai.configure(api_key=api_key)
|
@@ -90,6 +96,7 @@ def generate_podcast_script(api_key, content, duration, num_hosts):
|
|
90 |
return clean_text
|
91 |
|
92 |
def text_to_speech(text, voice):
|
|
|
93 |
inputs = tokenizer(text, return_tensors="pt").to(device)
|
94 |
with torch.no_grad():
|
95 |
output = model.generate(**inputs, max_new_tokens=256)
|
@@ -131,6 +138,10 @@ def render_podcast(api_key, script, voice1, voice2, num_hosts):
|
|
131 |
with gr.Blocks() as demo:
|
132 |
gr.Markdown("# AI Podcast Generator")
|
133 |
|
|
|
|
|
|
|
|
|
134 |
api_key_input = gr.Textbox(label="Enter your Gemini API Key", type="password")
|
135 |
|
136 |
with gr.Row():
|
@@ -153,6 +164,13 @@ with gr.Blocks() as demo:
|
|
153 |
render_btn = gr.Button("Render Podcast")
|
154 |
audio_output = gr.Audio(label="Generated Podcast")
|
155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
def generate_script_wrapper(api_key, content, duration, num_hosts):
|
157 |
return generate_podcast_script(api_key, content, duration, num_hosts)
|
158 |
|
|
|
5 |
import re
|
6 |
import torch
|
7 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
8 |
+
from huggingface_hub import snapshot_download, login
|
9 |
import torchaudio
|
10 |
from torchaudio.functional import resample
|
11 |
import threading
|
|
|
16 |
logging.basicConfig(level=logging.INFO)
|
17 |
logger = logging.getLogger(__name__)
|
18 |
|
|
|
|
|
|
|
19 |
# Set up device
|
20 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
21 |
|
22 |
+
# Model name
|
|
|
23 |
model_name = "canopylabs/orpheus-3b-0.1-ft"
|
24 |
|
25 |
+
def load_model(hf_token):
|
26 |
+
login(token=hf_token)
|
27 |
+
|
28 |
+
print("Loading Orpheus model...")
|
29 |
+
snapshot_download(
|
30 |
+
repo_id=model_name,
|
31 |
+
use_auth_token=hf_token,
|
32 |
+
allow_patterns=[
|
33 |
+
"config.json",
|
34 |
+
"*.safetensors",
|
35 |
+
"model.safetensors.index.json",
|
36 |
+
],
|
37 |
+
ignore_patterns=[
|
38 |
+
"optimizer.pt",
|
39 |
+
"pytorch_model.bin",
|
40 |
+
"training_args.bin",
|
41 |
+
"scheduler.pt",
|
42 |
+
"tokenizer.json",
|
43 |
+
"tokenizer_config.json",
|
44 |
+
"special_tokens_map.json",
|
45 |
+
"vocab.json",
|
46 |
+
"merges.txt",
|
47 |
+
"tokenizer.*"
|
48 |
+
]
|
49 |
+
)
|
50 |
+
|
51 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16)
|
52 |
+
model.to(device)
|
53 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
54 |
+
print(f"Orpheus model loaded to {device}")
|
55 |
+
return model, tokenizer
|
56 |
+
|
57 |
+
# Initialize as None, will be loaded when HF token is provided
|
58 |
+
model = None
|
59 |
+
tokenizer = None
|
60 |
|
61 |
def generate_podcast_script(api_key, content, duration, num_hosts):
|
62 |
genai.configure(api_key=api_key)
|
|
|
96 |
return clean_text
|
97 |
|
98 |
def text_to_speech(text, voice):
|
99 |
+
global model, tokenizer
|
100 |
inputs = tokenizer(text, return_tensors="pt").to(device)
|
101 |
with torch.no_grad():
|
102 |
output = model.generate(**inputs, max_new_tokens=256)
|
|
|
138 |
with gr.Blocks() as demo:
|
139 |
gr.Markdown("# AI Podcast Generator")
|
140 |
|
141 |
+
hf_token_input = gr.Textbox(label="Enter your Hugging Face API Token", type="password")
|
142 |
+
load_model_btn = gr.Button("Load Orpheus Model")
|
143 |
+
model_status = gr.Markdown("Model not loaded")
|
144 |
+
|
145 |
api_key_input = gr.Textbox(label="Enter your Gemini API Key", type="password")
|
146 |
|
147 |
with gr.Row():
|
|
|
164 |
render_btn = gr.Button("Render Podcast")
|
165 |
audio_output = gr.Audio(label="Generated Podcast")
|
166 |
|
167 |
+
def load_model_wrapper(hf_token):
|
168 |
+
global model, tokenizer
|
169 |
+
model, tokenizer = load_model(hf_token)
|
170 |
+
return "Model loaded successfully"
|
171 |
+
|
172 |
+
load_model_btn.click(load_model_wrapper, inputs=[hf_token_input], outputs=[model_status])
|
173 |
+
|
174 |
def generate_script_wrapper(api_key, content, duration, num_hosts):
|
175 |
return generate_podcast_script(api_key, content, duration, num_hosts)
|
176 |
|