Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,10 +3,9 @@ import requests
|
|
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 |
|
11 |
app = Flask(__name__)
|
12 |
|
@@ -46,7 +45,7 @@ def handle_message():
|
|
46 |
body = request.get_json()
|
47 |
|
48 |
for entry in body["entry"]:
|
49 |
-
page_id = entry["id"]
|
50 |
page_token = PAGE_CREDENTIALS.get(page_id)
|
51 |
|
52 |
if not page_token:
|
@@ -66,6 +65,7 @@ def handle_message():
|
|
66 |
if attachment["type"] == "image":
|
67 |
image_url = attachment["payload"]["url"]
|
68 |
try:
|
|
|
69 |
img_response = requests.get(image_url)
|
70 |
img_response.raise_for_status()
|
71 |
temp_path = f"temp_{uuid.uuid4()}.jpg"
|
@@ -73,6 +73,7 @@ def handle_message():
|
|
73 |
with open(temp_path, "wb") as f:
|
74 |
f.write(img_response.content)
|
75 |
|
|
|
76 |
cdn_url = upload_image_to_cdn(BASE_URL, AUTH_TOKEN, temp_path)
|
77 |
if cdn_url:
|
78 |
image_cdn_urls.append(cdn_url)
|
@@ -82,20 +83,14 @@ def handle_message():
|
|
82 |
if os.path.exists(temp_path):
|
83 |
os.remove(temp_path)
|
84 |
|
85 |
-
# Build message content for Qwen
|
86 |
-
user_content = [
|
87 |
-
|
88 |
-
"type": "text",
|
89 |
-
|
90 |
-
|
91 |
-
{
|
92 |
-
"type": "image",
|
93 |
-
"image": image_cdn_urls
|
94 |
-
}
|
95 |
-
]
|
96 |
|
97 |
-
|
98 |
-
if user_content["text"] or user_content["images"]:
|
99 |
ai_response = get_qwen_response(user_content)
|
100 |
|
101 |
# Send response back to user
|
@@ -106,7 +101,7 @@ def handle_message():
|
|
106 |
payload = {
|
107 |
"recipient_id": sender_id,
|
108 |
"message_text": ai_response,
|
109 |
-
"page_access_token": page_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 # 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 |
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 |
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 |
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 |
if os.path.exists(temp_path):
|
84 |
os.remove(temp_path)
|
85 |
|
86 |
+
# Build message content for Qwen
|
87 |
+
user_content = []
|
88 |
+
if text_content:
|
89 |
+
user_content.append({"type": "text", "text": text_content})
|
90 |
+
for cdn_url in image_cdn_urls:
|
91 |
+
user_content.append({"type": "image", "image": cdn_url})
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
+
if user_content:
|
|
|
94 |
ai_response = get_qwen_response(user_content)
|
95 |
|
96 |
# Send response back to user
|
|
|
101 |
payload = {
|
102 |
"recipient_id": sender_id,
|
103 |
"message_text": ai_response,
|
104 |
+
"page_access_token": page_token # Include page-specific token
|
105 |
}
|
106 |
requests.post(AI_SERVICE_URL, json=payload, headers=headers)
|
107 |
|