Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,144 @@
|
|
1 |
-
from flask import Flask,
|
2 |
import os
|
3 |
-
|
4 |
-
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
token = os.environ.get("TOKEN")
|
|
|
|
|
12 |
|
13 |
-
|
14 |
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
|
|
|
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify
|
2 |
import os
|
3 |
+
import google.generativeai as genai
|
4 |
+
import time
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
8 |
+
# Configuration
|
|
|
|
|
9 |
token = os.environ.get("TOKEN")
|
10 |
+
if not token:
|
11 |
+
raise ValueError("La variable d'environnement TOKEN n'est pas définie.")
|
12 |
|
13 |
+
genai.configure(api_key=token)
|
14 |
|
15 |
+
generation_config = {
|
16 |
+
"temperature": 1,
|
17 |
+
"max_output_tokens": 8192,
|
18 |
+
}
|
19 |
|
20 |
+
safety_settings = [
|
21 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
22 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
23 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
24 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
25 |
+
]
|
26 |
+
|
27 |
+
model = genai.GenerativeModel(
|
28 |
+
model_name="gemini-1.5-flash-002",
|
29 |
+
generation_config=generation_config,
|
30 |
+
safety_settings=safety_settings
|
31 |
+
)
|
32 |
|
33 |
+
source_languages = ["Francais", "English", "Spanish"]
|
34 |
+
target_languages = ['nzebi', 'Fang(ntumu)','Fang(complet)','dikota', 'yipunu', 'omyene_nkomi']
|
35 |
|
36 |
+
lang_files = {
|
37 |
+
'nzebi': 'Inzèbi.txt',
|
38 |
+
'gisir': 'Gisir.txt',
|
39 |
+
'dikota': 'dd.txt',
|
40 |
+
'yipunu': 'yipunu.pdf',
|
41 |
+
'Fang(complet)':'document_modifie.pdf',
|
42 |
+
'akele': 'Akélé.txt',
|
43 |
+
'Ghétsogo': 'Ghétsogo.txt',
|
44 |
+
'Shimo': 'Shimo.txt',
|
45 |
+
'omyene_nkomi': 'Omyènè_Nkomi.txt',
|
46 |
+
'isangu': 'Isangu.txt',
|
47 |
+
'Liwanzi': 'Liwanzi.txt',
|
48 |
+
'Fang(ntumu)': 'Fang(ntumu).txt'
|
49 |
+
}
|
50 |
|
51 |
+
def main_fang(query):
|
52 |
+
return "En maintenance"
|
53 |
|
54 |
+
async def translate(input_text, source, target):
|
55 |
+
if target == 'yipunu':
|
56 |
+
try:
|
57 |
+
pdf_path = lang_files[target]
|
58 |
+
d = time.time()
|
59 |
+
if not os.path.exists(pdf_path):
|
60 |
+
return f"Erreur: Le fichier {pdf_path} n'existe pas."
|
61 |
+
|
62 |
+
sample_file = genai.upload_file(path=pdf_path, display_name="yipunu-reference")
|
63 |
+
s = time.time()
|
64 |
+
print(s-d)
|
65 |
+
|
66 |
+
prompt = f"""
|
67 |
+
Using the provided Yipunu language reference PDF, translate the following text:
|
68 |
+
Input text: {input_text}
|
69 |
+
|
70 |
+
Please provide only the translation without any additional explanation.
|
71 |
+
"""
|
72 |
+
|
73 |
+
print(input_text)
|
74 |
+
content = [sample_file, prompt]
|
75 |
+
response = model.generate_content(content, request_options={"timeout": 600})
|
76 |
+
|
77 |
+
genai.delete_file(sample_file.name)
|
78 |
+
print(response.text)
|
79 |
+
return response.text
|
80 |
+
|
81 |
+
except Exception as e:
|
82 |
+
return f"Erreur lors de la traduction: {str(e)}"
|
83 |
|
84 |
+
elif target == 'Fang(complet)':
|
85 |
+
try:
|
86 |
+
pdf_path = lang_files[target]
|
87 |
+
d = time.time()
|
88 |
+
if not os.path.exists(pdf_path):
|
89 |
+
return f"Erreur: Le fichier {pdf_path} n'existe pas."
|
90 |
+
|
91 |
+
sample_file = genai.upload_file(path=pdf_path, display_name="yipunu-reference")
|
92 |
+
s = time.time()
|
93 |
+
print(s-d)
|
94 |
+
|
95 |
+
prompt = f"""
|
96 |
+
Using the provided Fang language reference PDF, translate the following text:
|
97 |
+
Input text: {input_text}
|
98 |
+
|
99 |
+
Please provide only the translation without any additional explanation.
|
100 |
+
"""
|
101 |
+
|
102 |
+
print(input_text)
|
103 |
+
content = [sample_file, prompt]
|
104 |
+
response = model.generate_content(content, request_options={"timeout": 600})
|
105 |
+
|
106 |
+
genai.delete_file(sample_file.name)
|
107 |
+
print(response.text)
|
108 |
+
return response.text
|
109 |
+
|
110 |
+
except Exception as e:
|
111 |
+
return f"Erreur lors de la traduction: {str(e)}"
|
112 |
+
|
113 |
+
else:
|
114 |
+
chemin_fichier = lang_files[target]
|
115 |
+
with open(chemin_fichier, 'r', encoding='utf-8') as fichier:
|
116 |
+
contenu_langue_arrivee = fichier.read()
|
117 |
+
|
118 |
+
tt = f"""
|
119 |
+
contexte: {contenu_langue_arrivee}
|
120 |
+
|
121 |
+
Utillisez les éléments de contexte suivants pour répondre à la question à la fin.
|
122 |
+
Si vous ne connaissez pas la réponse, traduisez ce que vous pouvez et reecriver
|
123 |
+
les autre comme ca, n'essayez pas d'inventer une réponse. Je veux que tu agisses
|
124 |
+
comme un traducteur {target}. Je parle en français et tu traduis en {target} en
|
125 |
+
te basant sur le contexte. Je ne veux aucune explication. Juste la réponse.
|
126 |
+
Traduit ca < {input_text} >
|
127 |
+
"""
|
128 |
+
print(input_text)
|
129 |
+
response = model.generate_content(tt)
|
130 |
+
print(response.text)
|
131 |
+
return response.text
|
132 |
|
133 |
+
@app.route('/', methods=['GET', 'POST'])
|
134 |
+
def index():
|
135 |
+
if request.method == 'POST':
|
136 |
+
input_text = request.form['input_text']
|
137 |
+
source_language = request.form['source_language']
|
138 |
+
target_language = request.form['target_language']
|
139 |
+
translated_text = translate(input_text, source_language, target_language)
|
140 |
+
return jsonify({'translated_text': translated_text})
|
141 |
+
return render_template('index.html', source_languages=source_languages, target_languages=target_languages)
|
142 |
|
143 |
+
if __name__ == '__main__':
|
144 |
+
app.run(debug=True)
|