Skip to content

Repository files navigation

Call Analysis Backend

A FastAPI-based backend service for analyzing call transcripts using Google's Gemini AI.

Features

  • Transcript storage and retrieval
  • AI-powered call analysis using Google Gemini 2.0 Flash-Lite
  • Structured data extraction from call transcripts
  • JSON data storage in PostgreSQL
  • RESTful API for integration with frontend applications

Prerequisites

  • Python 3.12+
  • Google Gemini API key
  • uv >=0.6.9
  • Postgres >=17

Setup

1. Clone the repository

git clone git@github.com:ai-Posibl/mono_backend.git

2. Use uv to setup python venv and install packages

uv sync

3. Activate the virtual environment

# For Windows
.venv\Scripts\activate
# For Linux
source .venv/bin/activate 

3. Set up environment variables

Create .env file with reference to env.example

6. Run the application

uvicorn main:app --reload

The API will be available at http://localhost:8000 The docs will be presetn at http://localhost:8000

API Endpoints

Call Analysis

  • POST /analyze

    • Analyzes a call transcript using Gemini AI
    • Request body:
      { 
        "session_id": "string",
        "transcript": "string",
        "audio_url": "string"
      }
    • Response:
      {
        "session_id": "string",
        "transcript": "string",
        "summary": "string",
        "structured_output": {
          "business_name": "string",
          "business_offering": "string",
          "transactions_per_month": "string"
        },
        "success_evaluation": {
          "result": true
        },
        "audio_url": "string"
      }
  • GET /analysis/{session_id}

    • Retrieves a previously analyzed call by session ID
    • Response: Same as the analyze endpoint response
  • POST /add

    • Adds a new transcript to the database
    • Request body:
      { 
        "session_id": "string",
        "transcript": "string"
      }
  • GET /all

    • Retrieves all transcripts from the database

Project Structure

backend/
├── app/
│   ├── models/
│   │   ├── __init__.py
│   │   ├── audio_model.py
│   │   ├── transcript.py
│   │   └── callAnalysis_model.py
│   ├── routes/
│   │   └── callAnalysis_router.py
│   ├── database.py
│   ├── dependencies.py
│   └── main.py
├── .env
├── .gitignore
└── README.md

Development

Adding New Features

  1. Create new models in the app/models directory
  2. Add new routes in the app/routes directory
  3. Update the main application in app/main.py

Database Schema

The application uses PostgreSQL with the following tables:

  • transcripts: Stores raw call transcripts
  • audio: Stores audio file information
  • call_analysis: Stores call analysis results including JSON data

Working with JSON in PostgreSQL

This application uses PostgreSQL's JSON data type for storing structured data:

  • structured_output: Stores business information extracted from the call
  • success_evaluation: Stores the evaluation result of the call

To query JSON fields in PostgreSQL, you can use operators like:

-- Example: Find all successful calls
SELECT * FROM call_analysis WHERE success_evaluation->>'result' = 'true';

-- Example: Find calls for a specific business type
SELECT * FROM call_analysis WHERE structured_output->>'business_offering' LIKE '%retail%';

Common Issues

Gemini API Schema Limitations

When using structured output with Gemini API:

  • Union types (e.g., int | str) are not supported in response schemas
  • Always use single types for each field (e.g., str, int, bool)
  • For numeric values that might be strings, use str type and convert as needed

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages