Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import io
|
3 |
+
from IPython.display import Image, display, HTML
|
4 |
+
from PIL import Image
|
5 |
+
import base64
|
6 |
+
from dotenv import load_dotenv, find_dotenv
|
7 |
+
|
8 |
+
_ = load_dotenv(find_dotenv()) # read local .env file
|
9 |
+
hf_api_key = os.environ.getattribute("HF_API_KEY")
|
10 |
+
|
11 |
+
|
12 |
+
# Helper function
|
13 |
+
import requests, json
|
14 |
+
|
15 |
+
#Summarization endpoint
|
16 |
+
def get_completion(inputs, parameters=None,ENDPOINT_URL= os.environ.getattribute("API_URL")):
|
17 |
+
headers = {
|
18 |
+
"Authorization": f"Bearer {hf_api_key}",
|
19 |
+
"Content-Type": "application/json"
|
20 |
+
}
|
21 |
+
data = { "inputs": inputs }
|
22 |
+
if parameters is not None:
|
23 |
+
data.update({"parameters": parameters})
|
24 |
+
response = requests.request("POST",
|
25 |
+
ENDPOINT_URL, headers=headers,
|
26 |
+
data=json.dumps(data)
|
27 |
+
)
|
28 |
+
return json.loads(response.content.decode("utf-8"))
|
29 |
+
|
30 |
+
|
31 |
+
import gradio as gr
|
32 |
+
def summarize(input):
|
33 |
+
output = get_completion(input)
|
34 |
+
return output[0]['summary_text']
|
35 |
+
|
36 |
+
gr.close_all()
|
37 |
+
demo = gr.Interface(fn=summarize,
|
38 |
+
inputs=[gr.Textbox(label = "Text to Summarize", line = 6)],
|
39 |
+
outputs=[gr.Textbox(label = "Result", line = 3)],
|
40 |
+
title="Text Summarizer with Distilbart-cnn",
|
41 |
+
description= "Summarize any text using the `shleifer/distilbart-cnn-12-6` model under the hood!")
|
42 |
+
demo.launch(share=True)
|