Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -84,9 +84,10 @@ def list_models():
|
|
| 84 |
def tts_proxy():
|
| 85 |
data = request.get_json()
|
| 86 |
text = data.get('text', '')
|
|
|
|
| 87 |
if not text: return jsonify({"error": "No text provided"}), 400
|
| 88 |
try:
|
| 89 |
-
tts = gTTS(text=text, lang='en', tld=
|
| 90 |
mp3_fp = io.BytesIO()
|
| 91 |
tts.write_to_fp(mp3_fp)
|
| 92 |
mp3_fp.seek(0)
|
|
@@ -173,7 +174,6 @@ def analyze_image():
|
|
| 173 |
schema = { "type": "object", "properties": { "vocabulary": { "type": "array", "items": { "type": "object", "properties": { "term": {"type": "string"}, "definition": {"type": "string"} }, "required": ["term", "definition"] } } }, "required": ["vocabulary"] }
|
| 174 |
prompt = [ "Act as an English teacher. 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 ]
|
| 175 |
|
| 176 |
-
# CORRE脟脙O: Usa o objeto GenerationConfig
|
| 177 |
config = GenerationConfig(response_mime_type="application/json", response_schema=schema)
|
| 178 |
response = model.generate_content(prompt, generation_config=config)
|
| 179 |
|
|
@@ -183,7 +183,6 @@ def analyze_image():
|
|
| 183 |
|
| 184 |
@app.route('/chat-with-ai', methods=['POST'])
|
| 185 |
def chat_with_ai():
|
| 186 |
-
# ... (c贸digo existente sem altera莽玫es) ...
|
| 187 |
if not groq_client: return jsonify({"error": "GROQ_API_KEY not configured."}), 503
|
| 188 |
data = request.get_json()
|
| 189 |
history, user_message = data.get('history', []), data.get('message', '')
|
|
@@ -198,7 +197,6 @@ def chat_with_ai():
|
|
| 198 |
|
| 199 |
@app.route('/pronunciation-feedback', methods=['POST'])
|
| 200 |
def pronunciation_feedback():
|
| 201 |
-
# ... (c贸digo existente sem altera莽玫es) ...
|
| 202 |
if not groq_client: return jsonify({"error": "GROQ_API_KEY not configured."}), 503
|
| 203 |
data = request.get_json()
|
| 204 |
target_text, user_text = data.get('target_text'), data.get('user_text')
|
|
@@ -214,7 +212,6 @@ def pronunciation_feedback():
|
|
| 214 |
|
| 215 |
@app.route('/generate-image', methods=['POST'])
|
| 216 |
def generate_image():
|
| 217 |
-
# ... (c贸digo existente sem altera莽玫es) ...
|
| 218 |
if not genai_client: return jsonify({"error": "GEMINI_API_KEY not configured."}), 503
|
| 219 |
data = request.get_json()
|
| 220 |
prompt = data.get('prompt')
|
|
@@ -232,14 +229,13 @@ def get_ai_text_response(provider, model_name, system_instruction, user_prompt,
|
|
| 232 |
if provider == 'gemini':
|
| 233 |
model = genai_client.GenerativeModel(model_name, system_instruction=system_instruction)
|
| 234 |
|
| 235 |
-
# CORRE脟脙O:
|
| 236 |
config = None
|
| 237 |
if json_schema:
|
| 238 |
config = GenerationConfig(response_mime_type="application/json", response_schema=json_schema)
|
| 239 |
|
| 240 |
response = model.generate_content(user_prompt, generation_config=config)
|
| 241 |
|
| 242 |
-
# CORRE脟脙O CR脥TICA: S贸 faz parse de JSON se foi solicitado.
|
| 243 |
if json_schema:
|
| 244 |
parsed_json = json.loads(response.text)
|
| 245 |
required_keys = json_schema.get("required", [])
|
|
@@ -250,7 +246,6 @@ def get_ai_text_response(provider, model_name, system_instruction, user_prompt,
|
|
| 250 |
return response.text.strip()
|
| 251 |
|
| 252 |
elif provider == 'groq':
|
| 253 |
-
# ... (c贸digo existente sem altera莽玫es) ...
|
| 254 |
final_user_prompt = user_prompt
|
| 255 |
if json_schema:
|
| 256 |
final_user_prompt += f"\n\nYou MUST respond with a single JSON object that strictly follows this schema. Do not add any other text before or after the JSON object:\n{json.dumps(json_schema)}"
|
|
|
|
| 84 |
def tts_proxy():
|
| 85 |
data = request.get_json()
|
| 86 |
text = data.get('text', '')
|
| 87 |
+
tld = data.get('tld', 'co.uk')
|
| 88 |
if not text: return jsonify({"error": "No text provided"}), 400
|
| 89 |
try:
|
| 90 |
+
tts = gTTS(text=text, lang='en', tld=tld)
|
| 91 |
mp3_fp = io.BytesIO()
|
| 92 |
tts.write_to_fp(mp3_fp)
|
| 93 |
mp3_fp.seek(0)
|
|
|
|
| 174 |
schema = { "type": "object", "properties": { "vocabulary": { "type": "array", "items": { "type": "object", "properties": { "term": {"type": "string"}, "definition": {"type": "string"} }, "required": ["term", "definition"] } } }, "required": ["vocabulary"] }
|
| 175 |
prompt = [ "Act as an English teacher. 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 ]
|
| 176 |
|
|
|
|
| 177 |
config = GenerationConfig(response_mime_type="application/json", response_schema=schema)
|
| 178 |
response = model.generate_content(prompt, generation_config=config)
|
| 179 |
|
|
|
|
| 183 |
|
| 184 |
@app.route('/chat-with-ai', methods=['POST'])
|
| 185 |
def chat_with_ai():
|
|
|
|
| 186 |
if not groq_client: return jsonify({"error": "GROQ_API_KEY not configured."}), 503
|
| 187 |
data = request.get_json()
|
| 188 |
history, user_message = data.get('history', []), data.get('message', '')
|
|
|
|
| 197 |
|
| 198 |
@app.route('/pronunciation-feedback', methods=['POST'])
|
| 199 |
def pronunciation_feedback():
|
|
|
|
| 200 |
if not groq_client: return jsonify({"error": "GROQ_API_KEY not configured."}), 503
|
| 201 |
data = request.get_json()
|
| 202 |
target_text, user_text = data.get('target_text'), data.get('user_text')
|
|
|
|
| 212 |
|
| 213 |
@app.route('/generate-image', methods=['POST'])
|
| 214 |
def generate_image():
|
|
|
|
| 215 |
if not genai_client: return jsonify({"error": "GEMINI_API_KEY not configured."}), 503
|
| 216 |
data = request.get_json()
|
| 217 |
prompt = data.get('prompt')
|
|
|
|
| 229 |
if provider == 'gemini':
|
| 230 |
model = genai_client.GenerativeModel(model_name, system_instruction=system_instruction)
|
| 231 |
|
| 232 |
+
# CORRE脟脙O DEFINITIVA: Usa GenerationConfig para JSON e nada para texto.
|
| 233 |
config = None
|
| 234 |
if json_schema:
|
| 235 |
config = GenerationConfig(response_mime_type="application/json", response_schema=json_schema)
|
| 236 |
|
| 237 |
response = model.generate_content(user_prompt, generation_config=config)
|
| 238 |
|
|
|
|
| 239 |
if json_schema:
|
| 240 |
parsed_json = json.loads(response.text)
|
| 241 |
required_keys = json_schema.get("required", [])
|
|
|
|
| 246 |
return response.text.strip()
|
| 247 |
|
| 248 |
elif provider == 'groq':
|
|
|
|
| 249 |
final_user_prompt = user_prompt
|
| 250 |
if json_schema:
|
| 251 |
final_user_prompt += f"\n\nYou MUST respond with a single JSON object that strictly follows this schema. Do not add any other text before or after the JSON object:\n{json.dumps(json_schema)}"
|