can you give me an python code to read my text and summarize using the API for…
- User
can you give me an python code to read my text and summarize using the API for https://huggingface.co/spaces/yuntian-deng/ChatGPT
- Assistant
Certainly! Here's an example Python code that uses the Hugging Face API to read text and generate a summary using the ChatGPT model:
import requests import json def summarize_text(text): API_URL = "https://api-inference.huggingface.co/models/yuntian-deng/ChatGPT" headers = {"Authorization": "Bearer api_API_KEY"} # Replace 'API_KEY' with your actual API key payload = { "task": "summarization", "text": text, "options": { "use_cache": True, "use_gpu": True, "num_beams": 4, "max_length": 100, "top_p": 0.92, "do_sample": True } } response = requests.post(API_URL, headers=headers, json=payload) summary = json.loads(response.content.decode("utf-8"))["choices"][0]["text"] return summary # Example usage input_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. \ Aliquam eu ullamcorper turpis. Phasellus vel commodo enim. \ Donec at nunc sed libero consequat vestibulum. \ Apples are delicious fruits." output_summary = summarize_text(input_text) print("Summary:", output_summary)