Spaces:
Sleeping
Sleeping
Ajit Panday commited on
Commit ·
c9906d2
1
Parent(s): 27305e2
Update system to use customer-specific databases
Browse files- app/auth.py +21 -2
- app/models.py +28 -1
- main.py +64 -26
app/auth.py
CHANGED
|
@@ -91,9 +91,27 @@ async def create_customer(
|
|
| 91 |
name=customer_data["name"],
|
| 92 |
company_name=customer_data["company_name"],
|
| 93 |
email=customer_data["email"],
|
| 94 |
-
api_key=api_key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
)
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
db.add(customer)
|
| 98 |
db.commit()
|
| 99 |
db.refresh(customer)
|
|
@@ -103,7 +121,8 @@ async def create_customer(
|
|
| 103 |
"name": customer.name,
|
| 104 |
"company_name": customer.company_name,
|
| 105 |
"email": customer.email,
|
| 106 |
-
"api_key": customer.api_key
|
|
|
|
| 107 |
}
|
| 108 |
|
| 109 |
@router.get("/customers/", response_model=list)
|
|
|
|
| 91 |
name=customer_data["name"],
|
| 92 |
company_name=customer_data["company_name"],
|
| 93 |
email=customer_data["email"],
|
| 94 |
+
api_key=api_key,
|
| 95 |
+
# Database credentials
|
| 96 |
+
db_host=customer_data.get("db_host"),
|
| 97 |
+
db_port=customer_data.get("db_port"),
|
| 98 |
+
db_name=customer_data.get("db_name"),
|
| 99 |
+
db_user=customer_data.get("db_user"),
|
| 100 |
+
db_password=customer_data.get("db_password")
|
| 101 |
)
|
| 102 |
|
| 103 |
+
# Validate database connection if credentials are provided
|
| 104 |
+
if all([customer.db_host, customer.db_port, customer.db_name, customer.db_user, customer.db_password]):
|
| 105 |
+
try:
|
| 106 |
+
engine = customer.get_db_engine()
|
| 107 |
+
with engine.connect() as conn:
|
| 108 |
+
conn.execute("SELECT 1")
|
| 109 |
+
except Exception as e:
|
| 110 |
+
raise HTTPException(
|
| 111 |
+
status_code=400,
|
| 112 |
+
detail=f"Invalid database credentials: {str(e)}"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
db.add(customer)
|
| 116 |
db.commit()
|
| 117 |
db.refresh(customer)
|
|
|
|
| 121 |
"name": customer.name,
|
| 122 |
"company_name": customer.company_name,
|
| 123 |
"email": customer.email,
|
| 124 |
+
"api_key": customer.api_key,
|
| 125 |
+
"db_configured": bool(customer.get_db_url())
|
| 126 |
}
|
| 127 |
|
| 128 |
@router.get("/customers/", response_model=list)
|
app/models.py
CHANGED
|
@@ -4,6 +4,7 @@ from sqlalchemy.orm import sessionmaker, relationship
|
|
| 4 |
from datetime import datetime
|
| 5 |
import os
|
| 6 |
from dotenv import load_dotenv
|
|
|
|
| 7 |
|
| 8 |
# Load environment variables
|
| 9 |
load_dotenv()
|
|
@@ -29,6 +30,32 @@ class Customer(Base):
|
|
| 29 |
created_at = Column(DateTime, default=datetime.utcnow)
|
| 30 |
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Relationship with call records
|
| 33 |
call_records = relationship("CallRecord", back_populates="customer")
|
| 34 |
|
|
@@ -50,7 +77,7 @@ class CallRecord(Base):
|
|
| 50 |
# Relationship with customer
|
| 51 |
customer = relationship("Customer", back_populates="call_records")
|
| 52 |
|
| 53 |
-
# Create all tables
|
| 54 |
Base.metadata.create_all(engine)
|
| 55 |
|
| 56 |
# Create session factory
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
import os
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
+
import json
|
| 8 |
|
| 9 |
# Load environment variables
|
| 10 |
load_dotenv()
|
|
|
|
| 30 |
created_at = Column(DateTime, default=datetime.utcnow)
|
| 31 |
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
| 32 |
|
| 33 |
+
# Database credentials for customer's own database
|
| 34 |
+
db_host = Column(String(255))
|
| 35 |
+
db_port = Column(Integer)
|
| 36 |
+
db_name = Column(String(100))
|
| 37 |
+
db_user = Column(String(100))
|
| 38 |
+
db_password = Column(String(255))
|
| 39 |
+
|
| 40 |
+
def get_db_url(self):
|
| 41 |
+
"""Get the database URL for this customer"""
|
| 42 |
+
if all([self.db_host, self.db_port, self.db_name, self.db_user, self.db_password]):
|
| 43 |
+
return f"mysql+pymysql://{self.db_user}:{self.db_password}@{self.db_host}:{self.db_port}/{self.db_name}"
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
def get_db_engine(self):
|
| 47 |
+
"""Get a SQLAlchemy engine for this customer's database"""
|
| 48 |
+
db_url = self.get_db_url()
|
| 49 |
+
if db_url:
|
| 50 |
+
return create_engine(db_url)
|
| 51 |
+
return None
|
| 52 |
+
|
| 53 |
+
def create_tables(self):
|
| 54 |
+
"""Create necessary tables in customer's database"""
|
| 55 |
+
engine = self.get_db_engine()
|
| 56 |
+
if engine:
|
| 57 |
+
CallRecord.metadata.create_all(engine)
|
| 58 |
+
|
| 59 |
# Relationship with call records
|
| 60 |
call_records = relationship("CallRecord", back_populates="customer")
|
| 61 |
|
|
|
|
| 77 |
# Relationship with customer
|
| 78 |
customer = relationship("Customer", back_populates="call_records")
|
| 79 |
|
| 80 |
+
# Create all tables in the main database
|
| 81 |
Base.metadata.create_all(engine)
|
| 82 |
|
| 83 |
# Create session factory
|
main.py
CHANGED
|
@@ -11,6 +11,7 @@ from transformers import pipeline
|
|
| 11 |
from sqlalchemy.orm import Session
|
| 12 |
import asyncio
|
| 13 |
from app import models, auth
|
|
|
|
| 14 |
|
| 15 |
# Load environment variables
|
| 16 |
load_dotenv()
|
|
@@ -78,31 +79,50 @@ async def process_call(
|
|
| 78 |
summary = summarizer(transcription, max_length=130, min_length=30, do_sample=False)[0]["summary_text"]
|
| 79 |
sentiment = sentiment_analyzer(transcription)[0]["label"]
|
| 80 |
|
| 81 |
-
# Create call record
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
transcription=transcription,
|
| 89 |
-
summary=summary,
|
| 90 |
-
sentiment=sentiment
|
| 91 |
-
)
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
except Exception as e:
|
| 108 |
raise HTTPException(status_code=500, detail=str(e))
|
|
@@ -116,10 +136,28 @@ async def list_calls(
|
|
| 116 |
"""
|
| 117 |
List all calls for a specific customer.
|
| 118 |
"""
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
import uvicorn
|
|
|
|
| 11 |
from sqlalchemy.orm import Session
|
| 12 |
import asyncio
|
| 13 |
from app import models, auth
|
| 14 |
+
from sqlalchemy.orm import sessionmaker
|
| 15 |
|
| 16 |
# Load environment variables
|
| 17 |
load_dotenv()
|
|
|
|
| 79 |
summary = summarizer(transcription, max_length=130, min_length=30, do_sample=False)[0]["summary_text"]
|
| 80 |
sentiment = sentiment_analyzer(transcription)[0]["label"]
|
| 81 |
|
| 82 |
+
# Create call record in customer's database
|
| 83 |
+
customer_engine = customer.get_db_engine()
|
| 84 |
+
if not customer_engine:
|
| 85 |
+
raise HTTPException(
|
| 86 |
+
status_code=400,
|
| 87 |
+
detail="Customer database not configured"
|
| 88 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
# Create tables if they don't exist
|
| 91 |
+
customer.create_tables()
|
| 92 |
|
| 93 |
+
# Create a session for the customer's database
|
| 94 |
+
CustomerSession = sessionmaker(autocommit=False, autoflush=False, bind=customer_engine)
|
| 95 |
+
customer_db = CustomerSession()
|
| 96 |
|
| 97 |
+
try:
|
| 98 |
+
# Create call record
|
| 99 |
+
call_record = models.CallRecord(
|
| 100 |
+
id=process_id,
|
| 101 |
+
customer_id=customer.id,
|
| 102 |
+
caller_number=caller_number,
|
| 103 |
+
called_number=called_number,
|
| 104 |
+
file_path=temp_file_path,
|
| 105 |
+
transcription=transcription,
|
| 106 |
+
summary=summary,
|
| 107 |
+
sentiment=sentiment
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
customer_db.add(call_record)
|
| 111 |
+
customer_db.commit()
|
| 112 |
+
|
| 113 |
+
# Clean up temporary file
|
| 114 |
+
os.unlink(temp_file_path)
|
| 115 |
+
|
| 116 |
+
return {
|
| 117 |
+
"id": process_id,
|
| 118 |
+
"transcription": transcription,
|
| 119 |
+
"summary": summary,
|
| 120 |
+
"sentiment": sentiment,
|
| 121 |
+
"timestamp": datetime.utcnow().isoformat() + "Z"
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
finally:
|
| 125 |
+
customer_db.close()
|
| 126 |
|
| 127 |
except Exception as e:
|
| 128 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 136 |
"""
|
| 137 |
List all calls for a specific customer.
|
| 138 |
"""
|
| 139 |
+
customer = db.query(models.Customer).filter(models.Customer.id == customer_id).first()
|
| 140 |
+
if not customer:
|
| 141 |
+
raise HTTPException(status_code=404, detail="Customer not found")
|
| 142 |
+
|
| 143 |
+
customer_engine = customer.get_db_engine()
|
| 144 |
+
if not customer_engine:
|
| 145 |
+
raise HTTPException(
|
| 146 |
+
status_code=400,
|
| 147 |
+
detail="Customer database not configured"
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# Create a session for the customer's database
|
| 151 |
+
CustomerSession = sessionmaker(autocommit=False, autoflush=False, bind=customer_engine)
|
| 152 |
+
customer_db = CustomerSession()
|
| 153 |
+
|
| 154 |
+
try:
|
| 155 |
+
calls = customer_db.query(models.CallRecord).filter(
|
| 156 |
+
models.CallRecord.customer_id == customer_id
|
| 157 |
+
).all()
|
| 158 |
+
return calls
|
| 159 |
+
finally:
|
| 160 |
+
customer_db.close()
|
| 161 |
|
| 162 |
if __name__ == "__main__":
|
| 163 |
import uvicorn
|