amauricunha commited on
Commit
9f9f17f
verified
1 Parent(s): 2e1dae3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -7
app.py CHANGED
@@ -76,9 +76,28 @@ def explain_proxy():
76
  return jsonify({"explanation": get_ai_text_response(model_provider, model_name, system_instruction_base, custom_prompt)})
77
  if not word: return jsonify({"error": "No word selected."}), 400
78
  if for_flashcard:
79
- schema = {"type": "object", "properties": {"term": {"type": "string"}, "translation": {"type": "string"}, "context_sentence": {"type": "string"}, "gapped_sentence": {"type": "string"}, "definition": {"type": "string"}}, "required": ["term", "translation", "context_sentence", "gapped_sentence", "definition"]}
80
- prompt = f"Analyze '{word}' in context: '{context}'. Generate a JSON for a flashcard. The 'gapped_sentence' must replace '{word}' with '______________'."
81
- return jsonify(get_ai_text_response(model_provider, model_name, system_instruction_base, prompt, json_schema=schema))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  else:
83
  prompt = f"Analyze '{word}' in context: '{context}'. Provide a one-sentence English explanation, then '---', then the Portuguese translation."
84
  parts = get_ai_text_response(model_provider, model_name, system_instruction_base, prompt).split('---', 1)
@@ -152,11 +171,10 @@ def generate_image():
152
  # --- FUN脟脙O AUXILIAR E ROTA RAIZ ---
153
  def get_ai_text_response(provider, model_name, system_instruction, user_prompt, json_schema=None):
154
  if provider == 'gemini':
155
- full_model_name = f'models/{model_name}'
156
- model = genai_client.GenerativeModel(full_model_name, system_instruction=system_instruction)
 
157
 
158
- # CORRE脟脙O DEFINITIVA: O `generation_config` deve ser um dicion谩rio simples.
159
- # O uso do `genai.types.GenerationConfig` estava causando o erro de inicializa莽茫o.
160
  config = {}
161
  if json_schema:
162
  config = {
 
76
  return jsonify({"explanation": get_ai_text_response(model_provider, model_name, system_instruction_base, custom_prompt)})
77
  if not word: return jsonify({"error": "No word selected."}), 400
78
  if for_flashcard:
79
+ schema = {
80
+ "type": "object",
81
+ "properties": {
82
+ "term": {"type": "string"},
83
+ "translation": {"type": "string"},
84
+ "context_sentence": {"type": "string"},
85
+ "gapped_sentence": {"type": "string"},
86
+ "definition": {"type": "string"}
87
+ },
88
+ "required": ["term", "translation", "context_sentence", "gapped_sentence", "definition"]
89
+ }
90
+ prompt = f"Analyze the term '{word}' within the context: '{context}'. Generate a JSON object for a flashcard. The 'gapped_sentence' must replace the term '{word}' with '______________'."
91
+
92
+ card_data = get_ai_text_response(model_provider, model_name, system_instruction_base, prompt, json_schema=schema)
93
+
94
+ # Valida莽茫o para garantir que a IA retornou os dados corretos
95
+ required_keys = schema['required']
96
+ if not all(key in card_data and card_data[key] for key in required_keys):
97
+ print(f"Dados incompletos da IA: {card_data}")
98
+ raise Exception("AI returned incomplete or invalid data for the flashcard.")
99
+
100
+ return jsonify(card_data)
101
  else:
102
  prompt = f"Analyze '{word}' in context: '{context}'. Provide a one-sentence English explanation, then '---', then the Portuguese translation."
103
  parts = get_ai_text_response(model_provider, model_name, system_instruction_base, prompt).split('---', 1)
 
171
  # --- FUN脟脙O AUXILIAR E ROTA RAIZ ---
172
  def get_ai_text_response(provider, model_name, system_instruction, user_prompt, json_schema=None):
173
  if provider == 'gemini':
174
+ # CORRE脟脙O: Removemos o prefixo 'models/' que estava causando o erro 404.
175
+ # A pr贸pria biblioteca gerencia isso.
176
+ model = genai_client.GenerativeModel(model_name, system_instruction=system_instruction)
177
 
 
 
178
  config = {}
179
  if json_schema:
180
  config = {