Browse Source

Sep 23: [FIX] Bug Fixed 'openai_product_images'

16.0
Risvana Cybro 5 hours ago
parent
commit
21598243fc
  1. 1
      openai_product_images/models/dalle_image_suggestion.py
  2. 2
      openai_product_images/models/product_template.py
  3. 74
      openai_product_images/views/dalle_image_suggestion.xml
  4. 51
      openai_product_images/views/product_template_views.xml
  5. 131
      openai_product_images/wizard/image_suggestion.py
  6. 48
      openai_product_images/wizard/image_suggestion_views.xml

1
openai_product_images/models/dalle_image_suggestion.py

@ -39,7 +39,6 @@ class DalleImageSuggestion(models.Model):
Returns:
returns the product form view image of corresponding product
"""
self.product_tmpl_id.image_1920 = self.product_image
return {
'name': self.product_tmpl_id.name,

2
openai_product_images/models/product_template.py

@ -33,7 +33,6 @@ class ProductTemplate(models.Model):
Returns:
returns the image of corresponding product
"""
return {
'name': self.name,
'view_mode': 'form',
@ -49,7 +48,6 @@ class ProductTemplate(models.Model):
Returns:
returns the created image of corresponding product
"""
return {
'name': self.name,
'view_mode': 'tree,form',

74
openai_product_images/views/dalle_image_suggestion.xml

@ -1,43 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="dalle_image_suggestion_form_view" model="ir.ui.view">
<field name="name">dalle.image.suggestion.form.view</field>
<field name="model">dalle.image.suggestion</field>
<field name="arch" type="xml">
<form string="Dall-E Image Suggestion">
<header>
<button name="action_make_as_product_image" type="object"
string="Make as Product Image"/>
</header>
<sheet>
<div class="oe_title">
<label for="product_tmpl_id"/>
<h1>
<field name="product_tmpl_id"
placeholder="Title"/>
</h1>
</div>
<!-- form view for the model dalle.image.suggestion-->
<record id="dalle_image_suggestion_form_view" model="ir.ui.view">
<field name="name">dalle.image.suggestion.form.view</field>
<field name="model">dalle.image.suggestion</field>
<field name="arch" type="xml">
<form string="Dall-E Image Suggestion">
<header>
<button name="action_make_as_product_image" type="object"
string="Make as Product Image"/>
</header>
<sheet>
<div class="oe_title">
<label for="product_tmpl_id"/>
<h1>
<field name="product_tmpl_id"
placeholder="Title"/>
</h1>
</div>
<group>
<group>
<group>
<field name="product_image" widget="image"/>
</group>
<field name="product_image" widget="image"/>
</group>
</sheet>
</form>
</field>
</record>
<record id="dalle_image_suggestion_tree_view" model="ir.ui.view">
<field name="name">
openai_product_images.dalle_image_suggestion.tree
</field>
<field name="model">dalle.image.suggestion</field>
<field name="arch" type="xml">
<tree string="Dall-E Image Suggestion">
<field name="product_tmpl_id"/>
<field name="product_image" widget="image"/>
</tree>
</field>
</record>
</data>
</group>
</sheet>
</form>
</field>
</record>
<!-- tree view for the model dalle.image.suggestion-->
<record id="dalle_image_suggestion_tree_view" model="ir.ui.view">
<field name="name">dalle_image_suggestion.view.tree</field>
<field name="model">dalle.image.suggestion</field>
<field name="arch" type="xml">
<tree string="Dall-E Image Suggestion">
<field name="product_tmpl_id"/>
<field name="product_image" widget="image"/>
</tree>
</field>
</record>
</odoo>

51
openai_product_images/views/product_template_views.xml

@ -1,28 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="action_product_template_dalle" model="ir.actions.server">
<field name="name">Generate Image Using Dall-E</field>
<field name="model_id" ref="product.model_product_template"/>
<field name="binding_model_id"
ref="product.model_product_template"/>
<field name="binding_view_types">list,form</field>
<field name="state">code</field>
<field name="code">action =records.action_open_image_prompt_wizard()
</field>
</record>
<record id="num_product" model="ir.ui.view">
<field name="inherit_id"
ref="product.product_template_only_form_view"/>
<field name="model">product.template</field>
<field name="arch" type="xml">
<div name="button_box" position="inside">
<button name="get_dall_e_image" type="object"
class="oe_stat_button" icon="fa-id-card-o"
string="Dall E Image">
</button>
</div>
</field>
</record>
</data>
<!-- Added a server action in the model product template-->
<record id="action_product_template_dalle" model="ir.actions.server">
<field name="name">Generate Image Using Dall-E</field>
<field name="model_id" ref="product.model_product_template"/>
<field name="binding_model_id"
ref="product.model_product_template"/>
<field name="binding_view_types">tree,form</field>
<field name="state">code</field>
<field name="code">action =records.action_open_image_prompt_wizard()
</field>
</record>
<!--Added smart button in the product.template form-->
<record id="product_template_only_form_view" model="ir.ui.view">
<field name="name">product.template.view.form.inherit.openai.product.images</field>
<field name="inherit_id" ref="product.product_template_only_form_view"/>
<field name="model">product.template</field>
<field name="arch" type="xml">
<div name="button_box" position="inside">
<button name="get_dall_e_image" type="object"
class="oe_stat_button" icon="fa-id-card-o"
string="Dall E Image">
</button>
</div>
</field>
</record>
</odoo>

131
openai_product_images/wizard/image_suggestion.py

@ -19,72 +19,89 @@
# If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
from odoo import fields, models
import requests
from requests.structures import CaseInsensitiveDict
import json
from odoo import fields, models, _
from odoo.exceptions import UserError, ValidationError
from urllib.request import urlopen
from openai import OpenAI
import base64
from odoo.exceptions import ValidationError
class ImageSuggestion(models.TransientModel):
"""image for product from dalle"""
"""Image generator for product from OpenAI DALL·E 3"""
_name = 'image.suggestion'
_rec_name = 'product_tmpl_id'
_description = 'model for creation of dalle image '
_description = 'Model for creation of product images using DALL·E'
image_prompt = fields.Char(string="Prompt for image",
help="field to fill prompt", required=True)
product_tmpl_id = fields.Many2one('product.template', string="Product",
help="field to store product",
required=True)
num_image = fields.Integer(string="Number of image needed",
help="field to store number of image",
required=True,
default=1)
image_prompt = fields.Char(
string="Prompt for Image",
help="Describe the image you want to generate",
required=True
)
product_tmpl_id = fields.Many2one(
'product.template',
string="Product",
help="Select the product for which image needs to be generated",
required=True
)
num_image = fields.Integer(
string="Number of Images",
help="Number of images to generate",
required=True,
default=1
)
size_image = fields.Selection(
[('256x256', '256x256'), ('512x512', '512x512')], string="Resolution",
help="field to store image size", required=True)
[
('1024x1024', '1024x1024'),
('1024x1792', '1024x1792 (Portrait)'),
('1792x1024', '1792x1024 (Landscape)')
],
string="Resolution",
help="Resolution of generated images",
required=True,
default='1024x1024'
)
quality = fields.Selection(
[
('standard', 'Standard'),
('hd', 'High Definition')
],
string="Quality",
help="Image quality setting for DALL·E 3",
required=True,
default='standard'
)
def action_search(self):
"""Summary:
Function to search image suggestion from dalle
Returns:
returns the created media image of corresponding product
"""
"""Generate product images from OpenAI DALL·E 3"""
# Hardcoded API key as requested
api_key = self.env['ir.config_parameter'].sudo().get_param('openai_api_key')
client = OpenAI(api_key=api_key)
try:
response = client.images.generate(
model="dall-e-3",
prompt=self.image_prompt, # Use image_prompt field
size=self.size_image,
quality=self.quality,
n=self.num_image,
)
# Process the first image (DALL-E 3 typically returns one image)
image_url = response.data[0].url
image_content = urlopen(image_url).read()
image_b64_encoded = base64.b64encode(image_content)
# Update the product's image_1920 field
self.product_tmpl_id.write({'image_1920': image_b64_encoded})
api_key = self.env['ir.config_parameter'].sudo().get_param(
'openai_api_key')
headers = CaseInsensitiveDict()
headers["Content-Type"] = "application/json"
headers["Authorization"] = f"Bearer {api_key}"
data = '{"model": "image-alpha-001",' \
' "prompt": "' + self.image_prompt + '", "num_images": ' +\
str(self.num_image) + ', "size": "' \
+ self.size_image + '", "response_format": "url"}'
# Optionally store in dalle.image.suggestion (if needed for history)
self.env['dalle.image.suggestion'].create({
'product_image': image_b64_encoded,
'product_tmpl_id': self.product_tmpl_id.id,
})
resp = requests.post("https://api.openai.com/v1/images/generations",
headers=headers, data=data)
if resp.status_code != 200:
raise ValidationError("Failed to generate image")
response_text = json.loads(resp.text)
for url in response_text['data']:
image_url = url['url']
# Get image data and encode it in base64
image_resp = requests.get(image_url)
if image_resp.status_code != 200:
raise ValidationError("Failed to retrieve image")
image_b64 = base64.b64encode(image_resp.content)
# Store encoded image as binary field on model
self.env['dalle.image.suggestion'].create(
{'product_image': image_b64,
'product_tmpl_id': self.product_tmpl_id.id, })
return {
'name': self.product_tmpl_id.name,
'view_mode': 'tree,form',
'res_model': 'dalle.image.suggestion',
'type': 'ir.actions.act_window',
'domain': [('product_tmpl_id', '=', self.product_tmpl_id.id)],
'target': 'current',
}
# Return action to close wizard or refresh product form
return {
'type': 'ir.actions.act_window_close',
}
except Exception as e:
raise UserError(f"Error generating image: {str(e)}")

48
openai_product_images/wizard/image_suggestion_views.xml

@ -1,29 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="image_suggestion_form_view" model="ir.ui.view">
<field name="name">image.suggestion.form.view</field>
<field name="model">image.suggestion</field>
<field name="arch" type="xml">
<form string="Image Suggestion">
<sheet>
<!--form view for the model image.suggestion-->
<record id="image_suggestion_view_form" model="ir.ui.view">
<field name="name">image.suggestion.view.form</field>
<field name="model">image.suggestion</field>
<field name="arch" type="xml">
<form string="Image Suggestion">
<sheet>
<group>
<group>
<group>
<field name="product_tmpl_id"/>
<field name="image_prompt"/>
<field name="num_image"/>
<field name="size_image"/>
</group>
<field name="product_tmpl_id"/>
<field name="image_prompt"/>
<field name="num_image"/>
<field name="size_image"/>
<field name="quality"/>
</group>
<footer>
<button name="action_search" type="object"
string="Search" class="btn-primary"/>
<button string="Cancel" special="cancel"
data-hotkey="z"/>
</footer>
</sheet>
</form>
</field>
</record>
</data>
</group>
<footer>
<button name="action_search" type="object"
string="Search" class="btn-primary"/>
<button string="Cancel" special="cancel"
data-hotkey="z"/>
</footer>
</sheet>
</form>
</field>
</record>
</odoo>
Loading…
Cancel
Save