vBot-2.1 / app.py
Ajit Panday
Add application files for Hugging Face Spaces
60ed731
from fastapi import FastAPI, UploadFile, File, Form, HTTPException, Header
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
import os
import shutil
import tempfile
from typing import Optional
import logging
from datetime import datetime
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI(title="vBot API", description="API for processing call recordings")
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# API key validation
API_KEY = os.getenv("API_KEY", "your_api_key_here")
def verify_api_key(x_api_key: str = Header(None)):
if not x_api_key or x_api_key != API_KEY:
raise HTTPException(status_code=401, detail="Invalid API key")
@app.post("/api/v1/process-call")
async def process_call(
file: UploadFile = File(...),
caller_number: str = Form(...),
called_number: str = Form(...),
x_api_key: str = Header(None)
):
try:
# Verify API key
verify_api_key(x_api_key)
# Create temporary directory for processing
with tempfile.TemporaryDirectory() as temp_dir:
# Save uploaded file
file_path = os.path.join(temp_dir, file.filename)
with open(file_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
# Process the file (placeholder for actual processing)
# TODO: Add actual processing logic here
return JSONResponse(
status_code=200,
content={
"status": "success",
"message": "Call processed successfully",
"timestamp": datetime.now().isoformat(),
"caller_number": caller_number,
"called_number": called_number,
"file_name": file.filename
}
)
except Exception as e:
logger.error(f"Error processing call: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.get("/")
async def root():
return {"message": "vBot API is running"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)