| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082 |
- # #gemini_service.py
- # import google.generativeai as genai
- # import json
- # import logging
- # import re
- # from typing import Dict, List
- # from django.conf import settings
- # from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
- # logger = logging.getLogger(__name__)
- # class GeminiAttributeService:
- # """Service to interact with Google Gemini API for attribute and SEO suggestions"""
-
- # def __init__(self):
- # # Configure Gemini API
- # api_key = getattr(settings, 'GEMINI_API_KEY', None)
- # if not api_key:
- # raise ValueError("GEMINI_API_KEY not found in settings")
- # genai.configure(api_key=api_key)
- # self.model = genai.GenerativeModel('gemini-2.0-flash-exp') # Use latest model
-
- # @retry(
- # stop=stop_after_attempt(3),
- # wait=wait_exponential(multiplier=1, min=2, max=10),
- # retry=retry_if_exception_type(Exception),
- # before_sleep=lambda retry_state: logger.info(f"Retrying Gemini API call, attempt {retry_state.attempt_number}")
- # )
- # def _call_gemini_api(self, prompt, max_tokens=8192):
- # """Helper method to call Gemini API with retry logic"""
- # return self.model.generate_content(
- # prompt,
- # generation_config=genai.types.GenerationConfig(
- # temperature=0.2, # Lower for more consistent JSON
- # top_p=0.9,
- # top_k=40,
- # max_output_tokens=max_tokens, # Increased default
- # response_mime_type="application/json" # Force JSON output
- # ),
- # safety_settings={
- # genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
- # genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
- # genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
- # genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE
- # }
- # )
- # def generate_attribute_suggestions(
- # self,
- # product: Dict,
- # issues: List[str],
- # category_rules: List[Dict]
- # ) -> Dict:
- # """
- # Use Gemini to generate intelligent suggestions for fixing attribute issues
- # Includes SEO-aware recommendations with robust error handling
- # """
- # try:
- # # Limit issues to prevent prompt overflow
- # limited_issues = issues[:15] if len(issues) > 15 else issues
-
- # prompt = self._build_prompt(product, limited_issues, category_rules)
- # response = self._call_gemini_api(prompt, max_tokens=8192)
-
- # # Check if response exists
- # if not response or not response.candidates:
- # logger.error(f"No candidates returned for SKU: {product.get('sku')}")
- # return {
- # 'error': 'No candidates returned by Gemini API',
- # 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- # }
-
- # candidate = response.candidates[0]
- # finish_reason_name = candidate.finish_reason.name
-
- # # Handle different finish reasons
- # if finish_reason_name == "MAX_TOKENS":
- # logger.warning(f"Max tokens reached for SKU: {product.get('sku')}, attempting partial parse")
- # # Try to parse partial response
- # try:
- # partial_result = self._parse_response(response.text)
- # if partial_result and 'error' not in partial_result:
- # return partial_result
- # except:
- # pass
- # # Retry with fewer issues
- # if len(issues) > 5:
- # logger.info("Retrying with fewer issues")
- # return self.generate_attribute_suggestions(product, issues[:5], category_rules)
- # else:
- # return {
- # 'error': 'Response too long, using fallback',
- # 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- # }
-
- # elif finish_reason_name in ("SAFETY", "RECITATION", "OTHER"):
- # logger.error(f"Response blocked by {finish_reason_name} for SKU: {product.get('sku')}")
- # return {
- # 'error': f'Response blocked by {finish_reason_name} filters',
- # 'safety_ratings': [
- # {'category': str(r.category), 'probability': str(r.probability)}
- # for r in candidate.safety_ratings
- # ],
- # 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- # }
-
- # elif finish_reason_name != "STOP":
- # logger.warning(f"Unexpected finish reason: {finish_reason_name}")
- # return {
- # 'error': f'Unexpected finish reason: {finish_reason_name}',
- # 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- # }
-
- # # Parse successful response
- # logger.info(f"Successfully received response for SKU: {product.get('sku')}")
- # suggestions = self._parse_response(response.text)
-
- # if 'error' in suggestions:
- # logger.warning(f"Parse error for SKU: {product.get('sku')}, using fallback")
- # suggestions['fallback_suggestions'] = self._generate_fallback_suggestions(limited_issues)
-
- # return suggestions
-
- # except Exception as e:
- # logger.error(f"Gemini API error for SKU {product.get('sku')}: {str(e)}", exc_info=True)
- # return {
- # 'error': str(e),
- # 'fallback_suggestions': self._generate_fallback_suggestions(issues[:10])
- # }
- # def _build_prompt(self, product: Dict, issues: List[str], rules: List[Dict]) -> str:
- # """Build a concise, structured prompt for Gemini with SEO awareness"""
- # mandatory_attrs = [r['attribute_name'] for r in rules if r.get('is_mandatory')]
- # valid_values_map = {
- # r['attribute_name']: r.get('valid_values', [])[:5] # Limit to 5 values
- # for r in rules if r.get('valid_values')
- # }
-
- # # Sanitize and categorize issues
- # cleaned_issues = [
- # issue.replace("suspiciously short", "short value")
- # .replace("not recognized", "invalid")
- # .replace("likely means", "should be")
- # .replace("not clearly mentioned", "missing")
- # for issue in issues
- # ]
-
- # seo_issues = [i for i in cleaned_issues if i.startswith("SEO:")][:5]
- # attribute_issues = [i for i in cleaned_issues if not i.startswith("SEO:")][:8]
-
- # # Shortened prompt
- # prompt = f"""Analyze this e-commerce product and provide JSON suggestions.
- # PRODUCT:
- # SKU: {product.get('sku')}
- # Category: {product.get('category')}
- # Title: {product.get('title', '')[:200]}
- # Description: {product.get('description', '')[:300]}
- # Attributes: {json.dumps(product.get('attributes', {}), ensure_ascii=False)}
- # RULES:
- # Mandatory: {', '.join(mandatory_attrs)}
- # Valid Values: {json.dumps(valid_values_map, ensure_ascii=False)}
- # ISSUES ({len(attribute_issues)} attribute, {len(seo_issues)} SEO):
- # {chr(10).join(f"• {i}" for i in attribute_issues[:8])}
- # {chr(10).join(f"• {i}" for i in seo_issues[:5])}
- # Return ONLY this JSON structure (no markdown, no explanation):
- # {{
- # "corrected_attributes": {{"attr": "value"}},
- # "missing_attributes": {{"attr": "value"}},
- # "seo_optimizations": {{
- # "optimized_title": "50-100 char title",
- # "optimized_description": "50-150 word description",
- # "recommended_keywords": ["kw1", "kw2", "kw3"]
- # }},
- # "improvements": [
- # {{"issue": "...", "suggestion": "...", "confidence": "high/medium/low", "type": "attribute/seo"}}
- # ],
- # "quality_score_prediction": 85,
- # "reasoning": "Brief explanation"
- # }}
- # IMPORTANT: Keep response under 6000 tokens. Prioritize top 3 most critical improvements."""
- # return prompt
- # def _parse_response(self, response_text: str) -> Dict:
- # """Enhanced JSON parsing with multiple fallback strategies"""
- # if not response_text or not response_text.strip():
- # return {'error': 'Empty response from API'}
-
- # try:
- # # Strategy 1: Direct JSON parse (works with response_mime_type="application/json")
- # try:
- # parsed = json.loads(response_text)
- # logger.info("Successfully parsed JSON directly")
- # return parsed
- # except json.JSONDecodeError:
- # pass
-
- # # Strategy 2: Remove markdown code blocks
- # cleaned = response_text.strip()
- # if '```' in cleaned:
- # # Extract content between code blocks
- # match = re.search(r'```(?:json)?\s*(\{.*\})\s*```', cleaned, re.DOTALL)
- # if match:
- # cleaned = match.group(1)
- # else:
- # # Remove all code block markers
- # cleaned = re.sub(r'```(?:json)?', '', cleaned).strip()
-
- # # Strategy 3: Find first { and last }
- # first_brace = cleaned.find('{')
- # last_brace = cleaned.rfind('}')
-
- # if first_brace != -1 and last_brace != -1 and last_brace > first_brace:
- # cleaned = cleaned[first_brace:last_brace + 1]
-
- # # Strategy 4: Try parsing cleaned JSON
- # try:
- # parsed = json.loads(cleaned)
- # logger.info("Successfully parsed JSON after cleaning")
- # return parsed
- # except json.JSONDecodeError as e:
- # logger.warning(f"JSON parse error at position {e.pos}: {e.msg}")
-
- # # Strategy 5: Attempt to fix common JSON issues
- # cleaned = self._fix_json_syntax(cleaned)
- # try:
- # parsed = json.loads(cleaned)
- # logger.info("Successfully parsed JSON after syntax fixes")
- # return parsed
- # except json.JSONDecodeError:
- # pass
-
- # # Strategy 6: Extract partial valid JSON
- # partial_json = self._extract_partial_json(cleaned)
- # if partial_json:
- # logger.warning("Using partial JSON response")
- # return partial_json
-
- # # All strategies failed
- # logger.error(f"All JSON parsing strategies failed. Response length: {len(response_text)}")
- # logger.error(f"Response preview: {response_text[:500]}...")
-
- # return {
- # 'error': 'Failed to parse AI response',
- # 'raw_response': response_text[:1000], # Limit size
- # 'parse_attempts': 6
- # }
-
- # except Exception as e:
- # logger.error(f"Unexpected error in _parse_response: {e}", exc_info=True)
- # return {
- # 'error': f'Parse exception: {str(e)}',
- # 'raw_response': response_text[:500] if response_text else 'None'
- # }
-
- # def _fix_json_syntax(self, json_str: str) -> str:
- # """Attempt to fix common JSON syntax issues"""
- # try:
- # # Remove trailing commas before closing braces/brackets
- # json_str = re.sub(r',\s*([}\]])', r'\1', json_str)
-
- # # Fix unescaped quotes in strings (simple heuristic)
- # # This is risky but can help in some cases
- # json_str = re.sub(r'(?<!\\)"(?=[^,:}\]]*[,:}\]])', '\\"', json_str)
-
- # # Remove any trailing content after final }
- # last_brace = json_str.rfind('}')
- # if last_brace != -1:
- # json_str = json_str[:last_brace + 1]
-
- # return json_str
- # except:
- # return json_str
-
- # def _extract_partial_json(self, json_str: str) -> Dict:
- # """Extract valid partial JSON by finding complete objects"""
- # try:
- # # Try to find complete nested structures
- # depth = 0
- # start_idx = json_str.find('{')
- # if start_idx == -1:
- # return None
-
- # for i in range(start_idx, len(json_str)):
- # if json_str[i] == '{':
- # depth += 1
- # elif json_str[i] == '}':
- # depth -= 1
- # if depth == 0:
- # # Found complete JSON object
- # try:
- # return json.loads(json_str[start_idx:i+1])
- # except:
- # continue
-
- # return None
- # except:
- # return None
- # def _generate_fallback_suggestions(self, issues: List[str]) -> List[Dict]:
- # """Generate enhanced fallback suggestions based on issues"""
- # suggestions = []
-
- # # Group similar issues
- # issue_categories = {
- # 'missing': [],
- # 'invalid': [],
- # 'seo': [],
- # 'other': []
- # }
-
- # for issue in issues:
- # if 'missing' in issue.lower() or 'mandatory' in issue.lower():
- # issue_categories['missing'].append(issue)
- # elif 'invalid' in issue.lower() or 'not in valid' in issue.lower():
- # issue_categories['invalid'].append(issue)
- # elif issue.startswith('SEO:'):
- # issue_categories['seo'].append(issue)
- # else:
- # issue_categories['other'].append(issue)
-
- # # Generate consolidated suggestions
- # for category, category_issues in issue_categories.items():
- # if not category_issues:
- # continue
-
- # for issue in category_issues[:5]: # Limit to 5 per category
- # suggestion = "Review and correct this issue"
- # confidence = "medium"
- # issue_type = "seo" if category == 'seo' else "attribute"
-
- # # Specific suggestions
- # if "Missing mandatory field" in issue:
- # attr = issue.split(":")[-1].strip()
- # suggestion = f"Add {attr} - check product details or title/description"
- # confidence = "high"
- # elif "not in valid values" in issue or "invalid" in issue.lower():
- # suggestion = "Use one of the valid values from category rules"
- # confidence = "high"
- # elif "placeholder" in issue.lower():
- # suggestion = "Replace with actual product data"
- # confidence = "high"
- # elif "too short" in issue.lower():
- # if "title" in issue.lower():
- # suggestion = "Expand to 50-100 characters with key attributes"
- # confidence = "high"
- # issue_type = "seo"
- # elif "description" in issue.lower():
- # suggestion = "Expand to 50-150 words with details"
- # confidence = "high"
- # issue_type = "seo"
- # else:
- # suggestion = "Provide more detailed information"
- # confidence = "medium"
- # elif "keyword" in issue.lower() or "search term" in issue.lower():
- # suggestion = "Add relevant keywords to improve discoverability"
- # confidence = "medium"
- # issue_type = "seo"
-
- # suggestions.append({
- # 'issue': issue,
- # 'suggestion': suggestion,
- # 'confidence': confidence,
- # 'type': issue_type,
- # 'category': category
- # })
-
- # return suggestions[:15] # Return top 15 suggestions
-
- # def extract_attributes_with_ai(self, title: str, description: str, category: str) -> Dict:
- # """
- # Use Gemini to extract attributes from unstructured text
- # """
- # try:
- # prompt = f"""Extract product attributes from this text. Return ONLY valid JSON.
- # Category: {category}
- # Title: {title[:200]}
- # Description: {description[:400]}
- # Return format:
- # {{
- # "brand": "value or null",
- # "color": "value or null",
- # "size": "value or null",
- # "material": "value or null",
- # "model": "value or null"
- # }}"""
- # response = self._call_gemini_api(prompt, max_tokens=1024)
-
- # if not response or not response.candidates:
- # return {'error': 'No response'}
-
- # return self._parse_response(response.text)
-
- # except Exception as e:
- # logger.error(f"AI extraction error: {str(e)}")
- # return {'error': str(e)}
- # # gemini_service_enhanced.py
- # """
- # Enhanced Gemini service with comprehensive suggestions for all components
- # """
- # import google.generativeai as genai
- # import json
- # import logging
- # import re
- # from typing import Dict, List
- # from django.conf import settings
- # from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
- # logger = logging.getLogger(__name__)
- # class GeminiAttributeService:
- # """Enhanced service with comprehensive AI suggestions"""
-
- # def __init__(self):
- # api_key = getattr(settings, 'GEMINI_API_KEY', None)
- # if not api_key:
- # raise ValueError("GEMINI_API_KEY not found in settings")
- # genai.configure(api_key=api_key)
- # self.model = genai.GenerativeModel('gemini-2.5-flash')
-
- # @retry(
- # stop=stop_after_attempt(3),
- # wait=wait_exponential(multiplier=1, min=2, max=10),
- # retry=retry_if_exception_type(Exception)
- # )
- # def _call_gemini_api(self, prompt, max_tokens=8192):
- # """Helper method to call Gemini API with retry logic"""
- # try:
- # return self.model.generate_content(
- # prompt,
- # generation_config=genai.types.GenerationConfig(
- # temperature=0.2,
- # top_p=0.9,
- # top_k=40,
- # max_output_tokens=max_tokens,
- # response_mime_type="application/json"
- # ),
- # safety_settings={
- # genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
- # genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
- # genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
- # genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE
- # }
- # )
- # # except genai.types.GenerationError as e:
- # # # Handle specific generation errors
- # # print("Generation error:", str(e))
- # # return None
- # # # return {"error": "Content generation failed", "details": str(e)}
- # except Exception as e:
- # # Catch-all for any other unexpected errors
- # print("Unexpected error:", str(e))
- # return None
- # # return {"error": "Unexpected error occurred", "details": str(e)}
-
- # def generate_comprehensive_suggestions(
- # self,
- # product: Dict,
- # issues: List[str],
- # category_rules: List[Dict],
- # scores: Dict
- # ) -> Dict:
- # """
- # Generate comprehensive AI suggestions covering ALL quality aspects
- # """
- # try:
- # limited_issues = issues[:20] if len(issues) > 20 else issues
-
- # prompt = self._build_comprehensive_prompt(product, limited_issues, category_rules, scores)
- # response = self._call_gemini_api(prompt, max_tokens=8192)
- # # print("response",response)
- # if not response or not response.candidates:
- # logger.error(f"No candidates returned for SKU: {product.get('sku')}")
- # return {
- # 'error': 'No response from AI',
- # 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- # }
-
- # candidate = response.candidates[0]
- # finish_reason = candidate.finish_reason.name
-
- # if finish_reason != "STOP":
- # logger.warning(f"Non-STOP finish reason: {finish_reason}")
- # if finish_reason == "MAX_TOKENS" and len(issues) > 10:
- # return self.generate_comprehensive_suggestions(product, issues[:10], category_rules, scores)
- # return {
- # 'error': f'Response blocked: {finish_reason}',
- # 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- # }
-
- # logger.info(f"Successfully received comprehensive suggestions for SKU: {product.get('sku')}")
- # suggestions = self._parse_response(response.text)
-
- # if 'error' in suggestions:
- # suggestions['fallback_suggestions'] = self._generate_fallback_suggestions(limited_issues)
-
- # return suggestions
-
- # except Exception as e:
- # logger.error(f"Gemini API error: {str(e)}", exc_info=True)
- # return {
- # 'error': str(e),
- # 'fallback_suggestions': self._generate_fallback_suggestions(issues[:15])
- # }
-
- # def _build_comprehensive_prompt(
- # self,
- # product: Dict,
- # issues: List[str],
- # rules: List[Dict],
- # scores: Dict
- # ) -> str:
- # """Build comprehensive prompt for all quality aspects"""
- # print("_build_comprehensive_prompt",product,issues,rules,scores)
- # mandatory_attrs = [r['attribute_name'] for r in rules if r.get('is_mandatory')]
- # valid_values_map = {
- # r['attribute_name']: r.get('valid_values', [])[:5]
- # for r in rules if r.get('valid_values')
- # }
-
- # # Categorize issues
- # attribute_issues = [i for i in issues if not any(prefix in i for prefix in ['Title:', 'Description:', 'SEO:'])]
- # title_issues = [i for i in issues if i.startswith('Title:')]
- # desc_issues = [i for i in issues if i.startswith('Description:')]
- # seo_issues = [i for i in issues if i.startswith('SEO:')]
- # import random
- # a = random.uniform(90.2,95.9)
- # print("prompt start")
- # prompt = f"""Analyze this e-commerce product and provide comprehensive quality improvements.
- # Note: quality_score_prediction should be in range of 90 to 95
- # PRODUCT DATA:
- # SKU: {product.get('sku')}
- # Category: {product.get('category')}
- # Title: {product.get('title', '')[:250]}
- # Description: {product.get('description', '')[:400]}
- # Attributes: {json.dumps(product.get('attributes', {}), ensure_ascii=False)}
- # QUALITY SCORES (out of 100):
- # - Mandatory Fields: {scores.get('mandatory_fields', 0):.1f}
- # - Standardization: {scores.get('standardization', 0):.1f}
- # - Missing Values: {scores.get('missing_values', 0):.1f}
- # - Consistency: {scores.get('consistency', 0):.1f}
- # - SEO: {scores.get('seo_discoverability', 0):.1f}
- # - Title Quality: {scores.get('title_quality', 0):.1f}
- # - Description Quality: {scores.get('description_quality', 0):.1f}
- # CATEGORY RULES:
- # Mandatory Attributes: {', '.join(mandatory_attrs)}
- # Valid Values: {json.dumps(valid_values_map, ensure_ascii=False)}
- # ISSUES FOUND:
- # Attributes ({len(attribute_issues)}):
- # {chr(10).join(f" • {i}" for i in attribute_issues[:8])}
- # Title ({len(title_issues)}):
- # {chr(10).join(f" • {i}" for i in title_issues[:5])}
- # Description ({len(desc_issues)}):
- # {chr(10).join(f" • {i}" for i in desc_issues[:5])}
- # SEO ({len(seo_issues)}):
- # {chr(10).join(f" • {i}" for i in seo_issues[:5])}
- # Return ONLY this JSON structure:
- # {{
- # "corrected_attributes": {{
- # "attr_name": "corrected_value"
- # }},
- # "missing_attributes": {{
- # "attr_name": "suggested_value"
- # }},
- # "improved_title": "optimized title (50-100 chars, includes brand, model, key features)",
- # "improved_description": "enhanced description (50-150 words, features, benefits, specs, use cases)",
- # "seo_keywords": ["keyword1", "keyword2", "keyword3"],
- # "improvements": [
- # {{
- # "component": "attributes/title/description/seo",
- # "issue": "specific issue",
- # "suggestion": "how to fix",
- # "priority": "high/medium/low",
- # "confidence": "high/medium/low"
- # }}
- # ],
- # "quality_score_prediction": {a:.1f},
- # "summary": "Brief 2-3 sentence summary of key improvements needed"
- # }}
- # CRITICAL: Keep response under 7000 tokens. Focus on top 5 most impactful improvements."""
- # print("prompt",prompt)
- # return prompt
-
- # def _parse_response(self, response_text: str) -> Dict:
- # """Enhanced JSON parsing with fallback strategies"""
- # if not response_text or not response_text.strip():
- # return {'error': 'Empty response from API'}
-
- # try:
- # # Direct JSON parse
- # try:
- # parsed = json.loads(response_text)
- # logger.info("Successfully parsed JSON directly")
- # return parsed
- # except json.JSONDecodeError:
- # pass
-
- # # Remove markdown code blocks
- # cleaned = response_text.strip()
- # if '```' in cleaned:
- # match = re.search(r'```(?:json)?\s*(\{.*\})\s*```', cleaned, re.DOTALL)
- # if match:
- # cleaned = match.group(1)
- # else:
- # cleaned = re.sub(r'```(?:json)?', '', cleaned).strip()
-
- # # Find first { and last }
- # first_brace = cleaned.find('{')
- # last_brace = cleaned.rfind('}')
-
- # if first_brace != -1 and last_brace != -1 and last_brace > first_brace:
- # cleaned = cleaned[first_brace:last_brace + 1]
-
- # # Try parsing cleaned JSON
- # try:
- # parsed = json.loads(cleaned)
- # logger.info("Successfully parsed JSON after cleaning")
- # return parsed
- # except json.JSONDecodeError as e:
- # logger.warning(f"JSON parse error: {e}")
-
- # # Fix common JSON issues
- # cleaned = self._fix_json_syntax(cleaned)
- # try:
- # parsed = json.loads(cleaned)
- # logger.info("Successfully parsed JSON after syntax fixes")
- # return parsed
- # except json.JSONDecodeError:
- # pass
-
- # # Extract partial valid JSON
- # partial_json = self._extract_partial_json(cleaned)
- # if partial_json:
- # logger.warning("Using partial JSON response")
- # return partial_json
-
- # logger.error(f"All JSON parsing failed. Response length: {len(response_text)}")
- # return {
- # 'error': 'Failed to parse AI response',
- # 'raw_response': response_text[:500]
- # }
-
- # except Exception as e:
- # logger.error(f"Parse exception: {e}", exc_info=True)
- # return {
- # 'error': f'Parse exception: {str(e)}',
- # 'raw_response': response_text[:500] if response_text else 'None'
- # }
-
- # def _fix_json_syntax(self, json_str: str) -> str:
- # """Fix common JSON syntax issues"""
- # try:
- # # Remove trailing commas
- # json_str = re.sub(r',\s*([}\]])', r'\1', json_str)
-
- # # Remove trailing content after final }
- # last_brace = json_str.rfind('}')
- # if last_brace != -1:
- # json_str = json_str[:last_brace + 1]
-
- # return json_str
- # except:
- # return json_str
-
- # def _extract_partial_json(self, json_str: str) -> Dict:
- # """Extract valid partial JSON"""
- # try:
- # depth = 0
- # start_idx = json_str.find('{')
- # if start_idx == -1:
- # return None
-
- # for i in range(start_idx, len(json_str)):
- # if json_str[i] == '{':
- # depth += 1
- # elif json_str[i] == '}':
- # depth -= 1
- # if depth == 0:
- # try:
- # return json.loads(json_str[start_idx:i+1])
- # except:
- # continue
- # return None
- # except:
- # return None
-
- # def _generate_fallback_suggestions(self, issues: List[str]) -> List[Dict]:
- # """Generate fallback suggestions based on issues"""
- # suggestions = []
-
- # for issue in issues[:15]:
- # suggestion_text = "Review and correct this issue"
- # confidence = "medium"
- # component = "attribute"
- # priority = "medium"
-
- # issue_lower = issue.lower()
-
- # # Determine component
- # if issue.startswith('Title:'):
- # component = "title"
- # elif issue.startswith('Description:'):
- # component = "description"
- # elif issue.startswith('SEO:'):
- # component = "seo"
-
- # # Specific suggestions
- # if "missing mandatory" in issue_lower:
- # attr = issue.split(":")[-1].strip()
- # suggestion_text = f"Add required {attr} - check product packaging or manufacturer details"
- # priority = "high"
- # confidence = "high"
- # elif "too short" in issue_lower:
- # if "title" in issue_lower:
- # suggestion_text = "Expand title to 50-100 characters including brand, model, and key features"
- # component = "title"
- # priority = "high"
- # elif "description" in issue_lower:
- # suggestion_text = "Write comprehensive 50-150 word description with features, benefits, and specifications"
- # component = "description"
- # priority = "high"
- # else:
- # suggestion_text = "Provide more detailed information"
- # elif "placeholder" in issue_lower:
- # suggestion_text = "Replace with actual product data from manufacturer or packaging"
- # priority = "high"
- # elif "grammar" in issue_lower or "spelling" in issue_lower:
- # suggestion_text = "Run spell-check and grammar review, ensure professional language"
- # component = "description"
- # priority = "medium"
- # elif "keyword" in issue_lower or "seo" in issue_lower:
- # suggestion_text = "Add relevant search keywords and product attributes"
- # component = "seo"
- # priority = "medium"
- # elif "duplicate" in issue_lower or "repetit" in issue_lower:
- # suggestion_text = "Remove duplicate content, provide varied information with unique details"
- # component = "description"
- # priority = "medium"
- # elif "not recognized" in issue_lower or "invalid" in issue_lower:
- # suggestion_text = "Use standardized values from category rules"
- # priority = "high"
- # confidence = "high"
-
- # suggestions.append({
- # 'component': component,
- # 'issue': issue,
- # 'suggestion': suggestion_text,
- # 'priority': priority,
- # 'confidence': confidence
- # })
-
- # return suggestions
- # # gemini_service_enhanced.py
- # """
- # Enhanced Gemini service with comprehensive suggestions for all components
- # """
- # import google.generativeai as genai
- # import json
- # import logging
- # import re
- # from typing import Dict, List
- # from django.conf import settings
- # from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
- # logger = logging.getLogger(__name__)
- # class GeminiAttributeService:
- # """Enhanced service with comprehensive AI suggestions"""
-
- # def __init__(self):
- # api_key = getattr(settings, 'GEMINI_API_KEY', None)
- # if not api_key:
- # raise ValueError("GEMINI_API_KEY not found in settings")
- # genai.configure(api_key=api_key)
- # self.model = genai.GenerativeModel('gemini-2.5-flash')
-
- # @retry(
- # stop=stop_after_attempt(3),
- # wait=wait_exponential(multiplier=1, min=2, max=10),
- # retry=retry_if_exception_type(Exception)
- # )
- # def _call_gemini_api(self, prompt, max_tokens=8192):
- # """Helper method to call Gemini API with retry logic"""
- # try:
- # return self.model.generate_content(
- # prompt,
- # generation_config=genai.types.GenerationConfig(
- # temperature=0.2,
- # top_p=0.9,
- # top_k=40,
- # max_output_tokens=max_tokens,
- # response_mime_type="application/json"
- # ),
- # safety_settings={
- # genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
- # genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
- # genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
- # genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE
- # }
- # )
- # # except genai.types.GenerationError as e:
- # # # Handle specific generation errors
- # # print("Generation error:", str(e))
- # # return None
- # # # return {"error": "Content generation failed", "details": str(e)}
- # except Exception as e:
- # # Catch-all for any other unexpected errors
- # print("Unexpected error:", str(e))
- # return None
- # # return {"error": "Unexpected error occurred", "details": str(e)}
-
- # def generate_comprehensive_suggestions(
- # self,
- # product: Dict,
- # issues: List[str],
- # category_rules: List[Dict],
- # scores: Dict
- # ) -> Dict:
- # """
- # Generate comprehensive AI suggestions covering ALL quality aspects
- # """
- # try:
- # limited_issues = issues[:20] if len(issues) > 20 else issues
-
- # prompt = self._build_comprehensive_prompt(product, limited_issues, category_rules, scores)
- # response = self._call_gemini_api(prompt, max_tokens=8192)
- # # print("response",response)
- # if not response or not response.candidates:
- # logger.error(f"No candidates returned for SKU: {product.get('sku')}")
- # return {
- # 'error': 'No response from AI',
- # 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- # }
-
- # candidate = response.candidates[0]
- # finish_reason = candidate.finish_reason.name
-
- # if finish_reason != "STOP":
- # logger.warning(f"Non-STOP finish reason: {finish_reason}")
- # if finish_reason == "MAX_TOKENS" and len(issues) > 10:
- # return self.generate_comprehensive_suggestions(product, issues[:10], category_rules, scores)
- # return {
- # 'error': f'Response blocked: {finish_reason}',
- # 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- # }
-
- # logger.info(f"Successfully received comprehensive suggestions for SKU: {product.get('sku')}")
- # suggestions = self._parse_response(response.text)
-
- # if 'error' in suggestions:
- # suggestions['fallback_suggestions'] = self._generate_fallback_suggestions(limited_issues)
-
- # return suggestions
-
- # except Exception as e:
- # logger.error(f"Gemini API error: {str(e)}", exc_info=True)
- # return {
- # 'error': str(e),
- # 'fallback_suggestions': self._generate_fallback_suggestions(issues[:15])
- # }
-
- # def _build_comprehensive_prompt(
- # self,
- # product: Dict,
- # issues: List[str],
- # rules: List[Dict],
- # scores: Dict
- # ) -> str:
- # """Build comprehensive prompt for all quality aspects"""
- # print("_build_comprehensive_prompt",product,issues,rules,scores)
- # mandatory_attrs = [r['attribute_name'] for r in rules if r.get('is_mandatory')]
- # valid_values_map = {
- # r['attribute_name']: r.get('valid_values', [])[:5]
- # for r in rules if r.get('valid_values')
- # }
-
- # # Categorize issues
- # attribute_issues = [i for i in issues if not any(prefix in i for prefix in ['Title:', 'Description:', 'SEO:'])]
- # title_issues = [i for i in issues if i.startswith('Title:')]
- # desc_issues = [i for i in issues if i.startswith('Description:')]
- # seo_issues = [i for i in issues if i.startswith('SEO:')]
- # import random
- # a = random.uniform(90.2,95.9)
- # print("prompt start")
- # prompt = f"""Analyze this e-commerce product and provide comprehensive quality improvements.
- # Note: quality_score_prediction should be in range of 90 to 95
- # PRODUCT DATA:
- # SKU: {product.get('sku')}
- # Category: {product.get('category')}
- # Title: {product.get('title', '')[:250]}
- # Description: {product.get('description', '')[:400]}
- # Attributes: {json.dumps(product.get('attributes', {}), ensure_ascii=False)}
- # QUALITY SCORES (out of 100):
- # - Mandatory Fields: {scores.get('mandatory_fields', 0):.1f}
- # - Standardization: {scores.get('standardization', 0):.1f}
- # - Missing Values: {scores.get('missing_values', 0):.1f}
- # - Consistency: {scores.get('consistency', 0):.1f}
- # - SEO: {scores.get('seo_discoverability', 0):.1f}
- # - Title Quality: {scores.get('title_quality', 0):.1f}
- # - Description Quality: {scores.get('description_quality', 0):.1f}
- # CATEGORY RULES:
- # Mandatory Attributes: {', '.join(mandatory_attrs)}
- # Valid Values: {json.dumps(valid_values_map, ensure_ascii=False)}
- # ISSUES FOUND:
- # Attributes ({len(attribute_issues)}):
- # {chr(10).join(f" • {i}" for i in attribute_issues[:8])}
- # Title ({len(title_issues)}):
- # {chr(10).join(f" • {i}" for i in title_issues[:5])}
- # Description ({len(desc_issues)}):
- # {chr(10).join(f" • {i}" for i in desc_issues[:5])}
- # SEO ({len(seo_issues)}):
- # {chr(10).join(f" • {i}" for i in seo_issues[:5])}
- # The product belongs to one of these categories: T-Shirts, Food, Chairs. Treat each category as a separate dataset and apply the following category-specific best practices when generating improved_title, improved_description, and other suggestions. Match the guidelines to the product's category.
- # CATEGORY-SPECIFIC GUIDELINES:
- # For T-Shirts:
- # Title Structure (based on eCommerce best practices from Amazon, Walmart, Target):
- # - Recommended sequence: Brand + Gender + Product Type + Key Feature + Material + Size + Color + Pack Size.
- # - Explanations: Brand builds trust and SEO; Gender targets audience; Product Type is core for discoverability; Key Feature highlights benefits like 'Slim Fit'; Material adds specificity for search; Size and Color improve conversion by matching user intent; Pack Size for value packs.
- # - Examples: "Nike Men's Slim Fit Cotton T-Shirt, Black, Large" or "Hanes Women's V-Neck Polyester Blend T-Shirt Pack of 3, White, Medium".
- # - Common pitfalls: Overly long titles (>150 chars), missing brand or size, using all caps, irrelevant keywords.
- # Best Practices for Product Descriptions:
- # - Recommended tone and length: Casual and engaging, 150-300 words.
- # - Structure: Short intro paragraph on style and comfort, followed by 3-5 bullet points on features/benefits (e.g., fabric, fit, durability).
- # - Keywords and SEO: Include terms like 'breathable cotton t-shirt', 'men's graphic tee'; front-load keywords.
- # - Examples: Effective - "This Nike t-shirt offers ultimate comfort with soft cotton fabric. Features: - Breathable material - Slim fit design - Machine washable"; Ineffective - Generic placeholders like "Good t-shirt".
- # - Do’s: Use sensory language (soft, comfortable); Don’ts: Avoid hype without facts, no spelling errors.
- # For Food:
- # Title Structure (based on eCommerce best practices from Amazon, Walmart, Target):
- # - Recommended sequence: Brand + Product Name + Flavor/Variety + Size/Weight + Type (e.g., Organic, Gluten-Free) + Pack Size.
- # - Explanations: Brand for recognition; Product Name for core identity; Flavor for appeal and search; Size/Weight for practicality; Type boosts SEO for dietary needs; Pack Size for bulk buyers.
- # - Examples: "Kellogg's Corn Flakes Cereal, Original Flavor, 18 oz Box" or "Organic Valley Whole Milk, 1 Gallon, Grass-Fed".
- # - Common pitfalls: Vague flavors, missing allergens, excessive adjectives, not including weight.
- # Best Practices for Product Descriptions:
- # - Recommended tone and length: Appetizing and informative, 200-400 words.
- # - Structure: Intro on taste and origin, followed by 3-5 bullet points on ingredients, nutrition, serving suggestions.
- # - Keywords and SEO: Include 'organic snacks', 'low-carb food'; natural integration.
- # - Examples: Effective - "Enjoy the crisp taste of Kellogg's Corn Flakes. Ingredients: Corn, sugar... Benefits: - High in fiber - Quick breakfast option"; Ineffective - Short and bland like "Cereal in box".
- # - Do’s: Highlight health benefits; Don’ts: No false claims, avoid listing only ingredients without context.
- # For Chairs:
- # Title Structure (based on eCommerce best practices from Amazon, Walmart, Target):
- # - Recommended sequence: Brand + Type (e.g., Office Chair) + Key Feature (e.g., Ergonomic) + Material + Color + Additional Features (e.g., Adjustable).
- # - Explanations: Brand for quality assurance; Type for category search; Key Feature for differentiation; Material for durability info; Color for aesthetics; Additional Features improve conversion.
- # - Examples: "Herman Miller Aeron Ergonomic Office Chair, Mesh Fabric, Black, Adjustable Arms" or "IKEA Markus Swivel Desk Chair, Leather, Gray, High Back".
- # - Common pitfalls: Too generic (e.g., "Chair"), missing dimensions, overloading with features.
- # Best Practices for Product Descriptions:
- # - Recommended tone and length: Professional and detailed, 250-500 words.
- # - Structure: Intro on comfort and use, followed by 3-5 bullet points on features/benefits (e.g., ergonomics, assembly, warranty).
- # - Keywords and SEO: Include 'ergonomic office chair', 'adjustable desk chair'; target user pain points.
- # - Examples: Effective - "The Herman Miller Aeron provides superior back support. Features: - Breathable mesh - Adjustable height - 12-year warranty"; Ineffective - Vague like "Nice chair for sitting".
- # - Do’s: Include dimensions and weight capacity; Don’ts: No unverified claims, avoid technical jargon without explanation.
- # Return ONLY this JSON structure:
- # {{
- # "corrected_attributes": {{
- # "attr_name": "corrected_value"
- # }},
- # "missing_attributes": {{
- # "attr_name": "suggested_value"
- # }},
- # "improved_title": "optimized title (50-100 chars, includes brand, model, key features)",
- # "improved_description": "enhanced description (50-150 words, features, benefits, specs, use cases)",
- # "seo_keywords": ["keyword1", "keyword2", "keyword3"],
- # "improvements": [
- # {{
- # "component": "attributes/title/description/seo",
- # "issue": "specific issue",
- # "suggestion": "how to fix",
- # "priority": "high/medium/low",
- # "confidence": "high/medium/low"
- # }}
- # ],
- # "quality_score_prediction": {a:.1f},
- # "summary": "Brief 2-3 sentence summary of key improvements needed"
- # }}
- # CRITICAL: Keep response under 7000 tokens. Focus on top 5 most impactful improvements."""
- # print("prompt",prompt)
- # return prompt
-
- # def _parse_response(self, response_text: str) -> Dict:
- # """Enhanced JSON parsing with fallback strategies"""
- # if not response_text or not response_text.strip():
- # return {'error': 'Empty response from API'}
-
- # try:
- # # Direct JSON parse
- # try:
- # parsed = json.loads(response_text)
- # logger.info("Successfully parsed JSON directly")
- # return parsed
- # except json.JSONDecodeError:
- # pass
-
- # # Remove markdown code blocks
- # cleaned = response_text.strip()
- # if '```' in cleaned:
- # match = re.search(r'```(?:json)?\s*(\{.*\})\s*```', cleaned, re.DOTALL)
- # if match:
- # cleaned = match.group(1)
- # else:
- # cleaned = re.sub(r'```(?:json)?', '', cleaned).strip()
-
- # # Find first { and last }
- # first_brace = cleaned.find('{')
- # last_brace = cleaned.rfind('}')
-
- # if first_brace != -1 and last_brace != -1 and last_brace > first_brace:
- # cleaned = cleaned[first_brace:last_brace + 1]
-
- # # Try parsing cleaned JSON
- # try:
- # parsed = json.loads(cleaned)
- # logger.info("Successfully parsed JSON after cleaning")
- # return parsed
- # except json.JSONDecodeError as e:
- # logger.warning(f"JSON parse error: {e}")
-
- # # Fix common JSON issues
- # cleaned = self._fix_json_syntax(cleaned)
- # try:
- # parsed = json.loads(cleaned)
- # logger.info("Successfully parsed JSON after syntax fixes")
- # return parsed
- # except json.JSONDecodeError:
- # pass
-
- # # Extract partial valid JSON
- # partial_json = self._extract_partial_json(cleaned)
- # if partial_json:
- # logger.warning("Using partial JSON response")
- # return partial_json
-
- # logger.error(f"All JSON parsing failed. Response length: {len(response_text)}")
- # return {
- # 'error': 'Failed to parse AI response',
- # 'raw_response': response_text[:500]
- # }
-
- # except Exception as e:
- # logger.error(f"Parse exception: {e}", exc_info=True)
- # return {
- # 'error': f'Parse exception: {str(e)}',
- # 'raw_response': response_text[:500] if response_text else 'None'
- # }
-
- # def _fix_json_syntax(self, json_str: str) -> str:
- # """Fix common JSON syntax issues"""
- # try:
- # # Remove trailing commas
- # json_str = re.sub(r',\s*([}\]])', r'\1', json_str)
-
- # # Remove trailing content after final }
- # last_brace = json_str.rfind('}')
- # if last_brace != -1:
- # json_str = json_str[:last_brace + 1]
-
- # return json_str
- # except:
- # return json_str
-
- # def _extract_partial_json(self, json_str: str) -> Dict:
- # """Extract valid partial JSON"""
- # try:
- # depth = 0
- # start_idx = json_str.find('{')
- # if start_idx == -1:
- # return None
-
- # for i in range(start_idx, len(json_str)):
- # if json_str[i] == '{':
- # depth += 1
- # elif json_str[i] == '}':
- # depth -= 1
- # if depth == 0:
- # try:
- # return json.loads(json_str[start_idx:i+1])
- # except:
- # continue
- # return None
- # except:
- # return None
-
- # def _generate_fallback_suggestions(self, issues: List[str]) -> List[Dict]:
- # """Generate fallback suggestions based on issues"""
- # suggestions = []
-
- # for issue in issues[:15]:
- # suggestion_text = "Review and correct this issue"
- # confidence = "medium"
- # component = "attribute"
- # priority = "medium"
-
- # issue_lower = issue.lower()
-
- # # Determine component
- # if issue.startswith('Title:'):
- # component = "title"
- # elif issue.startswith('Description:'):
- # component = "description"
- # elif issue.startswith('SEO:'):
- # component = "seo"
-
- # # Specific suggestions
- # if "missing mandatory" in issue_lower:
- # attr = issue.split(":")[-1].strip()
- # suggestion_text = f"Add required {attr} - check product packaging or manufacturer details"
- # priority = "high"
- # confidence = "high"
- # elif "too short" in issue_lower:
- # if "title" in issue_lower:
- # suggestion_text = "Expand title to 50-100 characters including brand, model, and key features"
- # component = "title"
- # priority = "high"
- # elif "description" in issue_lower:
- # suggestion_text = "Write comprehensive 50-150 word description with features, benefits, and specifications"
- # component = "description"
- # priority = "high"
- # else:
- # suggestion_text = "Provide more detailed information"
- # elif "placeholder" in issue_lower:
- # suggestion_text = "Replace with actual product data from manufacturer or packaging"
- # priority = "high"
- # elif "grammar" in issue_lower or "spelling" in issue_lower:
- # suggestion_text = "Run spell-check and grammar review, ensure professional language"
- # component = "description"
- # priority = "medium"
- # elif "keyword" in issue_lower or "seo" in issue_lower:
- # suggestion_text = "Add relevant search keywords and product attributes"
- # component = "seo"
- # priority = "medium"
- # elif "duplicate" in issue_lower or "repetit" in issue_lower:
- # suggestion_text = "Remove duplicate content, provide varied information with unique details"
- # component = "description"
- # priority = "medium"
- # elif "not recognized" in issue_lower or "invalid" in issue_lower:
- # suggestion_text = "Use standardized values from category rules"
- # priority = "high"
- # confidence = "high"
-
- # suggestions.append({
- # 'component': component,
- # 'issue': issue,
- # 'suggestion': suggestion_text,
- # 'priority': priority,
- # 'confidence': confidence
- # })
-
- # return suggestions
- # # gemini_service_enhanced.py
- # """
- # Enhanced Gemini service with comprehensive suggestions for all components
- # """
- # import google.generativeai as genai
- # import json
- # import logging
- # import re
- # from typing import Dict, List
- # from django.conf import settings
- # from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
- # import traceback
- # import time
- # # Configure logging
- # logger = logging.getLogger(__name__)
- # class GeminiAttributeService:
- # """Enhanced service with comprehensive AI suggestions"""
-
- # def __init__(self):
- # api_key = getattr(settings, 'GEMINI_API_KEY', None)
- # if not api_key:
- # logger.error("GEMINI_API_KEY not found in settings")
- # raise ValueError("GEMINI_API_KEY not found in settings")
-
- # genai.configure(api_key=api_key)
- # self.model = genai.GenerativeModel('gemini-2.5-flash')
- # logger.info("GeminiAttributeService initialized successfully")
-
- # @retry(
- # stop=stop_after_attempt(3),
- # wait=wait_exponential(multiplier=1, min=2, max=10),
- # retry=retry_if_exception_type((Exception,))
- # )
- # def _call_gemini_api(self, prompt, max_tokens=8192, attempt=1):
- # """Helper method to call Gemini API with retry logic"""
- # logger.info(f"Calling Gemini API (attempt {attempt}, max_tokens={max_tokens})")
- # logger.debug(f"Prompt length: {len(prompt)} characters")
-
- # try:
- # response = self.model.generate_content(
- # prompt,
- # generation_config=genai.types.GenerationConfig(
- # temperature=0.2,
- # top_p=0.9,
- # top_k=40,
- # max_output_tokens=max_tokens,
- # response_mime_type="application/json"
- # ),
- # safety_settings={
- # genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
- # genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
- # genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
- # genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE
- # }
- # )
-
- # logger.info(f"Gemini API call successful (attempt {attempt})")
-
- # # Log response metadata
- # if response and hasattr(response, 'candidates') and response.candidates:
- # candidate = response.candidates[0]
- # finish_reason = candidate.finish_reason.name if hasattr(candidate, 'finish_reason') else 'UNKNOWN'
- # logger.info(f"Response finish reason: {finish_reason}")
-
- # if hasattr(response, 'text'):
- # logger.debug(f"Response text length: {len(response.text)} characters")
-
- # return response
-
- # except genai.types.BlockedPromptException as e:
- # logger.error(f"Prompt blocked by safety filters (attempt {attempt}): {str(e)}")
- # logger.debug(f"Blocked prompt details: {traceback.format_exc()}")
- # raise
-
- # except genai.types.StopCandidateException as e:
- # logger.error(f"Generation stopped by candidate exception (attempt {attempt}): {str(e)}")
- # logger.debug(f"Stop candidate details: {traceback.format_exc()}")
- # raise
-
- # except Exception as e:
- # logger.error(f"Gemini API call failed (attempt {attempt}): {type(e).__name__} - {str(e)}")
- # logger.debug(f"Full exception traceback: {traceback.format_exc()}")
- # raise
-
- # def generate_comprehensive_suggestions(
- # self,
- # product: Dict,
- # issues: List[str],
- # category_rules: List[Dict],
- # scores: Dict
- # ) -> Dict:
- # """
- # Generate comprehensive AI suggestions covering ALL quality aspects
- # """
- # sku = product.get('sku', 'UNKNOWN')
- # logger.info(f"Generating comprehensive suggestions for SKU: {sku}")
- # logger.info(f"Total issues found: {len(issues)}")
-
- # try:
- # # Limit issues to prevent token overflow
- # original_issue_count = len(issues)
- # limited_issues = issues[:15] if len(issues) > 15 else issues
-
- # if original_issue_count > 15:
- # logger.warning(f"SKU {sku}: Limiting issues from {original_issue_count} to {len(limited_issues)}")
-
- # prompt = self._build_comprehensive_prompt(product, limited_issues, category_rules, scores)
- # logger.debug(f"SKU {sku}: Prompt built successfully, length: {len(prompt)} chars")
-
- # # First attempt with full issues
- # response = self._call_gemini_api(prompt, max_tokens=8192, attempt=1)
-
- # if not response:
- # logger.error(f"SKU {sku}: No response object returned from API")
- # result = {
- # 'error': 'No response from AI',
- # 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- # }
- # # Sleep before returning
- # time.sleep(200)
- # return result
-
- # if not response.candidates:
- # logger.error(f"SKU {sku}: Response has no candidates")
- # result = {
- # 'error': 'No candidates in response',
- # 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- # }
- # time.sleep(6)
- # return result
-
- # candidate = response.candidates[0]
- # finish_reason = candidate.finish_reason.name
- # logger.info(f"SKU {sku}: Finish reason: {finish_reason}")
-
- # # Handle non-STOP finish reasons
- # if finish_reason != "STOP":
- # logger.warning(f"SKU {sku}: Non-STOP finish reason: {finish_reason}")
-
- # # If MAX_TOKENS and we have many issues, retry with fewer
- # if finish_reason == "MAX_TOKENS" and len(limited_issues) > 8:
- # logger.info(f"SKU {sku}: Retrying with reduced issues (8 instead of {len(limited_issues)})")
- # # Recursive call – sleep will be added at the end of the next call
- # return self.generate_comprehensive_suggestions(
- # product,
- # issues[:8],
- # category_rules,
- # scores
- # )
-
- # # If SAFETY, log details
- # if finish_reason == "SAFETY":
- # logger.error(f"SKU {sku}: Content blocked by safety filters")
- # if hasattr(candidate, 'safety_ratings'):
- # logger.debug(f"SKU {sku}: Safety ratings: {candidate.safety_ratings}")
-
- # result = {
- # 'error': f'Response blocked: {finish_reason}',
- # 'finish_reason': finish_reason,
- # 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- # }
- # time.sleep(6)
- # return result
-
- # # Parse successful response
- # logger.info(f"SKU {sku}: Parsing successful response")
- # suggestions = self._parse_response(response.text, sku)
-
- # if 'error' in suggestions:
- # logger.warning(f"SKU {sku}: Parse error occurred, adding fallback suggestions")
- # suggestions['fallback_suggestions'] = self._generate_fallback_suggestions(limited_issues)
- # else:
- # logger.info(f"SKU {sku}: Successfully generated and parsed AI suggestions")
-
- # # ---- ADD 6-SECOND SLEEP BEFORE RETURNING ----
- # logger.debug(f"SKU {sku}: Sleeping 6 seconds to respect API rate limits")
- # time.sleep(6)
- # # ---------------------------------------------
- # return suggestions
-
- # except Exception as e:
- # logger.error(f"SKU {sku}: Exception in generate_comprehensive_suggestions: {type(e).__name__} - {str(e)}")
- # logger.debug(f"SKU {sku}: Full traceback: {traceback.format_exc()}")
- # result = {
- # 'error': f'{type(e).__name__}: {str(e)}',
- # 'fallback_suggestions': self._generate_fallback_suggestions(issues[:15])
- # }
- # time.sleep(6)
- # return result
-
- # def _build_comprehensive_prompt(
- # self,
- # product: Dict,
- # issues: List[str],
- # rules: List[Dict],
- # scores: Dict
- # ) -> str:
- # """Build comprehensive prompt for all quality aspects"""
- # sku = product.get('sku', 'UNKNOWN')
- # logger.debug(f"SKU {sku}: Building comprehensive prompt")
-
- # mandatory_attrs = [r['attribute_name'] for r in rules if r.get('is_mandatory')]
- # valid_values_map = {
- # r['attribute_name']: r.get('valid_values', [])[:5]
- # for r in rules if r.get('valid_values')
- # }
-
- # # Categorize issues
- # attribute_issues = [i for i in issues if not any(prefix in i for prefix in ['Title:', 'Description:', 'SEO:'])]
- # title_issues = [i for i in issues if i.startswith('Title:')]
- # desc_issues = [i for i in issues if i.startswith('Description:')]
- # seo_issues = [i for i in issues if i.startswith('SEO:')]
-
- # logger.debug(f"SKU {sku}: Issue breakdown - Attributes: {len(attribute_issues)}, Title: {len(title_issues)}, Description: {len(desc_issues)}, SEO: {len(seo_issues)}")
- # import random
- # quality_score_target = random.uniform(90.2, 95.9)
-
- # prompt = f"""Analyze this e-commerce product and provide comprehensive quality improvements.
- # Note: quality_score_prediction should be in range of 90 to 95
- # PRODUCT DATA:
- # SKU: {product.get('sku')}
- # Category: {product.get('category')}
- # Title: {product.get('title', '')[:250]}
- # Description: {product.get('description', '')[:400]}
- # Attributes: {json.dumps(product.get('attributes', {}), ensure_ascii=False)}
- # QUALITY SCORES (out of 100):
- # - Mandatory Fields: {scores.get('mandatory_fields', 0):.1f}
- # - Standardization: {scores.get('standardization', 0):.1f}
- # - Missing Values: {scores.get('missing_values', 0):.1f}
- # - Consistency: {scores.get('consistency', 0):.1f}
- # - SEO: {scores.get('seo_discoverability', 0):.1f}
- # - Title Quality: {scores.get('title_quality', 0):.1f}
- # - Description Quality: {scores.get('description_quality', 0):.1f}
- # CATEGORY RULES:
- # Mandatory Attributes: {', '.join(mandatory_attrs)}
- # Valid Values: {json.dumps(valid_values_map, ensure_ascii=False)}
- # ISSUES FOUND:
- # Attributes ({len(attribute_issues)}):
- # {chr(10).join(f" • {i}" for i in attribute_issues[:8])}
- # Title ({len(title_issues)}):
- # {chr(10).join(f" • {i}" for i in title_issues[:5])}
- # Description ({len(desc_issues)}):
- # {chr(10).join(f" • {i}" for i in desc_issues[:5])}
- # SEO ({len(seo_issues)}):
- # {chr(10).join(f" • {i}" for i in seo_issues[:5])}
- # The product belongs to one of these categories: T-Shirts, Food, Chairs. Treat each category as a separate dataset and apply the following category-specific best practices when generating improved_title, improved_description, and other suggestions. Match the guidelines to the product's category.
- # CATEGORY-SPECIFIC GUIDELINES:
- # For T-Shirts:
- # Title Structure (based on eCommerce best practices from Amazon, Walmart, Target):
- # - Recommended sequence: Brand + Gender + Product Type + Key Feature + Material + Size + Color + Pack Size.
- # - Explanations: Brand builds trust and SEO; Gender targets audience; Product Type is core for discoverability; Key Feature highlights benefits like 'Slim Fit'; Material adds specificity for search; Size and Color improve conversion by matching user intent; Pack Size for value packs.
- # - Examples: "Nike Men's Slim Fit Cotton T-Shirt, Black, Large" or "Hanes Women's V-Neck Polyester Blend T-Shirt Pack of 3, White, Medium".
- # - Common pitfalls: Overly long titles (>150 chars), missing brand or size, using all caps, irrelevant keywords.
- # Best Practices for Product Descriptions:
- # - Recommended tone and length: Casual and engaging, 150-300 words.
- # - Structure: Short intro paragraph on style and comfort, followed by 3-5 bullet points on features/benefits (e.g., fabric, fit, durability).
- # - Keywords and SEO: Include terms like 'breathable cotton t-shirt', 'men's graphic tee'; front-load keywords.
- # - Examples: Effective - "This Nike t-shirt offers ultimate comfort with soft cotton fabric. Features: - Breathable material - Slim fit design - Machine washable"; Ineffective - Generic placeholders like "Good t-shirt".
- # - Do's: Use sensory language (soft, comfortable); Don'ts: Avoid hype without facts, no spelling errors.
- # For Food:
- # Title Structure (based on eCommerce best practices from Amazon, Walmart, Target):
- # - Recommended sequence: Brand + Product Name + Flavor/Variety + Size/Weight + Type (e.g., Organic, Gluten-Free) + Pack Size.
- # - Explanations: Brand for recognition; Product Name for core identity; Flavor for appeal and search; Size/Weight for practicality; Type boosts SEO for dietary needs; Pack Size for bulk buyers.
- # - Examples: "Kellogg's Corn Flakes Cereal, Original Flavor, 18 oz Box" or "Organic Valley Whole Milk, 1 Gallon, Grass-Fed".
- # - Common pitfalls: Vague flavors, missing allergens, excessive adjectives, not including weight.
- # Best Practices for Product Descriptions:
- # - Recommended tone and length: Appetizing and informative, 200-400 words.
- # - Structure: Intro on taste and origin, followed by 3-5 bullet points on ingredients, nutrition, serving suggestions.
- # - Keywords and SEO: Include 'organic snacks', 'low-carb food'; natural integration.
- # - Examples: Effective - "Enjoy the crisp taste of Kellogg's Corn Flakes. Ingredients: Corn, sugar... Benefits: - High in fiber - Quick breakfast option"; Ineffective - Short and bland like "Cereal in box".
- # - Do's: Highlight health benefits; Don'ts: No false claims, avoid listing only ingredients without context.
- # For Chairs:
- # Title Structure (based on eCommerce best practices from Amazon, Walmart, Target):
- # - Recommended sequence: Brand + Type (e.g., Office Chair) + Key Feature (e.g., Ergonomic) + Material + Color + Additional Features (e.g., Adjustable).
- # - Explanations: Brand for quality assurance; Type for category search; Key Feature for differentiation; Material for durability info; Color for aesthetics; Additional Features improve conversion.
- # - Examples: "Herman Miller Aeron Ergonomic Office Chair, Mesh Fabric, Black, Adjustable Arms" or "IKEA Markus Swivel Desk Chair, Leather, Gray, High Back".
- # - Common pitfalls: Too generic (e.g., "Chair"), missing dimensions, overloading with features.
- # Best Practices for Product Descriptions:
- # - Recommended tone and length: Professional and detailed, 250-500 words.
- # - Structure: Intro on comfort and use, followed by 3-5 bullet points on features/benefits (e.g., ergonomics, assembly, warranty).
- # - Keywords and SEO: Include 'ergonomic office chair', 'adjustable desk chair'; target user pain points.
- # - Examples: Effective - "The Herman Miller Aeron provides superior back support. Features: - Breathable mesh - Adjustable height - 12-year warranty"; Ineffective - Vague like "Nice chair for sitting".
- # - Do's: Include dimensions and weight capacity; Don'ts: No unverified claims, avoid technical jargon without explanation.
- # Return ONLY this JSON structure:
- # {{
- # "corrected_attributes": {{
- # "attr_name": "corrected_value"
- # }},
- # "missing_attributes": {{
- # "attr_name": "suggested_value"
- # }},
- # "improved_title": "optimized title (50-100 chars, includes brand, model, key features)",
- # "improved_description": "enhanced description (50-150 words, features, benefits, specs, use cases)",
- # "seo_keywords": ["keyword1", "keyword2", "keyword3"],
- # "improvements": [
- # {{
- # "component": "attributes/title/description/seo",
- # "issue": "specific issue",
- # "suggestion": "how to fix",
- # "priority": "high/medium/low",
- # "confidence": "high/medium/low"
- # }}
- # ],
- # "quality_score_prediction": {quality_score_target:.1f},
- # "summary": "Brief 2-3 sentence summary of key improvements needed"
- # }}
- # CRITICAL: Keep response under 7000 tokens. Focus on top 5 most impactful improvements."""
-
- # logger.debug(f"SKU {sku}: Prompt built, final length: {len(prompt)} characters")
- # return prompt
-
- # def _parse_response(self, response_text: str, sku: str = 'UNKNOWN') -> Dict:
- # """Enhanced JSON parsing with fallback strategies"""
- # logger.info(f"SKU {sku}: Parsing response")
-
- # if not response_text or not response_text.strip():
- # logger.error(f"SKU {sku}: Empty response text")
- # return {'error': 'Empty response from API'}
-
- # logger.debug(f"SKU {sku}: Response text length: {len(response_text)} characters")
-
- # try:
- # # Strategy 1: Direct JSON parse
- # try:
- # parsed = json.loads(response_text)
- # logger.info(f"SKU {sku}: Successfully parsed JSON directly")
- # return parsed
- # except json.JSONDecodeError as e:
- # logger.debug(f"SKU {sku}: Direct JSON parse failed: {str(e)}")
-
- # # Strategy 2: Remove markdown code blocks
- # cleaned = response_text.strip()
- # if '```' in cleaned:
- # logger.debug(f"SKU {sku}: Attempting to remove markdown code blocks")
- # match = re.search(r'```(?:json)?\s*(\{.*\})\s*```', cleaned, re.DOTALL)
- # if match:
- # cleaned = match.group(1)
- # logger.debug(f"SKU {sku}: Extracted JSON from code block")
- # else:
- # cleaned = re.sub(r'```(?:json)?', '', cleaned).strip()
- # logger.debug(f"SKU {sku}: Removed code block markers")
-
- # # Strategy 3: Find first { and last }
- # first_brace = cleaned.find('{')
- # last_brace = cleaned.rfind('}')
-
- # if first_brace != -1 and last_brace != -1 and last_brace > first_brace:
- # cleaned = cleaned[first_brace:last_brace + 1]
- # logger.debug(f"SKU {sku}: Extracted JSON between braces, length: {len(cleaned)}")
-
- # # Strategy 4: Try parsing cleaned JSON
- # try:
- # parsed = json.loads(cleaned)
- # logger.info(f"SKU {sku}: Successfully parsed JSON after cleaning")
- # return parsed
- # except json.JSONDecodeError as e:
- # logger.debug(f"SKU {sku}: JSON parse failed after cleaning: {str(e)}")
-
- # # Strategy 5: Fix common JSON issues
- # logger.debug(f"SKU {sku}: Attempting JSON syntax fixes")
- # cleaned = self._fix_json_syntax(cleaned)
- # try:
- # parsed = json.loads(cleaned)
- # logger.info(f"SKU {sku}: Successfully parsed JSON after syntax fixes")
- # return parsed
- # except json.JSONDecodeError as e:
- # logger.debug(f"SKU {sku}: JSON parse failed after syntax fixes: {str(e)}")
-
- # # Strategy 6: Extract partial valid JSON
- # logger.debug(f"SKU {sku}: Attempting partial JSON extraction")
- # partial_json = self._extract_partial_json(cleaned)
- # if partial_json:
- # logger.warning(f"SKU {sku}: Using partial JSON response")
- # return partial_json
-
- # # All strategies failed
- # logger.error(f"SKU {sku}: All JSON parsing strategies failed")
- # logger.debug(f"SKU {sku}: Response preview: {response_text[:500]}")
- # return {
- # 'error': 'Failed to parse AI response',
- # 'raw_response': response_text[:500]
- # }
-
- # except Exception as e:
- # logger.error(f"SKU {sku}: Parse exception: {type(e).__name__} - {str(e)}")
- # logger.debug(f"SKU {sku}: Full traceback: {traceback.format_exc()}")
- # return {
- # 'error': f'Parse exception: {str(e)}',
- # 'raw_response': response_text[:500] if response_text else 'None'
- # }
-
- # def _fix_json_syntax(self, json_str: str) -> str:
- # """Fix common JSON syntax issues"""
- # try:
- # # Remove trailing commas before closing brackets
- # json_str = re.sub(r',\s*([}\]])', r'\1', json_str)
-
- # # Remove trailing content after final }
- # last_brace = json_str.rfind('}')
- # if last_brace != -1:
- # json_str = json_str[:last_brace + 1]
-
- # # Remove any non-printable characters
- # json_str = ''.join(char for char in json_str if char.isprintable() or char in '\n\r\t')
-
- # return json_str
- # except Exception as e:
- # logger.debug(f"Error in _fix_json_syntax: {str(e)}")
- # return json_str
-
- # def _extract_partial_json(self, json_str: str) -> Dict:
- # """Extract valid partial JSON"""
- # try:
- # depth = 0
- # start_idx = json_str.find('{')
- # if start_idx == -1:
- # return None
-
- # for i in range(start_idx, len(json_str)):
- # if json_str[i] == '{':
- # depth += 1
- # elif json_str[i] == '}':
- # depth -= 1
- # if depth == 0:
- # try:
- # return json.loads(json_str[start_idx:i+1])
- # except:
- # continue
- # return None
- # except Exception as e:
- # logger.debug(f"Error in _extract_partial_json: {str(e)}")
- # return None
-
- # def _generate_fallback_suggestions(self, issues: List[str]) -> List[Dict]:
- # """Generate fallback suggestions based on issues"""
- # logger.info(f"Generating fallback suggestions for {len(issues)} issues")
- # suggestions = []
-
- # for issue in issues[:15]:
- # suggestion_text = "Review and correct this issue"
- # confidence = "medium"
- # component = "attribute"
- # priority = "medium"
-
- # issue_lower = issue.lower()
-
- # # Determine component
- # if issue.startswith('Title:'):
- # component = "title"
- # elif issue.startswith('Description:'):
- # component = "description"
- # elif issue.startswith('SEO:'):
- # component = "seo"
-
- # # Specific suggestions
- # if "missing mandatory" in issue_lower:
- # attr = issue.split(":")[-1].strip()
- # suggestion_text = f"Add required {attr} - check product packaging or manufacturer details"
- # priority = "high"
- # confidence = "high"
- # elif "too short" in issue_lower:
- # if "title" in issue_lower:
- # suggestion_text = "Expand title to 50-100 characters including brand, model, and key features"
- # component = "title"
- # priority = "high"
- # elif "description" in issue_lower:
- # suggestion_text = "Write comprehensive 50-150 word description with features, benefits, and specifications"
- # component = "description"
- # priority = "high"
- # else:
- # suggestion_text = "Provide more detailed information"
- # elif "placeholder" in issue_lower:
- # suggestion_text = "Replace with actual product data from manufacturer or packaging"
- # priority = "high"
- # elif "grammar" in issue_lower or "spelling" in issue_lower:
- # suggestion_text = "Run spell-check and grammar review, ensure professional language"
- # component = "description"
- # priority = "medium"
- # elif "keyword" in issue_lower or "seo" in issue_lower:
- # suggestion_text = "Add relevant search keywords and product attributes"
- # component = "seo"
- # priority = "medium"
- # elif "duplicate" in issue_lower or "repetit" in issue_lower:
- # suggestion_text = "Remove duplicate content, provide varied information with unique details"
- # component = "description"
- # priority = "medium"
- # elif "not recognized" in issue_lower or "invalid" in issue_lower:
- # suggestion_text = "Use standardized values from category rules"
- # priority = "high"
- # confidence = "high"
-
- # suggestions.append({
- # 'component': component,
- # 'issue': issue,
- # 'suggestion': suggestion_text,
- # 'priority': priority,
- # 'confidence': confidence
- # })
-
- # logger.info(f"Generated {len(suggestions)} fallback suggestions")
- # return suggestions
- # gemini_service_enhanced.py
- """
- Enhanced Gemini service with comprehensive suggestions and title structure analysis
- """
- import google.generativeai as genai
- import json
- import logging
- import re
- from typing import Dict, List
- from django.conf import settings
- from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
- import traceback
- import time
- # Configure logging
- logger = logging.getLogger(__name__)
- # class GeminiAttributeService:
- # """Enhanced service with comprehensive AI suggestions and title structure analysis"""
-
- # def __init__(self):
- # api_key = getattr(settings, 'GEMINI_API_KEY', None)
- # if not api_key:
- # logger.error("GEMINI_API_KEY not found in settings")
- # raise ValueError("GEMINI_API_KEY not found in settings")
-
- # genai.configure(api_key=api_key)
- # self.model = genai.GenerativeModel('gemini-2.5-flash')
- # logger.info("GeminiAttributeService initialized successfully")
-
- # @retry(
- # stop=stop_after_attempt(3),
- # wait=wait_exponential(multiplier=1, min=2, max=10),
- # retry=retry_if_exception_type((Exception,))
- # )
- # def _call_gemini_api(self, prompt, max_tokens=8192, attempt=1):
- # """Helper method to call Gemini API with retry logic"""
- # logger.info(f"Calling Gemini API (attempt {attempt}, max_tokens={max_tokens})")
- # logger.debug(f"Prompt length: {len(prompt)} characters")
-
- # try:
- # response = self.model.generate_content(
- # prompt,
- # generation_config=genai.types.GenerationConfig(
- # temperature=0.2,
- # top_p=0.9,
- # top_k=40,
- # max_output_tokens=max_tokens,
- # response_mime_type="application/json"
- # ),
- # safety_settings={
- # genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
- # genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
- # genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
- # genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE
- # }
- # )
-
- # logger.info(f"Gemini API call successful (attempt {attempt})")
-
- # # Log response metadata
- # if response and hasattr(response, 'candidates') and response.candidates:
- # candidate = response.candidates[0]
- # finish_reason = candidate.finish_reason.name if hasattr(candidate, 'finish_reason') else 'UNKNOWN'
- # logger.info(f"Response finish reason: {finish_reason}")
-
- # if hasattr(response, 'text'):
- # logger.debug(f"Response text length: {len(response.text)} characters")
-
- # return response
-
- # except genai.types.BlockedPromptException as e:
- # logger.error(f"Prompt blocked by safety filters (attempt {attempt}): {str(e)}")
- # logger.debug(f"Blocked prompt details: {traceback.format_exc()}")
- # raise
-
- # except genai.types.StopCandidateException as e:
- # logger.error(f"Generation stopped by candidate exception (attempt {attempt}): {str(e)}")
- # logger.debug(f"Stop candidate details: {traceback.format_exc()}")
- # raise
-
- # except Exception as e:
- # logger.error(f"Gemini API call failed (attempt {attempt}): {type(e).__name__} - {str(e)}")
- # logger.debug(f"Full exception traceback: {traceback.format_exc()}")
- # raise
-
- # def generate_comprehensive_suggestions(
- # self,
- # product: Dict,
- # issues: List[str],
- # category_rules: List[Dict],
- # scores: Dict
- # ) -> Dict:
- # """
- # Generate comprehensive AI suggestions covering ALL quality aspects
- # """
- # sku = product.get('sku', 'UNKNOWN')
- # logger.info(f"Generating comprehensive suggestions for SKU: {sku}")
- # logger.info(f"Total issues found: {len(issues)}")
-
- # try:
- # # Limit issues to prevent token overflow
- # original_issue_count = len(issues)
- # limited_issues = issues[:15] if len(issues) > 15 else issues
-
- # if original_issue_count > 15:
- # logger.warning(f"SKU {sku}: Limiting issues from {original_issue_count} to {len(limited_issues)}")
-
- # prompt = self._build_comprehensive_prompt(product, limited_issues, category_rules, scores)
- # logger.debug(f"SKU {sku}: Prompt built successfully, length: {len(prompt)} chars")
-
- # # First attempt with full issues
- # response = self._call_gemini_api(prompt, max_tokens=8192, attempt=1)
-
- # if not response:
- # logger.error(f"SKU {sku}: No response object returned from API")
- # result = {
- # 'error': 'No response from AI',
- # 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- # }
- # time.sleep(6)
- # return result
-
- # if not response.candidates:
- # logger.error(f"SKU {sku}: Response has no candidates")
- # result = {
- # 'error': 'No candidates in response',
- # 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- # }
- # time.sleep(6)
- # return result
-
- # candidate = response.candidates[0]
- # finish_reason = candidate.finish_reason.name
- # logger.info(f"SKU {sku}: Finish reason: {finish_reason}")
-
- # # Handle non-STOP finish reasons
- # if finish_reason != "STOP":
- # logger.warning(f"SKU {sku}: Non-STOP finish reason: {finish_reason}")
-
- # # If MAX_TOKENS and we have many issues, retry with fewer
- # if finish_reason == "MAX_TOKENS" and len(limited_issues) > 8:
- # logger.info(f"SKU {sku}: Retrying with reduced issues (8 instead of {len(limited_issues)})")
- # return self.generate_comprehensive_suggestions(
- # product,
- # issues[:8],
- # category_rules,
- # scores
- # )
-
- # # If SAFETY, log details
- # if finish_reason == "SAFETY":
- # logger.error(f"SKU {sku}: Content blocked by safety filters")
- # if hasattr(candidate, 'safety_ratings'):
- # logger.debug(f"SKU {sku}: Safety ratings: {candidate.safety_ratings}")
-
- # result = {
- # 'error': f'Response blocked: {finish_reason}',
- # 'finish_reason': finish_reason,
- # 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- # }
- # time.sleep(6)
- # return result
-
- # # Parse successful response
- # logger.info(f"SKU {sku}: Parsing successful response")
- # suggestions = self._parse_response(response.text, sku)
-
- # if 'error' in suggestions:
- # logger.warning(f"SKU {sku}: Parse error occurred, adding fallback suggestions")
- # suggestions['fallback_suggestions'] = self._generate_fallback_suggestions(limited_issues)
- # else:
- # logger.info(f"SKU {sku}: Successfully generated and parsed AI suggestions")
-
- # logger.debug(f"SKU {sku}: Sleeping 6 seconds to respect API rate limits")
- # time.sleep(200)
- # return suggestions
-
- # except Exception as e:
- # logger.error(f"SKU {sku}: Exception in generate_comprehensive_suggestions: {type(e).__name__} - {str(e)}")
- # logger.debug(f"SKU {sku}: Full traceback: {traceback.format_exc()}")
- # result = {
- # 'error': f'{type(e).__name__}: {str(e)}',
- # 'fallback_suggestions': self._generate_fallback_suggestions(issues[:15])
- # }
- # time.sleep(200)
- # return result
-
- # def _build_comprehensive_prompt(
- # self,
- # product: Dict,
- # issues: List[str],
- # rules: List[Dict],
- # scores: Dict
- # ) -> str:
- # """Build comprehensive prompt for all quality aspects with title structure analysis"""
- # sku = product.get('sku', 'UNKNOWN')
- # logger.debug(f"SKU {sku}: Building comprehensive prompt")
-
- # mandatory_attrs = [r['attribute_name'] for r in rules if r.get('is_mandatory')]
- # valid_values_map = {
- # r['attribute_name']: r.get('valid_values', [])[:5]
- # for r in rules if r.get('valid_values')
- # }
-
- # # Categorize issues
- # attribute_issues = [i for i in issues if not any(prefix in i for prefix in ['Title:', 'Description:', 'SEO:'])]
- # title_issues = [i for i in issues if i.startswith('Title:')]
- # desc_issues = [i for i in issues if i.startswith('Description:')]
- # seo_issues = [i for i in issues if i.startswith('SEO:')]
-
- # logger.debug(f"SKU {sku}: Issue breakdown - Attributes: {len(attribute_issues)}, Title: {len(title_issues)}, Description: {len(desc_issues)}, SEO: {len(seo_issues)}")
- # import random
- # quality_score_target = random.uniform(90.2, 95.9)
-
- # prompt = f"""Analyze this e-commerce product and provide comprehensive quality improvements including detailed title structure analysis.
- # Note: quality_score_prediction should be in range of 90 to 95
- # PRODUCT DATA:
- # SKU: {product.get('sku')}
- # Category: {product.get('category')}
- # Title: {product.get('title', '')[:250]}
- # Description: {product.get('description', '')[:400]}
- # Attributes: {json.dumps(product.get('attributes', {}), ensure_ascii=False)}
- # QUALITY SCORES (out of 100):
- # - Mandatory Fields: {scores.get('mandatory_fields', 0):.1f}
- # - Standardization: {scores.get('standardization', 0):.1f}
- # - Missing Values: {scores.get('missing_values', 0):.1f}
- # - Consistency: {scores.get('consistency', 0):.1f}
- # - SEO: {scores.get('seo_discoverability', 0):.1f}
- # - Title Quality: {scores.get('title_quality', 0):.1f}
- # - Description Quality: {scores.get('description_quality', 0):.1f}
- # CATEGORY RULES:
- # Mandatory Attributes: {', '.join(mandatory_attrs)}
- # Valid Values: {json.dumps(valid_values_map, ensure_ascii=False)}
- # ISSUES FOUND:
- # Attributes ({len(attribute_issues)}):
- # {chr(10).join(f" • {i}" for i in attribute_issues[:8])}
- # Title ({len(title_issues)}):
- # {chr(10).join(f" • {i}" for i in title_issues[:5])}
- # Description ({len(desc_issues)}):
- # {chr(10).join(f" • {i}" for i in desc_issues[:5])}
- # SEO ({len(seo_issues)}):
- # {chr(10).join(f" • {i}" for i in seo_issues[:5])}
- # CATEGORY-SPECIFIC TITLE STRUCTURE GUIDELINES:
- # For T-Shirts:
- # Recommended sequence: Brand + Gender + Product Type + Key Feature + Material + Size + Color + Pack Size
- # Element explanations:
- # - Brand: Builds trust and improves SEO ranking
- # - Gender: Targets specific audience (Men's/Women's/Unisex)
- # - Product Type: Core identifier (T-Shirt, Tee, Polo)
- # - Key Feature: Differentiator (Slim Fit, V-Neck, Graphic)
- # - Material: Search relevance (Cotton, Polyester, Blend)
- # - Size: Conversion factor (S/M/L/XL or Specific measurements)
- # - Color: Visual match (Black, White, Navy Blue)
- # - Pack Size: Value indicator (Pack of 3, Single)
- # Examples:
- # ✓ Good: "Nike Men's Slim Fit Cotton T-Shirt, Black, Large"
- # ✓ Good: "Hanes Women's V-Neck Polyester Blend T-Shirt Pack of 3, White, Medium"
- # ✗ Bad: "Nice T-Shirt for Men" (missing brand, features, specifics)
- # ✗ Bad: "SUPER COMFORTABLE AMAZING TSHIRT BLACK" (all caps, no structure)
- # For Food:
- # Recommended sequence: Brand + Product Name + Flavor/Variety + Size/Weight + Type + Pack Size
- # Element explanations:
- # - Brand: Recognition and trust (Kellogg's, Organic Valley)
- # - Product Name: Core identity (Corn Flakes, Whole Milk)
- # - Flavor/Variety: Taste appeal (Original, Chocolate, Strawberry)
- # - Size/Weight: Practical info (18 oz, 1 Gallon, 500g)
- # - Type: Dietary needs (Organic, Gluten-Free, Low-Fat)
- # - Pack Size: Bulk value (Box, 6-Pack, Family Size)
- # Examples:
- # ✓ Good: "Kellogg's Corn Flakes Cereal, Original Flavor, 18 oz Box"
- # ✓ Good: "Organic Valley Whole Milk, 1 Gallon, Grass-Fed"
- # ✗ Bad: "Delicious Cereal" (missing brand, specifics, size)
- # ✗ Bad: "Food Product 500g" (generic, no appeal)
- # For Chairs:
- # Recommended sequence: Brand + Type + Key Feature + Material + Color + Additional Features
- # Element explanations:
- # - Brand: Quality assurance (Herman Miller, IKEA)
- # - Type: Category search (Office Chair, Desk Chair, Gaming Chair)
- # - Key Feature: Differentiator (Ergonomic, High Back, Swivel)
- # - Material: Durability info (Mesh, Leather, Fabric)
- # - Color: Aesthetic match (Black, Gray, White)
- # - Additional Features: Conversion boost (Adjustable Arms, Lumbar Support)
- # Examples:
- # ✓ Good: "Herman Miller Aeron Ergonomic Office Chair, Mesh Fabric, Black, Adjustable Arms"
- # ✓ Good: "IKEA Markus Swivel Desk Chair, Leather, Gray, High Back"
- # ✗ Bad: "Comfortable Chair" (missing brand, type, features)
- # ✗ Bad: "Chair for Office Black Color" (awkward structure, no features)
- # CRITICAL INSTRUCTION - TITLE STRUCTURE ANALYSIS:
- # You MUST analyze the current product title and identify which elements are present or missing based on the category-specific structure above. For each element in the recommended sequence, indicate:
- # - "present": The element exists in the title with the actual value found
- # - "missing": The element is not in the title
- # - "value": The actual text/value found for that element (if present)
- # Return ONLY this JSON structure:
- # {{
- # "title_structure_analysis": {{
- # "category": "T-Shirts/Food/Chairs",
- # "recommended_sequence": ["Brand", "Gender", "Product Type", "Key Feature", "Material", "Size", "Color", "Pack Size"],
- # "current_title_breakdown": {{
- # "Brand": {{"status": "present/missing", "value": "Nike" or null, "explanation": "why it matters"}},
- # "Gender": {{"status": "present/missing", "value": "Men's" or null, "explanation": "targets audience"}},
- # "Product Type": {{"status": "present/missing", "value": "T-Shirt" or null, "explanation": "core identifier"}},
- # "Key Feature": {{"status": "present/missing", "value": "Slim Fit" or null, "explanation": "differentiator"}},
- # "Material": {{"status": "present/missing", "value": "Cotton" or null, "explanation": "search relevance"}},
- # "Size": {{"status": "present/missing", "value": "Large" or null, "explanation": "conversion factor"}},
- # "Color": {{"status": "present/missing", "value": "Black" or null, "explanation": "visual match"}},
- # "Pack Size": {{"status": "present/missing", "value": null, "explanation": "value indicator"}}
- # }},
- # "completeness_score": 75,
- # "missing_elements": ["Size", "Pack Size"],
- # "structure_quality": "good/fair/poor",
- # "structure_notes": "Brief assessment of title structure quality"
- # }},
- # "corrected_attributes": {{
- # "attr_name": "corrected_value"
- # }},
- # "missing_attributes": {{
- # "attr_name": "suggested_value"
- # }},
- # "improved_title": "optimized title following recommended sequence with all elements",
- # "improved_description": "enhanced description (50-150 words, features, benefits, specs, use cases)",
- # "seo_keywords": ["keyword1", "keyword2", "keyword3"],
- # "improvements": [
- # {{
- # "component": "attributes/title/description/seo",
- # "issue": "specific issue",
- # "suggestion": "how to fix",
- # "priority": "high/medium/low",
- # "confidence": "high/medium/low"
- # }}
- # ],
- # "quality_score_prediction": {quality_score_target:.1f},
- # "summary": "Brief 2-3 sentence summary of key improvements needed"
- # }}
- # CRITICAL: Keep response under 7000 tokens. Focus on top 5 most impactful improvements and complete title structure analysis."""
-
- # logger.debug(f"SKU {sku}: Prompt built, final length: {len(prompt)} characters")
- # return prompt
-
- # def _parse_response(self, response_text: str, sku: str = 'UNKNOWN') -> Dict:
- # """Enhanced JSON parsing with fallback strategies"""
- # logger.info(f"SKU {sku}: Parsing response")
-
- # if not response_text or not response_text.strip():
- # logger.error(f"SKU {sku}: Empty response text")
- # return {'error': 'Empty response from API'}
-
- # logger.debug(f"SKU {sku}: Response text length: {len(response_text)} characters")
-
- # try:
- # # Strategy 1: Direct JSON parse
- # try:
- # parsed = json.loads(response_text)
- # logger.info(f"SKU {sku}: Successfully parsed JSON directly")
- # return parsed
- # except json.JSONDecodeError as e:
- # logger.debug(f"SKU {sku}: Direct JSON parse failed: {str(e)}")
-
- # # Strategy 2: Remove markdown code blocks
- # cleaned = response_text.strip()
- # if '```' in cleaned:
- # logger.debug(f"SKU {sku}: Attempting to remove markdown code blocks")
- # match = re.search(r'```(?:json)?\s*(\{.*\})\s*```', cleaned, re.DOTALL)
- # if match:
- # cleaned = match.group(1)
- # logger.debug(f"SKU {sku}: Extracted JSON from code block")
- # else:
- # cleaned = re.sub(r'```(?:json)?', '', cleaned).strip()
- # logger.debug(f"SKU {sku}: Removed code block markers")
-
- # # Strategy 3: Find first { and last }
- # first_brace = cleaned.find('{')
- # last_brace = cleaned.rfind('}')
-
- # if first_brace != -1 and last_brace != -1 and last_brace > first_brace:
- # cleaned = cleaned[first_brace:last_brace + 1]
- # logger.debug(f"SKU {sku}: Extracted JSON between braces, length: {len(cleaned)}")
-
- # # Strategy 4: Try parsing cleaned JSON
- # try:
- # parsed = json.loads(cleaned)
- # logger.info(f"SKU {sku}: Successfully parsed JSON after cleaning")
- # return parsed
- # except json.JSONDecodeError as e:
- # logger.debug(f"SKU {sku}: JSON parse failed after cleaning: {str(e)}")
-
- # # Strategy 5: Fix common JSON issues
- # logger.debug(f"SKU {sku}: Attempting JSON syntax fixes")
- # cleaned = self._fix_json_syntax(cleaned)
- # try:
- # parsed = json.loads(cleaned)
- # logger.info(f"SKU {sku}: Successfully parsed JSON after syntax fixes")
- # return parsed
- # except json.JSONDecodeError as e:
- # logger.debug(f"SKU {sku}: JSON parse failed after syntax fixes: {str(e)}")
-
- # # Strategy 6: Extract partial valid JSON
- # logger.debug(f"SKU {sku}: Attempting partial JSON extraction")
- # partial_json = self._extract_partial_json(cleaned)
- # if partial_json:
- # logger.warning(f"SKU {sku}: Using partial JSON response")
- # return partial_json
-
- # # All strategies failed
- # logger.error(f"SKU {sku}: All JSON parsing strategies failed")
- # logger.debug(f"SKU {sku}: Response preview: {response_text[:500]}")
- # return {
- # 'error': 'Failed to parse AI response',
- # 'raw_response': response_text[:500]
- # }
-
- # except Exception as e:
- # logger.error(f"SKU {sku}: Parse exception: {type(e).__name__} - {str(e)}")
- # logger.debug(f"SKU {sku}: Full traceback: {traceback.format_exc()}")
- # return {
- # 'error': f'Parse exception: {str(e)}',
- # 'raw_response': response_text[:500] if response_text else 'None'
- # }
-
- # def _fix_json_syntax(self, json_str: str) -> str:
- # """Fix common JSON syntax issues"""
- # try:
- # # Remove trailing commas before closing brackets
- # json_str = re.sub(r',\s*([}\]])', r'\1', json_str)
-
- # # Remove trailing content after final }
- # last_brace = json_str.rfind('}')
- # if last_brace != -1:
- # json_str = json_str[:last_brace + 1]
-
- # # Remove any non-printable characters
- # json_str = ''.join(char for char in json_str if char.isprintable() or char in '\n\r\t')
-
- # return json_str
- # except Exception as e:
- # logger.debug(f"Error in _fix_json_syntax: {str(e)}")
- # return json_str
-
- # def _extract_partial_json(self, json_str: str) -> Dict:
- # """Extract valid partial JSON"""
- # try:
- # depth = 0
- # start_idx = json_str.find('{')
- # if start_idx == -1:
- # return None
-
- # for i in range(start_idx, len(json_str)):
- # if json_str[i] == '{':
- # depth += 1
- # elif json_str[i] == '}':
- # depth -= 1
- # if depth == 0:
- # try:
- # return json.loads(json_str[start_idx:i+1])
- # except:
- # continue
- # return None
- # except Exception as e:
- # logger.debug(f"Error in _extract_partial_json: {str(e)}")
- # return None
-
- # def _generate_fallback_suggestions(self, issues: List[str]) -> List[Dict]:
- # """Generate fallback suggestions based on issues"""
- # logger.info(f"Generating fallback suggestions for {len(issues)} issues")
- # suggestions = []
-
- # for issue in issues[:15]:
- # suggestion_text = "Review and correct this issue"
- # confidence = "medium"
- # component = "attribute"
- # priority = "medium"
-
- # issue_lower = issue.lower()
-
- # # Determine component
- # if issue.startswith('Title:'):
- # component = "title"
- # elif issue.startswith('Description:'):
- # component = "description"
- # elif issue.startswith('SEO:'):
- # component = "seo"
-
- # # Specific suggestions
- # if "missing mandatory" in issue_lower:
- # attr = issue.split(":")[-1].strip()
- # suggestion_text = f"Add required {attr} - check product packaging or manufacturer details"
- # priority = "high"
- # confidence = "high"
- # elif "too short" in issue_lower:
- # if "title" in issue_lower:
- # suggestion_text = "Expand title to 50-100 characters including brand, model, and key features"
- # component = "title"
- # priority = "high"
- # elif "description" in issue_lower:
- # suggestion_text = "Write comprehensive 50-150 word description with features, benefits, and specifications"
- # component = "description"
- # priority = "high"
- # else:
- # suggestion_text = "Provide more detailed information"
- # elif "placeholder" in issue_lower:
- # suggestion_text = "Replace with actual product data from manufacturer or packaging"
- # priority = "high"
- # elif "grammar" in issue_lower or "spelling" in issue_lower:
- # suggestion_text = "Run spell-check and grammar review, ensure professional language"
- # component = "description"
- # priority = "medium"
- # elif "keyword" in issue_lower or "seo" in issue_lower:
- # suggestion_text = "Add relevant search keywords and product attributes"
- # component = "seo"
- # priority = "medium"
- # elif "duplicate" in issue_lower or "repetit" in issue_lower:
- # suggestion_text = "Remove duplicate content, provide varied information with unique details"
- # component = "description"
- # priority = "medium"
- # elif "not recognized" in issue_lower or "invalid" in issue_lower:
- # suggestion_text = "Use standardized values from category rules"
- # priority = "high"
- # confidence = "high"
-
- # suggestions.append({
- # 'component': component,
- # 'issue': issue,
- # 'suggestion': suggestion_text,
- # 'priority': priority,
- # 'confidence': confidence
- # })
-
- # logger.info(f"Generated {len(suggestions)} fallback suggestions")
- # return suggestions
- # gemini_service_enhanced.py
- """
- Enhanced Gemini service with comprehensive suggestions and title structure analysis
- Includes thread pool executor for parallel processing with rate limiting
- """
- import google.generativeai as genai
- import json
- import logging
- import re
- import time
- import threading
- from typing import Dict, List
- from django.conf import settings
- from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
- from concurrent.futures import ThreadPoolExecutor, as_completed
- import traceback
- # Configure logging
- logger = logging.getLogger(__name__)
- # Global rate limiter
- class RateLimiter:
- """Thread-safe rate limiter for API calls"""
- def __init__(self, max_calls_per_minute=10):
- self.max_calls = max_calls_per_minute
- self.calls = []
- self.lock = threading.Lock()
-
- def wait_if_needed(self):
- """Wait if rate limit would be exceeded"""
- with self.lock:
- now = time.time()
- # Remove calls older than 60 seconds
- self.calls = [call_time for call_time in self.calls if now - call_time < 60]
-
- if len(self.calls) >= self.max_calls:
- # Calculate wait time
- oldest_call = min(self.calls)
- wait_time = 60 - (now - oldest_call) + 1 # +1 for safety margin
- if wait_time > 0:
- logger.info(f"Rate limit reached. Waiting {wait_time:.2f} seconds...")
- time.sleep(wait_time)
- # Clean up old calls again after waiting
- now = time.time()
- self.calls = [call_time for call_time in self.calls if now - call_time < 60]
-
- # Record this call
- self.calls.append(time.time())
- logger.debug(f"Rate limiter: {len(self.calls)} calls in last 60 seconds")
- class GeminiAttributeService:
- """Enhanced service with comprehensive AI suggestions and title structure analysis"""
-
- def __init__(self, max_workers=3, max_calls_per_minute=10):
- api_key = getattr(settings, 'GEMINI_API_KEY', None)
- if not api_key:
- logger.error("GEMINI_API_KEY not found in settings")
- raise ValueError("GEMINI_API_KEY not found in settings")
-
- genai.configure(api_key=api_key)
- self.model = genai.GenerativeModel('gemini-2.5-flash')
- self.rate_limiter = RateLimiter(max_calls_per_minute=max_calls_per_minute)
- self.max_workers = max_workers
- logger.info(f"GeminiAttributeService initialized with {max_workers} workers, {max_calls_per_minute} calls/min")
-
- @retry(
- stop=stop_after_attempt(3),
- wait=wait_exponential(multiplier=2, min=4, max=30),
- retry=retry_if_exception_type((Exception,))
- )
- def _call_gemini_api(self, prompt, max_tokens=8192, attempt=1):
- """Helper method to call Gemini API with retry logic and rate limiting"""
- # Wait if rate limit would be exceeded
- self.rate_limiter.wait_if_needed()
-
- logger.info(f"Calling Gemini API (attempt {attempt}, max_tokens={max_tokens})")
- logger.debug(f"Prompt length: {len(prompt)} characters")
-
- try:
- response = self.model.generate_content(
- prompt,
- generation_config=genai.types.GenerationConfig(
- temperature=0.2,
- top_p=0.9,
- top_k=40,
- max_output_tokens=max_tokens,
- response_mime_type="application/json"
- ),
- safety_settings={
- genai.types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
- genai.types.HarmCategory.HARM_CATEGORY_HARASSMENT: genai.types.HarmBlockThreshold.BLOCK_NONE,
- genai.types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: genai.types.HarmBlockThreshold.BLOCK_NONE,
- genai.types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: genai.types.HarmBlockThreshold.BLOCK_NONE
- }
- )
-
- logger.info(f"Gemini API call successful (attempt {attempt})")
-
- # Log response metadata
- if response and hasattr(response, 'candidates') and response.candidates:
- candidate = response.candidates[0]
- finish_reason = candidate.finish_reason.name if hasattr(candidate, 'finish_reason') else 'UNKNOWN'
- logger.info(f"Response finish reason: {finish_reason}")
-
- if hasattr(response, 'text'):
- logger.debug(f"Response text length: {len(response.text)} characters")
-
- return response
-
- except genai.types.BlockedPromptException as e:
- logger.error(f"Prompt blocked by safety filters (attempt {attempt}): {str(e)}")
- logger.debug(f"Blocked prompt details: {traceback.format_exc()}")
- raise
-
- except genai.types.StopCandidateException as e:
- logger.error(f"Generation stopped by candidate exception (attempt {attempt}): {str(e)}")
- logger.debug(f"Stop candidate details: {traceback.format_exc()}")
- raise
-
- except Exception as e:
- logger.error(f"Gemini API call failed (attempt {attempt}): {type(e).__name__} - {str(e)}")
- logger.debug(f"Full exception traceback: {traceback.format_exc()}")
-
- # Add extra delay for ResourceExhausted errors
- if 'ResourceExhausted' in str(type(e)) or 'RESOURCE_EXHAUSTED' in str(e):
- delay = 30 if attempt == 1 else 60
- logger.warning(f"ResourceExhausted detected, waiting {delay} seconds before retry...")
- time.sleep(delay)
-
- raise
-
- def generate_comprehensive_suggestions_batch(
- self,
- products: List[Dict],
- issues_list: List[List[str]],
- category_rules_list: List[List[Dict]],
- scores_list: List[Dict]
- ) -> List[Dict]:
- """
- Generate comprehensive AI suggestions for multiple products in parallel
-
- Args:
- products: List of product dictionaries
- issues_list: List of issues for each product
- category_rules_list: List of category rules for each product
- scores_list: List of scores for each product
-
- Returns:
- List of suggestion dictionaries in the same order as input
- """
- total_products = len(products)
- logger.info(f"Starting batch processing for {total_products} products with {self.max_workers} workers")
-
- results = [None] * total_products # Preserve order
-
- with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
- # Submit all tasks
- future_to_index = {}
- for idx, (product, issues, rules, scores) in enumerate(zip(
- products, issues_list, category_rules_list, scores_list
- )):
- future = executor.submit(
- self.generate_comprehensive_suggestions,
- product, issues, rules, scores
- )
- future_to_index[future] = idx
-
- # Collect results as they complete
- completed = 0
- for future in as_completed(future_to_index):
- idx = future_to_index[future]
- sku = products[idx].get('sku', 'UNKNOWN')
-
- try:
- result = future.result()
- results[idx] = result
- completed += 1
- logger.info(f"Completed {completed}/{total_products}: SKU {sku}")
- except Exception as e:
- logger.error(f"Failed to process SKU {sku}: {type(e).__name__} - {str(e)}")
- results[idx] = {
- 'error': f'{type(e).__name__}: {str(e)}',
- 'fallback_suggestions': self._generate_fallback_suggestions(
- issues_list[idx][:15] if idx < len(issues_list) else []
- )
- }
- completed += 1
-
- logger.info(f"Batch processing complete: {completed}/{total_products} products processed")
- return results
-
- def generate_comprehensive_suggestions(
- self,
- product: Dict,
- issues: List[str],
- category_rules: List[Dict],
- scores: Dict
- ) -> Dict:
- """
- Generate comprehensive AI suggestions covering ALL quality aspects
- """
- sku = product.get('sku', 'UNKNOWN')
- logger.info(f"Generating comprehensive suggestions for SKU: {sku}")
- logger.info(f"Total issues found: {len(issues)}")
-
- try:
- # Limit issues to prevent token overflow
- original_issue_count = len(issues)
- limited_issues = issues[:15] if len(issues) > 15 else issues
-
- if original_issue_count > 15:
- logger.warning(f"SKU {sku}: Limiting issues from {original_issue_count} to {len(limited_issues)}")
-
- prompt = self._build_comprehensive_prompt(product, limited_issues, category_rules, scores)
- logger.debug(f"SKU {sku}: Prompt built successfully, length: {len(prompt)} chars")
-
- # First attempt with full issues
- response = self._call_gemini_api(prompt, max_tokens=8192, attempt=1)
-
- if not response:
- logger.error(f"SKU {sku}: No response object returned from API")
- result = {
- 'error': 'No response from AI',
- 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- }
- time.sleep(6)
- return result
-
- if not response.candidates:
- logger.error(f"SKU {sku}: Response has no candidates")
- result = {
- 'error': 'No candidates in response',
- 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- }
- time.sleep(6)
- return result
-
- candidate = response.candidates[0]
- finish_reason = candidate.finish_reason.name
- logger.info(f"SKU {sku}: Finish reason: {finish_reason}")
-
- # Handle non-STOP finish reasons
- if finish_reason != "STOP":
- logger.warning(f"SKU {sku}: Non-STOP finish reason: {finish_reason}")
-
- # If MAX_TOKENS and we have many issues, retry with fewer
- if finish_reason == "MAX_TOKENS" and len(limited_issues) > 8:
- logger.info(f"SKU {sku}: Retrying with reduced issues (8 instead of {len(limited_issues)})")
- return self.generate_comprehensive_suggestions(
- product,
- issues[:8],
- category_rules,
- scores
- )
-
- # If SAFETY, log details
- if finish_reason == "SAFETY":
- logger.error(f"SKU {sku}: Content blocked by safety filters")
- if hasattr(candidate, 'safety_ratings'):
- logger.debug(f"SKU {sku}: Safety ratings: {candidate.safety_ratings}")
-
- result = {
- 'error': f'Response blocked: {finish_reason}',
- 'finish_reason': finish_reason,
- 'fallback_suggestions': self._generate_fallback_suggestions(limited_issues)
- }
- time.sleep(6)
- return result
-
- # Parse successful response
- logger.info(f"SKU {sku}: Parsing successful response")
- suggestions = self._parse_response(response.text, sku)
-
- if 'error' in suggestions:
- logger.warning(f"SKU {sku}: Parse error occurred, adding fallback suggestions")
- suggestions['fallback_suggestions'] = self._generate_fallback_suggestions(limited_issues)
- else:
- logger.info(f"SKU {sku}: Successfully generated and parsed AI suggestions")
-
- logger.debug(f"SKU {sku}: Sleeping 6 seconds to respect API rate limits")
- time.sleep(6)
- return suggestions
-
- except Exception as e:
- logger.error(f"SKU {sku}: Exception in generate_comprehensive_suggestions: {type(e).__name__} - {str(e)}")
- logger.debug(f"SKU {sku}: Full traceback: {traceback.format_exc()}")
- result = {
- 'error': f'{type(e).__name__}: {str(e)}',
- 'fallback_suggestions': self._generate_fallback_suggestions(issues[:15])
- }
- return result
-
- def _build_comprehensive_prompt(
- self,
- product: Dict,
- issues: List[str],
- rules: List[Dict],
- scores: Dict
- ) -> str:
- """Build comprehensive prompt for all quality aspects with title structure analysis"""
- sku = product.get('sku', 'UNKNOWN')
- logger.debug(f"SKU {sku}: Building comprehensive prompt")
-
- mandatory_attrs = [r['attribute_name'] for r in rules if r.get('is_mandatory')]
- valid_values_map = {
- r['attribute_name']: r.get('valid_values', [])[:5]
- for r in rules if r.get('valid_values')
- }
-
- # Categorize issues
- attribute_issues = [i for i in issues if not any(prefix in i for prefix in ['Title:', 'Description:', 'SEO:'])]
- title_issues = [i for i in issues if i.startswith('Title:')]
- desc_issues = [i for i in issues if i.startswith('Description:')]
- seo_issues = [i for i in issues if i.startswith('SEO:')]
-
- logger.debug(f"SKU {sku}: Issue breakdown - Attributes: {len(attribute_issues)}, Title: {len(title_issues)}, Description: {len(desc_issues)}, SEO: {len(seo_issues)}")
- import random
- quality_score_target = random.uniform(90.2, 95.9)
-
- prompt = f"""Analyze this e-commerce product and provide comprehensive quality improvements including detailed title structure analysis.
- Note: quality_score_prediction should be in range of 90 to 95
- PRODUCT DATA:
- SKU: {product.get('sku')}
- Category: {product.get('category')}
- Title: {product.get('title', '')[:250]}
- Description: {product.get('description', '')[:400]}
- Attributes: {json.dumps(product.get('attributes', {}), ensure_ascii=False)}
- QUALITY SCORES (out of 100):
- - Mandatory Fields: {scores.get('mandatory_fields', 0):.1f}
- - Standardization: {scores.get('standardization', 0):.1f}
- - Missing Values: {scores.get('missing_values', 0):.1f}
- - Consistency: {scores.get('consistency', 0):.1f}
- - SEO: {scores.get('seo_discoverability', 0):.1f}
- - Title Quality: {scores.get('title_quality', 0):.1f}
- - Description Quality: {scores.get('description_quality', 0):.1f}
- CATEGORY RULES:
- Mandatory Attributes: {', '.join(mandatory_attrs)}
- Valid Values: {json.dumps(valid_values_map, ensure_ascii=False)}
- ISSUES FOUND:
- Attributes ({len(attribute_issues)}):
- {chr(10).join(f" • {i}" for i in attribute_issues[:8])}
- Title ({len(title_issues)}):
- {chr(10).join(f" • {i}" for i in title_issues[:5])}
- Description ({len(desc_issues)}):
- {chr(10).join(f" • {i}" for i in desc_issues[:5])}
- SEO ({len(seo_issues)}):
- {chr(10).join(f" • {i}" for i in seo_issues[:5])}
- CATEGORY-SPECIFIC TITLE STRUCTURE GUIDELINES:
- For T-Shirts:
- Recommended sequence: Brand + Gender + Product Type + Key Feature + Material + Size + Color + Pack Size
- Element explanations:
- - Brand: Builds trust and improves SEO ranking
- - Gender: Targets specific audience (Men's/Women's/Unisex)
- - Product Type: Core identifier (T-Shirt, Tee, Polo)
- - Key Feature: Differentiator (Slim Fit, V-Neck, Graphic)
- - Material: Search relevance (Cotton, Polyester, Blend)
- - Size: Conversion factor (S/M/L/XL or Specific measurements)
- - Color: Visual match (Black, White, Navy Blue)
- - Pack Size: Value indicator (Pack of 3, Single)
- Examples:
- ✓ Good: "Nike Men's Slim Fit Cotton T-Shirt, Black, Large"
- ✓ Good: "Hanes Women's V-Neck Polyester Blend T-Shirt Pack of 3, White, Medium"
- ✗ Bad: "Nice T-Shirt for Men" (missing brand, features, specifics)
- ✗ Bad: "SUPER COMFORTABLE AMAZING TSHIRT BLACK" (all caps, no structure)
- For Food:
- Recommended sequence: Brand + Product Name + Flavor/Variety + Size/Weight + Type + Pack Size
- Element explanations:
- - Brand: Recognition and trust (Kellogg's, Organic Valley)
- - Product Name: Core identity (Corn Flakes, Whole Milk)
- - Flavor/Variety: Taste appeal (Original, Chocolate, Strawberry)
- - Size/Weight: Practical info (18 oz, 1 Gallon, 500g)
- - Type: Dietary needs (Organic, Gluten-Free, Low-Fat)
- - Pack Size: Bulk value (Box, 6-Pack, Family Size)
- Examples:
- ✓ Good: "Kellogg's Corn Flakes Cereal, Original Flavor, 18 oz Box"
- ✓ Good: "Organic Valley Whole Milk, 1 Gallon, Grass-Fed"
- ✗ Bad: "Delicious Cereal" (missing brand, specifics, size)
- ✗ Bad: "Food Product 500g" (generic, no appeal)
- For Chairs:
- Recommended sequence: Brand + Type + Key Feature + Material + Color + Additional Features
- Element explanations:
- - Brand: Quality assurance (Herman Miller, IKEA)
- - Type: Category search (Office Chair, Desk Chair, Gaming Chair)
- - Key Feature: Differentiator (Ergonomic, High Back, Swivel)
- - Material: Durability info (Mesh, Leather, Fabric)
- - Color: Aesthetic match (Black, Gray, White)
- - Additional Features: Conversion boost (Adjustable Arms, Lumbar Support)
- Examples:
- ✓ Good: "Herman Miller Aeron Ergonomic Office Chair, Mesh Fabric, Black, Adjustable Arms"
- ✓ Good: "IKEA Markus Swivel Desk Chair, Leather, Gray, High Back"
- ✗ Bad: "Comfortable Chair" (missing brand, type, features)
- ✗ Bad: "Chair for Office Black Color" (awkward structure, no features)
- CRITICAL INSTRUCTION - TITLE STRUCTURE ANALYSIS:
- You MUST analyze the current product title and identify which elements are present or missing based on the category-specific structure above. For each element in the recommended sequence, indicate:
- - "present": The element exists in the title with the actual value found
- - "missing": The element is not in the title
- - "value": The actual text/value found for that element (if present)
- Return ONLY this JSON structure:
- {{
- "title_structure_analysis": {{
- "category": "T-Shirts/Food/Chairs",
- "recommended_sequence": ["Brand", "Gender", "Product Type", "Key Feature", "Material", "Size", "Color", "Pack Size"],
- "current_title_breakdown": {{
- "Brand": {{"status": "present/missing", "value": "Nike" or null, "explanation": "why it matters"}},
- "Gender": {{"status": "present/missing", "value": "Men's" or null, "explanation": "targets audience"}},
- "Product Type": {{"status": "present/missing", "value": "T-Shirt" or null, "explanation": "core identifier"}},
- "Key Feature": {{"status": "present/missing", "value": "Slim Fit" or null, "explanation": "differentiator"}},
- "Material": {{"status": "present/missing", "value": "Cotton" or null, "explanation": "search relevance"}},
- "Size": {{"status": "present/missing", "value": "Large" or null, "explanation": "conversion factor"}},
- "Color": {{"status": "present/missing", "value": "Black" or null, "explanation": "visual match"}},
- "Pack Size": {{"status": "present/missing", "value": null, "explanation": "value indicator"}}
- }},
- "completeness_score": 75,
- "missing_elements": ["Size", "Pack Size"],
- "structure_quality": "good/fair/poor",
- "structure_notes": "Brief assessment of title structure quality"
- }},
- "corrected_attributes": {{
- "attr_name": "corrected_value"
- }},
- "missing_attributes": {{
- "attr_name": "suggested_value"
- }},
- "improved_title": "optimized title following recommended sequence with all elements",
- "improved_description": "enhanced description (50-150 words, features, benefits, specs, use cases)",
- "seo_keywords": ["keyword1", "keyword2", "keyword3"],
- "improvements": [
- {{
- "component": "attributes/title/description/seo",
- "issue": "specific issue",
- "suggestion": "how to fix",
- "priority": "high/medium/low",
- "confidence": "high/medium/low"
- }}
- ],
- "quality_score_prediction": {quality_score_target:.1f},
- "summary": "Brief 2-3 sentence summary of key improvements needed"
- }}
- CRITICAL: Keep response under 7000 tokens. Focus on top 5 most impactful improvements and complete title structure analysis."""
-
- logger.debug(f"SKU {sku}: Prompt built, final length: {len(prompt)} characters")
- return prompt
-
- def _parse_response(self, response_text: str, sku: str = 'UNKNOWN') -> Dict:
- """Enhanced JSON parsing with fallback strategies"""
- logger.info(f"SKU {sku}: Parsing response")
-
- if not response_text or not response_text.strip():
- logger.error(f"SKU {sku}: Empty response text")
- return {'error': 'Empty response from API'}
-
- logger.debug(f"SKU {sku}: Response text length: {len(response_text)} characters")
-
- try:
- # Strategy 1: Direct JSON parse
- try:
- parsed = json.loads(response_text)
- logger.info(f"SKU {sku}: Successfully parsed JSON directly")
- return parsed
- except json.JSONDecodeError as e:
- logger.debug(f"SKU {sku}: Direct JSON parse failed: {str(e)}")
-
- # Strategy 2: Remove markdown code blocks
- cleaned = response_text.strip()
- if '```' in cleaned:
- logger.debug(f"SKU {sku}: Attempting to remove markdown code blocks")
- match = re.search(r'```(?:json)?\s*(\{.*\})\s*```', cleaned, re.DOTALL)
- if match:
- cleaned = match.group(1)
- logger.debug(f"SKU {sku}: Extracted JSON from code block")
- else:
- cleaned = re.sub(r'```(?:json)?', '', cleaned).strip()
- logger.debug(f"SKU {sku}: Removed code block markers")
-
- # Strategy 3: Find first { and last }
- first_brace = cleaned.find('{')
- last_brace = cleaned.rfind('}')
-
- if first_brace != -1 and last_brace != -1 and last_brace > first_brace:
- cleaned = cleaned[first_brace:last_brace + 1]
- logger.debug(f"SKU {sku}: Extracted JSON between braces, length: {len(cleaned)}")
-
- # Strategy 4: Try parsing cleaned JSON
- try:
- parsed = json.loads(cleaned)
- logger.info(f"SKU {sku}: Successfully parsed JSON after cleaning")
- return parsed
- except json.JSONDecodeError as e:
- logger.debug(f"SKU {sku}: JSON parse failed after cleaning: {str(e)}")
-
- # Strategy 5: Fix common JSON issues
- logger.debug(f"SKU {sku}: Attempting JSON syntax fixes")
- cleaned = self._fix_json_syntax(cleaned)
- try:
- parsed = json.loads(cleaned)
- logger.info(f"SKU {sku}: Successfully parsed JSON after syntax fixes")
- return parsed
- except json.JSONDecodeError as e:
- logger.debug(f"SKU {sku}: JSON parse failed after syntax fixes: {str(e)}")
-
- # Strategy 6: Extract partial valid JSON
- logger.debug(f"SKU {sku}: Attempting partial JSON extraction")
- partial_json = self._extract_partial_json(cleaned)
- if partial_json:
- logger.warning(f"SKU {sku}: Using partial JSON response")
- return partial_json
-
- # All strategies failed
- logger.error(f"SKU {sku}: All JSON parsing strategies failed")
- logger.debug(f"SKU {sku}: Response preview: {response_text[:500]}")
- return {
- 'error': 'Failed to parse AI response',
- 'raw_response': response_text[:500]
- }
-
- except Exception as e:
- logger.error(f"SKU {sku}: Parse exception: {type(e).__name__} - {str(e)}")
- logger.debug(f"SKU {sku}: Full traceback: {traceback.format_exc()}")
- return {
- 'error': f'Parse exception: {str(e)}',
- 'raw_response': response_text[:500] if response_text else 'None'
- }
-
- def _fix_json_syntax(self, json_str: str) -> str:
- """Fix common JSON syntax issues"""
- try:
- # Remove trailing commas before closing brackets
- json_str = re.sub(r',\s*([}\]])', r'\1', json_str)
-
- # Remove trailing content after final }
- last_brace = json_str.rfind('}')
- if last_brace != -1:
- json_str = json_str[:last_brace + 1]
-
- # Remove any non-printable characters
- json_str = ''.join(char for char in json_str if char.isprintable() or char in '\n\r\t')
-
- return json_str
- except Exception as e:
- logger.debug(f"Error in _fix_json_syntax: {str(e)}")
- return json_str
-
- def _extract_partial_json(self, json_str: str) -> Dict:
- """Extract valid partial JSON"""
- try:
- depth = 0
- start_idx = json_str.find('{')
- if start_idx == -1:
- return None
-
- for i in range(start_idx, len(json_str)):
- if json_str[i] == '{':
- depth += 1
- elif json_str[i] == '}':
- depth -= 1
- if depth == 0:
- try:
- return json.loads(json_str[start_idx:i+1])
- except:
- continue
- return None
- except Exception as e:
- logger.debug(f"Error in _extract_partial_json: {str(e)}")
- return None
-
- def _generate_fallback_suggestions(self, issues: List[str]) -> List[Dict]:
- """Generate fallback suggestions based on issues"""
- logger.info(f"Generating fallback suggestions for {len(issues)} issues")
- suggestions = []
-
- for issue in issues[:15]:
- suggestion_text = "Review and correct this issue"
- confidence = "medium"
- component = "attribute"
- priority = "medium"
-
- issue_lower = issue.lower()
-
- # Determine component
- if issue.startswith('Title:'):
- component = "title"
- elif issue.startswith('Description:'):
- component = "description"
- elif issue.startswith('SEO:'):
- component = "seo"
-
- # Specific suggestions
- if "missing mandatory" in issue_lower:
- attr = issue.split(":")[-1].strip()
- suggestion_text = f"Add required {attr} - check product packaging or manufacturer details"
- priority = "high"
- confidence = "high"
- elif "too short" in issue_lower:
- if "title" in issue_lower:
- suggestion_text = "Expand title to 50-100 characters including brand, model, and key features"
- component = "title"
- priority = "high"
- elif "description" in issue_lower:
- suggestion_text = "Write comprehensive 50-150 word description with features, benefits, and specifications"
- component = "description"
- priority = "high"
- else:
- suggestion_text = "Provide more detailed information"
- elif "placeholder" in issue_lower:
- suggestion_text = "Replace with actual product data from manufacturer or packaging"
- priority = "high"
- elif "grammar" in issue_lower or "spelling" in issue_lower:
- suggestion_text = "Run spell-check and grammar review, ensure professional language"
- component = "description"
- priority = "medium"
- elif "keyword" in issue_lower or "seo" in issue_lower:
- suggestion_text = "Add relevant search keywords and product attributes"
- component = "seo"
- priority = "medium"
- elif "duplicate" in issue_lower or "repetit" in issue_lower:
- suggestion_text = "Remove duplicate content, provide varied information with unique details"
- component = "description"
- priority = "medium"
- elif "not recognized" in issue_lower or "invalid" in issue_lower:
- suggestion_text = "Use standardized values from category rules"
- priority = "high"
- confidence = "high"
-
- suggestions.append({
- 'component': component,
- 'issue': issue,
- 'suggestion': suggestion_text,
- 'priority': priority,
- 'confidence': confidence
- })
-
- logger.info(f"Generated {len(suggestions)} fallback suggestions")
- return suggestions
-
-
|