Update inference.py
Browse files- inference.py +40 -7
inference.py
CHANGED
|
@@ -4,6 +4,7 @@ import os
|
|
| 4 |
import threading
|
| 5 |
import base64
|
| 6 |
import os
|
|
|
|
| 7 |
import threading
|
| 8 |
from google import genai
|
| 9 |
from google.genai import types
|
|
@@ -39,11 +40,11 @@ def feedback(query, context, history):
|
|
| 39 |
msg["From"] = mail
|
| 40 |
msg["To"] = mail
|
| 41 |
try:
|
| 42 |
-
with smtplib.SMTP_SSL("smtp.gmail.com",
|
| 43 |
server.login(mail, APP_PASSWORD)
|
| 44 |
server.send_message(msg)
|
| 45 |
-
except
|
| 46 |
-
print(f'envio fallido {query}
|
| 47 |
|
| 48 |
def query_db(query, limit = 5, filters = {}, measure = "cosine_distance", include_value = True, include_metadata=True, table = "2023"):
|
| 49 |
query_embeds = vx.get_or_create_collection(name= table, dimension=384)
|
|
@@ -187,12 +188,44 @@ def generate(query, context, message_history):
|
|
| 187 |
)
|
| 188 |
return response.text
|
| 189 |
|
| 190 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
if context == None or len(context) <= 0 or len(history) <= 0:
|
| 192 |
vector_query_results = vector_query(query)
|
| 193 |
context = context_draft(vector_query_results, query)
|
| 194 |
t = threading.Thread(target=feedback, args=(query, context, history))
|
| 195 |
-
|
| 196 |
-
return generate(query, context, history), context
|
| 197 |
else:
|
| 198 |
-
return generate(query, context, history), context
|
|
|
|
| 4 |
import threading
|
| 5 |
import base64
|
| 6 |
import os
|
| 7 |
+
import re
|
| 8 |
import threading
|
| 9 |
from google import genai
|
| 10 |
from google.genai import types
|
|
|
|
| 40 |
msg["From"] = mail
|
| 41 |
msg["To"] = mail
|
| 42 |
try:
|
| 43 |
+
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=ssl.create_default_context()) as server:
|
| 44 |
server.login(mail, APP_PASSWORD)
|
| 45 |
server.send_message(msg)
|
| 46 |
+
except:
|
| 47 |
+
print(f'envio fallido {query}')
|
| 48 |
|
| 49 |
def query_db(query, limit = 5, filters = {}, measure = "cosine_distance", include_value = True, include_metadata=True, table = "2023"):
|
| 50 |
query_embeds = vx.get_or_create_collection(name= table, dimension=384)
|
|
|
|
| 188 |
)
|
| 189 |
return response.text
|
| 190 |
|
| 191 |
+
def generate_links(judgments):
|
| 192 |
+
output = []
|
| 193 |
+
|
| 194 |
+
for judgment in judgments:
|
| 195 |
+
# Match patterns like 'C-186-19', 'T-092-93', 'SU-000-20'
|
| 196 |
+
match = re.match(r'([A-Za-z]+)-(\d{3})-(\d{2})', judgment)
|
| 197 |
+
if match:
|
| 198 |
+
type_code = match.group(1).lower() # 'c', 't', 'su'
|
| 199 |
+
number = match.group(2)
|
| 200 |
+
year_suffix = match.group(3)
|
| 201 |
+
|
| 202 |
+
# Determine full 4-digit year
|
| 203 |
+
year = '20' + year_suffix if int(year_suffix) <= 68 else '19' + year_suffix
|
| 204 |
+
|
| 205 |
+
# Format radicado for URL
|
| 206 |
+
if type_code == 'su':
|
| 207 |
+
radicado_url = f"su{number.zfill(3)}-{year_suffix}"
|
| 208 |
+
else:
|
| 209 |
+
radicado_url = f"{type_code}-{number}-{year_suffix}" # note the dash after type
|
| 210 |
+
|
| 211 |
+
# Build URL
|
| 212 |
+
url = f"https://www.corteconstitucional.gov.co/relatoria/{year}/{radicado_url}.htm"
|
| 213 |
+
|
| 214 |
+
# Append as a single sublist with judgment and url
|
| 215 |
+
output.append([[judgment], [url]])
|
| 216 |
+
else:
|
| 217 |
+
print(f"Invalid format: {judgment}")
|
| 218 |
+
|
| 219 |
+
return output
|
| 220 |
+
|
| 221 |
+
def link_constructor(judgements):
|
| 222 |
+
return generate_links([x.rsplit('.rtf')[0] for x in judgements])
|
| 223 |
+
|
| 224 |
+
def inference(query, history, context, links):
|
| 225 |
if context == None or len(context) <= 0 or len(history) <= 0:
|
| 226 |
vector_query_results = vector_query(query)
|
| 227 |
context = context_draft(vector_query_results, query)
|
| 228 |
t = threading.Thread(target=feedback, args=(query, context, history))
|
| 229 |
+
return generate(query, context, history), context, link_constructor(vector_query_results)
|
|
|
|
| 230 |
else:
|
| 231 |
+
return generate(query, context, history), context, links
|