Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Environment variables for API details
|
6 |
+
API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN") # Fetching the API token from environment variable
|
7 |
+
|
8 |
+
# Function to query Hugging Face API
|
9 |
+
def query_huggingface_api(api_url, prompt):
|
10 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
11 |
+
data = {"inputs": prompt}
|
12 |
+
response = requests.post(api_url, headers=headers, json=data)
|
13 |
+
|
14 |
+
if response.status_code == 200:
|
15 |
+
# Assuming the API returns binary image data
|
16 |
+
return response.content
|
17 |
+
else:
|
18 |
+
return None, f"Error {response.status_code}: {response.text}"
|
19 |
+
|
20 |
+
# Gradio function for generating the image
|
21 |
+
def generate_image(api_url, prompt):
|
22 |
+
result, error = query_huggingface_api(f"https://api-inference.huggingface.co/models/{api_url}", prompt)
|
23 |
+
|
24 |
+
if result:
|
25 |
+
return result, None
|
26 |
+
else:
|
27 |
+
return None, error
|
28 |
+
|
29 |
+
# Create Gradio Blocks Interface
|
30 |
+
with gr.Blocks() as demo:
|
31 |
+
gr.Markdown(
|
32 |
+
"""
|
33 |
+
# Text to Image Generator
|
34 |
+
Enter a text prompt, and the custom model will generate an image.
|
35 |
+
"""
|
36 |
+
)
|
37 |
+
|
38 |
+
with gr.Row():
|
39 |
+
with gr.Column():
|
40 |
+
text_input = gr.Textbox(
|
41 |
+
label="Enter your prompt",
|
42 |
+
placeholder="Type something here..."
|
43 |
+
)
|
44 |
+
model_input = gr.Textbox(
|
45 |
+
label="Model URL",
|
46 |
+
placeholder="Enter the model URL...",
|
47 |
+
value="user/sdwarm"
|
48 |
+
)
|
49 |
+
generate_btn = gr.Button("Generate Image")
|
50 |
+
|
51 |
+
with gr.Column():
|
52 |
+
image_output = gr.Image(label="Generated Image")
|
53 |
+
error_output = gr.Textbox(label="Error", interactive=False)
|
54 |
+
|
55 |
+
# Define the action for the button
|
56 |
+
generate_btn.click(
|
57 |
+
fn=generate_image,
|
58 |
+
inputs=[model_input, text_input], # Pass both model URL and prompt
|
59 |
+
outputs=[image_output, error_output]
|
60 |
+
)
|
61 |
+
|
62 |
+
# Launch the Gradio Blocks WebUI
|
63 |
+
demo.launch()
|