File size: 4,867 Bytes
2bd9669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
---
language:
  - fa
tags:
  - g2p
  - grapheme-to-phoneme
  - persian
  - farsi
  - text-to-speech
  - tts
  - phonetics
  - speech-synthesis
  - seq2seq
library_name: transformers
pipeline_tag: translation
---

# Neura Persian G2P (`Neurai/Persian_G2P`)

Welcome to the **Neura Persian G2P (Grapheme-to-Phoneme)** model repository by [Neurai](https://huggingface.co/Neurai). This Sequence-to-Sequence (`Seq2SeqLM`) model converts Persian (Farsi) text orthography (graphemes) into accurate phonetic transcriptions (phonemes), serving as an essential frontend component for Text-to-Speech (TTS), Automatic Speech Recognition (ASR), and Persian NLP pipelines.

## Overview

Written Persian omits short vowels (*a*, *e*, *o*) in everyday orthography and relies heavily on context for grammatical constructs such as the *Ezafe* (`-e` / `-ye`), silent letters (e.g., *vav* in «خواهر»), and colloquial pronunciations. `Neurai/Persian_G2P` handles both formal and conversational Persian sentences to output reliable phonetic sequences.

* **Developed by:** [Neurai (Neura.info)](https://huggingface.co/Neurai)
* **Model Repository:** [Neurai/Persian_G2P](https://huggingface.co/Neurai/Persian_G2P)
* **Architecture:** `AutoModelForSeq2SeqLM` (Transformers)
* **Language:** Persian (`fa`)
* **Task:** Grapheme-to-Phoneme (G2P) / Phonetic Transcription

## Installation

Install the required dependencies using `pip`:

```bash
pip install torch transformers gradio
```

## Usage

### Python Inference

You can run inference directly using `transformers` and `torch`:

```python
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

MODEL_ID = "Neurai/Persian_G2P"

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID).to(device)
model.eval()

def text_to_phoneme(text: str) -> str:
    if not text.strip():
        return ""

    with torch.no_grad():
        inputs = tokenizer(
            text,
            return_tensors="pt",
            padding=True,
            truncation=True
        ).to(device)

        outputs = model.generate(
            **inputs,
            max_new_tokens=128,
            do_sample=False
        )

        return tokenizer.decode(outputs[0], skip_special_tokens=True)

sample_text = "نه بابا چی میگی این عدده نه هزار رو پانصد و بیست و نه هست"
phonemes = text_to_phoneme(sample_text)

print("Grapheme:", sample_text)
print("Phoneme :", phonemes)
```

### Gradio Web Demo

Launch an interactive web interface locally with Gradio:

```python
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

MODEL_ID = "Neurai/Persian_G2P"

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID).to(device)
model.eval()

def generate(text):
    if not text.strip():
        return ""

    with torch.no_grad():
        inputs = tokenizer(
            text,
            return_tensors="pt",
            padding=True,
            truncation=True
        ).to(device)

        outputs = model.generate(
            **inputs,
            max_new_tokens=128,
            do_sample=False
        )

        return tokenizer.decode(outputs[0], skip_special_tokens=True)

demo = gr.Interface(
    fn=generate,
    inputs=gr.Textbox(
        lines=4,
        label="Grapheme",
        value="نه بابا چی میگی این عدده نه هزار رو پانصد و بیست و نه هست"
    ),
    outputs=gr.Textbox(lines=4, label="Phoneme"),
    title="Neura Persian G2P"
)

demo.launch()
```

## Limitations & Considerations

* **Sequence Length:** The default generation configuration uses `max_new_tokens=128`. For very long paragraphs, split input text into sentence-level chunks before running inference.
* **Homographs:** Context-dependent Persian homographs (words spelled identically with different meanings and pronunciations) rely on surrounding sentence context for accurate disambiguation.
* **Out-of-Vocabulary Terms:** Rare foreign loanwords, technical jargon, or uncommon named entities may occasionally produce fallback phonetic approximations.

## Citation

If you use this model in your research, TTS pipeline, or production system, please cite:

```bibtex
@misc{neurai_persian_g2p_model,
  author = {Neurai},
  title = {Neura Persian G2P: Neural Grapheme-to-Phoneme Model for Persian},
  year = {2025},
  publisher = {Hugging Face},
  url = {https://huggingface.co/Neurai/Persian_G2P}
}
```

## License

Please refer to the repository metadata and `LICENSE` file in [Neurai/Persian_G2P](https://huggingface.co/Neurai/Persian_G2P) for usage and redistribution permissions.