Spaces:
Build error
Build error
Update utils/database.py
Browse files- utils/database.py +24 -0
utils/database.py
CHANGED
|
@@ -242,6 +242,30 @@ async def generate_document_tags(content: str) -> List[str]:
|
|
| 242 |
except Exception as e:
|
| 243 |
st.error(f"Error generating tags: {e}")
|
| 244 |
return []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
def add_document_tags(conn: sqlite3.Connection, document_id: int, tags: List[str]) -> bool:
|
| 247 |
"""Add tags to a document."""
|
|
|
|
| 242 |
except Exception as e:
|
| 243 |
st.error(f"Error generating tags: {e}")
|
| 244 |
return []
|
| 245 |
+
|
| 246 |
+
def generate_document_tags(content: str) -> List[str]:
|
| 247 |
+
"""Generate tags for a document using AI."""
|
| 248 |
+
try:
|
| 249 |
+
llm = ChatOpenAI(temperature=0.2, model="gpt-3.5-turbo")
|
| 250 |
+
|
| 251 |
+
prompt = """Analyze the following document content and generate relevant tags/keywords.
|
| 252 |
+
Focus on key themes, topics, and important terminology.
|
| 253 |
+
Return only the tags as a comma-separated list.
|
| 254 |
+
Content: {content}"""
|
| 255 |
+
|
| 256 |
+
response = llm.invoke([
|
| 257 |
+
SystemMessage(content="You are a document analysis assistant. Generate relevant tags as a comma-separated list only."),
|
| 258 |
+
HumanMessage(content=prompt.format(content=content[:2000]))
|
| 259 |
+
])
|
| 260 |
+
|
| 261 |
+
# Extract content from the AI message
|
| 262 |
+
tags_text = response.content
|
| 263 |
+
# Split the comma-separated string into a list
|
| 264 |
+
tags = [tag.strip() for tag in tags_text.split(',')]
|
| 265 |
+
return tags
|
| 266 |
+
except Exception as e:
|
| 267 |
+
st.error(f"Error generating tags: {e}")
|
| 268 |
+
return []
|
| 269 |
|
| 270 |
def add_document_tags(conn: sqlite3.Connection, document_id: int, tags: List[str]) -> bool:
|
| 271 |
"""Add tags to a document."""
|