Vibow commited on
Commit
bdb9ab4
·
verified ·
1 Parent(s): 7b52704

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -49
app.py CHANGED
@@ -729,56 +729,52 @@ def chat():
729
  # ======================
730
  image_base64 = data.get("image_base64")
731
  if image_base64:
732
- print("[VISION] 🖼️ Image detected → Cohere c4ai-aya-vision-32b")
733
-
734
- # validate base64
735
- try:
736
- test_b64 = image_base64.split(",", 1)[1] if image_base64.startswith("data:") else image_base64
737
- base64.b64decode(test_b64, validate=True)
738
- except Exception:
739
- return Response("Invalid base64 image", mimetype="text/plain", status=400)
740
-
741
- cohere_url = "https://api.cohere.ai/v2/chat"
742
- payload = {
743
- "model": "c4ai-aya-vision-32b",
744
- "messages": [
745
- {
746
- "role": "user",
747
- "content": [
748
- {"type": "text", "text": prompt or "Describe this image."},
749
- {
750
- "type": "image_url",
751
- "image_url": {
752
- "url": image_base64,
753
- "detail": "auto"
754
  }
755
- }
756
- ]
757
- }
758
- ]
759
- }
760
-
761
- headers = {
762
- "Authorization": f"Bearer {COHERE_API_KEY}",
763
- "Content-Type": "application/json"
764
- }
765
-
766
- res = requests.post(cohere_url, json=payload, headers=headers, timeout=60)
767
-
768
- try:
769
- res_json = res.json()
770
- except Exception:
771
- return Response("Cohere non-json response", mimetype="text/plain", status=500)
772
-
773
- # 🔥 AMBIL TEKS SAJA
774
- ai_text = (
775
- res_json
776
- .get("message", {})
777
- .get("content", [{}])[0]
778
- .get("text", "")
779
- )
780
-
781
- return Response(ai_text, mimetype="text/plain")
782
  # =====================================================
783
  # 🧩 🎚️ (VISION DONE) — LANJUTKAN MODE TEXT SEPERTI BIASA
784
  # =====================================================
 
729
  # ======================
730
  image_base64 = data.get("image_base64")
731
  if image_base64:
732
+ print("[VISION] 🖼️ Image detected → Cohere c4ai-aya-vision-32b")
733
+ try:
734
+ test_b64 = image_base64.split(",", 1)[1] if image_base64.startswith("data:") else image_base64
735
+ base64.b64decode(test_b64, validate=True)
736
+ except Exception:
737
+ return Response("Invalid base64 image", mimetype="text/plain", status=400)
738
+ cohere_url = "https://api.cohere.ai/v2/chat"
739
+ payload = {
740
+ "model": "c4ai-aya-vision-32b",
741
+ "messages": [
742
+ {
743
+ "role": "user",
744
+ "content": [
745
+ {"type": "text", "text": prompt or "Describe this image."},
746
+ {
747
+ "type": "image_url",
748
+ "image_url": {
749
+ "url": image_base64,
750
+ "detail": "auto"
751
+ }
 
 
752
  }
753
+ ]
754
+ }
755
+ ]
756
+ }
757
+
758
+ headers = {
759
+ "Authorization": f"Bearer {COHERE_API_KEY}",
760
+ "Content-Type": "application/json"
761
+ }
762
+
763
+ res = requests.post(cohere_url, json=payload, headers=headers, timeout=60)
764
+
765
+ try:
766
+ res_json = res.json()
767
+ except Exception:
768
+ return Response("Cohere non-json response", mimetype="text/plain", status=500)
769
+
770
+ ai_text = (
771
+ res_json
772
+ .get("message", {})
773
+ .get("content", [{}])[0]
774
+ .get("text", "")
775
+ )
776
+
777
+ return Response(ai_text, mimetype="text/plain")
 
 
778
  # =====================================================
779
  # 🧩 🎚️ (VISION DONE) — LANJUTKAN MODE TEXT SEPERTI BIASA
780
  # =====================================================