Spaces:
Paused
Paused
Add JSON extraction utility and enhance analysis methods for better error handling
Browse files43 unmodified lines44454647484950249 unmodified lines30030130230330430530630730830931031110 unmodified lines3223233243253263273286 unmodified lines33533633733833934034144 unmodified lines386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433180 unmodified lines61461561661761861962062162262362462562662762862963063163263363463563663763863964064165 unmodified lines70770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776850 unmodified lines81982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886943 unmodified linesclean_text,extract_court_decision_text,get_links_html,get_links_html_lp)from embeddings import GeminiEmbedding249 unmodified linesresponse_format=response_format,temperature=0)return response.choices[0].message.contentexcept Exception as e:raise RuntimeError(f"Error in OpenAI analysis: {str(e)}")async def _analyze_with_deepseek(self, prompt: str) -> str:"""Analyze text using OpenAI."""messages = [ChatMessage(role="system", content=SYSTEM_PROMPT),ChatMessage(role="user", content=prompt)10 unmodified linesresponse_format=response_format,temperature=0)return response.choices[0].message.contentexcept Exception as e:raise RuntimeError(f"Error in DeepSeek analysis: {str(e)}")6 unmodified linessystem=SYSTEM_PROMPT,messages=[{"role": "user", "content": prompt}])return response.content[0].textexcept Exception as e:raise RuntimeError(f"Error in Anthropic analysis: {str(e)}")44 unmodified linesif not response_text:raise RuntimeError("Empty response from Gemini")# Витягуємо JSON з відповідіtext = response_text.strip()# Знаходимо перший { і останній }start = text.find('{')end = text.rfind('}') + 1if start == -1 or end == 0:# Якщо JSON не знайдено, створюємо структурований JSON з текстуreturn json.dumps({"relevant_positions": [{"lp_id": "unknown","source_index": "1","description": text}]}, ensure_ascii=False)json_str = text[start:end]# Перевіряємо, чи є це валідним JSONtry:parsed_json = json.loads(json_str)if "relevant_positions" not in parsed_json:parsed_json = {"relevant_positions": [{"lp_id": "unknown","source_index": "1","description": json.dumps(parsed_json)}]}return json.dumps(parsed_json, ensure_ascii=False)except json.JSONDecodeError:# Якщо не вдалося розпарсити JSON, повертаємо весь текст як описreturn json.dumps({"relevant_positions": [{"lp_id": "unknown","source_index": "1","description": text}]}, ensure_ascii=False)180 unmodified linesChatMessage(role="system", content=system_prompt),ChatMessage(role="user", content=content),]response = llm.chat(messages, response_format=LEGAL_POSITION_SCHEMA)return json.loads(response.message.content)if provider == ModelProvider.DEEPSEEK.value:client = OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com")response = client.chat.completions.create(model=model_name,messages=[{"role": "system", "content": system_prompt},{"role": "user", "content": content},],temperature=GENERATION_TEMPERATURE,max_tokens=MAX_TOKENS_CONFIG["deepseek"],response_format={'type': 'json_object'},stream=False)try:return json.loads(response.choices[0].message.content)except json.JSONDecodeError:raise Exception("Помилка при парсингу відповіді від моделі Deepseek")elif provider == ModelProvider.ANTHROPIC.value:client = Anthropic(api_key=ANTHROPIC_API_KEY)65 unmodified linesprint(f"[DEBUG] Anthropic response text length: {len(response_text)}")print(f"[DEBUG] Response preview (first 500 chars): {response_text[:500]}")# Try to extract JSON from markdown code blocks if presenttext_to_parse = response_text.strip()# Remove markdown code blocks if presentif text_to_parse.startswith("```json"):text_to_parse = text_to_parse[7:]elif text_to_parse.startswith("```"):text_to_parse = text_to_parse[3:]if text_to_parse.endswith("```"):text_to_parse = text_to_parse[:-3]text_to_parse = text_to_parse.strip()# Try to find JSON object in the textstart_idx = text_to_parse.find('{')end_idx = text_to_parse.rfind('}')if start_idx != -1 and end_idx != -1:text_to_parse = text_to_parse[start_idx:end_idx + 1]else:print(f"[WARNING] No JSON object delimiters found in response")# Try to parse JSONtry:parsed_json = json.loads(text_to_parse)# Validate required fieldsrequired = ["title", "text", "proceeding", "category"]missing = [f for f in required if f not in parsed_json]if missing:print(f"[WARNING] Missing fields in JSON: {missing}")# Try to fill missing fieldsfor field in missing:if field not in parsed_json:parsed_json[field] = "Не вказано"return parsed_jsonexcept json.JSONDecodeError as je:print(f"[ERROR] JSON parsing failed: {je}")print(f"[ERROR] Attempted to parse: {text_to_parse[:1000]}")# Fallback: create structured response from raw textfallback = {"title": "Автоматично згенерований заголовок","text": response_text.strip(),"proceeding": "Не визначено","category": "Помилка парсингу JSON"}print(f"[WARNING] Using fallback response structure")return fallbackexcept Exception as e:print(f"[ERROR] Exception during response processing: {type(e).__name__}: {e}")raise Exception(f"Помилка при обробці відповіді від моделі Anthropic: {str(e)}")elif provider == ModelProvider.GEMINI.value:if not os.environ.get("GEMINI_API_KEY"):50 unmodified linesif not response_text:raise Exception("Пуста відповідь від моделі Gemini")# Спробуємо розпарсити JSONtry:# Try to extract JSON from markdown code blocks if presenttext_to_parse = response_text.strip()# Remove markdown code blocks if presentif text_to_parse.startswith("```json"):text_to_parse = text_to_parse[7:] # Remove ```jsonelif text_to_parse.startswith("```"):text_to_parse = text_to_parse[3:] # Remove ```if text_to_parse.endswith("```"):text_to_parse = text_to_parse[:-3] # Remove trailing ```text_to_parse = text_to_parse.strip()# Try to find JSON object in the textstart_idx = text_to_parse.find('{')end_idx = text_to_parse.rfind('}')if start_idx != -1 and end_idx != -1:text_to_parse = text_to_parse[start_idx:end_idx + 1]json_response = json.loads(text_to_parse)# Перевіряємо наявність всіх необхідних полівrequired_fields = ["title", "text", "proceeding", "category"]if all(field in json_response for field in required_fields):return json_responseelse:missing_fields = [field for field in required_fields if field not in json_response]raise Exception(f"Відсутні обов'язкові поля у відповіді: {', '.join(missing_fields)}")except json.JSONDecodeError as je:print(f"JSON parsing error: {str(je)}")print(f"Response text: {response_text[:500]}") # Log first 500 chars# Якщо відповідь не в форматі JSON, спробуємо створити структурований об'єкт# з текстової відповіді (fallback mechanism)fallback_response = {"title": "Автоматично сформований заголовок","text": response_text.strip(),"proceeding": "Не визначено","category": "Автоматично визначена категорія"}return fallback_responseexcept Exception as e:print(f"Error in Gemini generation: {str(e)}")43 unmodified lines4445464748495051249 unmodified lines30130230330430530630730830931031131231331431531610 unmodified lines3273283293303313323333343353363376 unmodified lines34434534634734834935035135235335435535644 unmodified lines401402403404405406407408409410411412413414415416417418419420421422423424425426427428429180 unmodified lines61061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466565 unmodified lines73173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376450 unmodified lines81581681781881982082182282382482582682782882983083183283383483583683783883984084184284384443 unmodified linesclean_text,extract_court_decision_text,get_links_html,get_links_html_lp,extract_json_from_text)from embeddings import GeminiEmbedding249 unmodified linesresponse_format=response_format,temperature=0)response_text = response.choices[0].message.content# Verify it's valid JSONjson_data = extract_json_from_text(response_text)return json.dumps(json_data, ensure_ascii=False) if json_data else response_textexcept Exception as e:raise RuntimeError(f"Error in OpenAI analysis: {str(e)}")async def _analyze_with_deepseek(self, prompt: str) -> str:"""Analyze text using DeepSeek."""messages = [ChatMessage(role="system", content=SYSTEM_PROMPT),ChatMessage(role="user", content=prompt)10 unmodified linesresponse_format=response_format,temperature=0)response_text = response.choices[0].message.content# Verify and clean JSONjson_data = extract_json_from_text(response_text)return json.dumps(json_data, ensure_ascii=False) if json_data else response_textexcept Exception as e:raise RuntimeError(f"Error in DeepSeek analysis: {str(e)}")6 unmodified linessystem=SYSTEM_PROMPT,messages=[{"role": "user", "content": prompt}])response_text = response.content[0].text# Extract JSON from potential markdown blocksjson_data = extract_json_from_text(response_text)if json_data:return json.dumps(json_data, ensure_ascii=False)return response_textexcept Exception as e:raise RuntimeError(f"Error in Anthropic analysis: {str(e)}")44 unmodified linesif not response_text:raise RuntimeError("Empty response from Gemini")# Витягуємо JSON з відповіді за допомогою універсальної функціїjson_data = extract_json_from_text(response_text)if json_data:if "relevant_positions" not in json_data:json_data = {"relevant_positions": [{"lp_id": "unknown","source_index": "1","description": json.dumps(json_data, ensure_ascii=False)}]}return json.dumps(json_data, ensure_ascii=False)else:# Якщо JSON не знайдено, створюємо структурований JSON з текстуreturn json.dumps({"relevant_positions": [{"lp_id": "unknown","source_index": "1","description": response_text}]}, ensure_ascii=False)180 unmodified linesChatMessage(role="system", content=system_prompt),ChatMessage(role="user", content=content),]try:response = llm.chat(messages, response_format=LEGAL_POSITION_SCHEMA)response_text = response.message.contentjson_response = extract_json_from_text(response_text)if json_response and all(key in json_response for key in ["title", "text", "proceeding", "category"]):return json_responseelse:raise ValueError(f"Invalid JSON structure from OpenAI: {response_text[:200]}...")except Exception as e:print(f"[ERROR] OpenAI generation/parsing failed: {e}")return {"title": "Автоматично сформований заголовок (OpenAI)","text": content[:500] if not 'response_text' in locals() else response_text,"proceeding": "Не визначено","category": "Помилка парсингу"}if provider == ModelProvider.DEEPSEEK.value:client = OpenAI(api_key=DEEPSEEK_API_KEY, base_url="https://api.deepseek.com")try:response = client.chat.completions.create(model=model_name,messages=[{"role": "system", "content": system_prompt},{"role": "user", "content": content},],temperature=GENERATION_TEMPERATURE,max_tokens=MAX_TOKENS_CONFIG["deepseek"],response_format={'type': 'json_object'},stream=False)response_text = response.choices[0].message.contentjson_response = extract_json_from_text(response_text)if json_response and all(key in json_response for key in ["title", "text", "proceeding", "category"]):return json_responseelse:raise ValueError(f"Invalid JSON structure from DeepSeek: {response_text[:200]}...")except Exception as e:print(f"[ERROR] DeepSeek generation/parsing failed: {e}")return {"title": "Автоматично сформований заголовок (DeepSeek)","text": "Помилка при отриманні відповіді від DeepSeek","proceeding": "Не визначено","category": "Помилка API/Парсингу"}elif provider == ModelProvider.ANTHROPIC.value:client = Anthropic(api_key=ANTHROPIC_API_KEY)65 unmodified linesprint(f"[DEBUG] Anthropic response text length: {len(response_text)}")print(f"[DEBUG] Response preview (first 500 chars): {response_text[:500]}")# Спробуємо розпарсити JSON за допомогою універсальної функціїjson_response = extract_json_from_text(response_text)if json_response:# Validate required fieldsrequired = ["title", "text", "proceeding", "category"]missing = [f for f in required if f not in json_response]if missing:print(f"[WARNING] Missing fields in Anthropic JSON: {missing}")for field in missing:if field not in json_response:json_response[field] = "Не вказано"return json_responseelse:print(f"[ERROR] Could not extract JSON from Anthropic response")# Fallback: create structured response from raw textreturn {"title": "Автоматично згенерований заголовок","text": response_text.strip(),"proceeding": "Не визначено","category": "Помилка парсингу JSON"}except Exception as e:# Скидання помилки для подальшого аналізуerror_details = str(e)if hasattr(e, 'response'):error_details += f"\nResponse: {e.response}"raise RuntimeError(f"Error in Anthropic analysis: {error_details}")elif provider == ModelProvider.GEMINI.value:if not os.environ.get("GEMINI_API_KEY"):50 unmodified linesif not response_text:raise Exception("Пуста відповідь від моделі Gemini")# Спробуємо розпарсити JSON за допомогою універсальної функціїjson_response = extract_json_from_text(response_text)if json_response:# Перевіряємо наявність всіх необхідних полівrequired_fields = ["title", "text", "proceeding", "category"]if all(field in json_response for field in required_fields):return json_responseelse:missing_fields = [field for field in required_fields if field not in json_response]print(f"[WARNING] Gemini response missing fields: {missing_fields}")# Fallback for missing fieldsfor field in required_fields:if field not in json_response:json_response[field] = "Не визначено"return json_responseelse:print(f"[ERROR] Could not extract JSON from Gemini response: {response_text[:300]}...")return {"title": "Автоматично сформований заголовок","text": response_text.strip(),"proceeding": "Не визначено","category": "Помилка парсингу"}except Exception as e:print(f"Error in Gemini generation: {str(e)}")
139 unmodified lines140141142143139 unmodified linesreturn ""links = [f"[ПП ВС: {lp_id}](https://lpd.court.gov.ua/home/search/{lp_id})"for lp_id in parsed_ids]return ", ".join(links)139 unmodified lines140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185139 unmodified linesreturn ""links = [f"[ПП ВС: {lp_id}](https://lpd.court.gov.ua/home/search/{lp_id})"for lp_id in parsed_ids]return ", ".join(links)def extract_json_from_text(text: str) -> Optional[Dict]:"""Extract and parse JSON from text, handling markdown blocks and other noise."""if not text:return Nonetry:# 1. Try direct parsingreturn json.loads(text.strip())except json.JSONDecodeError:pass# 2. Try to find JSON within markdown or other texttext_to_parse = text.strip()# Remove markdown code blocksif "```json" in text_to_parse:parts = text_to_parse.split("```json")if len(parts) > 1:text_to_parse = parts[1].split("```")[0].strip()elif "```" in text_to_parse:parts = text_to_parse.split("```")if len(parts) > 1:text_to_parse = parts[1].strip()try:return json.loads(text_to_parse)except json.JSONDecodeError:pass# 3. Last resort: find the first { and last }start_idx = text_to_parse.find('{')end_idx = text_to_parse.rfind('}')if start_idx != -1 and end_idx != -1:text_to_parse = text_to_parse[start_idx:end_idx + 1]try:return json.loads(text_to_parse)except json.JSONDecodeError:passreturn None
main.py+107-132
utils.py+43-1