Yashodeep2006 commited on
Commit
e01c89a
·
verified ·
1 Parent(s): a71f33b

Clear all Space files

Browse files
.dockerignore DELETED
@@ -1,11 +0,0 @@
1
- .venv
2
- .git
3
- .gitignore
4
- __pycache__
5
- *.pyc
6
- *.pyo
7
- *.pyd
8
- downloads/
9
- temp_uploads/
10
- .env
11
- .dockerignore
 
 
 
 
 
 
 
 
 
 
 
 
.env DELETED
@@ -1,2 +0,0 @@
1
- GROQ_API_KEY=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
2
- MISTRAL_API_KEY=your_mistral_api_key_here
 
 
 
.gitignore DELETED
@@ -1,11 +0,0 @@
1
- # Python-generated files
2
- __pycache__/
3
- *.py[oc]
4
- build/
5
- dist/
6
- wheels/
7
- *.egg-info
8
-
9
- # Virtual environments
10
- .venv
11
- .env
 
 
 
 
 
 
 
 
 
 
 
 
.python-version DELETED
@@ -1 +0,0 @@
1
- 3.12
 
 
Dockerfile DELETED
@@ -1,28 +0,0 @@
1
- # Use official lightweight Python image
2
- FROM python:3.12-slim
3
-
4
- # Install system dependencies (ffmpeg & ffprobe required for pydub audio processing)
5
- RUN apt-get update && apt-get install -y --no-install-recommends \
6
- ffmpeg \
7
- git \
8
- build-essential \
9
- && rm -rf /var/lib/apt/lists/*
10
-
11
- # Create non-root user required by Hugging Face Spaces (UID 1000)
12
- RUN useradd -m -u 1000 user
13
- USER user
14
- ENV PATH="/home/user/.local/bin:${PATH}"
15
- WORKDIR /home/user/app
16
-
17
- # Copy requirements file and install dependencies
18
- COPY --chown=user requirements.txt .
19
- RUN pip install --no-cache-dir -r requirements.txt
20
-
21
- # Copy application codebase
22
- COPY --chown=user . .
23
-
24
- # Hugging Face Spaces expects containers to serve on port 7860
25
- EXPOSE 7860
26
-
27
- # Run Streamlit on port 7860 listening on all interfaces
28
- CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md DELETED
@@ -1,8 +0,0 @@
1
- ---
2
- title: mp3-rag
3
- sdk: streamlit
4
- emoji: 💻
5
- colorFrom: gray
6
- colorTo: red
7
- sdk_version: 1.61.1
8
- ---
 
 
 
 
 
 
 
 
 
app.py DELETED
@@ -1,210 +0,0 @@
1
- import os
2
- import shutil
3
- import tempfile
4
- import streamlit as st
5
- from dotenv import load_dotenv
6
-
7
- from src.audio_downloader_and_processor import process_source_input
8
- from src.audio_transcriber import transcribe_chunked_audio
9
- from src.audio_translator import translate_chunked_audio
10
- from src.rag_pipeline import rag_engine
11
-
12
- # Load environment variables from .env file if available
13
- load_dotenv()
14
-
15
- st.set_page_config(
16
- page_title="MP3 to RAG Chatbot",
17
- page_icon="🎙️",
18
- layout="wide"
19
- )
20
-
21
- # -----------------------------------------------------------------------------
22
- # Session State Initialization
23
- # -----------------------------------------------------------------------------
24
- if "messages" not in st.session_state:
25
- st.session_state.messages = []
26
-
27
- if "rag_chain" not in st.session_state:
28
- st.session_state.rag_chain = None
29
-
30
- if "transcript" not in st.session_state:
31
- st.session_state.transcript = None
32
-
33
- if "file_processed" not in st.session_state:
34
- st.session_state.file_processed = False
35
-
36
- if "audio_filename" not in st.session_state:
37
- st.session_state.audio_filename = ""
38
-
39
-
40
- def reset_session():
41
- st.session_state.messages = []
42
- st.session_state.rag_chain = None
43
- st.session_state.transcript = None
44
- st.session_state.file_processed = False
45
- st.session_state.audio_filename = ""
46
-
47
-
48
- # -----------------------------------------------------------------------------
49
- # Sidebar Configuration
50
- # -----------------------------------------------------------------------------
51
- with st.sidebar:
52
- st.header("⚙️ Configuration")
53
-
54
- env_key = os.getenv("GROQ_API_KEY", "")
55
- groq_api_key = st.text_input(
56
- "Groq API Key",
57
- value=env_key,
58
- type="password",
59
- help="Get your API key from https://console.groq.com/"
60
- )
61
-
62
- st.subheader("🎧 Select Your Audio Language")
63
- mode = st.radio(
64
- "Select Mode:",
65
- ["Transcription (English Audio)", "Translation (Non-English Audio)"],
66
- index=0,
67
- help="Transcription keeps original audio language. Translation converts non-English audio directly into English using Groq Whisper."
68
- )
69
-
70
- st.markdown("---")
71
- if st.button("🗑️ Reset Session & Chat", use_container_width=True):
72
- reset_session()
73
- st.rerun()
74
-
75
- # -----------------------------------------------------------------------------
76
- # Main Application UI
77
- # -----------------------------------------------------------------------------
78
- st.title("🎙️ MP3 / Audio to RAG Chatbot")
79
- st.markdown(
80
- "Upload any MP3 or audio file to transcribe/translate it, index it into a vector store, "
81
- "and chat with an AI assistant about its content!"
82
- )
83
-
84
- # File Uploader
85
- uploaded_file = st.file_uploader(
86
- "Choose an audio file",
87
- type=["mp3", "wav", "m4a", "ogg", "flac", "aac"],
88
- help="Supported formats: MP3, WAV, M4A, OGG, FLAC, AAC"
89
- )
90
-
91
- if uploaded_file is not None:
92
- # Audio Preview
93
- st.audio(uploaded_file, format=f"audio/{uploaded_file.name.split('.')[-1]}")
94
-
95
- # Process Button
96
- if st.button("🚀 Process Audio & Build RAG Index", type="primary", use_container_width=True):
97
- if not groq_api_key.strip():
98
- st.error("⚠️ Please provide a valid Groq API Key in the sidebar before proceeding.")
99
- else:
100
- reset_session()
101
- st.session_state.audio_filename = uploaded_file.name
102
-
103
- temp_dir = tempfile.mkdtemp(prefix="mp3_rag_")
104
- download_dir = os.path.join(temp_dir, "downloads")
105
-
106
- try:
107
- with st.status("Processing Audio Pipeline...", expanded=True) as status:
108
- # Step 1: Save uploaded file
109
- status.update(label="Step 1/4: Saving uploaded audio file...")
110
- input_path = os.path.join(temp_dir, uploaded_file.name)
111
- with open(input_path, "wb") as f:
112
- f.write(uploaded_file.getbuffer())
113
-
114
- # Step 2: Convert and Chunk
115
- CHUNK_MINUTES = 10
116
- status.update(label=f"Step 2/4: Downsampling & chunking audio into {CHUNK_MINUTES}-min segments...")
117
- chunks = process_source_input(
118
- source=input_path,
119
- download_dir=download_dir,
120
- chunk_minutes=CHUNK_MINUTES
121
- )
122
- st.write(f"✔️ Created {len(chunks)} audio chunk(s).")
123
-
124
- # Step 3: Transcribe or Translate
125
- if "Transcription" in mode:
126
- status.update(label="Step 3/4: Transcribing audio chunks via Groq Whisper...")
127
- transcript_text = transcribe_chunked_audio(chunks, groq_api_key)
128
- else:
129
- status.update(label="Step 3/4: Translating audio chunks to English via Groq Whisper...")
130
- transcript_text = translate_chunked_audio(chunks, groq_api_key)
131
-
132
- st.write("✔️ Audio speech processing completed.")
133
-
134
- # Step 4: Build RAG Pipeline
135
- status.update(label="Step 4/4: Generating embeddings & building Vector Store RAG chain...")
136
- rag_chain = rag_engine(transcript_text, groq_api_key)
137
-
138
- st.session_state.transcript = transcript_text
139
- st.session_state.rag_chain = rag_chain
140
- st.session_state.file_processed = True
141
-
142
- status.update(label="🎉 RAG Pipeline Ready! You can now ask questions below.", state="complete")
143
-
144
- except Exception as e:
145
- st.error(f"❌ Error during processing: {str(e)}")
146
- finally:
147
- # Clean up temporary upload files
148
- if os.path.exists(temp_dir):
149
- shutil.rmtree(temp_dir, ignore_errors=True)
150
-
151
- # -----------------------------------------------------------------------------
152
- # Results Section & RAG Chat
153
- # -----------------------------------------------------------------------------
154
- if st.session_state.file_processed and st.session_state.transcript:
155
- st.markdown("---")
156
- st.subheader(f"📄 Generated Transcript (`{st.session_state.audio_filename}`)")
157
-
158
- with st.expander("Click to view full transcript", expanded=False):
159
- st.write(st.session_state.transcript)
160
-
161
- st.download_button(
162
- label="📥 Download Transcript as TXT",
163
- data=st.session_state.transcript,
164
- file_name=f"{os.path.splitext(st.session_state.audio_filename)[0]}_transcript.txt",
165
- mime="text/plain"
166
- )
167
-
168
- st.markdown("---")
169
- st.subheader("💬 Chat with Audio Content")
170
-
171
- # Render Chat History
172
- for msg in st.session_state.messages:
173
- with st.chat_message(msg["role"]):
174
- st.markdown(msg["content"])
175
- if "context" in msg and msg["context"]:
176
- with st.expander("🔍 View Retrieved Context Snippets"):
177
- for idx, doc in enumerate(msg["context"], 1):
178
- st.markdown(f"**Chunk {idx}:**\n> {doc.page_content}")
179
-
180
- # Chat Input
181
- if user_query := st.chat_input("Ask a question about the audio..."):
182
- # Display User Message
183
- st.session_state.messages.append({"role": "user", "content": user_query})
184
- with st.chat_message("user"):
185
- st.markdown(user_query)
186
-
187
- # Generate Answer
188
- with st.chat_message("assistant"):
189
- with st.spinner("Searching transcript & generating answer..."):
190
- try:
191
- response = st.session_state.rag_chain.invoke({"input": user_query})
192
- answer = response.get("answer", "No answer generated.")
193
- context_docs = response.get("context", [])
194
-
195
- st.markdown(answer)
196
-
197
- if context_docs:
198
- with st.expander("🔍 View Retrieved Context Snippets"):
199
- for idx, doc in enumerate(context_docs, 1):
200
- st.markdown(f"**Chunk {idx}:**\n> {doc.page_content}")
201
-
202
- st.session_state.messages.append({
203
- "role": "assistant",
204
- "content": answer,
205
- "context": context_docs
206
- })
207
- except Exception as e:
208
- error_msg = f"❌ Error querying RAG chain: {str(e)}"
209
- st.error(error_msg)
210
- st.session_state.messages.append({"role": "assistant", "content": error_msg})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
packages.txt DELETED
@@ -1 +0,0 @@
1
- ffmpeg
 
 
pyproject.toml DELETED
@@ -1,33 +0,0 @@
1
- [project]
2
- name = "mp3-to-rag"
3
- version = "0.1.0"
4
- description = "Add your description here"
5
- readme = "README.md"
6
- requires-python = ">=3.12"
7
- dependencies = [
8
- "fastapi>=0.141.1",
9
- "fpdf2>=2.8.8",
10
- "groq>=0.37.1",
11
- "langchain>=1.3.15",
12
- "langchain-community>=0.4.2",
13
- "langchain-core>=1.5.6",
14
- "langchain-groq>=1.1.3",
15
- "langchain-huggingface>=1.2.2",
16
- "langchain-mistralai>=1.1.6",
17
- "langchain-text-splitters>=1.1.2",
18
- "mistralai>=2.9.3",
19
- "numpy>=2.5.2",
20
- "pydub>=0.25.1",
21
- "python-dotenv>=1.2.3",
22
- "python-multipart>=0.0.32",
23
- "reportlab>=5.0.0",
24
- "requests>=2.34.2",
25
- "sentence-transformers>=6.0.0",
26
- "streamlit>=1.61.1",
27
- "streamlit-extras>=1.6.0",
28
- "tiktoken>=0.14.0",
29
- "torchvision>=0.28.0",
30
- "tqdm>=4.70.0",
31
- "uvicorn>=0.52.4",
32
- "watchdog>=6.0.0",
33
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt DELETED
@@ -1,27 +0,0 @@
1
- pydub
2
- torchvision
3
-
4
- langchain
5
- langchain-text-splitters
6
- langchain-core
7
- langchain-community
8
- langchain-mistralai
9
- langchain-groq
10
- mistralai
11
- groq
12
- sentence-transformers
13
- langchain-huggingface
14
- tiktoken
15
- streamlit
16
- streamlit-extras
17
- watchdog
18
- reportlab
19
- fpdf2
20
- python-dotenv
21
- numpy
22
- tqdm
23
- requests
24
- fastapi
25
- uvicorn
26
- python-multipart
27
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/__init__.py DELETED
File without changes
src/__pycache__/__init__.cpython-311.pyc DELETED
Binary file (167 Bytes)
 
src/__pycache__/__init__.cpython-312.pyc DELETED
Binary file (155 Bytes)
 
src/__pycache__/audio_downloader_and_processor.cpython-312.pyc DELETED
Binary file (3.4 kB)
 
src/__pycache__/audio_transcriber.cpython-312.pyc DELETED
Binary file (2.07 kB)
 
src/__pycache__/audio_translator.cpython-312.pyc DELETED
Binary file (2.14 kB)
 
src/__pycache__/rag_pipeline.cpython-311.pyc DELETED
Binary file (3.24 kB)
 
src/__pycache__/rag_pipeline.cpython-312.pyc DELETED
Binary file (3.06 kB)
 
src/__pycache__/transcript_summarizer.cpython-312.pyc DELETED
Binary file (4.8 kB)
 
src/audio_downloader_and_processor.py DELETED
@@ -1,60 +0,0 @@
1
- from pydub import AudioSegment
2
- import os
3
-
4
-
5
- def _ensure_dir(path: str) -> None:
6
- os.makedirs(path, exist_ok=True)
7
-
8
-
9
- def convert_to_mp3(input_path: str, download_dir: str) -> str:
10
- """Convert any audio/video file to MP3 format using pydub."""
11
- _ensure_dir(download_dir)
12
- base_name = os.path.splitext(os.path.basename(input_path))[0]
13
- output_path = os.path.join(download_dir, f"{base_name}_converted.mp3")
14
-
15
- audio = AudioSegment.from_file(input_path)
16
- # Downsampling helps keep file size low without losing transcription accuracy
17
- audio = audio.set_channels(1).set_frame_rate(16000)
18
- audio.export(output_path, format="mp3", bitrate="128k")
19
-
20
- return output_path
21
-
22
-
23
- def chunk_audio(audio_path: str, chunk_minutes: int = 10, download_dir: str = "downloads") -> list:
24
- """Chunks the MP3 file into smaller segments for Groq."""
25
- _ensure_dir(download_dir)
26
- audio = AudioSegment.from_file(audio_path)
27
- chunk_ms = chunk_minutes * 60 * 1000
28
- base_name = os.path.splitext(os.path.basename(audio_path))[0]
29
-
30
- chunks = []
31
-
32
- for i, start in enumerate(range(0, len(audio), chunk_ms)):
33
- chunk = audio[start: start + chunk_ms]
34
- chunk_path = os.path.join(download_dir, f"{base_name}_chunk_{i}.mp3")
35
- chunk.export(chunk_path, format="mp3", bitrate="128k")
36
- chunks.append(chunk_path)
37
-
38
- return chunks
39
-
40
-
41
- def process_source_input(source: str, download_dir: str = "downloads", chunk_minutes: int = 10) -> list:
42
-
43
- print("\n\n[INFO] Detected local file. Converting to MP3...")
44
- audio_path = convert_to_mp3(source, download_dir)
45
-
46
- print("\n\n[INFO] Chunking audio...")
47
-
48
- chunks = chunk_audio(audio_path, chunk_minutes=chunk_minutes, download_dir=download_dir)
49
- print(f"\n✅✅ [INFO] Audio ready — {len(chunks)} chunk(s) created.")
50
-
51
- # Cleanup the original long file to save space
52
- if "_converted.mp3" in audio_path:
53
- try:
54
- os.remove(audio_path)
55
- print(f"\n❌ [INFO] Removed source audio: {audio_path}\n")
56
-
57
- except FileNotFoundError:
58
- pass
59
-
60
- return chunks
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/audio_transcriber.py DELETED
@@ -1,42 +0,0 @@
1
- from groq import Groq
2
- import os
3
-
4
-
5
- def transcribe_chunked_audio(chunk_paths: list, groq_api_key: str) -> str:
6
- """
7
- Transcribes audio chunks using Groq's whisper-large-v3-turbo.
8
- """
9
- client = Groq(
10
- api_key=groq_api_key,
11
- timeout=60.0
12
- )
13
- full_transcript = []
14
- print(f"\n[INFO] Starting Groq cloud transcription...")
15
-
16
- for i, path in enumerate(chunk_paths):
17
- file_name = os.path.basename(path) # Extract just 'chunk_0.mp3'
18
- print(f"Sending Chunk {i+1}/{len(chunk_paths)} to Groq: {file_name}")
19
-
20
- try:
21
- with open(path, "rb") as file:
22
- transcription = client.audio.transcriptions.create(
23
- # Use the base file_name here
24
- file=(file_name, file.read()),
25
- model="whisper-large-v3-turbo",
26
- response_format="verbose_json",
27
- )
28
-
29
- if transcription.text:
30
- full_transcript.append(transcription.text)
31
-
32
- except Exception as e:
33
- print(f"❌ Error transcribing {file_name}: {e}")
34
-
35
- continue
36
-
37
- result = " ".join(full_transcript)
38
-
39
- with open("transcript_original.txt", "w", encoding="utf-8") as f:
40
- f.write(result)
41
-
42
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/audio_translator.py DELETED
@@ -1,42 +0,0 @@
1
- from groq import Groq
2
- import os
3
-
4
-
5
- def translate_chunked_audio(chunk_paths: list, groq_api_key: str) -> str:
6
- """
7
- Translates audio chunks directly to English using Groq's whisper-large-v3.
8
- Note: The 'translations' endpoint always outputs English.
9
- """
10
- client = Groq(
11
- api_key=groq_api_key,
12
- timeout=60.0
13
- )
14
- full_translation = []
15
- print(f"\n[INFO] Starting Groq cloud translation (Audio -> English)...")
16
-
17
- for i, path in enumerate(chunk_paths):
18
- file_name = os.path.basename(path)
19
- print(f"Translating Chunk {i+1}/{len(chunk_paths)}: {file_name}")
20
-
21
- try:
22
- with open(path, "rb") as file:
23
-
24
- translation = client.audio.translations.create(
25
- file=(file_name, file.read()),
26
- model="whisper-large-v3",
27
- response_format="verbose_json",
28
- )
29
-
30
- if translation.text:
31
- full_translation.append(translation.text)
32
-
33
- except Exception as e:
34
- print(f"❌ Error translating {file_name}: {e}")
35
- continue
36
-
37
- result = " ".join(full_translation)
38
-
39
- with open("translation_english.txt", "w", encoding="utf-8") as f:
40
- f.write(result)
41
-
42
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/rag_pipeline.py DELETED
@@ -1,75 +0,0 @@
1
- try:
2
- from langchain.chains.combine_documents import create_stuff_documents_chain
3
- from langchain.chains import create_retrieval_chain
4
- except ImportError:
5
- from langchain_classic.chains.combine_documents import create_stuff_documents_chain
6
- from langchain_classic.chains import create_retrieval_chain
7
-
8
- from langchain_text_splitters import RecursiveCharacterTextSplitter
9
- from langchain_huggingface.embeddings import HuggingFaceEmbeddings
10
- from langchain_core.vectorstores import InMemoryVectorStore
11
- from langchain_community.document_loaders import TextLoader
12
- from langchain_core.prompts import ChatPromptTemplate
13
- from langchain_groq import ChatGroq
14
- from langchain_core.documents import Document
15
- import torch
16
- import os
17
-
18
-
19
- def _set_groq_api_key(groq_api_key: str) -> None:
20
- os.environ["GROQ_API_KEY"] = groq_api_key
21
-
22
-
23
- def rag_engine(transcript: str, groq_api_key: str):
24
- _set_groq_api_key(groq_api_key)
25
- embeddings = HuggingFaceEmbeddings(
26
- model_name="sentence-transformers/all-MiniLM-L6-v2",
27
- model_kwargs={"device": "cuda"} if torch.cuda.is_available() else {
28
- "device": "cpu"}
29
- )
30
-
31
- # loader = TextLoader(transcript)
32
- # documents = loader.load()
33
-
34
- documents = [Document(page_content=transcript)]
35
-
36
- text_splitter = RecursiveCharacterTextSplitter(
37
- chunk_size=1200, chunk_overlap=100)
38
- splitted_docs = text_splitter.split_documents(documents)
39
-
40
- vectorstore = InMemoryVectorStore.from_documents(splitted_docs, embeddings)
41
-
42
- retriever = vectorstore.as_retriever(
43
- search_type="mmr",
44
- search_kwargs={
45
- "k": 4,
46
-
47
- "fetch_k": 10,
48
-
49
- "lambda_mult": 0.5
50
- }
51
- )
52
-
53
- llm = ChatGroq(model="openai/gpt-oss-20b", temperature=0.2)
54
-
55
- # This chain expects {context} and {input}
56
- prompt = ChatPromptTemplate.from_template("""
57
- You are an Expert AI Audio Assistant. Answer the user's question based ONLY on the audio transcript provided below for the context.
58
-
59
- If the answer is not found in the context, say:
60
- "I could not find this information in the audio."
61
- DONT make up answers.
62
-
63
- Always be concise and precise. If quoting someone, mention it clearly.
64
-
65
- <context>
66
- {context}
67
- </context>
68
-
69
- Question: {input}
70
- """)
71
-
72
- document_chain = create_stuff_documents_chain(llm, prompt)
73
- rag_chain = create_retrieval_chain(retriever, document_chain)
74
-
75
- return rag_chain
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/transcript_summarizer.py DELETED
@@ -1,117 +0,0 @@
1
- import os
2
- from langchain_mistralai import ChatMistralAI
3
- from langchain_core.prompts import ChatPromptTemplate
4
- from langchain_core.output_parsers import StrOutputParser
5
- from langchain_text_splitters import RecursiveCharacterTextSplitter
6
-
7
-
8
- def split_transcript(transcript: str) -> list:
9
- splitter = RecursiveCharacterTextSplitter(
10
- chunk_size=5000,
11
- chunk_overlap=200
12
- )
13
- return splitter.split_text(transcript)
14
-
15
-
16
- def _set_mistral_api_key(mistral_api_key: str) -> None:
17
- os.environ["MISTRAL_API_KEY"] = mistral_api_key
18
-
19
-
20
- def build_chain(system_prompt: str, mistral_api_key: str):
21
- _set_mistral_api_key(mistral_api_key)
22
- llm = ChatMistralAI(model_name="mistral-small-latest", temperature=0.2)
23
-
24
- prompt = ChatPromptTemplate.from_messages([
25
- ("system", system_prompt),
26
- ("human", "{text}"),
27
- ])
28
-
29
- return prompt | llm | StrOutputParser()
30
-
31
-
32
- def extract_action_items(transcript: str, mistral_api_key: str) -> str:
33
- chain = build_chain(
34
- "You are an expert meeting analyst. Extract all action items from the transcript. "
35
- "For each provide:\n- Task description\n- Owner (who is responsible)\n- Deadline (if mentioned)\n"
36
- "Format as a numbered list. If none found, say 'No action items found.'",
37
- mistral_api_key
38
- )
39
-
40
- result = chain.invoke({"text": transcript})
41
-
42
- return result
43
-
44
-
45
- def extract_key_decisions(transcript: str, mistral_api_key: str) -> str:
46
- chain = build_chain(
47
- "You are an expert meeting analyst. Extract all key decisions made. "
48
- "Format as a numbered list. If none found, say 'No key decisions found.'",
49
- mistral_api_key
50
- )
51
- result = chain.invoke({"text": transcript})
52
-
53
- return result
54
-
55
-
56
- def extract_questions(transcript: str, mistral_api_key: str) -> str:
57
- chain = build_chain(
58
- "Extract all unresolved questions or topics needing follow-up from the transcript. "
59
- "Format as a numbered list. If none found, say 'No open questions found.'"
60
- "DONT make up questions - only extract those that are explicitly mentioned as open or needing follow-up.",
61
- mistral_api_key
62
- )
63
- result = chain.invoke({"text": transcript})
64
-
65
- return result
66
-
67
-
68
- # main summarization function that uses the above helper functions
69
- def summarize_transcript(transcript: str, mistral_api_key: str) -> dict:
70
- _set_mistral_api_key(mistral_api_key)
71
- llm = ChatMistralAI(model_name="mistral-small-latest", temperature=0.2)
72
-
73
- # 1. Summarize individual chunks
74
- map_prompt = ChatPromptTemplate.from_template(
75
- "Summarize this part of a video transcript concisely:\n\n{text}"
76
- )
77
- map_chain = map_prompt | llm | StrOutputParser()
78
-
79
- chunks = split_transcript(transcript)
80
- print(f"[INFO] Summarizing {len(chunks)} chunks...")
81
-
82
- chunk_summaries = [map_chain.invoke({"text": chunk}) for chunk in chunks]
83
-
84
- # 2. Combine partial summaries into one text
85
- combined_partial_summaries = "\n\n".join(chunk_summaries)
86
-
87
- # 3. Combine into final bullet points
88
- final_prompt = ChatPromptTemplate.from_template(
89
- """
90
- You are a professional video summarizer.
91
- Combine these partial summaries into one final professional video summary using bullet points and give suitable title.
92
-
93
- Summaries:
94
- {text}
95
- """
96
- )
97
-
98
- final_chain = final_prompt | llm | StrOutputParser()
99
-
100
- # 4. Full summary
101
- full_summary = final_chain.invoke(
102
- {"text": combined_partial_summaries})
103
-
104
- with open("full_summary.txt", "w", encoding="utf-8") as f:
105
- f.write(full_summary)
106
-
107
- # 5. Extract actionable items, key decisions, and open questions from the combined partial summaries
108
- actionable_items = extract_action_items(combined_partial_summaries, mistral_api_key)
109
- key_decisions = extract_key_decisions(combined_partial_summaries, mistral_api_key)
110
- open_questions = extract_questions(combined_partial_summaries, mistral_api_key)
111
-
112
- return {
113
- "summary": full_summary,
114
- "action_items": actionable_items,
115
- "key_decisions": key_decisions,
116
- "open_questions": open_questions
117
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
translation_english.txt DELETED
@@ -1 +0,0 @@
1
- Music Thank you I have given my heart I will give my life Music Hindu Muslim Sikh Christian we are the nation we are the name of the nation Hindu Muslim Sikh Christian We are the name of the nation Those who do something different Are not accused of religion We will live and die Thank you. Ah I love you. We are looting our homes We play without knowing our own blood We will live or we will die This is our country for you We have given our hearts to you Thank you.
 
 
uv.lock DELETED
The diff for this file is too large to render. See raw diff