Vibow commited on
Commit
7427f10
Β·
verified Β·
1 Parent(s): 6bab603

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -87
app.py CHANGED
@@ -726,98 +726,61 @@ def chat():
726
  # ======================
727
  # πŸ–ΌοΈ VISION MODE (AUTO DETECT - BASE64 ONLY)
728
  # ======================
729
- image_base64 = data.get("image_base64")
730
- if image_base64:
731
- print("[VISION] πŸ–ΌοΈ Image detected β†’ using c4ai-aya-vision-32b (Cohere)")
 
 
 
732
 
733
- # Validate base64 quickly
734
- try:
735
- # If user sends full data URI like "data:image/png;base64,....", strip prefix for decoding test
736
- test_b64 = image_base64
737
- if test_b64.startswith("data:"):
738
- test_b64 = test_b64.split(",", 1)[1]
739
- base64.b64decode(test_b64, validate=True)
740
- except Exception as e:
741
- print(f"[VISION] ❌ Invalid base64: {e}")
742
- return jsonify({"error": "Invalid base64 image"}), 400
743
-
744
- # Build payload for Cohere v2 chat (vision)
745
- cohere_url = "https://api.cohere.ai/v2/chat"
746
- payload = {
747
- "model": "c4ai-aya-vision-32b",
748
- "messages": [
749
- {
750
- "role": "user",
751
- "content": [
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
- # Robust extraction of text reply (support several possible shapes)
784
- ai_reply = None
785
- # Try common v2 shape: res_json["messages"][0]["content"][0]["text"]
786
- try:
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
- except requests.exceptions.RequestException as e:
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
  # =====================================================