Update app.py
Browse files
app.py
CHANGED
@@ -13,6 +13,9 @@ from fastrtc import AsyncStreamHandler, Stream, get_twilio_turn_credentials, wai
|
|
13 |
from pydantic import BaseModel
|
14 |
import uvicorn
|
15 |
|
|
|
|
|
|
|
16 |
# --- Document processing and RAG libraries ---
|
17 |
import PyPDF2
|
18 |
import docx
|
@@ -195,15 +198,18 @@ class RAGVoiceHandler(AsyncStreamHandler):
|
|
195 |
self.quit.set()
|
196 |
|
197 |
# ====================================================
|
198 |
-
# 4.
|
199 |
# ====================================================
|
200 |
|
201 |
-
#
|
|
|
|
|
|
|
202 |
stream = Stream(
|
203 |
modality="audio",
|
204 |
mode="send-receive",
|
205 |
handler=RAGVoiceHandler(),
|
206 |
-
rtc_configuration=
|
207 |
concurrency_limit=5,
|
208 |
time_limit=90,
|
209 |
)
|
@@ -220,18 +226,17 @@ async def input_hook(body: InputData):
|
|
220 |
stream.set_input(body.webrtc_id)
|
221 |
return {"status": "ok"}
|
222 |
|
223 |
-
# Endpoint to handle WebRTC offer from the client (
|
224 |
@app.post("/webrtc/offer")
|
225 |
async def webrtc_offer(offer: dict):
|
226 |
# This uses fastrtc's built-in handling of the offer to set up the connection.
|
227 |
return await stream.handle_offer(offer)
|
228 |
|
229 |
-
# Serve your existing HTML file (which contains your
|
230 |
@app.get("/")
|
231 |
async def index():
|
232 |
index_path = current_dir / "index.html"
|
233 |
html_content = index_path.read_text()
|
234 |
-
# If needed, replace any placeholders (for example, RTC configuration)
|
235 |
return HTMLResponse(content=html_content)
|
236 |
|
237 |
# ====================================================
|
@@ -248,7 +253,7 @@ if __name__ == "__main__":
|
|
248 |
iface = gr.Interface(fn=gradio_chat, inputs="text", outputs="text", title="Customer Support Chatbot")
|
249 |
iface.launch(server_port=7860)
|
250 |
elif mode == "PHONE":
|
251 |
-
# Run the FastAPI app so that callers can use the
|
252 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
253 |
else:
|
254 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
13 |
from pydantic import BaseModel
|
14 |
import uvicorn
|
15 |
|
16 |
+
# --- Import get_space to detect Hugging Face Spaces ---
|
17 |
+
from gradio.utils import get_space
|
18 |
+
|
19 |
# --- Document processing and RAG libraries ---
|
20 |
import PyPDF2
|
21 |
import docx
|
|
|
198 |
self.quit.set()
|
199 |
|
200 |
# ====================================================
|
201 |
+
# 4. Voice Streaming Setup & FastAPI Endpoints
|
202 |
# ====================================================
|
203 |
|
204 |
+
# When running on Hugging Face Spaces, we bypass the Twilio TURN credentials by setting rtc_configuration to None.
|
205 |
+
rtc_config = get_twilio_turn_credentials() if not get_space() else None
|
206 |
+
|
207 |
+
# Create a Stream instance using our RAGVoiceHandler.
|
208 |
stream = Stream(
|
209 |
modality="audio",
|
210 |
mode="send-receive",
|
211 |
handler=RAGVoiceHandler(),
|
212 |
+
rtc_configuration=rtc_config,
|
213 |
concurrency_limit=5,
|
214 |
time_limit=90,
|
215 |
)
|
|
|
226 |
stream.set_input(body.webrtc_id)
|
227 |
return {"status": "ok"}
|
228 |
|
229 |
+
# Endpoint to handle WebRTC offer from the client (for voice calls)
|
230 |
@app.post("/webrtc/offer")
|
231 |
async def webrtc_offer(offer: dict):
|
232 |
# This uses fastrtc's built-in handling of the offer to set up the connection.
|
233 |
return await stream.handle_offer(offer)
|
234 |
|
235 |
+
# Serve your existing HTML file (which contains your voice UI)
|
236 |
@app.get("/")
|
237 |
async def index():
|
238 |
index_path = current_dir / "index.html"
|
239 |
html_content = index_path.read_text()
|
|
|
240 |
return HTMLResponse(content=html_content)
|
241 |
|
242 |
# ====================================================
|
|
|
253 |
iface = gr.Interface(fn=gradio_chat, inputs="text", outputs="text", title="Customer Support Chatbot")
|
254 |
iface.launch(server_port=7860)
|
255 |
elif mode == "PHONE":
|
256 |
+
# Run the FastAPI app so that callers can use the voice functionality.
|
257 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
258 |
else:
|
259 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|