Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,13 +8,17 @@ from jwt import ExpiredSignatureError, InvalidTokenError
|
|
8 |
from starlette import status
|
9 |
from functions import *
|
10 |
import pandas as pd
|
11 |
-
from fastapi import FastAPI, File, UploadFile, HTTPException
|
12 |
from pydantic import BaseModel
|
13 |
from fastapi.middleware.cors import CORSMiddleware
|
14 |
from src.api.speech_api import speech_translator_router
|
15 |
from functions import client as supabase
|
16 |
from urllib.parse import urlparse
|
17 |
import nltk
|
|
|
|
|
|
|
|
|
18 |
|
19 |
|
20 |
nltk.download('punkt_tab')
|
@@ -526,4 +530,139 @@ async def chatHistory(vectorstore: str):
|
|
526 |
# @app.post("/trainChatbot")
|
527 |
# async def chatHistory(vectorstore: str):
|
528 |
# username, chatbotName = vectorstore.split("$")[1], vectorstore.split("$")[2]
|
529 |
-
# return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
from starlette import status
|
9 |
from functions import *
|
10 |
import pandas as pd
|
11 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException,Request
|
12 |
from pydantic import BaseModel
|
13 |
from fastapi.middleware.cors import CORSMiddleware
|
14 |
from src.api.speech_api import speech_translator_router
|
15 |
from functions import client as supabase
|
16 |
from urllib.parse import urlparse
|
17 |
import nltk
|
18 |
+
from collections import Counter, defaultdict
|
19 |
+
from datetime import datetime, timedelta
|
20 |
+
from dateutil.parser import isoparse
|
21 |
+
|
22 |
|
23 |
|
24 |
nltk.download('punkt_tab')
|
|
|
530 |
# @app.post("/trainChatbot")
|
531 |
# async def chatHistory(vectorstore: str):
|
532 |
# username, chatbotName = vectorstore.split("$")[1], vectorstore.split("$")[2]
|
533 |
+
# return response
|
534 |
+
|
535 |
+
def get_ip_info(ip: str):
|
536 |
+
try:
|
537 |
+
response = requests.get(f"https://ipinfo.io/{ip}/json")
|
538 |
+
data = response.json()
|
539 |
+
return data.get("city", "Unknown")
|
540 |
+
except Exception as e:
|
541 |
+
return "Unknown"
|
542 |
+
|
543 |
+
|
544 |
+
@app.post("/daily_chat_count")
|
545 |
+
async def daily_chat_count(days: int = 7):
|
546 |
+
response = supabase.table("ConversAI_ChatHistory").select("*").execute().data
|
547 |
+
|
548 |
+
seven_days_ago = datetime.now().astimezone() - timedelta(days=days)
|
549 |
+
dates = [
|
550 |
+
isoparse(i["timestamp"]).date()
|
551 |
+
for i in response
|
552 |
+
if isoparse(i["timestamp"]) >= seven_days_ago
|
553 |
+
]
|
554 |
+
|
555 |
+
date_count = Counter(dates)
|
556 |
+
|
557 |
+
data = [{"date": date.isoformat(), "count": count} for date, count in date_count.items()]
|
558 |
+
|
559 |
+
return {"data": data}
|
560 |
+
|
561 |
+
|
562 |
+
@app.post("/daily_active_end_user")
|
563 |
+
async def daily_active_end_user(days: int = 7):
|
564 |
+
response = supabase.table("ConversAI_ChatHistory").select("*").execute().data
|
565 |
+
|
566 |
+
seven_days_ago = datetime.now().astimezone() - timedelta(days=days)
|
567 |
+
|
568 |
+
ip_by_date = defaultdict(set)
|
569 |
+
|
570 |
+
for i in response:
|
571 |
+
timestamp = isoparse(i["timestamp"])
|
572 |
+
ip_address = i["IpAddress"]
|
573 |
+
if timestamp >= seven_days_ago:
|
574 |
+
date = timestamp.date()
|
575 |
+
ip_by_date[date].add(ip_address)
|
576 |
+
|
577 |
+
data = [{"date": date.isoformat(), "terminal": len(ips)} for date, ips in ip_by_date.items() if len(ips) > 1]
|
578 |
+
|
579 |
+
return {"data": data}
|
580 |
+
|
581 |
+
|
582 |
+
@app.post("/average_session_interaction")
|
583 |
+
async def average_session_interaction(days: int = 7):
|
584 |
+
response = supabase.table("ConversAI_ChatHistory").select("*").execute().data
|
585 |
+
|
586 |
+
seven_days_ago = datetime.now().astimezone() - timedelta(days=days)
|
587 |
+
|
588 |
+
total_messages_by_date = defaultdict(int)
|
589 |
+
unique_ips_by_date = defaultdict(set)
|
590 |
+
|
591 |
+
for i in response:
|
592 |
+
timestamp = isoparse(i["timestamp"])
|
593 |
+
ip_address = i["IpAddress"]
|
594 |
+
if timestamp >= seven_days_ago:
|
595 |
+
date = timestamp.date()
|
596 |
+
total_messages_by_date[date] += 1
|
597 |
+
unique_ips_by_date[date].add(ip_address)
|
598 |
+
|
599 |
+
data = []
|
600 |
+
for date in sorted(total_messages_by_date.keys()):
|
601 |
+
total_messages = total_messages_by_date[date]
|
602 |
+
unique_ips = len(unique_ips_by_date[date])
|
603 |
+
average_interactions = total_messages / unique_ips if unique_ips > 0 else 0
|
604 |
+
data.append({"date": date.isoformat(), "interactions": average_interactions})
|
605 |
+
|
606 |
+
return {"data": data}
|
607 |
+
|
608 |
+
|
609 |
+
@app.post("/token_usages")
|
610 |
+
async def token_usages(days: int = 7):
|
611 |
+
response = supabase.table("ConversAI_ChatHistory").select("*").execute().data
|
612 |
+
|
613 |
+
seven_days_ago = datetime.now().astimezone() - timedelta(days=days)
|
614 |
+
|
615 |
+
token_usage_by_date = defaultdict(int)
|
616 |
+
|
617 |
+
for i in response:
|
618 |
+
timestamp = isoparse(i["timestamp"])
|
619 |
+
if timestamp >= seven_days_ago:
|
620 |
+
date = timestamp.date()
|
621 |
+
response_token_count = i.get("ResponseTokenCount")
|
622 |
+
if response_token_count is not None:
|
623 |
+
token_usage_by_date[date] += response_token_count
|
624 |
+
|
625 |
+
data = [{"date": date.isoformat(), "total_tokens": total_tokens} for date, total_tokens in
|
626 |
+
token_usage_by_date.items()]
|
627 |
+
|
628 |
+
return {"data": data}
|
629 |
+
|
630 |
+
|
631 |
+
@app.post("/add_feedback")
|
632 |
+
async def add_feedback(request: Request, feedback: str, user_id: str):
|
633 |
+
client_ip = request.client.host
|
634 |
+
city = get_ip_info(client_ip)
|
635 |
+
|
636 |
+
response = supabase.table("ConversAI_Feedback").insert(
|
637 |
+
{"feedback": feedback, "user_id": user_id, "city": city, "ip": client_ip}).execute()
|
638 |
+
|
639 |
+
return {"message": "success"}
|
640 |
+
|
641 |
+
|
642 |
+
@app.post("/user_satisfaction_rate")
|
643 |
+
async def user_satisfaction_rate():
|
644 |
+
response = supabase.table("ConversAI_Feedback").select("*").execute().data
|
645 |
+
|
646 |
+
seven_days_ago = datetime.now().astimezone() - timedelta(days=7)
|
647 |
+
|
648 |
+
feedback_counts = defaultdict(lambda: {"like": 0, "dislike": 0})
|
649 |
+
|
650 |
+
for i in response:
|
651 |
+
timestamp = isoparse(i["timestamp"])
|
652 |
+
if timestamp >= seven_days_ago:
|
653 |
+
date = timestamp.date()
|
654 |
+
feedback = i.get("feedback")
|
655 |
+
if feedback == "like":
|
656 |
+
feedback_counts[date]["like"] += 1
|
657 |
+
elif feedback == "dislike":
|
658 |
+
feedback_counts[date]["dislike"] += 1
|
659 |
+
|
660 |
+
data = []
|
661 |
+
for date in sorted(feedback_counts.keys()):
|
662 |
+
like_count = feedback_counts[date]["like"]
|
663 |
+
dislike_count = feedback_counts[date]["dislike"]
|
664 |
+
total_feedback = like_count + dislike_count
|
665 |
+
satisfaction_rate = (like_count / total_feedback * 100) if total_feedback > 0 else 0
|
666 |
+
data.append({"date": date.isoformat(), "rate": satisfaction_rate})
|
667 |
+
|
668 |
+
return {"data": data}
|