Spaces:
Sleeping
Sleeping
| ### Helper function | |
| import os, requests, json | |
| hf_api_key = os.environ['HF_API_KEY'] | |
| ENDPOINT_URL=os.environ['HF_API_SUMMARY_BASE'] | |
| #Summarization endpoint | |
| def get_completion(inputs, parameters=None): | |
| headers = { | |
| "Authorization": f"Bearer {hf_api_key}", | |
| "Content-Type": "application/json" | |
| } | |
| payload = { "inputs": inputs } | |
| if parameters is not None: | |
| payload.update({"parameters": parameters}) | |
| response = requests.post(ENDPOINT_URL, | |
| headers=headers, | |
| data=json.dumps(payload)) | |
| return json.loads(response.content.decode("utf-8")) | |
| ### Gradio App | |
| import gradio as gr | |
| def summarize(input): | |
| output = get_completion(inputs = input, parameters = {"max_length" : 256}) | |
| return output[0]['summary_text'] | |
| gr.close_all() | |
| demo = gr.Interface(fn = summarize, | |
| inputs = [gr.Textbox(label = "Text to summarise", lines = 3)], | |
| outputs = [gr.Textbox(label = "Summary", lines = 3)], | |
| title = "Text Summarisation Demo", | |
| description = "Summarise text with `bart-large-cnn` model under the hood!", | |
| allow_flagging = "never", | |
| ) | |
| demo.launch() |