File size: 1,278 Bytes
ba5c8ab
dbf7d83
24819b6
f5046f4
ba5c8ab
f5046f4
24819b6
ba5c8ab
 
24819b6
dbf7d83
24819b6
 
ba5c8ab
 
 
24819b6
ba5c8ab
 
 
 
 
 
24819b6
 
ba5c8ab
 
9ee76fd
 
24819b6
dcd3aef
24819b6
 
 
9ee76fd
087f278
 
 
 
051397c
087f278
 
 
24819b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
### 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()