Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import tempfile
|
| 4 |
-
import json
|
| 5 |
import requests
|
| 6 |
from pathlib import Path
|
| 7 |
-
from transformers import pipeline
|
| 8 |
|
| 9 |
# Create necessary directories
|
| 10 |
DOCUMENTS_DIR = Path("documents")
|
|
@@ -35,32 +33,25 @@ DEFAULT_RESPONSES = {
|
|
| 35 |
# Simple document storage (in-memory for this example)
|
| 36 |
knowledge_base = []
|
| 37 |
|
| 38 |
-
# Create a classifier
|
| 39 |
-
try:
|
| 40 |
-
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 41 |
-
except Exception as e:
|
| 42 |
-
print(f"Error loading classifier: {e}")
|
| 43 |
-
classifier = None
|
| 44 |
-
|
| 45 |
def classify_intent(text):
|
| 46 |
-
"""
|
| 47 |
-
if not text
|
| 48 |
-
return "other", 0.
|
| 49 |
-
|
| 50 |
-
try:
|
| 51 |
-
results = classifier(
|
| 52 |
-
text,
|
| 53 |
-
candidate_labels=POSSIBLE_INTENTS,
|
| 54 |
-
hypothesis_template="This is a {} request."
|
| 55 |
-
)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
return
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
def load_pdf(file):
|
| 66 |
"""Load a PDF document into the knowledge base"""
|
|
@@ -130,19 +121,23 @@ def list_documents():
|
|
| 130 |
}
|
| 131 |
|
| 132 |
# Twilio voice handlers
|
| 133 |
-
def twilio_call_handler():
|
| 134 |
"""Handle incoming Twilio calls"""
|
| 135 |
twiml = """<?xml version="1.0" encoding="UTF-8"?>
|
| 136 |
<Response>
|
| 137 |
<Say>Hello! Thank you for calling. How can I help you today?</Say>
|
| 138 |
-
<Gather input="speech" action="https://huggingface.co/spaces/iajitpanday/vBot-1.5/api/
|
| 139 |
<Say>I didn't hear anything. Please call back when you're ready.</Say>
|
| 140 |
</Response>
|
| 141 |
"""
|
| 142 |
return twiml
|
| 143 |
|
| 144 |
-
def twilio_speech_handler(
|
| 145 |
"""Process speech from Twilio"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
intent, _ = classify_intent(speech_result)
|
| 147 |
response = generate_response(speech_result, intent)
|
| 148 |
|
|
@@ -151,30 +146,12 @@ def twilio_speech_handler(speech_result=""):
|
|
| 151 |
<Say>{response}</Say>
|
| 152 |
<Pause length="1"/>
|
| 153 |
<Say>Is there anything else I can help you with today?</Say>
|
| 154 |
-
<Gather input="speech" action="https://huggingface.co/spaces/iajitpanday/vBot-1.5/api/
|
| 155 |
<Say>Thank you for calling. Have a great day!</Say>
|
| 156 |
</Response>
|
| 157 |
"""
|
| 158 |
return twiml
|
| 159 |
|
| 160 |
-
def twilio_followup_handler(speech_result=""):
|
| 161 |
-
"""Handle follow-up responses"""
|
| 162 |
-
if any(word in speech_result.lower() for word in ["yes", "yeah", "sure", "please", "correct"]):
|
| 163 |
-
twiml = """<?xml version="1.0" encoding="UTF-8"?>
|
| 164 |
-
<Response>
|
| 165 |
-
<Say>Great! How else can I help you today?</Say>
|
| 166 |
-
<Gather input="speech" action="https://huggingface.co/spaces/iajitpanday/vBot-1.5/api/twilio/speech" method="POST" speechTimeout="auto" speechModel="phone_call"/>
|
| 167 |
-
<Say>I didn't hear anything. Thank you for calling. Goodbye!</Say>
|
| 168 |
-
</Response>
|
| 169 |
-
"""
|
| 170 |
-
else:
|
| 171 |
-
twiml = """<?xml version="1.0" encoding="UTF-8"?>
|
| 172 |
-
<Response>
|
| 173 |
-
<Say>Thank you for calling. Have a great day!</Say>
|
| 174 |
-
</Response>
|
| 175 |
-
"""
|
| 176 |
-
return twiml
|
| 177 |
-
|
| 178 |
# Create Gradio interface
|
| 179 |
with gr.Blocks(title="Call Assistant System") as demo:
|
| 180 |
gr.Markdown("# Call Assistant System")
|
|
@@ -236,35 +213,38 @@ with gr.Blocks(title="Call Assistant System") as demo:
|
|
| 236 |
gr.Markdown("""
|
| 237 |
## Twilio Integration Instructions
|
| 238 |
|
| 239 |
-
This app provides API
|
| 240 |
|
| 241 |
1. Log into your Twilio account
|
| 242 |
2. Go to Phone Numbers → Manage → Active numbers
|
| 243 |
3. Select your number (+19704064410)
|
| 244 |
-
4. For "A Call Comes In",
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
The system will:
|
| 249 |
-
-
|
| 250 |
-
- Process speech input
|
| 251 |
- Generate responses using your knowledge base
|
| 252 |
- Handle follow-up questions
|
| 253 |
""")
|
| 254 |
|
| 255 |
-
# API
|
| 256 |
-
def
|
| 257 |
-
"""API endpoint for
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
"""API endpoint for follow-up handling"""
|
| 266 |
-
return twilio_followup_handler(SpeechResult)
|
| 267 |
|
| 268 |
-
# Launch the interface
|
| 269 |
demo.queue()
|
| 270 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import tempfile
|
|
|
|
| 4 |
import requests
|
| 5 |
from pathlib import Path
|
|
|
|
| 6 |
|
| 7 |
# Create necessary directories
|
| 8 |
DOCUMENTS_DIR = Path("documents")
|
|
|
|
| 33 |
# Simple document storage (in-memory for this example)
|
| 34 |
knowledge_base = []
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def classify_intent(text):
|
| 37 |
+
"""Simple keyword-based intent classification"""
|
| 38 |
+
if not text:
|
| 39 |
+
return "other", 0.6
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
text = text.lower()
|
| 42 |
+
|
| 43 |
+
if any(word in text for word in ["buy", "purchase", "product", "price", "cost"]):
|
| 44 |
+
return "product_inquiry", 0.8
|
| 45 |
+
elif any(word in text for word in ["help", "issue", "problem", "fix", "broken"]):
|
| 46 |
+
return "technical_support", 0.8
|
| 47 |
+
elif any(word in text for word in ["bill", "payment", "charge", "invoice"]):
|
| 48 |
+
return "billing_question", 0.8
|
| 49 |
+
elif any(word in text for word in ["appointment", "schedule", "book", "meeting"]):
|
| 50 |
+
return "appointment_scheduling", 0.8
|
| 51 |
+
elif any(word in text for word in ["unhappy", "disappointed", "complaint", "bad"]):
|
| 52 |
+
return "complaint", 0.8
|
| 53 |
+
else:
|
| 54 |
+
return "general_information", 0.6
|
| 55 |
|
| 56 |
def load_pdf(file):
|
| 57 |
"""Load a PDF document into the knowledge base"""
|
|
|
|
| 121 |
}
|
| 122 |
|
| 123 |
# Twilio voice handlers
|
| 124 |
+
def twilio_call_handler(data=None):
|
| 125 |
"""Handle incoming Twilio calls"""
|
| 126 |
twiml = """<?xml version="1.0" encoding="UTF-8"?>
|
| 127 |
<Response>
|
| 128 |
<Say>Hello! Thank you for calling. How can I help you today?</Say>
|
| 129 |
+
<Gather input="speech" action="https://huggingface.co/spaces/iajitpanday/vBot-1.5/api/predict" method="POST" speechTimeout="auto" speechModel="phone_call"/>
|
| 130 |
<Say>I didn't hear anything. Please call back when you're ready.</Say>
|
| 131 |
</Response>
|
| 132 |
"""
|
| 133 |
return twiml
|
| 134 |
|
| 135 |
+
def twilio_speech_handler(data):
|
| 136 |
"""Process speech from Twilio"""
|
| 137 |
+
# Extract speech from Twilio request
|
| 138 |
+
speech_result = data.get("SpeechResult", "")
|
| 139 |
+
|
| 140 |
+
# Process the speech
|
| 141 |
intent, _ = classify_intent(speech_result)
|
| 142 |
response = generate_response(speech_result, intent)
|
| 143 |
|
|
|
|
| 146 |
<Say>{response}</Say>
|
| 147 |
<Pause length="1"/>
|
| 148 |
<Say>Is there anything else I can help you with today?</Say>
|
| 149 |
+
<Gather input="speech" action="https://huggingface.co/spaces/iajitpanday/vBot-1.5/api/predict" method="POST" speechTimeout="auto" speechModel="phone_call"/>
|
| 150 |
<Say>Thank you for calling. Have a great day!</Say>
|
| 151 |
</Response>
|
| 152 |
"""
|
| 153 |
return twiml
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
# Create Gradio interface
|
| 156 |
with gr.Blocks(title="Call Assistant System") as demo:
|
| 157 |
gr.Markdown("# Call Assistant System")
|
|
|
|
| 213 |
gr.Markdown("""
|
| 214 |
## Twilio Integration Instructions
|
| 215 |
|
| 216 |
+
This app provides an API endpoint for Twilio voice integration. Follow these steps to set up:
|
| 217 |
|
| 218 |
1. Log into your Twilio account
|
| 219 |
2. Go to Phone Numbers → Manage → Active numbers
|
| 220 |
3. Select your number (+19704064410)
|
| 221 |
+
4. For "A Call Comes In", create a TwiML bin with:
|
| 222 |
+
```xml
|
| 223 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 224 |
+
<Response>
|
| 225 |
+
<Say>Hello! Thank you for calling. How can I help you today?</Say>
|
| 226 |
+
<Gather input="speech" action="https://huggingface.co/spaces/iajitpanday/vBot-1.5/api/predict" method="POST" speechTimeout="auto" speechModel="phone_call"/>
|
| 227 |
+
<Say>I didn't hear anything. Please call back when you're ready.</Say>
|
| 228 |
+
</Response>
|
| 229 |
+
```
|
| 230 |
|
| 231 |
The system will:
|
| 232 |
+
- Process speech input through the /api/predict endpoint
|
|
|
|
| 233 |
- Generate responses using your knowledge base
|
| 234 |
- Handle follow-up questions
|
| 235 |
""")
|
| 236 |
|
| 237 |
+
# Define API prediction function for Twilio
|
| 238 |
+
def predict_api(data):
|
| 239 |
+
"""API endpoint for Twilio integration"""
|
| 240 |
+
# Check if this is a speech result from Twilio
|
| 241 |
+
if isinstance(data, dict) and "SpeechResult" in data:
|
| 242 |
+
return twilio_speech_handler(data)
|
| 243 |
+
|
| 244 |
+
# If it's a call initial request or other Twilio request
|
| 245 |
+
# Just return the call handler response
|
| 246 |
+
return twilio_call_handler(data)
|
|
|
|
|
|
|
| 247 |
|
| 248 |
+
# Launch the interface with API
|
| 249 |
demo.queue()
|
| 250 |
demo.launch()
|