File size: 8,019 Bytes
20aa1bc
 
 
 
 
 
 
 
77489ce
20aa1bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fea3fec
20aa1bc
 
 
 
 
9fb02e0
20aa1bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53e20bd
20aa1bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import gradio as gr
import os
import google.generativeai as genai
import time 

# Configuration
token = os.environ.get("TOKEN")

genai.configure(api_key=token)

generation_config = {
    "temperature": 1,
    "max_output_tokens": 8192,
}

safety_settings = [
    {
        "category": "HARM_CATEGORY_HARASSMENT",
        "threshold": "BLOCK_NONE"
    },
    {
        "category": "HARM_CATEGORY_HATE_SPEECH",
        "threshold": "BLOCK_NONE"
    },
    {
        "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
        "threshold": "BLOCK_NONE"
    },
    {
        "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
        "threshold": "BLOCK_NONE"
    },
]

model = genai.GenerativeModel(
    model_name="gemini-1.5-flash-002",
    generation_config=generation_config,
    safety_settings=safety_settings
)

source_languages = ["Francais", "English", "Spanish"]
#target_languages = ['nzebi', 'Fang(ntumu)','Fang(complet)', 'gisir', 'dikota', 'yipunu', 'akele', 'Ghétsogo', 'Shimo', 'omyene_nkomi', 'isangu', 'Liwanzi']

target_languages = ['Fang(ntumu)','dikota']


lang_files = {
    'nzebi': 'Inzèbi.txt',
    'gisir': 'Gisir.txt',
    'dikota': 'dikota.txt',
    'yipunu': 'yipunu.pdf',
    'Fang(complet)':'document_modifie.pdf',
    'omyene_nkomi': 'Omyènè_Nkomi.txt',
    'Fang(ntumu)': 'Fang(ntumu).txt'
}

def main_fang(query):
    return "En maintenance"

async def translate(input_text, source, target):
    if target == 'yipunu':
        try:
            pdf_path = lang_files[target]
            d = time.time()
            if not os.path.exists(pdf_path):
                return f"Erreur: Le fichier {pdf_path} n'existe pas."
            
            sample_file = genai.upload_file(path=pdf_path, display_name="yipunu-reference")
            s = time.time()
            print(s-d)
            
            prompt = f"""
            Using the provided Yipunu language reference PDF, translate the following text:
            Input text: {input_text}
            
            Please provide only the translation without any additional explanation.
            """
            
            print(input_text)
            content = [sample_file, prompt]
            response = model.generate_content(content, request_options={"timeout": 600})
            
            genai.delete_file(sample_file.name)
            print(response.text)
            return response.text
            
        except Exception as e:
            return f"Erreur lors de la traduction: {str(e)}"

    elif target == 'Fang(complet)':
        try:
            pdf_path = lang_files[target]
            d = time.time()
            if not os.path.exists(pdf_path):
                return f"Erreur: Le fichier {pdf_path} n'existe pas."
            
            sample_file = genai.upload_file(path=pdf_path, display_name="yipunu-reference")
            s = time.time()
            print(s-d)
            
            prompt = f"""
            Using the provided Fang language reference PDF, translate the following text:
            Input text: {input_text}
            
            Please provide only the translation without any additional explanation.
            """
            
            print(input_text)
            content = [sample_file, prompt]
            response = model.generate_content(content, request_options={"timeout": 600})
            
            genai.delete_file(sample_file.name)
            print(response.text)
            return response.text
            
        except Exception as e:
            return f"Erreur lors de la traduction: {str(e)}"
        


    
    else:
        chemin_fichier = lang_files[target]
        with open(chemin_fichier, 'r', encoding='utf-8') as fichier:
            contenu_langue_arrivee = fichier.read()
        
        tt = f"""
        contexte: {contenu_langue_arrivee}
        
        Utillisez les éléments de contexte suivants pour répondre à la question à la fin. 
        Si vous ne connaissez pas la réponse, traduisez ce que vous pouvez et reecriver 
        les autre comme ca, n'essayez pas d'inventer une réponse. Je veux que tu agisses 
        comme un traducteur {target}. Je parle en français et tu traduis en {target} en 
        te basant sur le contexte. Je ne veux aucune explication. Juste la réponse. 
        Traduit ca < {input_text} >
        """
        print(input_text)
        response = model.generate_content(tt)
        print(response.text)
        return response.text

with gr.Blocks(theme=gr.themes.Default()) as demo:
    gr.HTML("""
    <style>
        /* Global styles */
        body {
            background-color: #ffffff;
            margin: 0;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
        }
        
        /* Header bar */
        .header-bar {
            background-color: #ffd1dc;
            padding: 1rem;
            margin-bottom: 2rem;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        }
        
        h1 {
            text-align: center;
            color: #333333;
            font-size: 2em;
            font-weight: 600;
            margin: 0;
        }
        
        /* Container styles */
        .gradio-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 1rem;
        }
        
        /* Form elements */
        .input-textbox, .output-textbox {
            border: 1px solid #e0e0e0 !important;
            border-radius: 4px !important;
            background-color: white !important;
            transition: border-color 0.2s ease;
        }
        
        .input-textbox:focus, .output-textbox:focus {
            border-color: #ffd1dc !important;
            outline: none !important;
        }
        
        .gr-button {
            background-color: #ffd1dc !important;
            border: none !important;
            color: #333333 !important;
            padding: 0.75rem 1.5rem !important;
            border-radius: 4px !important;
            font-weight: 500 !important;
            transition: background-color 0.2s ease !important;
            margin: 1rem auto !important;
            display: block !important;
        }
        
        .gr-button:hover {
            background-color: #ffbfcf !important;
        }
        
        .gr-dropdown {
            border: 1px solid #e0e0e0 !important;
            border-radius: 4px !important;
            background-color: white !important;
        }
        
        /* Labels */
        label {
            color: #333333 !important;
            font-weight: 500 !important;
            margin-bottom: 0.5rem !important;
        }
        
        /* Spacing */
        .block {
            margin-bottom: 1.5rem;
        }
    </style>
    
    <div class="header-bar">
        <h1>BOMA LANGUE</h1>
    </div>
    """)
    
    with gr.Row():
        with gr.Column(scale=1):
            source_language_dropdown = gr.Dropdown(
                choices=source_languages,
                value="Francais",
                label="Langue source",
                allow_custom_value=True,
            )
            input_textbox = gr.Textbox(
                lines=5,
                placeholder="Entrez le texte à traduire",
                label="Texte source",
            )
        
        with gr.Column(scale=1):
            target_language_dropdown = gr.Dropdown(
                choices=target_languages,
                value="dikota",
                label="Langue cible",
                allow_custom_value=True,
            )
            translated_textbox = gr.Textbox(
                lines=5,
                placeholder="Traduction",
                label="Texte traduit",
            )
    
    with gr.Row():
        btn = gr.Button("Traduire ⇄")
        
    btn.click(
        translate,
        inputs=[input_textbox, source_language_dropdown, target_language_dropdown],
        outputs=translated_textbox
    )

if __name__ == "__main__":
    demo.launch()