muhammadhasnain100 commited on
Commit
44acc0c
·
verified ·
1 Parent(s): 1a9ef22

Upload 3 files

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