Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import os
|
|
| 6 |
from typing import List, Dict
|
| 7 |
from pgvector.psycopg2 import register_vector
|
| 8 |
import numpy as np
|
|
|
|
| 9 |
|
| 10 |
# DB ์ฐ๊ฒฐ ์ค์
|
| 11 |
def get_db_conn():
|
|
@@ -29,7 +30,7 @@ def get_embedding(text: str) -> List[float]:
|
|
| 29 |
)
|
| 30 |
return response.data[0].embedding
|
| 31 |
|
| 32 |
-
def search_similar_chats(query: str, maxResults: int =
|
| 33 |
"""
|
| 34 |
์ ์ฌํ ์ฑํ
๋ฌธ์๋ฅผ ๊ฒ์ํฉ๋๋ค.
|
| 35 |
|
|
@@ -63,26 +64,53 @@ def search_similar_chats(query: str, maxResults: int = 10) -> List[Dict]:
|
|
| 63 |
for row in rows
|
| 64 |
]
|
| 65 |
|
| 66 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
"""
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
Args:
|
| 71 |
category (str): ์นดํ
๊ณ ๋ฆฌ๋ช
|
|
|
|
|
|
|
| 72 |
maxResults (int): ๋ฐํํ ์ต๋ ๊ฒฐ๊ณผ ์
|
| 73 |
|
| 74 |
Returns:
|
| 75 |
List[Dict]: ๊ฒ์ ๊ฒฐ๊ณผ ๋ชฉ๋ก
|
| 76 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
conn = get_db_conn()
|
| 78 |
with conn.cursor() as cur:
|
| 79 |
-
cur.execute(
|
| 80 |
-
SELECT id, metadata, content
|
| 81 |
-
FROM vector_store
|
| 82 |
-
WHERE metadata->>'documentType' = 'chatAnalysis'
|
| 83 |
-
AND metadata->>'category' = %s
|
| 84 |
-
LIMIT %s
|
| 85 |
-
""", (category, maxResults))
|
| 86 |
rows = cur.fetchall()
|
| 87 |
conn.close()
|
| 88 |
return [
|
|
@@ -94,11 +122,18 @@ def search_chats_by_category(category: str, maxResults: int = 10) -> List[Dict]:
|
|
| 94 |
for row in rows
|
| 95 |
]
|
| 96 |
|
| 97 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
"""
|
| 99 |
-
์ง์ ๋ ๋ ์ง
|
|
|
|
| 100 |
|
| 101 |
Args:
|
|
|
|
| 102 |
startDate (str): ๊ฒ์ ์์ ๋ ์ง (YYYY-MM-DD)
|
| 103 |
endDate (str): ๊ฒ์ ์ข
๋ฃ ๋ ์ง (YYYY-MM-DD)
|
| 104 |
maxResults (int): ๋ฐํํ ์ต๋ ๊ฒฐ๊ณผ ์
|
|
@@ -106,7 +141,14 @@ def search_chats_by_date(startDate: str = None, endDate: str = None, maxResults:
|
|
| 106 |
Returns:
|
| 107 |
List[Dict]: ๊ฒ์ ๊ฒฐ๊ณผ ๋ชฉ๋ก
|
| 108 |
"""
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
query = """
|
| 111 |
SELECT id, metadata, content
|
| 112 |
FROM vector_store
|
|
@@ -115,6 +157,9 @@ def search_chats_by_date(startDate: str = None, endDate: str = None, maxResults:
|
|
| 115 |
AND (metadata->>'startTime') <> ''
|
| 116 |
"""
|
| 117 |
params = []
|
|
|
|
|
|
|
|
|
|
| 118 |
if startDate not in (None, ""):
|
| 119 |
query += " AND (metadata->>'startTime')::timestamp >= %s"
|
| 120 |
params.append(startDate)
|
|
@@ -123,6 +168,7 @@ def search_chats_by_date(startDate: str = None, endDate: str = None, maxResults:
|
|
| 123 |
params.append(endDate)
|
| 124 |
query += " LIMIT %s"
|
| 125 |
params.append(maxResults)
|
|
|
|
| 126 |
with conn.cursor() as cur:
|
| 127 |
cur.execute(query, tuple(params))
|
| 128 |
rows = cur.fetchall()
|
|
@@ -138,10 +184,10 @@ def search_chats_by_date(startDate: str = None, endDate: str = None, maxResults:
|
|
| 138 |
|
| 139 |
# Gradio Blocks์ ํจ์ ๋ฑ๋ก
|
| 140 |
with gr.Blocks() as demo:
|
| 141 |
-
gr.Markdown("#
|
| 142 |
gr.Interface(fn=search_similar_chats, inputs=["text", "number"], outputs="json", api_name="search_similar_chats")
|
| 143 |
-
gr.Interface(fn=
|
| 144 |
-
gr.Interface(fn=
|
| 145 |
|
| 146 |
if __name__ == "__main__":
|
| 147 |
demo.launch(mcp_server=True)
|
|
|
|
| 6 |
from typing import List, Dict
|
| 7 |
from pgvector.psycopg2 import register_vector
|
| 8 |
import numpy as np
|
| 9 |
+
from datetime import datetime
|
| 10 |
|
| 11 |
# DB ์ฐ๊ฒฐ ์ค์
|
| 12 |
def get_db_conn():
|
|
|
|
| 30 |
)
|
| 31 |
return response.data[0].embedding
|
| 32 |
|
| 33 |
+
def search_similar_chats(query: str, maxResults: int = 30000) -> List[Dict]:
|
| 34 |
"""
|
| 35 |
์ ์ฌํ ์ฑํ
๋ฌธ์๋ฅผ ๊ฒ์ํฉ๋๋ค.
|
| 36 |
|
|
|
|
| 64 |
for row in rows
|
| 65 |
]
|
| 66 |
|
| 67 |
+
def search_chats_by_category_and_date(
|
| 68 |
+
category: str,
|
| 69 |
+
startDate: str = None,
|
| 70 |
+
endDate: str = None,
|
| 71 |
+
maxResults: int = 30000
|
| 72 |
+
) -> List[Dict]:
|
| 73 |
"""
|
| 74 |
+
์นดํ
๊ณ ๋ฆฌ์ ์ง์ ๋ ๋ ์ง ๋ฒ์์ ํด๋นํ๋ ์ฑํ
๋ฌธ์๋ฅผ ๊ฒ์ํฉ๋๋ค.
|
| 75 |
+
(๊ฒ์ ๊ธฐ๊ฐ์ ์ต๋ 31์ผ๊น์ง ํ์ฉ)
|
| 76 |
|
| 77 |
Args:
|
| 78 |
category (str): ์นดํ
๊ณ ๋ฆฌ๋ช
|
| 79 |
+
startDate (str): ๊ฒ์ ์์ ๋ ์ง (YYYY-MM-DD)
|
| 80 |
+
endDate (str): ๊ฒ์ ์ข
๋ฃ ๋ ์ง (YYYY-MM-DD)
|
| 81 |
maxResults (int): ๋ฐํํ ์ต๋ ๊ฒฐ๊ณผ ์
|
| 82 |
|
| 83 |
Returns:
|
| 84 |
List[Dict]: ๊ฒ์ ๊ฒฐ๊ณผ ๋ชฉ๋ก
|
| 85 |
"""
|
| 86 |
+
if startDate not in (None, "") and endDate not in (None, ""):
|
| 87 |
+
try:
|
| 88 |
+
start_dt = datetime.strptime(startDate, "%Y-%m-%d")
|
| 89 |
+
end_dt = datetime.strptime(endDate, "%Y-%m-%d")
|
| 90 |
+
if (end_dt - start_dt).days > 31:
|
| 91 |
+
raise ValueError("๊ฒ์ ๊ธฐ๊ฐ์ ์ต๋ 31์ผ๊น์ง ํ์ฉ๋ฉ๋๋ค.")
|
| 92 |
+
except Exception as e:
|
| 93 |
+
raise ValueError(f"๋ ์ง ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์๊ฑฐ๋, ๊ธฐ๊ฐ์ด ๋๋ฌด ๊น๋๋ค: {e}")
|
| 94 |
+
query = """
|
| 95 |
+
SELECT id, metadata, content
|
| 96 |
+
FROM vector_store
|
| 97 |
+
WHERE metadata->>'documentType' = 'chatAnalysis'
|
| 98 |
+
AND metadata->>'category' = %s
|
| 99 |
+
AND (metadata->>'startTime') IS NOT NULL
|
| 100 |
+
AND (metadata->>'startTime') <> ''
|
| 101 |
+
"""
|
| 102 |
+
params = [category]
|
| 103 |
+
if startDate not in (None, ""):
|
| 104 |
+
query += " AND (metadata->>'startTime')::timestamp >= %s"
|
| 105 |
+
params.append(startDate)
|
| 106 |
+
if endDate not in (None, ""):
|
| 107 |
+
query += " AND (metadata->>'startTime')::timestamp < %s"
|
| 108 |
+
params.append(endDate)
|
| 109 |
+
query += " LIMIT %s"
|
| 110 |
+
params.append(maxResults)
|
| 111 |
conn = get_db_conn()
|
| 112 |
with conn.cursor() as cur:
|
| 113 |
+
cur.execute(query, tuple(params))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
rows = cur.fetchall()
|
| 115 |
conn.close()
|
| 116 |
return [
|
|
|
|
| 122 |
for row in rows
|
| 123 |
]
|
| 124 |
|
| 125 |
+
def search_chats_by_product_and_date(
|
| 126 |
+
productName: str = None,
|
| 127 |
+
startDate: str = None,
|
| 128 |
+
endDate: str = None,
|
| 129 |
+
maxResults: int = 30000
|
| 130 |
+
) -> List[Dict]:
|
| 131 |
"""
|
| 132 |
+
์ํ๋ช
๊ณผ ์ง์ ๋ ๋ ์ง ๋ฒ์์ ํด๋นํ๋ ์ฑํ
๋ฌธ์๋ฅผ ๊ฒ์ํฉ๋๋ค.
|
| 133 |
+
(๊ฒ์ ๊ธฐ๊ฐ์ ์ต๋ 31์ผ๊น์ง ํ์ฉ)
|
| 134 |
|
| 135 |
Args:
|
| 136 |
+
productName (str): ์ํ๋ช
(์ต์
)
|
| 137 |
startDate (str): ๊ฒ์ ์์ ๋ ์ง (YYYY-MM-DD)
|
| 138 |
endDate (str): ๊ฒ์ ์ข
๋ฃ ๋ ์ง (YYYY-MM-DD)
|
| 139 |
maxResults (int): ๋ฐํํ ์ต๋ ๊ฒฐ๊ณผ ์
|
|
|
|
| 141 |
Returns:
|
| 142 |
List[Dict]: ๊ฒ์ ๊ฒฐ๊ณผ ๋ชฉ๋ก
|
| 143 |
"""
|
| 144 |
+
if startDate not in (None, "") and endDate not in (None, ""):
|
| 145 |
+
try:
|
| 146 |
+
start_dt = datetime.strptime(startDate, "%Y-%m-%d")
|
| 147 |
+
end_dt = datetime.strptime(endDate, "%Y-%m-%d")
|
| 148 |
+
if (end_dt - start_dt).days > 31:
|
| 149 |
+
raise ValueError("๊ฒ์ ๊ธฐ๊ฐ์ ์ต๋ 31์ผ๊น์ง ํ์ฉ๋ฉ๋๋ค.")
|
| 150 |
+
except Exception as e:
|
| 151 |
+
raise ValueError(f"๋ ์ง ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์๊ฑฐ๋, ๊ธฐ๊ฐ์ด ๋๋ฌด ๊น๋๋ค: {e}")
|
| 152 |
query = """
|
| 153 |
SELECT id, metadata, content
|
| 154 |
FROM vector_store
|
|
|
|
| 157 |
AND (metadata->>'startTime') <> ''
|
| 158 |
"""
|
| 159 |
params = []
|
| 160 |
+
if productName not in (None, ""):
|
| 161 |
+
query += " AND (metadata->>'productName') = %s"
|
| 162 |
+
params.append(productName)
|
| 163 |
if startDate not in (None, ""):
|
| 164 |
query += " AND (metadata->>'startTime')::timestamp >= %s"
|
| 165 |
params.append(startDate)
|
|
|
|
| 168 |
params.append(endDate)
|
| 169 |
query += " LIMIT %s"
|
| 170 |
params.append(maxResults)
|
| 171 |
+
conn = get_db_conn()
|
| 172 |
with conn.cursor() as cur:
|
| 173 |
cur.execute(query, tuple(params))
|
| 174 |
rows = cur.fetchall()
|
|
|
|
| 184 |
|
| 185 |
# Gradio Blocks์ ํจ์ ๋ฑ๋ก
|
| 186 |
with gr.Blocks() as demo:
|
| 187 |
+
gr.Markdown("# Chat Analysis Search")
|
| 188 |
gr.Interface(fn=search_similar_chats, inputs=["text", "number"], outputs="json", api_name="search_similar_chats")
|
| 189 |
+
gr.Interface(fn=search_chats_by_product_and_date, inputs=["text", "text", "text", "number"], outputs="json", api_name="search_chats_by_product_and_date")
|
| 190 |
+
gr.Interface(fn=search_chats_by_category_and_date, inputs=["text", "text", "text", "number"], outputs="json", api_name="search_chats_by_category_and_date")
|
| 191 |
|
| 192 |
if __name__ == "__main__":
|
| 193 |
demo.launch(mcp_server=True)
|