Spaces:
Sleeping
Sleeping
import base64 | |
import io | |
import cv2 | |
import requests | |
import json | |
import gradio as gr | |
import os | |
from PIL import Image | |
import numpy as np | |
from PIL import ImageOps | |
# Accessing a specific environment variable | |
api_key = os.environ.get('devisionx') | |
# Checking if the environment variable exists | |
if not api_key: | |
print("devisionx environment variable is not set.") | |
exit() | |
# Define a function to call the API and get the results | |
def base64str_to_PILImage(base64str): | |
base64_img_bytes = base64str.encode('utf-8') | |
base64bytes = base64.b64decode(base64_img_bytes) | |
bytesObj = io.BytesIO(base64bytes) | |
return ImageOps.exif_transpose(Image.open(bytesObj)) | |
def get_results(image, prompt): | |
threshold = 0.5 | |
# Convert the NumPy array to PIL image | |
image = Image.fromarray(image) | |
# Convert the image to base64 string | |
with io.BytesIO() as output: | |
image.save(output, format="JPEG") | |
base64str = base64.b64encode(output.getvalue()).decode("utf-8") | |
# Prepare the payload (Adjust this part according to the API requirements) | |
payload = json.dumps({"base64str": base64str, "classes": prompt}) | |
# Send the request to the API | |
response = requests.put(api_key, data=payload) | |
# Parse the JSON response | |
data = response.json() | |
print(response.status_code) | |
print(data) | |
# Access the values (Adjust this part according to the API response format) | |
output_image_base64 = data['firstName'] # Assuming the API returns the output image as base64 | |
# Convert the output image from base64 to PIL and then to NumPy array | |
output_image = base64str_to_PILImage(output_image_base64) | |
output_image = np.array(output_image) | |
return output_image | |
# Define the input components for Gradio (adding a new input for the prompt) | |
image_input = gr.inputs.Image() | |
text_input = gr.inputs.Textbox(label="Prompt") # New input for the text prompt | |
# Define the output components for Gradio (including both image and text) | |
outputs = gr.Image(type="numpy", label="Output Image") | |
# Define the text description within an HTML <div> element | |
description_html = """ | |
<div class="footer"> | |
<p>Model by <a href="https://deci.ai" style="text-decoration: underline;" target="_blank">tuba.ai</a> - Gradio Demo by 🤗 Hugging Face | |
</p> | |
</div> | |
<div class="acknowledgments"> | |
<p><h4>LICENSE</h4> | |
The model is licensed with a <a href="" style="text-decoration: underline;" target="_blank">CreativeML Open RAIL-M</a> license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/Deci/DeciDiffusion-v1-0/blob/main/LICENSE-WEIGHTS.md" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a></p> | |
<p><h4>Biases and content acknowledgment</h4> | |
Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence. The model was trained on the <a href="https://laion.ai/blog/laion-5b/" style="text-decoration: underline;" target="_blank">LAION-5B dataset</a>, which scraped non-curated image-text-pairs from the internet (the exception being the removal of illegal content) and is meant for research purposes. You can read more in the <a href="https://huggingface.co/Deci/DeciDiffusion-v1-0" style="text-decoration: underline;" target="_blank">model card</a></p> | |
</div> | |
""" | |
# Launch the Gradio interface with the description below it | |
gr.Interface( | |
fn=get_results, | |
inputs=[image_input, text_input], | |
outputs=outputs, | |
description=description_html # Use the HTML description | |
).launch(share=False) |