Hugging face api error

The following is my streamlit secrets file:-


The following is my streamlit code:

import os
import streamlit as st
import requests
import json
from PIL import Image
import base64
from io import BytesIO
from dotenv import load_dotenv, find_dotenv

# Load environment variables

# Helper function to call API endpoint
def get_completion(inputs, parameters=None, ENDPOINT_URL=st.secrets['HF_API_TTI_BASE']):
    hf_api_key = st.secrets['HF_API_KEY']
    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",
    return json.loads(response.content.decode("utf-8"))

# Helper function for image processing
def image_to_base64_str(pil_image):
    byte_arr = BytesIO(), format='PNG')
    byte_arr = byte_arr.getvalue()
    return str(base64.b64encode(byte_arr).decode('utf-8'))

def base64_to_pil(img_base64):
    base64_decoded = base64.b64decode(img_base64)
    byte_stream = BytesIO(base64_decoded)
    pil_image =
    return pil_image

# Streamlit app code
def main():
    st.markdown("# Image Generation with Stable Diffusion")
    prompt = st.text_input("Enter your prompt here...")
    negative_prompt = st.text_input("Enter negative prompt here...")
    steps = st.slider("Inference Steps", min_value=1, max_value=100, value=25, step=1)
    guidance = st.slider("Guidance Scale", min_value=1, max_value=20, value=7, step=1)
    width = st.slider("Width", min_value=64, max_value=512, value=512, step=64)
    height = st.slider("Height", min_value=64, max_value=512, value=512, step=64)

    if st.button("Submit"):
        result_image = generate(prompt, negative_prompt, steps, guidance, width, height)
        st.image(result_image, caption="Generated Image", use_column_width=True)

        # Download button for the generated image
        download_button_str = download_button(result_image, "Download Generated Image", "generated_image.png")
        st.markdown(download_button_str, unsafe_allow_html=True)

# Other functions
def generate(prompt, negative_prompt, steps, guidance, width, height):
    inputs = {
        "prompt": prompt,
        "negative_prompt": negative_prompt,
        "steps": steps,
        "guidance": guidance,
        "width": width,
        "height": height
    result = get_completion(inputs)
    generated_image = base64_to_pil(result['image'])
    return generated_image

# Function to generate download button link
def download_button(image, download_text, filename):
    buffered = BytesIO(), format="PNG")
    img_str = base64.b64encode(buffered.getvalue()).decode()
    href = f'<a href="data:image/png;base64,{img_str}" download="{filename}">{download_text}</a>'
    return href

if __name__ == "__main__":

The following is my error:

ConnectionError: HTTPSConnectionPool(host='', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x112c79710>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known'))
File "/Users/codevlogger/Library/Python/3.11/lib/python/site-packages/streamlit/runtime/scriptrunner/", line 552, in _run_script
    exec(code, module.__dict__)
File "/Users/codevlogger/Downloads/hugginface/", line 83, in <module>
File "/Users/codevlogger/Downloads/hugginface/", line 53, in main
    result_image = generate(prompt, negative_prompt, steps, guidance, width, height)
File "/Users/codevlogger/Downloads/hugginface/", line 70, in generate
    result = get_completion(inputs)
File "/Users/codevlogger/Downloads/hugginface/", line 23, in get_completion
    response = requests.request("POST",
File "/Users/codevlogger/Library/Python/3.11/lib/python/site-packages/requests/", line 61, in request
    return session.request(method=method, url=url, **kwargs)
File "/Users/codevlogger/Library/Python/3.11/lib/python/site-packages/requests/", line 529, in request
    resp = self.send(prep, **send_kwargs)
File "/Users/codevlogger/Library/Python/3.11/lib/python/site-packages/requests/", line 645, in send
    r = adapter.send(request, **kwargs)
File "/Users/codevlogger/Library/Python/3.11/lib/python/site-packages/requests/", line 519, in send
    raise ConnectionError(e, request=request)

I have tried all the possible solutions from all the sources. I’m asking anyone to figure out the error.

Hi @Guna_Sekhar_Venkata,

Please avoid sharing API keys publicly in the future. I’ve edited your post to redact your API key but you may still want to retire that API key just in case.

Thanks for guidance. But I’m unable to find the where my code getting error?

The host address looks wrong. Where did you get it from?

Some where in the web. Could you suggest me the correct approach?

Which web?

Im trying to use hugging face runwayml model

This is what I think is the correct approach: go back to “some where in the web” and check the host address.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.