File size: 1,303 Bytes
f966bca
6934dad
f966bca
6934dad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f966bca
 
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
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()