Spaces:
Running
Running
Fix insert issue and search API added
Browse files
main.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import re
|
| 2 |
from fastapi import FastAPI
|
|
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForTokenClassification
|
| 5 |
import dateparser
|
|
@@ -24,7 +25,6 @@ import psycopg2
|
|
| 24 |
from psycopg2.extras import Json
|
| 25 |
import os
|
| 26 |
from dotenv import load_dotenv
|
| 27 |
-
import random
|
| 28 |
|
| 29 |
# Load environment variables
|
| 30 |
load_dotenv()
|
|
@@ -581,7 +581,7 @@ def predict_expense_category(text, detected_stores):
|
|
| 581 |
"khana kharch", "khana kharida", "khana diya", "khana laya", "khana banaya"
|
| 582 |
],
|
| 583 |
"transport": [
|
| 584 |
-
"uber", "ola", "taxi", "cab", "bus", "train", "metro", "flight", "auto", "rickshaw", "car", "gaadi", "yatra", "safar", "travel", "ticket", "plane", "udaan", "station", "airport"
|
| 585 |
],
|
| 586 |
"shopping": [
|
| 587 |
"amazon", "flipkart", "myntra", "shopping", "clothes", "kapde", "apparel", "shoes", "jeans", "tshirt", "store", "fashion", "dukaan", "mall", "bazaar", "market", "kharida", "order diya", "le liya"
|
|
@@ -686,6 +686,73 @@ async def not_found_handler(request: Request, exc):
|
|
| 686 |
async def internal_error_handler(request: Request, exc):
|
| 687 |
return ORJSONResponse(status_code=500, content={"error": "Internal server error: " + str(exc)})
|
| 688 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 689 |
@app.get("/visualyse/{user_id}", response_class=ORJSONResponse)
|
| 690 |
async def visualyse_dashboard(user_id: str):
|
| 691 |
try:
|
|
@@ -852,6 +919,9 @@ async def analyze(input: TextInput):
|
|
| 852 |
tags = generate_tags(best_label, text)
|
| 853 |
language_detected = detect_language(text)
|
| 854 |
sentiment_score = get_sentiment_score(text)
|
|
|
|
|
|
|
|
|
|
| 855 |
entities = await asyncio.to_thread(extract_entities, text)
|
| 856 |
people = entities["people"] # Extracted people entities
|
| 857 |
intent = infer_intent(best_label, text)
|
|
|
|
| 1 |
import re
|
| 2 |
from fastapi import FastAPI
|
| 3 |
+
from fastapi import Header
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForTokenClassification
|
| 6 |
import dateparser
|
|
|
|
| 25 |
from psycopg2.extras import Json
|
| 26 |
import os
|
| 27 |
from dotenv import load_dotenv
|
|
|
|
| 28 |
|
| 29 |
# Load environment variables
|
| 30 |
load_dotenv()
|
|
|
|
| 581 |
"khana kharch", "khana kharida", "khana diya", "khana laya", "khana banaya"
|
| 582 |
],
|
| 583 |
"transport": [
|
| 584 |
+
"uber", "ola", "taxi", "cab", "bus", "train", "metro", "flight", "auto", "rickshaw", "car", "gaadi", "yatra", "safar", "travel", "ticket", "plane", "udaan", "station", "airport", "rapido",
|
| 585 |
],
|
| 586 |
"shopping": [
|
| 587 |
"amazon", "flipkart", "myntra", "shopping", "clothes", "kapde", "apparel", "shoes", "jeans", "tshirt", "store", "fashion", "dukaan", "mall", "bazaar", "market", "kharida", "order diya", "le liya"
|
|
|
|
| 686 |
async def internal_error_handler(request: Request, exc):
|
| 687 |
return ORJSONResponse(status_code=500, content={"error": "Internal server error: " + str(exc)})
|
| 688 |
|
| 689 |
+
@app.get("/search", response_class=ORJSONResponse)
|
| 690 |
+
async def search_entries(
|
| 691 |
+
userid: str = Header(..., description="User ID"),
|
| 692 |
+
tags: str = "",
|
| 693 |
+
query: str = "",
|
| 694 |
+
startDate: str = "",
|
| 695 |
+
endDate: str = "",
|
| 696 |
+
type: str = ""
|
| 697 |
+
):
|
| 698 |
+
# Validate user_id from header
|
| 699 |
+
if not userid or not userid.strip():
|
| 700 |
+
return ORJSONResponse(status_code=400, content={"error": "Missing or empty userid header."})
|
| 701 |
+
|
| 702 |
+
# Build SQL filters
|
| 703 |
+
filters = ["user_id = %s"]
|
| 704 |
+
params = [userid]
|
| 705 |
+
|
| 706 |
+
if type:
|
| 707 |
+
filters.append("type = %s")
|
| 708 |
+
params.append(type)
|
| 709 |
+
|
| 710 |
+
if tags:
|
| 711 |
+
tag_list = [t.strip() for t in tags.split(",") if t.strip()]
|
| 712 |
+
filters.append("tags && %s")
|
| 713 |
+
params.append(tag_list)
|
| 714 |
+
|
| 715 |
+
if query:
|
| 716 |
+
filters.append("(raw_text ILIKE %s OR summary ILIKE %s)")
|
| 717 |
+
params.extend([f"%{query}%", f"%{query}%"])
|
| 718 |
+
|
| 719 |
+
if startDate:
|
| 720 |
+
try:
|
| 721 |
+
start_dt = datetime.strptime(startDate, "%d-%m-%Y")
|
| 722 |
+
filters.append("created_at >= %s")
|
| 723 |
+
params.append(start_dt)
|
| 724 |
+
except:
|
| 725 |
+
return ORJSONResponse(status_code=400, content={"error": "Invalid startDate format. Use DD-MM-YYYY."})
|
| 726 |
+
|
| 727 |
+
if endDate:
|
| 728 |
+
try:
|
| 729 |
+
end_dt = datetime.strptime(endDate, "%d-%m-%Y")
|
| 730 |
+
filters.append("created_at <= %s")
|
| 731 |
+
params.append(end_dt)
|
| 732 |
+
except:
|
| 733 |
+
return ORJSONResponse(status_code=400, content={"error": "Invalid endDate format. Use DD-MM-YYYY."})
|
| 734 |
+
|
| 735 |
+
where_clause = " AND ".join(filters)
|
| 736 |
+
query_sql = f"SELECT * FROM user_entries WHERE {where_clause} ORDER BY created_at DESC LIMIT 50"
|
| 737 |
+
|
| 738 |
+
try:
|
| 739 |
+
conn = psycopg2.connect(DATABASE_URL)
|
| 740 |
+
cur = conn.cursor()
|
| 741 |
+
cur.execute(query_sql, tuple(params))
|
| 742 |
+
rows = cur.fetchall()
|
| 743 |
+
columns = [desc[0] for desc in cur.description]
|
| 744 |
+
entries = [dict(zip(columns, row)) for row in rows]
|
| 745 |
+
# Remove raw_json from each entry in results
|
| 746 |
+
for entry in entries:
|
| 747 |
+
entry.pop("raw_json", None)
|
| 748 |
+
|
| 749 |
+
cur.close()
|
| 750 |
+
conn.close()
|
| 751 |
+
except Exception as e:
|
| 752 |
+
return ORJSONResponse(status_code=500, content={"error": str(e)})
|
| 753 |
+
|
| 754 |
+
return ORJSONResponse(content={"results": entries})
|
| 755 |
+
|
| 756 |
@app.get("/visualyse/{user_id}", response_class=ORJSONResponse)
|
| 757 |
async def visualyse_dashboard(user_id: str):
|
| 758 |
try:
|
|
|
|
| 919 |
tags = generate_tags(best_label, text)
|
| 920 |
language_detected = detect_language(text)
|
| 921 |
sentiment_score = get_sentiment_score(text)
|
| 922 |
+
if sentiment_score is None or sentiment_score == "":
|
| 923 |
+
sentiment_score = 0.0
|
| 924 |
+
|
| 925 |
entities = await asyncio.to_thread(extract_entities, text)
|
| 926 |
people = entities["people"] # Extracted people entities
|
| 927 |
intent = infer_intent(best_label, text)
|