Ticio commited on
Commit
fb073ad
verified
1 Parent(s): 3d89543

Upload 2 files

Browse files
Files changed (2) hide show
  1. gradio_test.py +21 -0
  2. inference.py +171 -0
gradio_test.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from inference import inference
3
+
4
+ theme = theme = gr.themes.Soft(
5
+ primary_hue="red",
6
+ secondary_hue="red",
7
+ )
8
+
9
+ with gr.Blocks(theme = theme) as demo:
10
+ with gr.Row():
11
+ gr.HTML("""
12
+ <div style="display: flex; align-items: center;">
13
+ <img src="https://ticio.co/public/Branding/ticio.svg" alt="Logo" style="width: 50px; height: auto; margin-right: 10px;">
14
+ <h1 style="color: #910A0A; margin: 0;">Ticio</h1>
15
+ </div>
16
+ """) # Replace with your logo image URL
17
+ chatbot = gr.Chatbot(type="messages")
18
+ hidden_text = gr.Textbox(visible = False) # Added hidden text component
19
+ gr.ChatInterface(fn=inference, chatbot=chatbot, show_api=False, type="messages", additional_inputs= hidden_text, additional_outputs= hidden_text, save_history= True).render()
20
+
21
+ demo.launch(show_api=False, debug= True, share=True)
inference.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import vecs
2
+ from dotenv import load_dotenv
3
+ import os
4
+ import threading
5
+ import base64
6
+ import os
7
+ from google import genai
8
+ from google.genai import types
9
+ from sentence_transformers.SentenceTransformer import SentenceTransformer
10
+
11
+ load_dotenv()
12
+
13
+ user = os.getenv("user")
14
+ password = os.getenv("password")
15
+ host = os.getenv("host")
16
+ port = os.getenv("port")
17
+ db_name = "postgres"
18
+ DB_CONNECTION = f"postgresql://{user}:{password}@{host}:{port}/{db_name}"
19
+ vx = vecs.create_client(DB_CONNECTION)
20
+ model = SentenceTransformer('Snowflake/snowflake-arctic-embed-xs', device="cpu")
21
+ client = genai.Client(api_key=os.getenv('GEMINI_API_KEY'))
22
+
23
+ def query_db(query, limit = 5, filters = {}, measure = "cosine_distance", include_value = True, include_metadata=True, table = "2023"):
24
+ query_embeds = vx.get_or_create_collection(name= table, dimension=384)
25
+ ans = query_embeds.query(
26
+ data=query,
27
+ limit=limit,
28
+ filters=filters,
29
+ measure=measure,
30
+ include_value=include_value,
31
+ include_metadata=include_metadata,
32
+ )
33
+ return ans
34
+
35
+ def sort_by_score(item):
36
+ return item[1]
37
+
38
+ def infa帽o(rad):
39
+ a = int(rad[len(rad)-2::])
40
+ if a > 89:
41
+ return a + 1900
42
+ else:
43
+ return a + 2000
44
+
45
+ def thread_query(query, target, year):
46
+ return target.extend(query_db(query, table=str(year)))
47
+
48
+
49
+ def vector_query(query, start = 1992, end = 2024):
50
+ results = []
51
+ vector_query = model.encode(query)
52
+ threads = []
53
+ for i in range(start, end + 1):
54
+ t = threading.Thread(target=thread_query, args=(vector_query, results, i))
55
+ threads.append(t)
56
+ t.start()
57
+ threads[-1].join()
58
+ results.sort(key=sort_by_score)
59
+ q = {}
60
+ for i in results:
61
+ if i[2]['sentencia'] not in q.keys():
62
+ q[i[2]['sentencia']] = 1
63
+ else:
64
+ q[i[2]['sentencia']] += 1
65
+ judgements = []
66
+
67
+ for i in q.keys():
68
+ if q[i] > 1:
69
+ judgements.append(i)
70
+ print(query, judgements)
71
+ return judgements
72
+
73
+ def context_builder_prompt_constructor(judgement):
74
+ return judgement
75
+
76
+ def context_builder(context_prompt, target):
77
+ model = "gemini-2.5-flash-lite"
78
+ contents = [
79
+ types.Content(
80
+ role="user",
81
+ parts=[
82
+ types.Part.from_text(text=context_prompt),
83
+ ],
84
+ ),
85
+ ]
86
+ tools = [
87
+ types.Tool(googleSearch=types.GoogleSearch(
88
+ )),]
89
+ generate_content_config = types.GenerateContentConfig(
90
+ thinking_config = types.ThinkingConfig(
91
+ thinking_budget=0,
92
+ ),
93
+ tools=tools,
94
+ system_instruction=[
95
+ types.Part.from_text(text=f"""resume el contenido de la sentencia de forma detallada, mencionando todos los puntos considerados en la sentencia"""),
96
+ ],
97
+ )
98
+
99
+ response = client.models.generate_content(
100
+ model=model,
101
+ contents=contents,
102
+ config=generate_content_config,
103
+ )
104
+ return target.append(response.text)
105
+
106
+ def context_draft(judgements, query):
107
+ context = []
108
+ threads = []
109
+ for i in judgements:
110
+ t = threading.Thread(target=context_builder, args=(context_builder_prompt_constructor(i), context))
111
+ threads.append(t)
112
+ t.start()
113
+
114
+ while len(context) < len(threads):
115
+ pass
116
+
117
+ draft = ''
118
+ for i in context:
119
+ draft += i + '\n'
120
+ return draft
121
+
122
+ def generate(query, context, message_history):
123
+ model = "gemini-2.5-flash-lite"
124
+
125
+ # Convert Hugging Face style message history to Gemini API format
126
+ gemini_contents = []
127
+ for message in message_history:
128
+ role = "user" if message["role"] == "user" else "model"
129
+ gemini_contents.append(
130
+ types.Content(
131
+ role=role,
132
+ parts=[types.Part.from_text(text=message["content"])],
133
+ )
134
+ )
135
+
136
+ # Add the current user query to the contents
137
+ gemini_contents.append(
138
+ types.Content(
139
+ role="user",
140
+ parts=[
141
+ types.Part.from_text(text=query),
142
+ ],
143
+ )
144
+ )
145
+
146
+
147
+ generate_content_config = types.GenerateContentConfig(
148
+ thinking_config = types.ThinkingConfig(
149
+ thinking_budget=0,
150
+ ),
151
+ system_instruction=[
152
+ types.Part.from_text(text=f"""Eres Ticio un asistente de investigaci贸n de jurisprudencia colombiana. Tienes acceso a un contexto especialmente dise帽ado para esta conversaci贸n. Tu tarea es contestar a las preguntas del usuario referenciando siempre las sentencias de donde viene la informaci贸n como si fueras un investigador experto.
153
+ {context}
154
+
155
+ """)]
156
+ )
157
+
158
+ response = client.models.generate_content(
159
+ model=model,
160
+ contents=gemini_contents,
161
+ config=generate_content_config,
162
+ )
163
+ return response.text
164
+
165
+ def inference(query, history, context):
166
+ if context == None or len(context) <= 0 or len(history) <= 0:
167
+ vector_query_results = vector_query(query)
168
+ context = context_draft(vector_query_results, query)
169
+ return generate(query, context, history), context
170
+ else:
171
+ return generate(query, context, history), context