requests 替代 gradio_client的方法

import os, requests


API_NAME = "process_json"
HF_TOKEN = os.getenv("HF_TOKEN", None)
base = f"http://10.0.2.11:9002/gradio_api"

auth_h = {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"} if HF_TOKEN else {}
r = requests.post(f"{base}/call/{API_NAME}", headers=auth_h, json={"data": ["查询热电公司今年与去年同期供暖季的月度统计实际耗热量与度日数功耗数据"]}, timeout=30)
r.raise_for_status()
eid = r.json()["event_id"]
import json
with requests.get(f"{base}/call/{API_NAME}/{eid}", headers={"Authorization": f"Bearer {HF_TOKEN}", },  timeout=300) as resp:
    a=resp.text.encode('utf-8').decode('unicode_escape').replace("event: complete\n",'').replace('data: ','')
    a=json.loads(a)[0]
    print(a)
    

posted on 2026-01-29 14:13  张博的博客  阅读(0)  评论(0)    收藏  举报

导航