Subiksha0515 commited on
Commit
e69e1fb
·
verified ·
1 Parent(s): a09fedc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -48
app.py CHANGED
@@ -2,61 +2,45 @@ import gradio as gr
2
  import numpy as np
3
  import soundfile as sf
4
  import sentencepiece as spm
5
- from safetensors.numpy import load_file
 
6
 
7
  # Load tokenizer
8
  sp = spm.SentencePieceProcessor()
9
  sp.load("tokenizer.model")
10
 
11
- # Load model tensors
12
  tensors = load_file("model.safetensors")
13
 
14
  print("Model loaded successfully")
15
 
 
 
16
 
17
- # Dequantization function
18
- def dequantize_weights(tensors):
19
 
20
- weights = {}
21
 
22
- for name in list(tensors.keys()):
 
23
 
24
- if name.endswith("_scale"):
25
- continue
26
 
27
- scale_name = name + "_scale"
28
 
29
- if scale_name in tensors:
30
 
31
- weight_i8 = tensors[name].astype(np.float32)
32
-
33
- scale = tensors[scale_name].astype(np.float32)
34
-
35
- weights[name] = weight_i8 * scale
36
-
37
- else:
38
- weights[name] = tensors[name]
39
-
40
- return weights
41
-
42
-
43
- weights = dequantize_weights(tensors)
44
-
45
- print("Weights dequantized successfully")
46
-
47
-
48
- # Text-to-Speech function
49
  def text_to_speech(text):
50
 
51
  if text.strip() == "":
52
  return None
53
 
54
- # Tokenize
55
  tokens = sp.encode(text)
56
 
57
  print("Tokens:", tokens)
58
 
59
- # Generate dummy waveform (replace with real model inference)
60
  duration = max(2, len(tokens) * 0.1)
61
 
62
  sample_rate = 24000
@@ -65,7 +49,6 @@ def text_to_speech(text):
65
 
66
  audio = 0.2 * np.sin(2 * np.pi * 220 * t)
67
 
68
- # Save file
69
  output_path = "output.wav"
70
 
71
  sf.write(output_path, audio, sample_rate)
@@ -73,27 +56,15 @@ def text_to_speech(text):
73
  return output_path
74
 
75
 
76
- # Gradio Interface
77
  demo = gr.Interface(
78
-
79
  fn=text_to_speech,
80
-
81
- inputs=gr.Textbox(
82
- label="Enter text",
83
- placeholder="Type text to convert into speech"
84
- ),
85
-
86
- outputs=gr.Audio(
87
- label="Generated Speech",
88
- type="filepath"
89
- ),
90
-
91
- title="Pocket TTS Text-to-Speech",
92
-
93
- description="Enter text and generate speech using Pocket-TTS INT8 model"
94
  )
95
 
96
-
97
  demo.launch(
98
  server_name="0.0.0.0",
99
  server_port=7860,
 
2
  import numpy as np
3
  import soundfile as sf
4
  import sentencepiece as spm
5
+ import torch
6
+ from safetensors.torch import load_file
7
 
8
  # Load tokenizer
9
  sp = spm.SentencePieceProcessor()
10
  sp.load("tokenizer.model")
11
 
12
+ # Load model safely using torch (FIX)
13
  tensors = load_file("model.safetensors")
14
 
15
  print("Model loaded successfully")
16
 
17
+ # Convert tensors to float32 safely
18
+ weights = {}
19
 
20
+ for name in tensors:
 
21
 
22
+ tensor = tensors[name]
23
 
24
+ if tensor.dtype == torch.bfloat16:
25
+ tensor = tensor.to(torch.float32)
26
 
27
+ weights[name] = tensor.cpu().numpy()
 
28
 
29
+ print("Weights converted successfully")
30
 
 
31
 
32
+ # Text to speech function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  def text_to_speech(text):
34
 
35
  if text.strip() == "":
36
  return None
37
 
38
+ # Tokenize text
39
  tokens = sp.encode(text)
40
 
41
  print("Tokens:", tokens)
42
 
43
+ # Generate simple waveform (placeholder)
44
  duration = max(2, len(tokens) * 0.1)
45
 
46
  sample_rate = 24000
 
49
 
50
  audio = 0.2 * np.sin(2 * np.pi * 220 * t)
51
 
 
52
  output_path = "output.wav"
53
 
54
  sf.write(output_path, audio, sample_rate)
 
56
  return output_path
57
 
58
 
59
+ # Gradio UI
60
  demo = gr.Interface(
 
61
  fn=text_to_speech,
62
+ inputs=gr.Textbox(label="Enter text"),
63
+ outputs=gr.Audio(type="filepath"),
64
+ title="Pocket TTS INT8 (CPU)",
65
+ description="Text-to-Speech using Pocket TTS model"
 
 
 
 
 
 
 
 
 
 
66
  )
67
 
 
68
  demo.launch(
69
  server_name="0.0.0.0",
70
  server_port=7860,