Spaces:
Sleeping
Sleeping
Commit
·
1248eed
1
Parent(s):
e760927
fixe the french
Browse files
app.py
CHANGED
|
@@ -32,7 +32,7 @@ class ZarmaLanguageAnalyzer:
|
|
| 32 |
return json.load(f)
|
| 33 |
|
| 34 |
def _setup_models(self):
|
| 35 |
-
"""Set up the Gemini-
|
| 36 |
# Get API key from environment variable
|
| 37 |
api_key = os.getenv("GOOGLE_API_KEY")
|
| 38 |
if not api_key:
|
|
@@ -41,7 +41,7 @@ class ZarmaLanguageAnalyzer:
|
|
| 41 |
self.model = genai.GenerativeModel("gemini-2.0-flash")
|
| 42 |
|
| 43 |
self.analysis_template = PromptTemplate(
|
| 44 |
-
input_variables=["sentence", "grammar_check", "glossary_info"],
|
| 45 |
template="""
|
| 46 |
You are a Zarma language expert. Analyze this Zarma sentence: "{sentence}"
|
| 47 |
Rely primarily on your expertise in Zarma grammar and meaning. Recognize proper nouns (e.g., names or places) as such unless the glossary explicitly contradicts this with a common Zarma meaning. Use the grammar check and glossary below as supplementary aids only—do not override your knowledge unless they provide clear, contextually relevant insight.
|
|
@@ -49,7 +49,7 @@ class ZarmaLanguageAnalyzer:
|
|
| 49 |
{grammar_check}
|
| 50 |
Glossary information (use it but prioritize your expertise to confirm):
|
| 51 |
{glossary_info}
|
| 52 |
-
Provide a detailed linguistic analysis in this exact format, with no extra text outside the sections:
|
| 53 |
1. WORD BREAKDOWN:
|
| 54 |
- [List each word with its grammatical role and meaning, e.g., "Ay: 1st person singular pronoun, meaning 'I'."]
|
| 55 |
2. LINGUISTIC INSIGHT:
|
|
@@ -247,17 +247,19 @@ class ZarmaLanguageAnalyzer:
|
|
| 247 |
" - Corrections: None provided."
|
| 248 |
)
|
| 249 |
|
| 250 |
-
def analyze_sentence(self, sentence: str) -> dict:
|
| 251 |
-
"""Full analysis pipeline for a Zarma sentence using Gemini-
|
| 252 |
grammar_issues = self.check_grammar(sentence)
|
| 253 |
formatted_grammar = self.format_grammar_issues(grammar_issues)
|
| 254 |
glossary_results = self.translate_and_explain_words(sentence)
|
| 255 |
formatted_glossary = self.format_glossary_info(glossary_results)
|
| 256 |
|
|
|
|
| 257 |
prompt = self.analysis_template.format(
|
| 258 |
sentence=sentence,
|
| 259 |
grammar_check=formatted_grammar,
|
| 260 |
-
glossary_info=formatted_glossary
|
|
|
|
| 261 |
)
|
| 262 |
|
| 263 |
raw_analysis = ""
|
|
@@ -279,54 +281,56 @@ class ZarmaLanguageAnalyzer:
|
|
| 279 |
}
|
| 280 |
|
| 281 |
def format_output(self, results: dict, lang: str = "en") -> str:
|
| 282 |
-
"""Format the analysis results for Gradio output in the selected language."""
|
| 283 |
if lang == "fr":
|
| 284 |
output = "=" * 80 + "\n"
|
| 285 |
-
output += "ANALYSEUR DE LANGUE ZARMA
|
| 286 |
output += "=" * 80 + "\n\n"
|
| 287 |
|
| 288 |
-
output += "
|
| 289 |
-
output +=
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
-
output += "
|
| 292 |
-
output +=
|
| 293 |
-
output += "
|
|
|
|
| 294 |
|
| 295 |
-
output += "
|
| 296 |
-
output +=
|
| 297 |
-
output += "-" * 80 + "\n"
|
| 298 |
-
|
| 299 |
-
output += "SOURCES DE CONTEXTE FIABLES :\n"
|
| 300 |
if results["retrieved_context"]:
|
| 301 |
for i, context in enumerate(results["retrieved_context"], 1):
|
| 302 |
-
output += f"Source {i}
|
| 303 |
-
output += context + "\n\n"
|
| 304 |
else:
|
| 305 |
-
output += "
|
|
|
|
| 306 |
output += "=" * 80
|
| 307 |
else: # Default to English
|
| 308 |
output = "=" * 80 + "\n"
|
| 309 |
-
output += "ZARMA LANGUAGE ANALYZER
|
| 310 |
output += "=" * 80 + "\n\n"
|
| 311 |
|
| 312 |
-
output += "
|
| 313 |
-
output +=
|
| 314 |
-
|
| 315 |
-
output += "
|
| 316 |
-
output +=
|
| 317 |
-
output += "-" * 80 + "\n"
|
| 318 |
|
| 319 |
-
output += "
|
| 320 |
-
output +=
|
| 321 |
-
output += "
|
|
|
|
| 322 |
|
| 323 |
-
output += "
|
|
|
|
| 324 |
if results["retrieved_context"]:
|
| 325 |
for i, context in enumerate(results["retrieved_context"], 1):
|
| 326 |
-
output += f"Source {i}
|
| 327 |
-
output += context + "\n\n"
|
| 328 |
else:
|
| 329 |
-
output += "
|
|
|
|
| 330 |
output += "=" * 80
|
| 331 |
|
| 332 |
return output
|
|
@@ -338,8 +342,8 @@ analyzer = ZarmaLanguageAnalyzer("grammar_rules.json", "glossary.json")
|
|
| 338 |
def analyze_zarma_sentence(sentence, output_in_english):
|
| 339 |
if not sentence.strip():
|
| 340 |
return "Please enter a valid Zarma sentence." if output_in_english else "Veuillez entrer une phrase Zarma valide."
|
| 341 |
-
results = analyzer.analyze_sentence(sentence)
|
| 342 |
lang = "en" if output_in_english else "fr"
|
|
|
|
| 343 |
return analyzer.format_output(results, lang=lang)
|
| 344 |
|
| 345 |
# Define the Gradio UI
|
|
|
|
| 32 |
return json.load(f)
|
| 33 |
|
| 34 |
def _setup_models(self):
|
| 35 |
+
"""Set up the Gemini-2.0-flash model via Google Generative AI API."""
|
| 36 |
# Get API key from environment variable
|
| 37 |
api_key = os.getenv("GOOGLE_API_KEY")
|
| 38 |
if not api_key:
|
|
|
|
| 41 |
self.model = genai.GenerativeModel("gemini-2.0-flash")
|
| 42 |
|
| 43 |
self.analysis_template = PromptTemplate(
|
| 44 |
+
input_variables=["sentence", "grammar_check", "glossary_info", "language"],
|
| 45 |
template="""
|
| 46 |
You are a Zarma language expert. Analyze this Zarma sentence: "{sentence}"
|
| 47 |
Rely primarily on your expertise in Zarma grammar and meaning. Recognize proper nouns (e.g., names or places) as such unless the glossary explicitly contradicts this with a common Zarma meaning. Use the grammar check and glossary below as supplementary aids only—do not override your knowledge unless they provide clear, contextually relevant insight.
|
|
|
|
| 49 |
{grammar_check}
|
| 50 |
Glossary information (use it but prioritize your expertise to confirm):
|
| 51 |
{glossary_info}
|
| 52 |
+
Provide a detailed linguistic analysis in {language} in this exact format, with no extra text outside the sections:
|
| 53 |
1. WORD BREAKDOWN:
|
| 54 |
- [List each word with its grammatical role and meaning, e.g., "Ay: 1st person singular pronoun, meaning 'I'."]
|
| 55 |
2. LINGUISTIC INSIGHT:
|
|
|
|
| 247 |
" - Corrections: None provided."
|
| 248 |
)
|
| 249 |
|
| 250 |
+
def analyze_sentence(self, sentence: str, lang: str = "en") -> dict:
|
| 251 |
+
"""Full analysis pipeline for a Zarma sentence using Gemini-2.0-flash."""
|
| 252 |
grammar_issues = self.check_grammar(sentence)
|
| 253 |
formatted_grammar = self.format_grammar_issues(grammar_issues)
|
| 254 |
glossary_results = self.translate_and_explain_words(sentence)
|
| 255 |
formatted_glossary = self.format_glossary_info(glossary_results)
|
| 256 |
|
| 257 |
+
language = "English" if lang == "en" else "French"
|
| 258 |
prompt = self.analysis_template.format(
|
| 259 |
sentence=sentence,
|
| 260 |
grammar_check=formatted_grammar,
|
| 261 |
+
glossary_info=formatted_glossary,
|
| 262 |
+
language=language
|
| 263 |
)
|
| 264 |
|
| 265 |
raw_analysis = ""
|
|
|
|
| 281 |
}
|
| 282 |
|
| 283 |
def format_output(self, results: dict, lang: str = "en") -> str:
|
| 284 |
+
"""Format the analysis results for Gradio output in the selected language with table structure."""
|
| 285 |
if lang == "fr":
|
| 286 |
output = "=" * 80 + "\n"
|
| 287 |
+
output += "| ANALYSEUR DE LANGUE ZARMA |\n"
|
| 288 |
output += "=" * 80 + "\n\n"
|
| 289 |
|
| 290 |
+
output += "| **Section** | **Détails** |\n"
|
| 291 |
+
output += "|-------------|-------------|\n"
|
| 292 |
+
output += f"| Phrase Analysée | \"{results['sentence']}\" |\n"
|
| 293 |
+
output += f"| État de la Grammaire | {'Problèmes détectés' if results['grammar_issues'] else 'Correct'} |\n"
|
| 294 |
+
output += "|-------------|-------------|\n\n"
|
| 295 |
|
| 296 |
+
output += "| **Analyse Détaillée** |\n"
|
| 297 |
+
output += "|-----------------------|\n"
|
| 298 |
+
output += f"| {results['analysis_result'].replace('\n', '<br>')} |\n"
|
| 299 |
+
output += "|-----------------------|\n\n"
|
| 300 |
|
| 301 |
+
output += "| **Sources de Contexte Fiables** |\n"
|
| 302 |
+
output += "|---------------------------------|\n"
|
|
|
|
|
|
|
|
|
|
| 303 |
if results["retrieved_context"]:
|
| 304 |
for i, context in enumerate(results["retrieved_context"], 1):
|
| 305 |
+
output += f"| Source {i} : {context.replace('\n', '<br>')} |\n"
|
|
|
|
| 306 |
else:
|
| 307 |
+
output += "| Aucune source de contexte fiable récupérée basée sur l'analyse. |\n"
|
| 308 |
+
output += "|---------------------------------|\n"
|
| 309 |
output += "=" * 80
|
| 310 |
else: # Default to English
|
| 311 |
output = "=" * 80 + "\n"
|
| 312 |
+
output += "| ZARMA LANGUAGE ANALYZER |\n"
|
| 313 |
output += "=" * 80 + "\n\n"
|
| 314 |
|
| 315 |
+
output += "| **Section** | **Details** |\n"
|
| 316 |
+
output += "|-------------|-------------|\n"
|
| 317 |
+
output += f"| Sentence Analyzed | \"{results['sentence']}\" |\n"
|
| 318 |
+
output += f"| Grammar Status | {'Issues detected' if results['grammar_issues'] else 'Correct'} |\n"
|
| 319 |
+
output += "|-------------|-------------|\n\n"
|
|
|
|
| 320 |
|
| 321 |
+
output += "| **Detailed Analysis** |\n"
|
| 322 |
+
output += "|-----------------------|\n"
|
| 323 |
+
output += f"| {results['analysis_result'].replace('\n', '<br>')} |\n"
|
| 324 |
+
output += "|-----------------------|\n\n"
|
| 325 |
|
| 326 |
+
output += "| **Reliable Context Sources** |\n"
|
| 327 |
+
output += "|------------------------------|\n"
|
| 328 |
if results["retrieved_context"]:
|
| 329 |
for i, context in enumerate(results["retrieved_context"], 1):
|
| 330 |
+
output += f"| Source {i}: {context.replace('\n', '<br>')} |\n"
|
|
|
|
| 331 |
else:
|
| 332 |
+
output += "| No reliable context sources retrieved based on the analysis. |\n"
|
| 333 |
+
output += "|------------------------------|\n"
|
| 334 |
output += "=" * 80
|
| 335 |
|
| 336 |
return output
|
|
|
|
| 342 |
def analyze_zarma_sentence(sentence, output_in_english):
|
| 343 |
if not sentence.strip():
|
| 344 |
return "Please enter a valid Zarma sentence." if output_in_english else "Veuillez entrer une phrase Zarma valide."
|
|
|
|
| 345 |
lang = "en" if output_in_english else "fr"
|
| 346 |
+
results = analyzer.analyze_sentence(sentence, lang=lang)
|
| 347 |
return analyzer.format_output(results, lang=lang)
|
| 348 |
|
| 349 |
# Define the Gradio UI
|