amauricunha commited on
Commit
0fa52bc
verified
1 Parent(s): e07e47b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -134,24 +134,18 @@ def pronunciation_feedback():
134
  except Exception as e:
135
  return jsonify({"error": f"Pronunciation analysis failed: {e}"}), 500
136
 
137
- # --- ROTA 6: GERA脟脙O DE IMAGEM (NOVO) ---
138
  @app.route('/generate-image', methods=['POST'])
139
  def generate_image():
140
- if not genai_client:
141
- return jsonify({"error": "GEMINI_API_KEY not configured for image generation."}), 503
142
  data = request.get_json()
143
  prompt = data.get('prompt')
144
- if not prompt:
145
- return jsonify({"error": "Image prompt is required."}), 400
146
  try:
147
- # Usa o modelo "Nano Banana"
148
  model = genai_client.GenerativeModel(model_name='gemini-1.5-flash-image-preview')
149
  response = model.generate_content(prompt)
150
-
151
- # A API retorna a imagem em base64 diretamente nos 'parts'
152
  base64_image_data = response.parts[0].inline_data.data
153
  return jsonify({"image_base64": base64_image_data})
154
-
155
  except Exception as e:
156
  return jsonify({"error": f"Image generation failed: {e}"}), 500
157
 
@@ -160,10 +154,18 @@ def get_ai_text_response(provider, model_name, system_instruction, user_prompt,
160
  if provider == 'gemini':
161
  full_model_name = f'models/{model_name}'
162
  model = genai_client.GenerativeModel(full_model_name, system_instruction=system_instruction)
163
- config = {"response_mime_type": "application/json", "response_schema": json_schema} if json_schema else None
164
- generation_config = genai.types.GenerationConfig(**config) if config else None
 
 
 
 
 
 
 
165
  response = model.generate_content(user_prompt, generation_config=generation_config)
166
  return json.loads(response.text) if json_schema else response.text
 
167
  elif provider == 'groq':
168
  messages = [{"role": "system", "content": system_instruction}, {"role": "user", "content": user_prompt}]
169
  config = {'response_format': {"type": "json_object"}} if json_schema else {}
 
134
  except Exception as e:
135
  return jsonify({"error": f"Pronunciation analysis failed: {e}"}), 500
136
 
137
+ # --- ROTA 6: GERA脟脙O DE IMAGEM ---
138
  @app.route('/generate-image', methods=['POST'])
139
  def generate_image():
140
+ if not genai_client: return jsonify({"error": "GEMINI_API_KEY not configured for image generation."}), 503
 
141
  data = request.get_json()
142
  prompt = data.get('prompt')
143
+ if not prompt: return jsonify({"error": "Image prompt is required."}), 400
 
144
  try:
 
145
  model = genai_client.GenerativeModel(model_name='gemini-1.5-flash-image-preview')
146
  response = model.generate_content(prompt)
 
 
147
  base64_image_data = response.parts[0].inline_data.data
148
  return jsonify({"image_base64": base64_image_data})
 
149
  except Exception as e:
150
  return jsonify({"error": f"Image generation failed: {e}"}), 500
151
 
 
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: Constr贸i o objeto GenerationConfig corretamente, como esperado pela biblioteca.
159
+ generation_config = None
160
+ if json_schema:
161
+ generation_config = genai.types.GenerationConfig(
162
+ response_mime_type="application/json",
163
+ response_schema=json_schema
164
+ )
165
+
166
  response = model.generate_content(user_prompt, generation_config=generation_config)
167
  return json.loads(response.text) if json_schema else response.text
168
+
169
  elif provider == 'groq':
170
  messages = [{"role": "system", "content": system_instruction}, {"role": "user", "content": user_prompt}]
171
  config = {'response_format': {"type": "json_object"}} if json_schema else {}