Browse Source

Sep 23: [FIX] Bug Fixed 'openai_product_images'

16.0
Risvana Cybro 7 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. 8
      openai_product_images/views/dalle_image_suggestion.xml
  4. 13
      openai_product_images/views/product_template_views.xml
  5. 125
      openai_product_images/wizard/image_suggestion.py
  6. 8
      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:
returns the product form view image of corresponding product returns the product form view image of corresponding product
""" """
self.product_tmpl_id.image_1920 = self.product_image self.product_tmpl_id.image_1920 = self.product_image
return { return {
'name': self.product_tmpl_id.name, 'name': self.product_tmpl_id.name,

2
openai_product_images/models/product_template.py

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

8
openai_product_images/views/dalle_image_suggestion.xml

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<odoo> <odoo>
<data> <!-- form view for the model dalle.image.suggestion-->
<record id="dalle_image_suggestion_form_view" model="ir.ui.view"> <record id="dalle_image_suggestion_form_view" model="ir.ui.view">
<field name="name">dalle.image.suggestion.form.view</field> <field name="name">dalle.image.suggestion.form.view</field>
<field name="model">dalle.image.suggestion</field> <field name="model">dalle.image.suggestion</field>
@ -27,10 +27,9 @@
</form> </form>
</field> </field>
</record> </record>
<!-- tree view for the model dalle.image.suggestion-->
<record id="dalle_image_suggestion_tree_view" model="ir.ui.view"> <record id="dalle_image_suggestion_tree_view" model="ir.ui.view">
<field name="name"> <field name="name">dalle_image_suggestion.view.tree</field>
openai_product_images.dalle_image_suggestion.tree
</field>
<field name="model">dalle.image.suggestion</field> <field name="model">dalle.image.suggestion</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<tree string="Dall-E Image Suggestion"> <tree string="Dall-E Image Suggestion">
@ -39,5 +38,4 @@
</tree> </tree>
</field> </field>
</record> </record>
</data>
</odoo> </odoo>

13
openai_product_images/views/product_template_views.xml

@ -1,19 +1,21 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<odoo> <odoo>
<data> <!-- Added a server action in the model product template-->
<record id="action_product_template_dalle" model="ir.actions.server"> <record id="action_product_template_dalle" model="ir.actions.server">
<field name="name">Generate Image Using Dall-E</field> <field name="name">Generate Image Using Dall-E</field>
<field name="model_id" ref="product.model_product_template"/> <field name="model_id" ref="product.model_product_template"/>
<field name="binding_model_id" <field name="binding_model_id"
ref="product.model_product_template"/> ref="product.model_product_template"/>
<field name="binding_view_types">list,form</field> <field name="binding_view_types">tree,form</field>
<field name="state">code</field> <field name="state">code</field>
<field name="code">action =records.action_open_image_prompt_wizard() <field name="code">action =records.action_open_image_prompt_wizard()
</field> </field>
</record> </record>
<record id="num_product" model="ir.ui.view">
<field name="inherit_id" <!--Added smart button in the product.template form-->
ref="product.product_template_only_form_view"/> <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="model">product.template</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<div name="button_box" position="inside"> <div name="button_box" position="inside">
@ -24,5 +26,4 @@
</div> </div>
</field> </field>
</record> </record>
</data>
</odoo> </odoo>

125
openai_product_images/wizard/image_suggestion.py

@ -19,72 +19,89 @@
# If not, see <http://www.gnu.org/licenses/>. # If not, see <http://www.gnu.org/licenses/>.
# #
################################################################################ ################################################################################
from odoo import fields, models from odoo import fields, models, _
import requests from odoo.exceptions import UserError, ValidationError
from requests.structures import CaseInsensitiveDict from urllib.request import urlopen
import json from openai import OpenAI
import base64 import base64
from odoo.exceptions import ValidationError
class ImageSuggestion(models.TransientModel): class ImageSuggestion(models.TransientModel):
"""image for product from dalle""" """Image generator for product from OpenAI DALL·E 3"""
_name = 'image.suggestion' _name = 'image.suggestion'
_rec_name = 'product_tmpl_id' _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", image_prompt = fields.Char(
help="field to fill prompt", required=True) string="Prompt for Image",
product_tmpl_id = fields.Many2one('product.template', string="Product", help="Describe the image you want to generate",
help="field to store product", required=True
required=True) )
num_image = fields.Integer(string="Number of image needed", product_tmpl_id = fields.Many2one(
help="field to store number of image", '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, required=True,
default=1) default=1
)
size_image = fields.Selection( 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): def action_search(self):
"""Summary: """Generate product images from OpenAI DALL·E 3"""
Function to search image suggestion from dalle # Hardcoded API key as requested
Returns: api_key = self.env['ir.config_parameter'].sudo().get_param('openai_api_key')
returns the created media image of corresponding product
""" 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( # Optionally store in dalle.image.suggestion (if needed for history)
'openai_api_key') self.env['dalle.image.suggestion'].create({
headers = CaseInsensitiveDict() 'product_image': image_b64_encoded,
headers["Content-Type"] = "application/json" 'product_tmpl_id': self.product_tmpl_id.id,
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"}'
resp = requests.post("https://api.openai.com/v1/images/generations", # Return action to close wizard or refresh product form
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 { return {
'name': self.product_tmpl_id.name, 'type': 'ir.actions.act_window_close',
'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',
} }
except Exception as e:
raise UserError(f"Error generating image: {str(e)}")

8
openai_product_images/wizard/image_suggestion_views.xml

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<odoo> <odoo>
<data> <!--form view for the model image.suggestion-->
<record id="image_suggestion_form_view" model="ir.ui.view"> <record id="image_suggestion_view_form" model="ir.ui.view">
<field name="name">image.suggestion.form.view</field> <field name="name">image.suggestion.view.form</field>
<field name="model">image.suggestion</field> <field name="model">image.suggestion</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<form string="Image Suggestion"> <form string="Image Suggestion">
@ -13,6 +13,7 @@
<field name="image_prompt"/> <field name="image_prompt"/>
<field name="num_image"/> <field name="num_image"/>
<field name="size_image"/> <field name="size_image"/>
<field name="quality"/>
</group> </group>
</group> </group>
<footer> <footer>
@ -25,5 +26,4 @@
</form> </form>
</field> </field>
</record> </record>
</data>
</odoo> </odoo>
Loading…
Cancel
Save