Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -72,32 +72,21 @@ def explain_proxy():
|
|
| 72 |
|
| 73 |
system_instruction_base = f"You are a professional English tutor focused on the context '{context_focus}'."
|
| 74 |
try:
|
|
|
|
| 75 |
if custom_prompt:
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
if not word: return jsonify({"error": "No word selected."}), 400
|
|
|
|
|
|
|
| 78 |
if for_flashcard:
|
| 79 |
-
schema = {
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 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,25 +160,18 @@ def generate_image():
|
|
| 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 = {
|
| 181 |
-
"response_mime_type": "application/json",
|
| 182 |
-
"response_schema": json_schema
|
| 183 |
-
}
|
| 184 |
-
|
| 185 |
response = model.generate_content(user_prompt, generation_config=config)
|
| 186 |
-
return json.loads(response.text) if json_schema else response.text
|
| 187 |
|
| 188 |
elif provider == 'groq':
|
| 189 |
messages = [{"role": "system", "content": system_instruction}, {"role": "user", "content": user_prompt}]
|
| 190 |
config = {'response_format': {"type": "json_object"}} if json_schema else {}
|
| 191 |
response = groq_client.chat.completions.create(model=model_name, messages=messages, **config)
|
| 192 |
-
return json.loads(response.choices[0].message.content) if json_schema else response.choices[0].message.content
|
| 193 |
raise Exception(f"Unsupported provider: {provider}")
|
| 194 |
|
| 195 |
@app.route('/')
|
|
|
|
| 72 |
|
| 73 |
system_instruction_base = f"You are a professional English tutor focused on the context '{context_focus}'."
|
| 74 |
try:
|
| 75 |
+
# Se for uma requisição de Geração de Atividade (custom_prompt)
|
| 76 |
if custom_prompt:
|
| 77 |
+
# A resposta será texto simples, então retornamos em um campo JSON
|
| 78 |
+
activity_text = get_ai_text_response(model_provider, model_name, system_instruction_base, custom_prompt)
|
| 79 |
+
return jsonify({"explanation": activity_text}) # Reutilizando a chave 'explanation'
|
| 80 |
+
|
| 81 |
if not word: return jsonify({"error": "No word selected."}), 400
|
| 82 |
+
|
| 83 |
+
# Lógica para Flashcard
|
| 84 |
if for_flashcard:
|
| 85 |
+
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"]}
|
| 86 |
+
prompt = f"Analyze '{word}' in context: '{context}'. Generate a JSON for a flashcard. The 'gapped_sentence' must replace '{word}' with '______________'."
|
| 87 |
+
return jsonify(get_ai_text_response(model_provider, model_name, system_instruction_base, prompt, json_schema=schema))
|
| 88 |
+
|
| 89 |
+
# Lógica para tradução rápida
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
else:
|
| 91 |
prompt = f"Analyze '{word}' in context: '{context}'. Provide a one-sentence English explanation, then '---', then the Portuguese translation."
|
| 92 |
parts = get_ai_text_response(model_provider, model_name, system_instruction_base, prompt).split('---', 1)
|
|
|
|
| 160 |
# --- FUNÇÃO AUXILIAR E ROTA RAIZ ---
|
| 161 |
def get_ai_text_response(provider, model_name, system_instruction, user_prompt, json_schema=None):
|
| 162 |
if provider == 'gemini':
|
|
|
|
|
|
|
| 163 |
model = genai_client.GenerativeModel(model_name, system_instruction=system_instruction)
|
|
|
|
| 164 |
config = {}
|
| 165 |
if json_schema:
|
| 166 |
+
config = {"response_mime_type": "application/json", "response_schema": json_schema}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
response = model.generate_content(user_prompt, generation_config=config)
|
| 168 |
+
return json.loads(response.text) if json_schema else response.text.strip()
|
| 169 |
|
| 170 |
elif provider == 'groq':
|
| 171 |
messages = [{"role": "system", "content": system_instruction}, {"role": "user", "content": user_prompt}]
|
| 172 |
config = {'response_format': {"type": "json_object"}} if json_schema else {}
|
| 173 |
response = groq_client.chat.completions.create(model=model_name, messages=messages, **config)
|
| 174 |
+
return json.loads(response.choices[0].message.content) if json_schema else response.choices[0].message.content.strip()
|
| 175 |
raise Exception(f"Unsupported provider: {provider}")
|
| 176 |
|
| 177 |
@app.route('/')
|