Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
Binary file modified __pycache__/metrics_logger.cpython-312.pyc
Binary file not shown.
Binary file added __pycache__/twilio_stream.cpython-312.pyc
Binary file not shown.
55 changes: 55 additions & 0 deletions call_twilio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os
from twilio.rest import Client
from dotenv import load_dotenv

load_dotenv()

def make_call(to_number):
try:
client = Client(os.getenv("TWILIO_ACCOUNT_SID"), os.getenv("TWILIO_AUTH_TOKEN"))

# Use your ngrok URL for the voice webhook
voice_url = f"{os.getenv('NGROK_URL')}/voice"

print(f"Making call to {to_number}")
print(f"Voice webhook URL: {voice_url}")

call = client.calls.create(
to=to_number,
from_=os.getenv("TWILIO_NUMBER"),
url=voice_url, # Twilio will fetch TwiML from this URL
method="POST"
)
print(f"✅ Call initiated successfully!")
print(f"Call SID: {call.sid}")
print(f"Call Status: {call.status}")

except Exception as e:
print(f"❌ Error making call: {e}")

def test_webhook():
"""Test if the webhook URL is accessible"""
try:
import requests
webhook_url = f"{os.getenv('NGROK_URL')}/voice"
response = requests.get(webhook_url)
if response.status_code == 200:
print(f"✅ Webhook URL is accessible: {webhook_url}")
else:
print(f"❌ Webhook URL returned status {response.status_code}: {webhook_url}")
except Exception as e:
print(f"❌ Cannot reach webhook URL: {e}")

if __name__ == "__main__":
print("=== Voice AI Agent Call Initiator ===")

# Test webhook first
print("\n1. Testing webhook URL...")
test_webhook()

# Make call
print("\n2. Initiating call...")
phone = input("Enter the phone number to call (with country code, e.g. +15551234567): ")
make_call(phone)

print("\n3. Call initiated! Answer your phone and start speaking.")
Binary file modified requirements.txt
Binary file not shown.
102 changes: 102 additions & 0 deletions twilio_stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import os
import requests
from fastapi import FastAPI, Request, Form
from fastapi.responses import Response
from dotenv import load_dotenv

load_dotenv()
app = FastAPI()

# Helper: Send text to Groq LLM and get response
def ask_groq(prompt):
try:
url = "https://api.groq.com/openai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {os.getenv('GROQ_API_KEY')}",
"Content-Type": "application/json"
}
data = {
"model": "llama3-8b-8192",
"messages": [
{"role": "system", "content": "You are a helpful AI assistant. Keep responses short and conversational for phone calls, under 50 words."},
{"role": "user", "content": prompt}
],
"max_tokens": 100,
"temperature": 0.7
}
resp = requests.post(url, headers=headers, json=data)
resp.raise_for_status()
return resp.json()["choices"][0]["message"]["content"]
except Exception as e:
print(f"Groq API error: {e}")
return "I'm sorry, I'm having trouble processing your request right now."

@app.get("/")
def read_root():
return {"message": "Voice AI Agent is running!"}

@app.api_route("/voice", methods=["GET", "POST"])
async def voice(request: Request):
print("📞 Twilio voice call received!")

# Initial greeting when call starts
message = "Hello! I am your AI assistant. How can I help you today?"

print(f"📞 AI says: {message}")

# Return TwiML to make Twilio speak and listen
response = f"""<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say voice="Polly.Joanna">{message}</Say>
<Gather input="speech" action="/process-speech" method="POST" speechTimeout="5" timeout="10">
<Say>Please speak now.</Say>
</Gather>
<Say>I didn't hear anything. Goodbye!</Say>
</Response>"""

return Response(content=response, media_type="application/xml")

@app.api_route("/process-speech", methods=["POST"])
async def process_speech(request: Request):
form = await request.form()
user_speech = form.get("SpeechResult", "")
confidence = form.get("Confidence", "0")

print(f"📞 User said: '{user_speech}' (confidence: {confidence})")

if user_speech and user_speech.strip():
# Send to Groq LLM
ai_response = ask_groq(user_speech)
print(f"📞 AI responds: {ai_response}")

# Continue conversation
response = f"""<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say voice="Polly.Joanna">{ai_response}</Say>
<Gather input="speech" action="/process-speech" method="POST" speechTimeout="5" timeout="10">
<Say>Is there anything else I can help you with?</Say>
</Gather>
<Say>Thank you for calling. Goodbye!</Say>
</Response>"""
else:
# No speech detected
response = f"""<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>I didn't hear anything clearly. Let me try again.</Say>
<Gather input="speech" action="/process-speech" method="POST" speechTimeout="5" timeout="10">
<Say>Please speak clearly.</Say>
</Gather>
<Say>Thank you for calling. Goodbye!</Say>
</Response>"""

return Response(content=response, media_type="application/xml")

# Health check endpoint
@app.get("/health")
def health_check():
return {"status": "healthy", "message": "Voice AI Agent is running"}

# Graceful shutdown
@app.on_event("shutdown")
async def shutdown_event():
print("Shutting down Voice AI Agent...")
Binary file modified venv/Lib/site-packages/__pycache__/deprecation.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file modified venv/Lib/site-packages/__pycache__/six.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified venv/Lib/site-packages/aenum/__pycache__/_common.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file modified venv/Lib/site-packages/aenum/__pycache__/_enum.cpython-312.pyc
Binary file not shown.
Binary file modified venv/Lib/site-packages/aenum/__pycache__/_py3.cpython-312.pyc
Binary file not shown.
Binary file modified venv/Lib/site-packages/aenum/__pycache__/_tuple.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file modified venv/Lib/site-packages/aiofiles/__pycache__/base.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified venv/Lib/site-packages/aiohttp/__pycache__/abc.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified venv/Lib/site-packages/aiohttp/__pycache__/hdrs.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file modified venv/Lib/site-packages/aiohttp/__pycache__/http.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified venv/Lib/site-packages/aiohttp/__pycache__/log.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified venv/Lib/site-packages/aiohttp/__pycache__/web.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dmitry Inyutin <inyutin.da@gmail.com>
8 changes: 8 additions & 0 deletions venv/Lib/site-packages/aiohttp_retry-2.9.1.dist-info/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
The MIT License (MIT)
Copyright (c) 2020 aiohttp_retry Authors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading