Commit
·
532c92e
1
Parent(s):
e695965
add files
Browse files- app.py +51 -0
- requirements.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import base64
|
| 5 |
+
from dotenv import load_dotenv, find_dotenv
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import requests,json
|
| 8 |
+
|
| 9 |
+
_ = load_dotenv(find_dotenv()) # read local .env file
|
| 10 |
+
|
| 11 |
+
hf_api_key = os.environ['HF_API_KEY']
|
| 12 |
+
|
| 13 |
+
# Adjusted Helper function
|
| 14 |
+
def get_completion(inputs, parameters=None, ENDPOINT_URL=os.environ['HF_API_TTI_BASE']):
|
| 15 |
+
headers = {
|
| 16 |
+
"Authorization": f"Bearer {hf_api_key}",
|
| 17 |
+
"Content-Type": "application/json"
|
| 18 |
+
}
|
| 19 |
+
data = {"inputs": inputs}
|
| 20 |
+
if parameters is not None:
|
| 21 |
+
data.update({"parameters": parameters})
|
| 22 |
+
response = requests.post(ENDPOINT_URL, headers=headers, json=data)
|
| 23 |
+
|
| 24 |
+
if 'application/json' in response.headers.get('Content-Type'):
|
| 25 |
+
return response.json() # If response is JSON
|
| 26 |
+
else:
|
| 27 |
+
# If response is not JSON, handle as binary (image data)
|
| 28 |
+
return base64.b64encode(response.content).decode('utf-8') # Convert binary image to base64
|
| 29 |
+
|
| 30 |
+
def base64_to_pil(img_base64):
|
| 31 |
+
base64_decoded = base64.b64decode(img_base64)
|
| 32 |
+
byte_stream = io.BytesIO(base64_decoded)
|
| 33 |
+
pil_image = Image.open(byte_stream)
|
| 34 |
+
return pil_image
|
| 35 |
+
|
| 36 |
+
#Updated generate function to handle base64 image string
|
| 37 |
+
def generate(prompt):
|
| 38 |
+
output = get_completion(prompt)
|
| 39 |
+
# Assuming output is now a base64 encoded string of the image
|
| 40 |
+
pil_image = base64_to_pil(output) # Convert base64 string to PIL Image
|
| 41 |
+
return pil_image
|
| 42 |
+
|
| 43 |
+
# Rest of your Gradio setup remains the same
|
| 44 |
+
gr.close_all()
|
| 45 |
+
demo = gr.Interface(fn=generate,
|
| 46 |
+
inputs=[gr.Textbox(label="Your prompt")],
|
| 47 |
+
outputs=[gr.Image(label="Result")],
|
| 48 |
+
title="Image Generation with Stable Diffusion",
|
| 49 |
+
description="Generate any image with Stable Diffusion",
|
| 50 |
+
allow_flagging="never")
|
| 51 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
Binary file (146 Bytes). View file
|
|
|