This project implements a web article summarization tool using the Dify API. Given a URL, the script fetches the content and summarizes it automatically. The API request is made using Python's requests library.
- Sends a URL to the Dify API for summarization.
- Retrieves and displays the summarized content.
- Uses API authentication via an API key.
- Supports
blockingandstreamingresponse modes.
Ensure you have the following installed:
- Python 3.8+
requestslibrary
pip install requestsModify the script with your Dify API key and the URL you want to summarize:
url = "https://api.dify.ai/v1/workflows/run"
api_key = "your_api_key" # Replace with your API KeyExecute the script to send a request and receive a summary:
python summarize.pyUpon successful execution, the script will return the summarized content:
成功取得摘要:
<summary_text>If the request fails, the script will output the error message and HTTP status code.
The script sets up an HTTP POST request to the Dify API with authentication headers:
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}The script sends the URL to the API for processing:
payload = {
"inputs": {"url": url_to_summarize},
"response_mode": "blocking",
"user": "unique_user_id"
}
response = requests.post(url, headers=headers, json=payload)If successful, the summarized text is extracted and printed:
if response.status_code == 200:
result = response.json()
summary = result['data']['outputs']
print("成功取得摘要:", summary)
else:
print(f"請求失敗,狀態碼: {response.status_code},訊息: {response.text}")- Ensure you replace
your_api_keywith a valid Dify API key. - The API supports different response modes (
blockingorstreaming). Adjustresponse_modeas needed. - If summarization fails, verify that the provided URL is accessible and contains valid content.
This project is licensed under the MIT License.