Hi all
I’ve decided to run lab 1 again and for my surprise it’s not working.
I’m using this code:
import os
import io
from IPython.display import Image, display, HTML
from PIL import Image
import base64
hf_api_key = “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”
HF_API_SUMMARY_BASE = “https://api-inference.huggingface.co/models/sshleifer/distilbart-cnn-12-6”
Helper function
import requests, json
#Summarization endpoint
def get_completion(inputs, parameters=None,ENDPOINT_URL=HF_API_SUMMARY_BASE):
headers = {
“Authorization”: f"Bearer {hf_api_key}",
“Content-Type”: “application/json”
}
data = { “inputs”: inputs }
if parameters is not None:
data.update({“parameters”: parameters})
response = requests.request(“POST”,
ENDPOINT_URL, headers=headers,
data=json.dumps(data)
)
return json.loads(response.content.decode(“utf-8”))
text = (‘’‘The tower is 324 metres (1,063 ft) tall, about the same height
as an 81-storey building, and the tallest structure in Paris.
Its base is square, measuring 125 metres (410 ft) on each side.
During its construction, the Eiffel Tower surpassed the Washington
Monument to become the tallest man-made structure in the world,
a title it held for 41 years until the Chrysler Building
in New York City was finished in 1930. It was the first structure
to reach a height of 300 metres. Due to the addition of a broadcasting
aerial at the top of the tower in 1957, it is now taller than the
Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the
Eiffel Tower is the second tallest free-standing structure in France
after the Millau Viaduct.’‘’)
get_completion(text)
Up to this point, everything works fine. The result was:
[{‘summary_text’: ’ The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building . It is the tallest structure in Paris and the second tallest free-standing structure in France after the Millau Viaduct . It was the first structure in the world to reach a height of 300 metres .'}]
But when I try the same approach with gradio, I receive an error in output box.
import gradio as gr
def summarize(input):
output = get_completion(input)
return output[0][‘summary_text’]
gr.close_all()
demo = gr.Interface(fn=summarize,
inputs=[gr.Textbox(label=“Text to summarize”, lines=6)],
outputs=[gr.Textbox(label=“Result”, lines=3)],
title=“Text summarization with distilbart-cnn”,
description=“Summarize any text using the sshleifer/distilbart-cnn-12-6
model under the hood!”
)
demo.launch()
For some reason, I can’t use gradio locally. When I try the same code in Google Colab, everything works fine.
Following the image of error:
All suggestions are welcome !
Fábio