Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,7 @@ from fastrtc import AsyncStreamHandler, Stream, wait_for_item
|
|
13 |
from pydantic import BaseModel
|
14 |
import uvicorn
|
15 |
|
16 |
-
# --- Import get_space
|
17 |
from gradio.utils import get_space
|
18 |
|
19 |
# --- Document processing and RAG libraries ---
|
@@ -37,7 +37,6 @@ current_dir = pathlib.Path(__file__).parent
|
|
37 |
# 1. Document Ingestion & RAG Pipeline Setup
|
38 |
# ====================================================
|
39 |
|
40 |
-
# Folder containing PDFs, Word docs, and text files (place this folder alongside app.py)
|
41 |
DOCS_FOLDER = current_dir / "docs"
|
42 |
|
43 |
def extract_text_from_pdf(file_path: pathlib.Path) -> str:
|
@@ -78,20 +77,17 @@ def split_text(text: str, max_length: int = 500, overlap: int = 100) -> List[str
|
|
78 |
start += max_length - overlap
|
79 |
return chunks
|
80 |
|
81 |
-
# Load and process documents
|
82 |
documents = load_documents(DOCS_FOLDER)
|
83 |
all_chunks = []
|
84 |
for doc in documents:
|
85 |
all_chunks.extend(split_text(doc))
|
86 |
|
87 |
-
# Compute embeddings and build FAISS index
|
88 |
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
89 |
chunk_embeddings = embedding_model.encode(all_chunks)
|
90 |
embedding_dim = chunk_embeddings.shape[1]
|
91 |
index = faiss.IndexFlatL2(embedding_dim)
|
92 |
index.add(np.array(chunk_embeddings))
|
93 |
|
94 |
-
# Setup a text-generation pipeline (using GPT-2 here as an example)
|
95 |
generator = pipeline("text-generation", model="gpt2", max_length=256)
|
96 |
|
97 |
def retrieve_context(query: str, k: int = 5) -> List[str]:
|
@@ -119,7 +115,6 @@ def generate_answer(query: str) -> str:
|
|
119 |
stt_model = whisper.load_model("base", device="cpu")
|
120 |
|
121 |
def speech_to_text(audio_array: np.ndarray, sample_rate: int = 16000) -> str:
|
122 |
-
# Convert int16 PCM to float32 normalized to [-1, 1]
|
123 |
audio_float = audio_array.astype(np.float32) / 32768.0
|
124 |
result = stt_model.transcribe(audio_float, fp16=False)
|
125 |
return result["text"]
|
@@ -197,9 +192,17 @@ class RAGVoiceHandler(AsyncStreamHandler):
|
|
197 |
# 4. Voice Streaming Setup & FastAPI Endpoints
|
198 |
# ====================================================
|
199 |
|
200 |
-
#
|
201 |
-
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
stream = Stream(
|
205 |
modality="audio",
|
|
|
13 |
from pydantic import BaseModel
|
14 |
import uvicorn
|
15 |
|
16 |
+
# --- Import get_space (optional) ---
|
17 |
from gradio.utils import get_space
|
18 |
|
19 |
# --- Document processing and RAG libraries ---
|
|
|
37 |
# 1. Document Ingestion & RAG Pipeline Setup
|
38 |
# ====================================================
|
39 |
|
|
|
40 |
DOCS_FOLDER = current_dir / "docs"
|
41 |
|
42 |
def extract_text_from_pdf(file_path: pathlib.Path) -> str:
|
|
|
77 |
start += max_length - overlap
|
78 |
return chunks
|
79 |
|
|
|
80 |
documents = load_documents(DOCS_FOLDER)
|
81 |
all_chunks = []
|
82 |
for doc in documents:
|
83 |
all_chunks.extend(split_text(doc))
|
84 |
|
|
|
85 |
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
|
86 |
chunk_embeddings = embedding_model.encode(all_chunks)
|
87 |
embedding_dim = chunk_embeddings.shape[1]
|
88 |
index = faiss.IndexFlatL2(embedding_dim)
|
89 |
index.add(np.array(chunk_embeddings))
|
90 |
|
|
|
91 |
generator = pipeline("text-generation", model="gpt2", max_length=256)
|
92 |
|
93 |
def retrieve_context(query: str, k: int = 5) -> List[str]:
|
|
|
115 |
stt_model = whisper.load_model("base", device="cpu")
|
116 |
|
117 |
def speech_to_text(audio_array: np.ndarray, sample_rate: int = 16000) -> str:
|
|
|
118 |
audio_float = audio_array.astype(np.float32) / 32768.0
|
119 |
result = stt_model.transcribe(audio_float, fp16=False)
|
120 |
return result["text"]
|
|
|
192 |
# 4. Voice Streaming Setup & FastAPI Endpoints
|
193 |
# ====================================================
|
194 |
|
195 |
+
# Supply a dummy (but valid) RTC configuration to satisfy fastrtc.
|
196 |
+
rtc_config = {
|
197 |
+
"iceServers": [
|
198 |
+
{"urls": "stun:stun.l.google.com:19302"},
|
199 |
+
{
|
200 |
+
"urls": "turn:turn.anyfirewall.com:443?transport=tcp",
|
201 |
+
"username": "webrtc",
|
202 |
+
"credential": "webrtc"
|
203 |
+
}
|
204 |
+
]
|
205 |
+
}
|
206 |
|
207 |
stream = Stream(
|
208 |
modality="audio",
|