Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -3,7 +3,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
from langchain_groq import ChatGroq
|
| 6 |
-
from langchain_core.
|
| 7 |
from groq import Groq
|
| 8 |
import os
|
| 9 |
import re
|
|
@@ -61,6 +61,7 @@ if GROQ_API_KEY:
|
|
| 61 |
available_ids = [m.id for m in models_data]
|
| 62 |
print(f"📋 Groq models accessible with this key: {available_ids}")
|
| 63 |
|
|
|
|
| 64 |
chat_models = [
|
| 65 |
m for m in available_ids
|
| 66 |
if not any(k in m.lower() for k in ["whisper", "guard", "safeguard", "canopylabs", "orpheus"])
|
|
@@ -198,13 +199,11 @@ Task:
|
|
| 198 |
2. If a User Rule exists, explicitly state if this clause violates it.
|
| 199 |
3. If it is risky, suggest a 1-sentence edit to make it safer."""
|
| 200 |
|
| 201 |
-
prompt = ChatPromptTemplate.from_messages([
|
| 202 |
-
("system", system_msg),
|
| 203 |
-
("human", "{clause_text}")
|
| 204 |
-
])
|
| 205 |
-
|
| 206 |
try:
|
| 207 |
-
ai_response =
|
|
|
|
|
|
|
|
|
|
| 208 |
explanation = ai_response.content
|
| 209 |
except Exception as e:
|
| 210 |
cause = getattr(e, '__cause__', None)
|
|
@@ -311,16 +310,16 @@ Only return valid, parseable JSON. Do not include markdown code block backticks.
|
|
| 311 |
--- ACCUSED TESTIMONY ---
|
| 312 |
{final_accused_text[:6000]}"""
|
| 313 |
|
| 314 |
-
prompt = ChatPromptTemplate.from_messages([
|
| 315 |
-
("system", system_prompt),
|
| 316 |
-
("human", "{comparison_input}")
|
| 317 |
-
])
|
| 318 |
-
|
| 319 |
try:
|
| 320 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
raw_content = ai_response.content.strip()
|
| 322 |
|
| 323 |
-
#
|
| 324 |
if "```" in raw_content:
|
| 325 |
raw_content = re.sub(r"^```(?:json)?\s*", "", raw_content, flags=re.MULTILINE)
|
| 326 |
raw_content = re.sub(r"\s*```$", "", raw_content, flags=re.MULTILINE)
|
|
@@ -331,7 +330,6 @@ Only return valid, parseable JSON. Do not include markdown code block backticks.
|
|
| 331 |
|
| 332 |
except json.JSONDecodeError as jde:
|
| 333 |
print(f"❌ Failed to decode JSON from Groq: {jde} | Raw content: {raw_content[:200]}")
|
| 334 |
-
# Safe fallback matching the interface
|
| 335 |
return {
|
| 336 |
"client_timeline": {"party": "Client", "events": []},
|
| 337 |
"accused_timeline": {"party": "Accused", "events": []},
|
|
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
| 5 |
from langchain_groq import ChatGroq
|
| 6 |
+
from langchain_core.messages import SystemMessage, HumanMessage
|
| 7 |
from groq import Groq
|
| 8 |
import os
|
| 9 |
import re
|
|
|
|
| 61 |
available_ids = [m.id for m in models_data]
|
| 62 |
print(f"📋 Groq models accessible with this key: {available_ids}")
|
| 63 |
|
| 64 |
+
# Filter out audio, guards/safeguards, and third-party models requiring terms
|
| 65 |
chat_models = [
|
| 66 |
m for m in available_ids
|
| 67 |
if not any(k in m.lower() for k in ["whisper", "guard", "safeguard", "canopylabs", "orpheus"])
|
|
|
|
| 199 |
2. If a User Rule exists, explicitly state if this clause violates it.
|
| 200 |
3. If it is risky, suggest a 1-sentence edit to make it safer."""
|
| 201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
try:
|
| 203 |
+
ai_response = llm.invoke([
|
| 204 |
+
SystemMessage(content=system_msg),
|
| 205 |
+
HumanMessage(content=clause)
|
| 206 |
+
])
|
| 207 |
explanation = ai_response.content
|
| 208 |
except Exception as e:
|
| 209 |
cause = getattr(e, '__cause__', None)
|
|
|
|
| 310 |
--- ACCUSED TESTIMONY ---
|
| 311 |
{final_accused_text[:6000]}"""
|
| 312 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
try:
|
| 314 |
+
# Direct message passing avoids template string bracket errors
|
| 315 |
+
messages = [
|
| 316 |
+
SystemMessage(content=system_prompt),
|
| 317 |
+
HumanMessage(content=user_prompt)
|
| 318 |
+
]
|
| 319 |
+
ai_response = llm.invoke(messages)
|
| 320 |
raw_content = ai_response.content.strip()
|
| 321 |
|
| 322 |
+
# Remove markdown code fences if returned by the LLM
|
| 323 |
if "```" in raw_content:
|
| 324 |
raw_content = re.sub(r"^```(?:json)?\s*", "", raw_content, flags=re.MULTILINE)
|
| 325 |
raw_content = re.sub(r"\s*```$", "", raw_content, flags=re.MULTILINE)
|
|
|
|
| 330 |
|
| 331 |
except json.JSONDecodeError as jde:
|
| 332 |
print(f"❌ Failed to decode JSON from Groq: {jde} | Raw content: {raw_content[:200]}")
|
|
|
|
| 333 |
return {
|
| 334 |
"client_timeline": {"party": "Client", "events": []},
|
| 335 |
"accused_timeline": {"party": "Accused", "events": []},
|