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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import google.generativeai as genai
|
4 |
+
import time
|
5 |
+
|
6 |
+
# Configuration
|
7 |
+
token = os.environ.get("TOKEN")
|
8 |
+
|
9 |
+
genai.configure(api_key=token)
|
10 |
+
|
11 |
+
generation_config = {
|
12 |
+
"temperature": 1,
|
13 |
+
"max_output_tokens": 8192,
|
14 |
+
}
|
15 |
+
|
16 |
+
safety_settings = [
|
17 |
+
{
|
18 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
19 |
+
"threshold": "BLOCK_NONE"
|
20 |
+
},
|
21 |
+
{
|
22 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
23 |
+
"threshold": "BLOCK_NONE"
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
27 |
+
"threshold": "BLOCK_NONE"
|
28 |
+
},
|
29 |
+
{
|
30 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
31 |
+
"threshold": "BLOCK_NONE"
|
32 |
+
},
|
33 |
+
]
|
34 |
+
|
35 |
+
model = genai.GenerativeModel(
|
36 |
+
model_name="gemini-1.5-flash-002",
|
37 |
+
generation_config=generation_config,
|
38 |
+
safety_settings=safety_settings
|
39 |
+
)
|
40 |
+
|
41 |
+
source_languages = ["Francais", "English", "Spanish"]
|
42 |
+
#target_languages = ['nzebi', 'Fang(ntumu)','Fang(complet)', 'gisir', 'dikota', 'yipunu', 'akele', 'Ghétsogo', 'Shimo', 'omyene_nkomi', 'isangu', 'Liwanzi']
|
43 |
+
|
44 |
+
target_languages = ['nzebi', 'Fang(ntumu)','Fang(complet)','dikota', 'yipunu', 'omyene_nkomi']
|
45 |
+
|
46 |
+
|
47 |
+
lang_files = {
|
48 |
+
'nzebi': 'Inzèbi.txt',
|
49 |
+
'gisir': 'Gisir.txt',
|
50 |
+
'dikota': 'dd.txt',
|
51 |
+
'yipunu': 'yipunu.pdf',
|
52 |
+
'Fang(complet)':'document_modifie.pdf',
|
53 |
+
'omyene_nkomi': 'Omyènè_Nkomi.txt',
|
54 |
+
'Fang(ntumu)': 'Fang(ntumu).txt'
|
55 |
+
}
|
56 |
+
|
57 |
+
def main_fang(query):
|
58 |
+
return "En maintenance"
|
59 |
+
|
60 |
+
async def translate(input_text, source, target):
|
61 |
+
if target == 'yipunu':
|
62 |
+
try:
|
63 |
+
pdf_path = lang_files[target]
|
64 |
+
d = time.time()
|
65 |
+
if not os.path.exists(pdf_path):
|
66 |
+
return f"Erreur: Le fichier {pdf_path} n'existe pas."
|
67 |
+
|
68 |
+
sample_file = genai.upload_file(path=pdf_path, display_name="yipunu-reference")
|
69 |
+
s = time.time()
|
70 |
+
print(s-d)
|
71 |
+
|
72 |
+
prompt = f"""
|
73 |
+
Using the provided Yipunu language reference PDF, translate the following text:
|
74 |
+
Input text: {input_text}
|
75 |
+
|
76 |
+
Please provide only the translation without any additional explanation.
|
77 |
+
"""
|
78 |
+
|
79 |
+
print(input_text)
|
80 |
+
content = [sample_file, prompt]
|
81 |
+
response = model.generate_content(content, request_options={"timeout": 600})
|
82 |
+
|
83 |
+
genai.delete_file(sample_file.name)
|
84 |
+
print(response.text)
|
85 |
+
return response.text
|
86 |
+
|
87 |
+
except Exception as e:
|
88 |
+
return f"Erreur lors de la traduction: {str(e)}"
|
89 |
+
|
90 |
+
elif target == 'Fang(complet)':
|
91 |
+
try:
|
92 |
+
pdf_path = lang_files[target]
|
93 |
+
d = time.time()
|
94 |
+
if not os.path.exists(pdf_path):
|
95 |
+
return f"Erreur: Le fichier {pdf_path} n'existe pas."
|
96 |
+
|
97 |
+
sample_file = genai.upload_file(path=pdf_path, display_name="yipunu-reference")
|
98 |
+
s = time.time()
|
99 |
+
print(s-d)
|
100 |
+
|
101 |
+
prompt = f"""
|
102 |
+
Using the provided Fang language reference PDF, translate the following text:
|
103 |
+
Input text: {input_text}
|
104 |
+
|
105 |
+
Please provide only the translation without any additional explanation.
|
106 |
+
"""
|
107 |
+
|
108 |
+
print(input_text)
|
109 |
+
content = [sample_file, prompt]
|
110 |
+
response = model.generate_content(content, request_options={"timeout": 600})
|
111 |
+
|
112 |
+
genai.delete_file(sample_file.name)
|
113 |
+
print(response.text)
|
114 |
+
return response.text
|
115 |
+
|
116 |
+
except Exception as e:
|
117 |
+
return f"Erreur lors de la traduction: {str(e)}"
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
else:
|
123 |
+
chemin_fichier = lang_files[target]
|
124 |
+
with open(chemin_fichier, 'r', encoding='utf-8') as fichier:
|
125 |
+
contenu_langue_arrivee = fichier.read()
|
126 |
+
|
127 |
+
tt = f"""
|
128 |
+
contexte: {contenu_langue_arrivee}
|
129 |
+
|
130 |
+
Utillisez les éléments de contexte suivants pour répondre à la question à la fin.
|
131 |
+
Si vous ne connaissez pas la réponse, traduisez ce que vous pouvez et reecriver
|
132 |
+
les autre comme ca, n'essayez pas d'inventer une réponse. Je veux que tu agisses
|
133 |
+
comme un traducteur {target}. Je parle en français et tu traduis en {target} en
|
134 |
+
te basant sur le contexte. Je ne veux aucune explication. Juste la réponse.
|
135 |
+
Traduit ca < {input_text} >
|
136 |
+
"""
|
137 |
+
print(input_text)
|
138 |
+
response = model.generate_content(tt)
|
139 |
+
print(response.text)
|
140 |
+
return response.text
|
141 |
+
|
142 |
+
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
143 |
+
gr.HTML("""
|
144 |
+
<style>
|
145 |
+
/* Global styles */
|
146 |
+
body {
|
147 |
+
background-color: #ffffff;
|
148 |
+
margin: 0;
|
149 |
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
150 |
+
}
|
151 |
+
|
152 |
+
/* Header bar */
|
153 |
+
.header-bar {
|
154 |
+
background-color: #ffd1dc;
|
155 |
+
padding: 1rem;
|
156 |
+
margin-bottom: 2rem;
|
157 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
158 |
+
}
|
159 |
+
|
160 |
+
h1 {
|
161 |
+
text-align: center;
|
162 |
+
color: #333333;
|
163 |
+
font-size: 2em;
|
164 |
+
font-weight: 600;
|
165 |
+
margin: 0;
|
166 |
+
}
|
167 |
+
|
168 |
+
/* Container styles */
|
169 |
+
.gradio-container {
|
170 |
+
max-width: 1200px;
|
171 |
+
margin: 0 auto;
|
172 |
+
padding: 0 1rem;
|
173 |
+
}
|
174 |
+
|
175 |
+
/* Form elements */
|
176 |
+
.input-textbox, .output-textbox {
|
177 |
+
border: 1px solid #e0e0e0 !important;
|
178 |
+
border-radius: 4px !important;
|
179 |
+
background-color: white !important;
|
180 |
+
transition: border-color 0.2s ease;
|
181 |
+
}
|
182 |
+
|
183 |
+
.input-textbox:focus, .output-textbox:focus {
|
184 |
+
border-color: #ffd1dc !important;
|
185 |
+
outline: none !important;
|
186 |
+
}
|
187 |
+
|
188 |
+
.gr-button {
|
189 |
+
background-color: #ffd1dc !important;
|
190 |
+
border: none !important;
|
191 |
+
color: #333333 !important;
|
192 |
+
padding: 0.75rem 1.5rem !important;
|
193 |
+
border-radius: 4px !important;
|
194 |
+
font-weight: 500 !important;
|
195 |
+
transition: background-color 0.2s ease !important;
|
196 |
+
margin: 1rem auto !important;
|
197 |
+
display: block !important;
|
198 |
+
}
|
199 |
+
|
200 |
+
.gr-button:hover {
|
201 |
+
background-color: #ffbfcf !important;
|
202 |
+
}
|
203 |
+
|
204 |
+
.gr-dropdown {
|
205 |
+
border: 1px solid #e0e0e0 !important;
|
206 |
+
border-radius: 4px !important;
|
207 |
+
background-color: white !important;
|
208 |
+
}
|
209 |
+
|
210 |
+
/* Labels */
|
211 |
+
label {
|
212 |
+
color: #333333 !important;
|
213 |
+
font-weight: 500 !important;
|
214 |
+
margin-bottom: 0.5rem !important;
|
215 |
+
}
|
216 |
+
|
217 |
+
/* Spacing */
|
218 |
+
.block {
|
219 |
+
margin-bottom: 1.5rem;
|
220 |
+
}
|
221 |
+
</style>
|
222 |
+
|
223 |
+
<div class="header-bar">
|
224 |
+
<h1>BOMA LANGUE</h1>
|
225 |
+
</div>
|
226 |
+
""")
|
227 |
+
|
228 |
+
with gr.Row():
|
229 |
+
with gr.Column(scale=1):
|
230 |
+
source_language_dropdown = gr.Dropdown(
|
231 |
+
choices=source_languages,
|
232 |
+
value="Francais",
|
233 |
+
label="Langue source",
|
234 |
+
allow_custom_value=True,
|
235 |
+
)
|
236 |
+
input_textbox = gr.Textbox(
|
237 |
+
lines=5,
|
238 |
+
placeholder="Entrez le texte à traduire",
|
239 |
+
label="Texte source",
|
240 |
+
)
|
241 |
+
|
242 |
+
with gr.Column(scale=1):
|
243 |
+
target_language_dropdown = gr.Dropdown(
|
244 |
+
choices=target_languages,
|
245 |
+
value="nzebi",
|
246 |
+
label="Langue cible",
|
247 |
+
allow_custom_value=True,
|
248 |
+
)
|
249 |
+
translated_textbox = gr.Textbox(
|
250 |
+
lines=5,
|
251 |
+
placeholder="Traduction",
|
252 |
+
label="Texte traduit",
|
253 |
+
)
|
254 |
+
|
255 |
+
with gr.Row():
|
256 |
+
btn = gr.Button("Traduire ⇄")
|
257 |
+
|
258 |
+
btn.click(
|
259 |
+
translate,
|
260 |
+
inputs=[input_textbox, source_language_dropdown, target_language_dropdown],
|
261 |
+
outputs=translated_textbox
|
262 |
+
)
|
263 |
+
|
264 |
+
if __name__ == "__main__":
|
265 |
+
demo.launch()
|