Skip to content

Commit

Permalink
add show image function
Browse files Browse the repository at this point in the history
  • Loading branch information
devashishtyagi committed Jan 21, 2024
1 parent b3fa9d6 commit 115c67f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
5 changes: 5 additions & 0 deletions samples/apps/fireworks-studio/notebooks/agent_spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
"title": "generate_and_save_images",
"file_name": "generate_and_save_images.py",
"content": "\nfrom typing import List\nimport uuid\nimport requests # to perform HTTP requests\nfrom pathlib import Path\nfrom typing_extensions import Annotated\nfrom openai import OpenAI\nfrom firestudio.utils.utils import schema_recorder\n\n@schema_recorder(description=\"Function to paint, draw or illustrate images based on the users query or request. Generates images from a given query using OpenAI's DALL-E model and saves them to disk. Use the code below anytime there is a request to create an image\")\ndef generate_and_save_images(query: Annotated[str, \"A natural language description of the image to be generated.\"], image_size: Annotated[str, \"The size of the image to be generated. default is '1024x1024'\"] = \"1024x1024\") -> List[str]:\n client = OpenAI() # Initialize the OpenAI client\n response = client.images.generate(model=\"dall-e-3\", prompt=query, n=1, size=image_size) # Generate images\n\n # List to store the file names of saved images\n saved_files = []\n\n # Check if the response is successful\n if response.data:\n for image_data in response.data:\n # Generate a random UUID as the file name\n file_name = str(uuid.uuid4()) + \".png\" # Assuming the image is a PNG\n file_path = Path(file_name)\n\n img_url = image_data.url\n img_response = requests.get(img_url)\n if img_response.status_code == 200:\n # Write the binary content to a file\n with open(file_path, \"wb\") as img_file:\n img_file.write(img_response.content)\n print(f\"Image saved to {file_path}\")\n saved_files.append(str(file_path))\n else:\n print(f\"Failed to download the image from {img_url}\")\n else:\n print(\"No image data found in the response!\")\n\n # Return the list of saved files\n return saved_files\n\n\n# Example usage of the function:\n# generate_and_save_images(\"A cute baby sea otter\")\n"
},
{
"title": "show_image",
"file_name": "show_image.py",
"content": "\nfrom typing import List\nimport uuid\nimport requests # to perform HTTP requests\nfrom pathlib import Path\nfrom typing_extensions import Annotated\nfrom firestudio.utils.utils import schema_recorder\nimport cv2\nfrom matplotlib import pyplot as plt\n\n@schema_recorder(description=\"A function that is capable for displaying an image given path to a image file in png or jpg or jpeg.\")\ndef show_image(path: Annotated[str, \"The path to the image file that needs to be displayed\"]) -> str:\n img = cv2.imread(path,-1)\n plt.imshow(img)\n plt.axis(\"off\")\n plt.show()\n return \"\"\n"
}
]
},
Expand Down
49 changes: 42 additions & 7 deletions samples/apps/fireworks-studio/notebooks/tutorial.ipynb

Large diffs are not rendered by default.

0 comments on commit 115c67f

Please sign in to comment.