GamerC0der commited on
Commit
7015481
·
verified ·
1 Parent(s): 6934dad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -18
app.py CHANGED
@@ -1,8 +1,11 @@
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"
@@ -13,28 +16,25 @@ def query_huggingface_api(api_key, model, prompt):
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()
 
1
  import gradio as gr
2
  import requests
3
+ import base64
4
+ from PIL import Image
5
+ from io import BytesIO
6
 
7
+ def query_hf_image_generation(api_key, prompt):
8
+ API_URL = f"https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
9
  headers = {
10
  "Authorization": f"Bearer {api_key}",
11
  "Content-Type": "application/json"
 
16
 
17
  response = requests.post(API_URL, headers=headers, json=data)
18
  result = response.json()
19
+
20
+ # Check if the API response contains an error.
21
+ if 'error' in result:
22
+ return f"Error: {result['error']}", None
23
 
24
+ # Assuming the API returns an image in base64 format.
25
+ image_data = result['data'][0] # You might need to adjust this path according to the actual API response
26
+ image = Image.open(BytesIO(base64.b64decode(image_data)))
27
+ return image
 
 
 
 
 
 
28
 
29
  iface = gr.Interface(
30
+ fn=query_hf_image_generation,
31
  inputs=[
32
  gr.Textbox(label="Hugging Face API Key", placeholder="Enter your Hugging Face API Key here..."),
33
+ gr.Textbox(lines=2, placeholder="Enter your prompt here...", label="Prompt"),
 
34
  ],
35
+ outputs=gr.outputs.Image(label="Generated Image"),
36
+ title="Stable Diffusion XL Image Generator",
37
+ description="Enter your API Key and a prompt to generate an image using the Stable Diffusion XL model from Hugging Face."
38
  )
39
 
40
  iface.launch()