hebaadel commited on
Commit
cdbe10d
·
verified ·
1 Parent(s): ae7c9b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -51
app.py CHANGED
@@ -4,23 +4,9 @@ import soundfile as sf
4
  import torch
5
  import gradio as gr
6
  import numpy as np
7
-
8
  from transformers import MarianMTModel, MarianTokenizer
9
 
10
- model_name = "Helsinki-NLP/opus-mt-en-ar"
11
-
12
- # Use MarianTokenizer specifically
13
- tokenizer = MarianTokenizer.from_pretrained(model_name)
14
- model = MarianMTModel.from_pretrained(model_name)
15
-
16
- def translate_to_arabic(text):
17
- inputs = tokenizer(text, return_tensors="pt", padding=True)
18
- outputs = model.generate(**inputs, max_length=100)
19
- translated = tokenizer.decode(outputs[0], skip_special_tokens=True)
20
- return translated
21
-
22
- # Test it
23
- print(translate_to_arabic("Hello, how are you?"))
24
 
25
  def predict_image(image):
26
  pipe = pipeline("image-classification", model="google/vit-base-patch16-224")
@@ -34,52 +20,22 @@ def translate_to_arabic(text):
34
  result=pipe(text , max_length=100)
35
  return result[0]['translation_text']
36
 
37
- # Use a pipeline as a high-level helper
38
- # Warning: Pipeline type "translation" is no longer supported in transformers v5.
39
- # You must load the model directly (see below) or downgrade to v4.x with:
40
- # 'pip install "transformers<5.0.0'
41
- from transformers import pipeline
42
-
43
- pipe = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ar")
44
 
45
  def translate_to_arabic(text):
 
 
 
 
 
 
46
  inputs = tokenizer(text, return_tensors="pt", padding=True)
47
  outputs = model.generate(**inputs, max_length=100)
48
  translated = tokenizer.decode(outputs[0], skip_special_tokens=True)
49
  return translated
50
 
51
- """✅ Because SpeechT5 requires a speaker embedding
52
-
53
- The model MBZUAI/speecht5_tts_clartts_ar is based on SpeechT5, and SpeechT5 needs a speaker embedding to generate speech.
54
-
55
- Think of it like this:
56
-
57
- The text tells the model what to say.
58
-
59
- The speaker embedding tells the model what the voice should sound like.
60
-
61
- Without a speaker embedding, the model does not know what voice to use → and the pipeline will fail or produce wrong audio.
62
-
63
- What is the dataset?
64
-
65
- herwoww/arabic_xvector_embeddings contains pre-computed speaker embeddings (x-vectors) for different Arabic speakers.
66
-
67
- Each embedding is like a "voice fingerprint".
68
-
69
- You pick one of them to generate speech in that voice.
70
-
71
- In your code, you choose speaker number 105:
72
- """
73
-
74
  def text_to_speech(text):
75
  pipe = pipeline("text-to-speech", model="MBZUAI/speecht5_tts_clartts_ar")
76
  embedding_dataset=load_dataset("herwoww/arabic_xvector_embeddings" , split="validation") #Those embeddings represent a speaker’s voice characteristics
77
- '''
78
- This is a 1D tensor.
79
- But most models expect: (batch_size, input_size)
80
- torch.Size([1, 3]) 1 sample (batch size = 1)
81
- (784,)>> (1, 784)>> (batch_size, 784)
82
- '''
83
  speaker_embedding=torch.tensor(embedding_dataset[100]['speaker_embeddings']).unsqueeze(0) ##It becomes a 2-D tensor
84
  speech=pipe(text , forward_params={'speaker_embeddings':speaker_embedding})
85
 
 
4
  import torch
5
  import gradio as gr
6
  import numpy as np
7
+ import
8
  from transformers import MarianMTModel, MarianTokenizer
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def predict_image(image):
12
  pipe = pipeline("image-classification", model="google/vit-base-patch16-224")
 
20
  result=pipe(text , max_length=100)
21
  return result[0]['translation_text']
22
 
 
 
 
 
 
 
 
23
 
24
  def translate_to_arabic(text):
25
+ model_name = "Helsinki-NLP/opus-mt-en-ar"
26
+
27
+ # Use MarianTokenizer specifically
28
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
29
+ model = MarianMTModel.from_pretrained(model_name)
30
+
31
  inputs = tokenizer(text, return_tensors="pt", padding=True)
32
  outputs = model.generate(**inputs, max_length=100)
33
  translated = tokenizer.decode(outputs[0], skip_special_tokens=True)
34
  return translated
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  def text_to_speech(text):
37
  pipe = pipeline("text-to-speech", model="MBZUAI/speecht5_tts_clartts_ar")
38
  embedding_dataset=load_dataset("herwoww/arabic_xvector_embeddings" , split="validation") #Those embeddings represent a speaker’s voice characteristics
 
 
 
 
 
 
39
  speaker_embedding=torch.tensor(embedding_dataset[100]['speaker_embeddings']).unsqueeze(0) ##It becomes a 2-D tensor
40
  speech=pipe(text , forward_params={'speaker_embeddings':speaker_embedding})
41