amauricunha commited on
Commit
4aaf4df
·
verified ·
1 Parent(s): d2b8207

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -3
app.py CHANGED
@@ -62,7 +62,8 @@ def tts_proxy():
62
  @app.route('/explain-proxy', methods=['POST'])
63
  def explain_proxy():
64
  data = request.get_json()
65
- model_provider, model_name = data.get('model', 'gemini:gemini-1.5-flash-latest').split(':', 1)
 
66
  word, context = data.get('word', '').strip(), data.get('context', '')
67
  for_flashcard, context_focus = data.get('for_flashcard', False), data.get('context_focus', 'General/Social')
68
  custom_prompt = data.get('custom_prompt', None)
@@ -100,10 +101,18 @@ def analyze_image():
100
  if not genai_client: return jsonify({"error": "GEMINI_API_KEY not configured."}), 503
101
  data = request.get_json()
102
  base64_image = data.get('image')
 
 
 
 
 
 
 
103
  if not base64_image: return jsonify({"error": "No image data."}), 400
104
  try:
105
  image = Image.open(io.BytesIO(base64.b64decode(base64_image.split(',')[1])))
106
- model = genai_client.GenerativeModel('gemini-1.5-flash-latest')
 
107
  schema = { "type": "object", "properties": { "vocabulary": { "type": "array", "items": { "type": "object", "properties": { "term": {"type": "string"}, "definition": {"type": "string"} }, "required": ["term", "definition"] } } }, "required": ["vocabulary"] }
108
  prompt = [ "Identify 5-7 key objects/concepts in this image. For each, provide its English name and a simple definition. Return a single JSON object conforming to the schema.", image ]
109
  response = model.generate_content(prompt, generation_config={"response_mime_type": "application/json", "response_schema": schema})
@@ -121,6 +130,7 @@ def chat_with_ai():
121
  try:
122
  system = "You are 'Groq Chat', a friendly English tutor. Keep responses concise (1-2 sentences). If the user makes a grammar mistake, gently correct it. Ask questions to keep the conversation flowing. Always respond in English."
123
  messages = [{"role": "system", "content": system}] + history + [{"role": "user", "content": user_message}]
 
124
  response = groq_client.chat.completions.create(model="llama-3.1-8b-instant", messages=messages, temperature=0.7)
125
  return jsonify({"response": response.choices[0].message.content.strip()})
126
  except Exception as e:
@@ -137,6 +147,7 @@ def pronunciation_feedback():
137
  system_instruction = "You are an expert American English pronunciation coach. The user tried to say a target sentence, and their speech was transcribed. Based on the likely pronunciation differences, provide brief, friendly, and actionable feedback in Portuguese. Focus on 1-2 key points. If it's very close, praise the user."
138
  user_prompt = f"Target: \"{target_text}\"\nTranscription: \"{user_text}\"\n\nProvide pronunciation feedback."
139
  messages = [{"role": "system", "content": system_instruction}, {"role": "user", "content": user_prompt}]
 
140
  response = groq_client.chat.completions.create(model="llama-3.1-8b-instant", messages=messages, temperature=0.5)
141
  return jsonify({"feedback": response.choices[0].message.content.strip()})
142
  except Exception as e:
@@ -150,7 +161,8 @@ def generate_image():
150
  prompt = data.get('prompt')
151
  if not prompt: return jsonify({"error": "Image prompt is required."}), 400
152
  try:
153
- model = genai_client.GenerativeModel(model_name='gemini-1.5-flash-image-preview')
 
154
  response = model.generate_content(prompt)
155
  base64_image_data = response.parts[0].inline_data.data
156
  return jsonify({"image_base64": base64_image_data})
 
62
  @app.route('/explain-proxy', methods=['POST'])
63
  def explain_proxy():
64
  data = request.get_json()
65
+ # CORREÇÃO: Atualizado o modelo padrão para a versão mais recente
66
+ model_provider, model_name = data.get('model', 'gemini:gemini-2.5-flash-latest').split(':', 1)
67
  word, context = data.get('word', '').strip(), data.get('context', '')
68
  for_flashcard, context_focus = data.get('for_flashcard', False), data.get('context_focus', 'General/Social')
69
  custom_prompt = data.get('custom_prompt', None)
 
101
  if not genai_client: return jsonify({"error": "GEMINI_API_KEY not configured."}), 503
102
  data = request.get_json()
103
  base64_image = data.get('image')
104
+ # CORREÇÃO: Pega o modelo do seletor, com um padrão seguro caso não seja um modelo Gemini.
105
+ model_value = data.get('model', 'gemini:gemini-2.5-flash-latest')
106
+ # Garante que estamos usando um modelo Gemini para análise de imagem
107
+ model_name = 'gemini-2.5-flash-latest'
108
+ if model_value.startswith('gemini:'):
109
+ model_name = model_value.split(':', 1)[1]
110
+
111
  if not base64_image: return jsonify({"error": "No image data."}), 400
112
  try:
113
  image = Image.open(io.BytesIO(base64.b64decode(base64_image.split(',')[1])))
114
+ # CORREÇÃO: Usa o nome do modelo dinâmico
115
+ model = genai_client.GenerativeModel(model_name)
116
  schema = { "type": "object", "properties": { "vocabulary": { "type": "array", "items": { "type": "object", "properties": { "term": {"type": "string"}, "definition": {"type": "string"} }, "required": ["term", "definition"] } } }, "required": ["vocabulary"] }
117
  prompt = [ "Identify 5-7 key objects/concepts in this image. For each, provide its English name and a simple definition. Return a single JSON object conforming to the schema.", image ]
118
  response = model.generate_content(prompt, generation_config={"response_mime_type": "application/json", "response_schema": schema})
 
130
  try:
131
  system = "You are 'Groq Chat', a friendly English tutor. Keep responses concise (1-2 sentences). If the user makes a grammar mistake, gently correct it. Ask questions to keep the conversation flowing. Always respond in English."
132
  messages = [{"role": "system", "content": system}] + history + [{"role": "user", "content": user_message}]
133
+ # Mantido fixo para garantir a experiência de baixa latência
134
  response = groq_client.chat.completions.create(model="llama-3.1-8b-instant", messages=messages, temperature=0.7)
135
  return jsonify({"response": response.choices[0].message.content.strip()})
136
  except Exception as e:
 
147
  system_instruction = "You are an expert American English pronunciation coach. The user tried to say a target sentence, and their speech was transcribed. Based on the likely pronunciation differences, provide brief, friendly, and actionable feedback in Portuguese. Focus on 1-2 key points. If it's very close, praise the user."
148
  user_prompt = f"Target: \"{target_text}\"\nTranscription: \"{user_text}\"\n\nProvide pronunciation feedback."
149
  messages = [{"role": "system", "content": system_instruction}, {"role": "user", "content": user_prompt}]
150
+ # Mantido fixo para garantir a experiência de baixa latência
151
  response = groq_client.chat.completions.create(model="llama-3.1-8b-instant", messages=messages, temperature=0.5)
152
  return jsonify({"feedback": response.choices[0].message.content.strip()})
153
  except Exception as e:
 
161
  prompt = data.get('prompt')
162
  if not prompt: return jsonify({"error": "Image prompt is required."}), 400
163
  try:
164
+ # CORREÇÃO: Atualizado para o modelo de geração de imagem mais recente
165
+ model = genai_client.GenerativeModel(model_name='gemini-2.5-flash-image-preview')
166
  response = model.generate_content(prompt)
167
  base64_image_data = response.parts[0].inline_data.data
168
  return jsonify({"image_base64": base64_image_data})