Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
def query_huggingface_api(api_key, model, prompt): | |
API_URL = f"https://api-inference.huggingface.co/models/{model}" | |
headers = { | |
"Authorization": f"Bearer {api_key}", | |
"Content-Type": "application/json" | |
} | |
data = { | |
"inputs": prompt | |
} | |
response = requests.post(API_URL, headers=headers, json=data) | |
result = response.json() | |
# Check for errors or handle the response appropriately. | |
if isinstance(result, dict) and 'error' in result: | |
return f"Error: {result['error']}" | |
return result['generated_text'] if isinstance(result, dict) and 'generated_text' in result else result | |
# List of some available models | |
model_list = [ | |
"stabilityai/stable-diffusion-xl-base-1.0", | |
] | |
iface = gr.Interface( | |
fn=query_huggingface_api, | |
inputs=[ | |
gr.Textbox(label="Hugging Face API Key", placeholder="Enter your Hugging Face API Key here..."), | |
gr.Dropdown(choices=model_list, label="Model"), | |
gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Prompt") | |
], | |
outputs="text", | |
title="Hugging Face Model API Explorer", | |
description="Enter your API Key, select a model, and enter a prompt to generate text using the Hugging Face Inference API." | |
) | |
iface.launch() |