Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,40 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
|
| 4 |
+
def query_huggingface_api(api_key, model, prompt):
|
| 5 |
+
API_URL = f"https://api-inference.huggingface.co/models/{model}"
|
| 6 |
+
headers = {
|
| 7 |
+
"Authorization": f"Bearer {api_key}",
|
| 8 |
+
"Content-Type": "application/json"
|
| 9 |
+
}
|
| 10 |
+
data = {
|
| 11 |
+
"inputs": prompt
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
response = requests.post(API_URL, headers=headers, json=data)
|
| 15 |
+
result = response.json()
|
| 16 |
+
|
| 17 |
+
# Check for errors or handle the response appropriately.
|
| 18 |
+
if isinstance(result, dict) and 'error' in result:
|
| 19 |
+
return f"Error: {result['error']}"
|
| 20 |
+
|
| 21 |
+
return result['generated_text'] if isinstance(result, dict) and 'generated_text' in result else result
|
| 22 |
+
|
| 23 |
+
# List of some available models
|
| 24 |
+
model_list = [
|
| 25 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=query_huggingface_api,
|
| 30 |
+
inputs=[
|
| 31 |
+
gr.Textbox(label="Hugging Face API Key", placeholder="Enter your Hugging Face API Key here..."),
|
| 32 |
+
gr.Dropdown(choices=model_list, label="Model"),
|
| 33 |
+
gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Prompt")
|
| 34 |
+
],
|
| 35 |
+
outputs="text",
|
| 36 |
+
title="Hugging Face Model API Explorer",
|
| 37 |
+
description="Enter your API Key, select a model, and enter a prompt to generate text using the Hugging Face Inference API."
|
| 38 |
+
)
|
| 39 |
|
|
|
|
| 40 |
iface.launch()
|