Dooratre commited on
Commit
51ccd3c
·
verified ·
1 Parent(s): 670b69c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -19
app.py CHANGED
@@ -3,9 +3,9 @@ import requests
3
  import json
4
  import os
5
  import uuid
6
- from db import fetch_json_from_github # Import the GitHub JSON fetcher
7
- from qwen import get_qwen_response # Import the Qwen response function
8
- from upload import upload_image_to_cdn # Import the new upload function
9
 
10
  app = Flask(__name__)
11
 
@@ -45,7 +45,7 @@ def handle_message():
45
  body = request.get_json()
46
 
47
  for entry in body["entry"]:
48
- page_id = entry["id"] # Extract page_id from the incoming message
49
  page_token = PAGE_CREDENTIALS.get(page_id)
50
 
51
  if not page_token:
@@ -65,7 +65,6 @@ def handle_message():
65
  if attachment["type"] == "image":
66
  image_url = attachment["payload"]["url"]
67
  try:
68
- # Download image from Messenger
69
  img_response = requests.get(image_url)
70
  img_response.raise_for_status()
71
  temp_path = f"temp_{uuid.uuid4()}.jpg"
@@ -73,7 +72,6 @@ def handle_message():
73
  with open(temp_path, "wb") as f:
74
  f.write(img_response.content)
75
 
76
- # Upload to Qwen CDN
77
  cdn_url = upload_image_to_cdn(BASE_URL, AUTH_TOKEN, temp_path)
78
  if cdn_url:
79
  image_cdn_urls.append(cdn_url)
@@ -83,19 +81,14 @@ def handle_message():
83
  if os.path.exists(temp_path):
84
  os.remove(temp_path)
85
 
86
- # Build message content for Qwen
87
- user_content = []
88
-
89
- # Add text content if it exists
90
- if text_content:
91
- user_content.append({"type": "text", "text": text_content})
92
-
93
- # Add image CDN URLs
94
- for cdn_url in image_cdn_urls:
95
- user_content.append({"type": "image", "image": cdn_url})
96
 
97
- # Send user_content to Qwen if it's not empty
98
- if user_content:
99
  ai_response = get_qwen_response(user_content)
100
 
101
  # Send response back to user
@@ -106,7 +99,7 @@ def handle_message():
106
  payload = {
107
  "recipient_id": sender_id,
108
  "message_text": ai_response,
109
- "page_access_token": page_token # Include page-specific token
110
  }
111
  requests.post(AI_SERVICE_URL, json=payload, headers=headers)
112
 
 
3
  import json
4
  import os
5
  import uuid
6
+ from db import fetch_json_from_github
7
+ from qwen import get_qwen_response
8
+ from upload import upload_image_to_cdn
9
 
10
  app = Flask(__name__)
11
 
 
45
  body = request.get_json()
46
 
47
  for entry in body["entry"]:
48
+ page_id = entry["id"]
49
  page_token = PAGE_CREDENTIALS.get(page_id)
50
 
51
  if not page_token:
 
65
  if attachment["type"] == "image":
66
  image_url = attachment["payload"]["url"]
67
  try:
 
68
  img_response = requests.get(image_url)
69
  img_response.raise_for_status()
70
  temp_path = f"temp_{uuid.uuid4()}.jpg"
 
72
  with open(temp_path, "wb") as f:
73
  f.write(img_response.content)
74
 
 
75
  cdn_url = upload_image_to_cdn(BASE_URL, AUTH_TOKEN, temp_path)
76
  if cdn_url:
77
  image_cdn_urls.append(cdn_url)
 
81
  if os.path.exists(temp_path):
82
  os.remove(temp_path)
83
 
84
+ # Build message content for Qwen (single object with text + images)
85
+ user_content = {
86
+ "text": text_content,
87
+ "images": image_cdn_urls
88
+ }
 
 
 
 
 
89
 
90
+ # Send to Qwen only if there's content
91
+ if user_content["text"] or user_content["images"]:
92
  ai_response = get_qwen_response(user_content)
93
 
94
  # Send response back to user
 
99
  payload = {
100
  "recipient_id": sender_id,
101
  "message_text": ai_response,
102
+ "page_access_token": page_token
103
  }
104
  requests.post(AI_SERVICE_URL, json=payload, headers=headers)
105