Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
from flask import Flask, request
|
2 |
import requests
|
3 |
import json
|
|
|
|
|
4 |
from db import fetch_json_from_github # Import the GitHub JSON fetcher
|
5 |
from qwen import get_qwen_response # Import the Qwen response function
|
6 |
|
@@ -22,6 +24,23 @@ except Exception as e:
|
|
22 |
AI_SERVICE_URL = "https://aoamrnuwara.pythonanywhere.com/send-message"
|
23 |
AI_SERVICE_TOKEN = "123400"
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
@app.route("/webhook", methods=["GET"])
|
26 |
def verify_webhook():
|
27 |
mode = request.args.get("hub.mode")
|
@@ -46,24 +65,57 @@ def handle_message():
|
|
46 |
continue
|
47 |
|
48 |
for messaging_event in entry["messaging"]:
|
49 |
-
if "message" in messaging_event
|
50 |
sender_id = messaging_event["sender"]["id"]
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
#
|
57 |
-
|
58 |
-
"
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
return "OK", 200
|
69 |
|
|
|
1 |
from flask import Flask, request
|
2 |
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 |
|
|
|
24 |
AI_SERVICE_URL = "https://aoamrnuwara.pythonanywhere.com/send-message"
|
25 |
AI_SERVICE_TOKEN = "123400"
|
26 |
|
27 |
+
# CDN Upload Configuration
|
28 |
+
UPLOAD_API_URL = "https://api.braininc.net/api/me/upload/studios"
|
29 |
+
AUTH_TOKEN = "72ec00483379076f580eb8126f29da802a5140c3"
|
30 |
+
|
31 |
+
def upload_image(file_path):
|
32 |
+
"""Upload image to get CDN URL"""
|
33 |
+
headers = {"authorization": f"token {AUTH_TOKEN}"}
|
34 |
+
try:
|
35 |
+
with open(file_path, "rb") as f:
|
36 |
+
files = {"file": (os.path.basename(file_path), f, "image/png")}
|
37 |
+
response = requests.post(UPLOAD_API_URL, headers=headers, files=files)
|
38 |
+
if response.status_code == 201:
|
39 |
+
return response.json().get("cdn_url", "")
|
40 |
+
except Exception as e:
|
41 |
+
print(f"Upload error: {str(e)}")
|
42 |
+
return ""
|
43 |
+
|
44 |
@app.route("/webhook", methods=["GET"])
|
45 |
def verify_webhook():
|
46 |
mode = request.args.get("hub.mode")
|
|
|
65 |
continue
|
66 |
|
67 |
for messaging_event in entry["messaging"]:
|
68 |
+
if "message" in messaging_event:
|
69 |
sender_id = messaging_event["sender"]["id"]
|
70 |
+
message = messaging_event["message"]
|
71 |
+
text_content = message.get("text", "")
|
72 |
+
attachments = message.get("attachments", [])
|
73 |
+
image_cdn_urls = []
|
74 |
+
|
75 |
+
# Process image attachments
|
76 |
+
for attachment in attachments:
|
77 |
+
if attachment["type"] == "image":
|
78 |
+
image_url = attachment["payload"]["url"]
|
79 |
+
try:
|
80 |
+
# Download image from Messenger
|
81 |
+
img_response = requests.get(image_url)
|
82 |
+
img_response.raise_for_status()
|
83 |
+
temp_path = f"temp_{uuid.uuid4()}.jpg"
|
84 |
+
|
85 |
+
with open(temp_path, "wb") as f:
|
86 |
+
f.write(img_response.content)
|
87 |
+
|
88 |
+
# Upload to CDN
|
89 |
+
cdn_url = upload_image(temp_path)
|
90 |
+
if cdn_url:
|
91 |
+
image_cdn_urls.append(cdn_url)
|
92 |
+
except Exception as e:
|
93 |
+
print(f"Image processing error: {str(e)}")
|
94 |
+
finally:
|
95 |
+
if os.path.exists(temp_path):
|
96 |
+
os.remove(temp_path)
|
97 |
+
|
98 |
+
# Build message content for Qwen
|
99 |
+
user_content = []
|
100 |
+
if text_content:
|
101 |
+
user_content.append({"type": "text", "text": text_content})
|
102 |
+
for cdn_url in image_cdn_urls:
|
103 |
+
user_content.append({"type": "image", "image": cdn_url})
|
104 |
+
|
105 |
+
if user_content:
|
106 |
+
ai_response = get_qwen_response(user_content)
|
107 |
+
|
108 |
+
# Send response back to user
|
109 |
+
headers = {
|
110 |
+
"Authorization": f"Bearer {AI_SERVICE_TOKEN}",
|
111 |
+
"Content-Type": "application/json"
|
112 |
+
}
|
113 |
+
payload = {
|
114 |
+
"recipient_id": sender_id,
|
115 |
+
"message_text": ai_response,
|
116 |
+
"page_access_token": page_token # Include page-specific token
|
117 |
+
}
|
118 |
+
requests.post(AI_SERVICE_URL, json=payload, headers=headers)
|
119 |
|
120 |
return "OK", 200
|
121 |
|