Update app.py
Browse files
app.py
CHANGED
|
@@ -726,98 +726,61 @@ def chat():
|
|
| 726 |
# ======================
|
| 727 |
# πΌοΈ VISION MODE (AUTO DETECT - BASE64 ONLY)
|
| 728 |
# ======================
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
|
|
|
|
|
|
|
|
|
| 732 |
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
-
|
| 744 |
-
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
"type": "text",
|
| 754 |
-
"text": prompt if prompt else "Describe this image."
|
| 755 |
-
},
|
| 756 |
-
{
|
| 757 |
-
# Cohere accepts data URI as image url - keep the data URI if present
|
| 758 |
-
"type": "image_url",
|
| 759 |
-
"image_url": {
|
| 760 |
-
"url": image_base64,
|
| 761 |
-
"detail": "auto"
|
| 762 |
-
}
|
| 763 |
}
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
headers = {
|
| 771 |
-
"Authorization": f"Bearer {COHERE_API_KEY}",
|
| 772 |
-
"Content-Type": "application/json"
|
| 773 |
-
}
|
| 774 |
-
|
| 775 |
-
try:
|
| 776 |
-
res = requests.post(cohere_url, json=payload, headers=headers, timeout=60)
|
| 777 |
-
try:
|
| 778 |
-
res_json = res.json()
|
| 779 |
-
except Exception:
|
| 780 |
-
print("[VISION] β Cohere returned non-json response")
|
| 781 |
-
return jsonify({"error": "Cohere returned non-json response", "status_code": res.status_code, "text": res.text}), 500
|
| 782 |
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
ai_reply = res_json.get("messages", [])[0].get("content", [])[0].get("text")
|
| 788 |
-
except Exception:
|
| 789 |
-
ai_reply = None
|
| 790 |
-
|
| 791 |
-
# Fallback: sometimes Cohere returns 'output' or 'text' fields
|
| 792 |
-
if not ai_reply:
|
| 793 |
-
# Try to find any 'text' in nested dicts
|
| 794 |
-
def find_text(obj):
|
| 795 |
-
if isinstance(obj, dict):
|
| 796 |
-
for k, v in obj.items():
|
| 797 |
-
if k == "text" and isinstance(v, str):
|
| 798 |
-
return v
|
| 799 |
-
else:
|
| 800 |
-
found = find_text(v)
|
| 801 |
-
if found:
|
| 802 |
-
return found
|
| 803 |
-
elif isinstance(obj, list):
|
| 804 |
-
for item in obj:
|
| 805 |
-
found = find_text(item)
|
| 806 |
-
if found:
|
| 807 |
-
return found
|
| 808 |
-
return None
|
| 809 |
-
ai_reply = find_text(res_json) or ""
|
| 810 |
-
|
| 811 |
-
return jsonify({
|
| 812 |
-
"mode": "vision",
|
| 813 |
-
"reply": ai_reply,
|
| 814 |
-
"raw": res_json
|
| 815 |
-
})
|
| 816 |
|
| 817 |
-
|
| 818 |
-
print(f"[VISION] β RequestException: {e}")
|
| 819 |
-
return jsonify({"error": "Failed to call Cohere", "details": str(e)}), 500
|
| 820 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 821 |
# =====================================================
|
| 822 |
# π§© ποΈ (VISION DONE) β LANJUTKAN MODE TEXT SEPERTI BIASA
|
| 823 |
# =====================================================
|
|
|
|
| 726 |
# ======================
|
| 727 |
# πΌοΈ VISION MODE (AUTO DETECT - BASE64 ONLY)
|
| 728 |
# ======================
|
| 729 |
+
# ======================
|
| 730 |
+
# πΌοΈ VISION MODE (Cohere v2) β TEXT ONLY
|
| 731 |
+
# ======================
|
| 732 |
+
image_base64 = data.get("image_base64")
|
| 733 |
+
if image_base64:
|
| 734 |
+
print("[VISION] πΌοΈ Image detected β Cohere c4ai-aya-vision-32b")
|
| 735 |
|
| 736 |
+
# validate base64
|
| 737 |
+
try:
|
| 738 |
+
test_b64 = image_base64.split(",", 1)[1] if image_base64.startswith("data:") else image_base64
|
| 739 |
+
base64.b64decode(test_b64, validate=True)
|
| 740 |
+
except Exception:
|
| 741 |
+
return Response("Invalid base64 image", mimetype="text/plain", status=400)
|
| 742 |
+
|
| 743 |
+
cohere_url = "https://api.cohere.ai/v2/chat"
|
| 744 |
+
payload = {
|
| 745 |
+
"model": "c4ai-aya-vision-32b",
|
| 746 |
+
"messages": [
|
| 747 |
+
{
|
| 748 |
+
"role": "user",
|
| 749 |
+
"content": [
|
| 750 |
+
{"type": "text", "text": prompt or "Describe this image."},
|
| 751 |
+
{
|
| 752 |
+
"type": "image_url",
|
| 753 |
+
"image_url": {
|
| 754 |
+
"url": image_base64,
|
| 755 |
+
"detail": "auto"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 756 |
}
|
| 757 |
+
}
|
| 758 |
+
]
|
| 759 |
+
}
|
| 760 |
+
]
|
| 761 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 762 |
|
| 763 |
+
headers = {
|
| 764 |
+
"Authorization": f"Bearer {COHERE_API_KEY}",
|
| 765 |
+
"Content-Type": "application/json"
|
| 766 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 767 |
|
| 768 |
+
res = requests.post(cohere_url, json=payload, headers=headers, timeout=60)
|
|
|
|
|
|
|
| 769 |
|
| 770 |
+
try:
|
| 771 |
+
res_json = res.json()
|
| 772 |
+
except Exception:
|
| 773 |
+
return Response("Cohere non-json response", mimetype="text/plain", status=500)
|
| 774 |
+
|
| 775 |
+
# π₯ AMBIL TEKS SAJA
|
| 776 |
+
ai_text = (
|
| 777 |
+
res_json
|
| 778 |
+
.get("message", {})
|
| 779 |
+
.get("content", [{}])[0]
|
| 780 |
+
.get("text", "")
|
| 781 |
+
)
|
| 782 |
+
|
| 783 |
+
return Response(ai_text, mimetype="text/plain")
|
| 784 |
# =====================================================
|
| 785 |
# π§© ποΈ (VISION DONE) β LANJUTKAN MODE TEXT SEPERTI BIASA
|
| 786 |
# =====================================================
|