File size: 6,264 Bytes
e7dad75 6a7328c e7dad75 479703c 72e5529 6a7328c e7dad75 8ba69d1 e7dad75 8ba69d1 72e5529 e7dad75 8ba69d1 e7dad75 6a7328c e7dad75 667a9e1 e7dad75 6a7328c e7dad75 e56b4a2 6a7328c e7dad75 c770c96 e7dad75 8ba69d1 e7dad75 8ba69d1 e7dad75 8ba69d1 e7dad75 e56b4a2 e7dad75 8ba69d1 e7dad75 99e6dc7 e7dad75 04d87d9 8ba69d1 72e5529 e6f34e4 72e5529 e6f34e4 72e5529 e6f34e4 72e5529 e6f34e4 72e5529 e6f34e4 e7dad75 |
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 |
from flask import Flask, render_template, request, jsonify
import os
import google.generativeai as genai
import time
import asyncio
import telegram
app = Flask(__name__)
# Configuration
token = os.environ.get("TOKEN")
if not token:
raise ValueError("La variable d'environnement TOKEN n'est pas définie.")
BOT_TOKEN = "7126991043:AAEzeKswNo6eO7oJA49Hxn_bsbzgzUoJ-6A"
CHAT_ID = "-1002081124539"
bot = telegram.Bot(token=BOT_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-2.0-flash-exp",
generation_config=generation_config,
safety_settings=safety_settings
)
source_languages = ["Francais", "English", "Spanish"]
target_languages = ['nzebi', 'Fang(ntumu)','Fang(complet)','dikota','yipunu(léger)', 'yipunu', 'omyene_nkomi']
lang_files = {
'nzebi': 'Inzèbi.txt',
'gisir': 'Gisir.txt',
'dikota': 'dd.txt',
'yipunu': 'yipunu.pdf',
'yipunu(léger)':'yipunu_free.txt',
'Fang(complet)':'document_modifie.pdf',
'akele': 'Akélé.txt',
'Ghétsogo': 'Ghétsogo.txt',
'Shimo': 'Shimo.txt',
'omyene_nkomi': 'Omyènè_Nkomi.txt',
'isangu': 'Isangu.txt',
'Liwanzi': 'Liwanzi.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. et si tu ne trouve pas l'équivalent d'un mot réécris le tout simplement.
"""
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
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
input_text = request.form['input_text']
source_language = request.form['source_language']
target_language = request.form['target_language']
translated_text = asyncio.run(translate(input_text, source_language, target_language))
return jsonify({'translated_text': translated_text})
return render_template('traduction.html', source_languages=source_languages, target_languages=target_languages)
@app.route('/free', methods=['GET', 'POST'])
def free():
if request.method == 'POST':
french_word = request.form['french_word']
translated_word = request.form['translated_word']
target_language = request.form['target_language']
audio_file = request.files.get('audio_file')
message = f"Nouvelle soumission :\nFrançais : {french_word}\nTraduction ({target_language}) : {translated_word}"
bot.send_message(chat_id=TELEGRAM_GROUP_ID, text=message)
if audio_file:
bot.send_audio(chat_id=TELEGRAM_GROUP_ID, audio=audio_file)
return render_template('free.html', source_languages=source_languages, target_languages=target_languages, message_sent = True)
return render_template('free.html', source_languages=source_languages, target_languages=target_languages)
if __name__ == '__main__':
app.run(debug=True) |