Spaces:
Sleeping
Sleeping
File size: 1,715 Bytes
44acc0c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
from flask import Flask, request, jsonify
from twilio.twiml.voice_response import VoiceResponse, Gather
import google.generativeai as genai
import requests
# Initialize the Flask app
app = Flask(__name__)
# Configure Google Generative AI
GOOGLE_API_KEY = "AIzaSyAs7z8GwXN-elBeq_mS-NGd..."
genai.configure(api_key=GOOGLE_API_KEY)
def generate_answer(question):
prompt = f"""
You're a calling agent who asks users if they're at home for their COD delivery
Question: {question}
"""
"""Generate an AI-based answer for a question."""
model = genai.GenerativeModel(
'gemini-1.5-flash-001',
generation_config={"response_mime_type": "application/json"}
)
response = model.generate(prompt)
return response.text
# Endpoint to handle outbound call interactions
@app.route("/voice", methods=["POST"])
def voice_interaction():
# Get the Twilio POST request data
incoming_data = request.form
user_speech = incoming_data.get("SpeechResult", "")
response = VoiceResponse()
if "goodbye" in user_speech.lower():
response.say("Goodbye! Ending the call.")
return str(response)
# Generate a response using Generative AI
try:
ai_response = generate_answer(user_speech)
ai_text_response = ai_response.get("text", "I could not generate a response.")
except Exception as e:
ai_text_response = "I'm sorry, I encountered an error generating a response."
# Gather further input from the user
gather = Gather(input="speech", action="/voice", method="POST")
gather.say(ai_text_response)
response.append(gather)
return str(response)
if __name__ == "__main__":
app.run(debug=True)
|