Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import base64
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
API_KEY = os.environ.get("API_KEY")
|
| 9 |
+
API_URL = "https://api.4llm.com/v1/images/generations"
|
| 10 |
+
|
| 11 |
+
def generate_image(prompt, size="1024x1024"):
|
| 12 |
+
headers = {
|
| 13 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 14 |
+
"Content-Type": "application/json"
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
data = {
|
| 18 |
+
"model": "4llm-image",
|
| 19 |
+
"prompt": prompt,
|
| 20 |
+
"n": 1,
|
| 21 |
+
"size": size
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
response = requests.post(API_URL, headers=headers, json=data)
|
| 26 |
+
response.raise_for_status()
|
| 27 |
+
result = response.json()
|
| 28 |
+
|
| 29 |
+
# Get the base64 image data
|
| 30 |
+
image_data = result["data"][0]["url"].split(",")[1]
|
| 31 |
+
image_bytes = base64.b64decode(image_data)
|
| 32 |
+
|
| 33 |
+
# Convert to PIL Image
|
| 34 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 35 |
+
return image, result["data"][0]["revised_prompt"]
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return None, f"Error: {str(e)}"
|
| 38 |
+
|
| 39 |
+
# Create the Gradio interface
|
| 40 |
+
with gr.Blocks(title="4LLM Image Generation") as demo:
|
| 41 |
+
gr.Markdown("# 4LLM Image Generation Demo")
|
| 42 |
+
gr.Markdown("Generate images using the 4LLM API with no rate limits!")
|
| 43 |
+
|
| 44 |
+
with gr.Row():
|
| 45 |
+
with gr.Column():
|
| 46 |
+
prompt = gr.Textbox(
|
| 47 |
+
label="Prompt",
|
| 48 |
+
placeholder="Describe the image you want to generate...",
|
| 49 |
+
lines=3
|
| 50 |
+
)
|
| 51 |
+
size = gr.Dropdown(
|
| 52 |
+
choices=["1024x1024", "512x512", "768x768"],
|
| 53 |
+
value="1024x1024",
|
| 54 |
+
label="Image Size"
|
| 55 |
+
)
|
| 56 |
+
generate_btn = gr.Button("Generate Image")
|
| 57 |
+
|
| 58 |
+
with gr.Column():
|
| 59 |
+
output_image = gr.Image(label="Generated Image")
|
| 60 |
+
revised_prompt = gr.Textbox(
|
| 61 |
+
label="Revised Prompt",
|
| 62 |
+
lines=3,
|
| 63 |
+
interactive=False
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
generate_btn.click(
|
| 67 |
+
fn=generate_image,
|
| 68 |
+
inputs=[prompt, size],
|
| 69 |
+
outputs=[output_image, revised_prompt]
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
demo.launch(share=False)
|