Spaces:
Sleeping
Sleeping
Upload 0.py
Browse files
0.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import base64
|
| 3 |
+
import urllib.request
|
| 4 |
+
from http.server import BaseHTTPRequestHandler, HTTPServer
|
| 5 |
+
|
| 6 |
+
# --- 配置区 (建议在 HF Space 的 Settings -> Variables 中设置) ---
|
| 7 |
+
FEISHU_APP_ID = "你的AppID"
|
| 8 |
+
FEISHU_APP_SECRET = "你的AppSecret"
|
| 9 |
+
HF_TOKEN = "你的HuggingFaceToken"
|
| 10 |
+
HF_MODEL_URL = "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-base"
|
| 11 |
+
|
| 12 |
+
def get_feishu_token():
|
| 13 |
+
url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
|
| 14 |
+
data = json.dumps({"app_id": FEISHU_APP_ID, "app_secret": FEISHU_APP_SECRET}).encode()
|
| 15 |
+
req = urllib.request.Request(url, data=data, method="POST")
|
| 16 |
+
with urllib.request.urlopen(req) as f:
|
| 17 |
+
return json.loads(f.read()).get("tenant_access_token")
|
| 18 |
+
|
| 19 |
+
class FeishuHandler(BaseHTTPRequestHandler):
|
| 20 |
+
def do_POST(self):
|
| 21 |
+
content_length = int(self.headers['Content-Length'])
|
| 22 |
+
post_data = self.rfile.read(content_length)
|
| 23 |
+
data = json.loads(post_data.decode())
|
| 24 |
+
|
| 25 |
+
# 1. 处理飞书 URL 验证
|
| 26 |
+
if "challenge" in data:
|
| 27 |
+
self.send_response(200)
|
| 28 |
+
self.end_headers()
|
| 29 |
+
self.wfile.write(json.dumps({"challenge": data["challenge"]}).encode())
|
| 30 |
+
return
|
| 31 |
+
|
| 32 |
+
# 2. 处理图片消息
|
| 33 |
+
event = data.get("event", {})
|
| 34 |
+
msg = event.get("message", {})
|
| 35 |
+
if msg.get("msg_type") == "image":
|
| 36 |
+
msg_id = msg["message_id"]
|
| 37 |
+
img_key = json.loads(msg["content"])["image_key"]
|
| 38 |
+
open_id = event["sender"]["sender_id"]["open_id"]
|
| 39 |
+
|
| 40 |
+
# 开启后台处理,防止 Webhook 超时
|
| 41 |
+
self.process_task(msg_id, img_key, open_id)
|
| 42 |
+
|
| 43 |
+
self.send_response(200)
|
| 44 |
+
self.end_headers()
|
| 45 |
+
|
| 46 |
+
def process_task(self, msg_id, img_key, open_id):
|
| 47 |
+
token = get_feishu_token()
|
| 48 |
+
|
| 49 |
+
# A. 内存中获取图片
|
| 50 |
+
img_url = f"https://open.feishu.cn/open-apis/im/v1/messages/{msg_id}/resources/{img_key}?type=image"
|
| 51 |
+
img_req = urllib.request.Request(img_url, headers={"Authorization": f"Bearer {token}"})
|
| 52 |
+
with urllib.request.urlopen(img_req) as f:
|
| 53 |
+
img_bin = f.read() # 图片数据直接在内存变量里
|
| 54 |
+
|
| 55 |
+
# B. 调用 HF 模型进行识别
|
| 56 |
+
hf_req = urllib.request.Request(HF_MODEL_URL, data=img_bin, headers={"Authorization": f"Bearer {HF_TOKEN}"})
|
| 57 |
+
with urllib.request.urlopen(hf_req) as f:
|
| 58 |
+
desc = json.loads(f.read())[0].get("generated_text", "识别失败")
|
| 59 |
+
|
| 60 |
+
# C. 回复飞书
|
| 61 |
+
reply_url = "https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=open_id"
|
| 62 |
+
reply_data = json.dumps({
|
| 63 |
+
"receive_id": open_id,
|
| 64 |
+
"msg_type": "text",
|
| 65 |
+
"content": json.dumps({"text": f"🤖 AI 识别结果:\n{desc}"})
|
| 66 |
+
}).encode()
|
| 67 |
+
urllib.request.urlopen(urllib.request.Request(reply_url, data=reply_data,
|
| 68 |
+
headers={"Authorization": f"Bearer {token}", "Content-Type": "application/json"}))
|
| 69 |
+
|
| 70 |
+
# 启动服务
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
# Hugging Face 默认监听 7860 端口
|
| 73 |
+
server = HTTPServer(('0.0.0.0', 7860), FeishuHandler)
|
| 74 |
+
print("Serving on port 7860...")
|
| 75 |
+
server.serve_forever()
|