Spaces:
Sleeping
Sleeping
update
Browse files
app.py
CHANGED
|
@@ -3,10 +3,9 @@ import warnings
|
|
| 3 |
import re
|
| 4 |
import os
|
| 5 |
import google.generativeai as genai
|
| 6 |
-
from
|
|
|
|
| 7 |
from langchain.schema import Document
|
| 8 |
-
from langchain.vectorstores import FAISS
|
| 9 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
| 10 |
from langchain.prompts import PromptTemplate
|
| 11 |
import gradio as gr
|
| 12 |
|
|
@@ -33,37 +32,30 @@ class ZarmaLanguageAnalyzer:
|
|
| 33 |
return json.load(f)
|
| 34 |
|
| 35 |
def _setup_models(self):
|
| 36 |
-
"""Set up the Gemini-
|
| 37 |
# Get API key from environment variable
|
| 38 |
api_key = os.getenv("GOOGLE_API_KEY")
|
| 39 |
if not api_key:
|
| 40 |
raise ValueError("GOOGLE_API_KEY environment variable not set.")
|
| 41 |
-
|
| 42 |
-
self.model = "gemini-2.0-flash"
|
| 43 |
|
| 44 |
self.analysis_template = PromptTemplate(
|
| 45 |
input_variables=["sentence", "grammar_check", "glossary_info"],
|
| 46 |
template="""
|
| 47 |
You are a Zarma language expert. Analyze this Zarma sentence: "{sentence}"
|
| 48 |
-
|
| 49 |
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.
|
| 50 |
-
|
| 51 |
Grammar check results (optional guide):
|
| 52 |
{grammar_check}
|
| 53 |
-
|
| 54 |
Glossary information (use it but prioritize your expertise to confirm):
|
| 55 |
{glossary_info}
|
| 56 |
-
|
| 57 |
Provide a detailed linguistic analysis in this exact format, with no extra text outside the sections:
|
| 58 |
-
|
| 59 |
1. WORD BREAKDOWN:
|
| 60 |
- [List each word with its grammatical role and meaning, e.g., "Ay: 1st person singular pronoun, meaning 'I'."]
|
| 61 |
-
|
| 62 |
2. LINGUISTIC INSIGHT:
|
| 63 |
- Word Order: [Describe typical Zarma word order (e.g., SOV, SVO) and how this sentence aligns or deviates]
|
| 64 |
- Tense/Aspect Markers: [Explain tense/aspect markers like 'ga', 'goono ga', or none for past, with examples like "Ay ga koy" (I will go)]
|
| 65 |
- Contextual Insight: [Discuss what the sentence might intend to convey and any external influences or errors]
|
| 66 |
-
|
| 67 |
3. CORRECTNESS ASSESSMENT:
|
| 68 |
- Is the sentence correct? [Yes/No, with explanation]
|
| 69 |
- Reason for Incorrectness (if applicable): [Detailed reason why it’s wrong, e.g., misplaced particle]
|
|
@@ -148,7 +140,7 @@ class ZarmaLanguageAnalyzer:
|
|
| 148 |
if exact_match:
|
| 149 |
fr_word = exact_match.get("fr", "")
|
| 150 |
dje_word = exact_match.get("dje", "")
|
| 151 |
-
notes =
|
| 152 |
|
| 153 |
word_info[clean_word] = {
|
| 154 |
"french": fr_word,
|
|
@@ -256,7 +248,7 @@ class ZarmaLanguageAnalyzer:
|
|
| 256 |
)
|
| 257 |
|
| 258 |
def analyze_sentence(self, sentence: str) -> dict:
|
| 259 |
-
"""Full analysis pipeline for a Zarma sentence using Gemini-
|
| 260 |
grammar_issues = self.check_grammar(sentence)
|
| 261 |
formatted_grammar = self.format_grammar_issues(grammar_issues)
|
| 262 |
glossary_results = self.translate_and_explain_words(sentence)
|
|
@@ -268,24 +260,10 @@ class ZarmaLanguageAnalyzer:
|
|
| 268 |
glossary_info=formatted_glossary
|
| 269 |
)
|
| 270 |
|
| 271 |
-
contents = [
|
| 272 |
-
types.Content(
|
| 273 |
-
role="user",
|
| 274 |
-
parts=[types.Part.from_text(text=prompt)],
|
| 275 |
-
),
|
| 276 |
-
]
|
| 277 |
-
generate_content_config = types.GenerateContentConfig(
|
| 278 |
-
response_mime_type="text/plain",
|
| 279 |
-
)
|
| 280 |
-
|
| 281 |
raw_analysis = ""
|
| 282 |
try:
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
contents=contents,
|
| 286 |
-
config=generate_content_config,
|
| 287 |
-
):
|
| 288 |
-
raw_analysis += chunk.text
|
| 289 |
except Exception as e:
|
| 290 |
raw_analysis = f"Error in analysis generation: {str(e)}"
|
| 291 |
|
|
@@ -354,4 +332,4 @@ with gr.Blocks(title="Zarma Language Analyzer") as demo:
|
|
| 354 |
)
|
| 355 |
|
| 356 |
# Launch the app
|
| 357 |
-
demo.launch()
|
|
|
|
| 3 |
import re
|
| 4 |
import os
|
| 5 |
import google.generativeai as genai
|
| 6 |
+
from langchain_community.vectorstores import FAISS
|
| 7 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 8 |
from langchain.schema import Document
|
|
|
|
|
|
|
| 9 |
from langchain.prompts import PromptTemplate
|
| 10 |
import gradio as gr
|
| 11 |
|
|
|
|
| 32 |
return json.load(f)
|
| 33 |
|
| 34 |
def _setup_models(self):
|
| 35 |
+
"""Set up the Gemini-Flash-1.5 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:
|
| 39 |
raise ValueError("GOOGLE_API_KEY environment variable not set.")
|
| 40 |
+
genai.configure(api_key=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"],
|
| 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.
|
|
|
|
| 48 |
Grammar check results (optional guide):
|
| 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:
|
| 56 |
- Word Order: [Describe typical Zarma word order (e.g., SOV, SVO) and how this sentence aligns or deviates]
|
| 57 |
- Tense/Aspect Markers: [Explain tense/aspect markers like 'ga', 'goono ga', or none for past, with examples like "Ay ga koy" (I will go)]
|
| 58 |
- Contextual Insight: [Discuss what the sentence might intend to convey and any external influences or errors]
|
|
|
|
| 59 |
3. CORRECTNESS ASSESSMENT:
|
| 60 |
- Is the sentence correct? [Yes/No, with explanation]
|
| 61 |
- Reason for Incorrectness (if applicable): [Detailed reason why it’s wrong, e.g., misplaced particle]
|
|
|
|
| 140 |
if exact_match:
|
| 141 |
fr_word = exact_match.get("fr", "")
|
| 142 |
dje_word = exact_match.get("dje", "")
|
| 143 |
+
notes = exact_match.get("notes", "No additional context available")
|
| 144 |
|
| 145 |
word_info[clean_word] = {
|
| 146 |
"french": fr_word,
|
|
|
|
| 248 |
)
|
| 249 |
|
| 250 |
def analyze_sentence(self, sentence: str) -> dict:
|
| 251 |
+
"""Full analysis pipeline for a Zarma sentence using Gemini-Flash-1.5."""
|
| 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)
|
|
|
|
| 260 |
glossary_info=formatted_glossary
|
| 261 |
)
|
| 262 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
raw_analysis = ""
|
| 264 |
try:
|
| 265 |
+
response = self.model.generate_content(prompt)
|
| 266 |
+
raw_analysis = response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
except Exception as e:
|
| 268 |
raw_analysis = f"Error in analysis generation: {str(e)}"
|
| 269 |
|
|
|
|
| 332 |
)
|
| 333 |
|
| 334 |
# Launch the app
|
| 335 |
+
demo.launch()
|