File size: 5,325 Bytes
15678e0
82da87b
15678e0
c8ef715
d8c4e42
 
15678e0
d8c4e42
 
 
ebb51c2
d8c4e42
 
 
 
 
 
 
 
 
 
 
 
ebb51c2
d8c4e42
 
 
 
 
ebb51c2
 
d8c4e42
 
 
 
15678e0
 
ebb51c2
 
 
 
c8ef715
ebb51c2
 
c8ef715
ebb51c2
 
 
 
c8ef715
ebb51c2
c8ef715
ebb51c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8ef715
 
 
ebb51c2
 
15678e0
419f944
 
d8c4e42
ebb51c2
c8ef715
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

# Load both translation models from Hugging Face
tokenizer_eng_to_darija = AutoTokenizer.from_pretrained("lachkarsalim/Helsinki-translation-English_Moroccan-Arabic")
model_eng_to_darija = AutoModelForSeq2SeqLM.from_pretrained("lachkarsalim/Helsinki-translation-English_Moroccan-Arabic")

tokenizer_darija_to_msa = AutoTokenizer.from_pretrained("Saidtaoussi/AraT5_Darija_to_MSA")
model_darija_to_msa = AutoModelForSeq2SeqLM.from_pretrained("Saidtaoussi/AraT5_Darija_to_MSA")

# Translation functions
def translate_darija_to_msa(darija_text):
    inputs = tokenizer_darija_to_msa(darija_text, return_tensors="pt", padding=True)
    translated = model_darija_to_msa.generate(**inputs)
    translated_text = tokenizer_darija_to_msa.decode(translated[0], skip_special_tokens=True)
    return translated_text

def translate_eng_to_darija(eng_text, direction="eng_to_darija"):
    if direction == "eng_to_darija":
        inputs = tokenizer_eng_to_darija(eng_text, return_tensors="pt", padding=True)
        translated = model_eng_to_darija.generate(**inputs)
        translated_text = tokenizer_eng_to_darija.decode(translated[0], skip_special_tokens=True)
    else:
        # Reverse translation (Darija to English)
        inputs = tokenizer_eng_to_darija(eng_text, return_tensors="pt", padding=True)
        translated = model_eng_to_darija.generate(**inputs)
        translated_text = tokenizer_eng_to_darija.decode(translated[0], skip_special_tokens=True)
    return translated_text

# Respond function
def respond(message, translation_choice):
    if translation_choice == "Moroccan Arabic to MSA":
        return translate_darija_to_msa(message)
    elif translation_choice == "English to Moroccan Arabic":
        return translate_eng_to_darija(message, direction="eng_to_darija")


# Gradio Interface Layout with organized components
with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            # Header with emojis and logo
            gr.Markdown("""
                <h1 style="text-align: center;">
                    πŸ‡²πŸ‡¦ πŸš€ Moroccan Arabic Translation Demo 😍
                </h1>
                <p style="text-align: center;">
                    🌟 Dima Meghrib 🌟 <br>
                    Select the translation direction and type your text. <br>
                    Get quick translations between **English** and **Moroccan Arabic (Darija)** or **Darija to Modern Standard Arabic (MSA)**! πŸ”₯
                </p>
                <img src="https://moroccan-culture-image.s3.eu-north-1.amazonaws.com/2159558.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=ASIAW3MD7RUI6BPOSTGF%2F20250225%2Feu-north-1%2Fs3%2Faws4_request&X-Amz-Date=20250225T213645Z&X-Amz-Expires=300&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEBUaCmV1LW5vcnRoLTEiSDBGAiEA2wC2GISJOKipdbfMelCGfMqoPHnVnM9aY0zmsgmINpACIQCkA5VvqMaCEHNVwJml3%2FBJu%2FQlrU120PByOy8dSEfkdSrqAwhPEAAaDDQ3MTExMjU4NDQ2NSIMosytLgjLE%2BjysHncKscD5SmK3%2FV7ycLFyJpg8GMUa2bi%2BB4Tlj4YLxW37IxScYWjA5dOLyc9NnJNQ87K73ZRJLFSv1sogcVvRQqOnq3Cjb1AQwtljD4MHBjPIpgy7QVBov%2BAdUMzehD7K%2FhL8ME2pLPMV70E12dSdRRpablepF3Y%2FrHU1S8s4HJ%2BDAmfEF6gmnexTRyXVIEYodn%2FqPtOR5oaUF17SGEmmKJBi4b2kLQN%2B353pYrkB8ciBn0gAScNVkkgs2PuDUmzcXtw%2F4qcASvZ%2F11za7YSO%2FPrK30tFrOuVQQgjaB2h4a5%2BETBuXdLDX6EkbnEmN2Rh2ZH0adBut2UnOR9AtR4lRHDtk7qnRrlof6byqCqjDHu9iaJVcBpNMojNo%2BFZMofqZrnKygggvCaJhI3a%2FmJo7v0moz0nRhljyCA18ieJ8PYIBwqGcc53MneKRBbyTrI%2F7lHimTQZ4RDiD8E42qyxyh5vYOOBd9ijYog%2FZEsvzzaLfQC1niWfT35NcpzNXn0QMp8vjHa3S57nV3vXY7uXqj7i6Hf0JGG8YrZ3bpizt3q%2FY1zSHc9RCrYALCmWm9xfXsxy4woC10vBTP9S9tYaKS1j60bOADWikoeemUw4Oj4vQY6kwKyzIUJZkcmGcdRVff6NtSS%2F7hCsUUtyoygbvXIudEFO2jQDChI%2Fx5YbO%2BPH3ynUKVjncXGmtWR7%2BoncsXjFg7xrzsYjBjmDuHWF1umN%2BnA%2FPng6CfJ%2Br%2BlI5VxjnZCAZsvbGEKp9xKSSUI9KXcdsOYC4sMzNm1NdzU3wuWPhlJHvv7Y6Ma%2FPQwUkfDCCBphGheYqiZrQieDLpcnI6TNiefFOzRipE27fbtJXwbzoXIIhlQdtML7LXsQq5XeISwXGRZjeIQo5bIF6ab5QpA%2BFcmvAiVQlAh%2Byqa2KMjZhVHiFxMcPZXb0lRO3y2lB40FGKbQ7SVyUrDJg6pE%2FF67NMhCkanOp34hhjZ4JlWbGcIeBTTPw%3D%3D&X-Amz-Signature=894ab5ded5a51c7c605a2cfa64e77f4e69692edc7401e4428441dcf0d43f9d3d&X-Amz-SignedHeaders=host&response-content-disposition=inline" 
                    style="width: 100px; display: block; margin: 20px auto;" alt="Dima Meghrib Logo" />
            """)

        with gr.Column():
            # Left Column for Inputs
            user_input = gr.Textbox(label="Enter Your Text", placeholder="Type your sentence here...")
            translation_choice = gr.Dropdown(
                label="Choose Translation Direction",
                choices=["English to Moroccan Arabic", "Moroccan Arabic to MSA"],
                value="English to Moroccan Arabic"
            )
            submit_button = gr.Button("Submit", elem_id="submit_button")
    
    with gr.Row():
        # Right Column for Outputs
        output = gr.Textbox(label="Translated Text", placeholder="Translation will appear here...")

    # Footer with your name at the bottom
    gr.Markdown("<p style='text-align: center; font-size: 14px;'>Created by Eng Amal 🌟</p>")

    # Define the action for submit
    submit_button.click(fn=respond, inputs=[user_input, translation_choice], outputs=output)

# Launch the interface
demo.launch()