Spaces:
Sleeping
Sleeping
samir72
commited on
Commit
·
9ca3a61
1
Parent(s):
7fbbe98
Youtube feature
Browse files
Youtubetranscription_summarizer.py
CHANGED
|
@@ -191,13 +191,16 @@ def download_youtube_audio_wav16k_api(
|
|
| 191 |
|
| 192 |
|
| 193 |
def transcribe_faster_whisper(wav_path:str, model_name="base.en"):
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
def summarize_with_phi(transcript_segments, sysprompt, userprompt, phi_client):
|
| 203 |
# map-reduce pseudo:
|
|
|
|
| 191 |
|
| 192 |
|
| 193 |
def transcribe_faster_whisper(wav_path:str, model_name="base.en"):
|
| 194 |
+
try:
|
| 195 |
+
model = WhisperModel(model_name)
|
| 196 |
+
segments, info = model.transcribe(wav_path, beam_size=1, vad_filter=True)
|
| 197 |
+
out = []
|
| 198 |
+
for s in segments:
|
| 199 |
+
out.append({"start": s.start, "end": s.end, "text": s.text})
|
| 200 |
+
#return {"language": info.language, "segments": out}
|
| 201 |
+
return {"segments": out}
|
| 202 |
+
except Exception as e:
|
| 203 |
+
return f"Faster-Whisper transcription failed: {e}"
|
| 204 |
|
| 205 |
def summarize_with_phi(transcript_segments, sysprompt, userprompt, phi_client):
|
| 206 |
# map-reduce pseudo:
|
__pycache__/Youtubetranscription_summarizer.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/Youtubetranscription_summarizer.cpython-313.pyc and b/__pycache__/Youtubetranscription_summarizer.cpython-313.pyc differ
|
|
|
__pycache__/app.cpython-313.pyc
ADDED
|
Binary file (14.1 kB). View file
|
|
|
app.py
CHANGED
|
@@ -9,6 +9,11 @@ from openai import AzureOpenAI # official OpenAI SDK, works with Azure endpoint
|
|
| 9 |
import json
|
| 10 |
import subprocess
|
| 11 |
import Youtubetranscription_summarizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
import re
|
| 13 |
|
| 14 |
# --- LLM call (Azure OpenAI with API key) -----------------------------------
|
|
@@ -24,6 +29,7 @@ def summarize_input(audio_b64: str = None, text_input: str = None, sys_prompt: s
|
|
| 24 |
deployment = os.getenv("AC_MODEL_DEPLOYMENT")
|
| 25 |
api_version = os.getenv("AC_OPENAI_API_VERSION")
|
| 26 |
|
|
|
|
| 27 |
if not endpoint or not api_key or not deployment:
|
| 28 |
return "Server misconfiguration: required env vars missing."
|
| 29 |
# Reset json_text for logging
|
|
@@ -84,7 +90,7 @@ def summarize_input(audio_b64: str = None, text_input: str = None, sys_prompt: s
|
|
| 84 |
)
|
| 85 |
Enddate = datetime.now()
|
| 86 |
Callduration = Enddate - Starttime[0]
|
| 87 |
-
print(f"
|
| 88 |
f"audio_size={len(audio_b64 or '')}, text_input_size={len(json_text or '')}")
|
| 89 |
return response.choices[0].message.content
|
| 90 |
|
|
@@ -129,16 +135,90 @@ def download_to_temp_mp3(url: str) -> str:
|
|
| 129 |
tmp.write(chunk)
|
| 130 |
return tmp.name
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
|
|
|
| 133 |
def process_audio(upload_path, record_path, url, sys_prompt, user_prompt):
|
| 134 |
tmp_to_cleanup = []
|
| 135 |
audio_b64 = None
|
| 136 |
text_input = None
|
| 137 |
domaincheck = None
|
|
|
|
|
|
|
|
|
|
| 138 |
try:
|
| 139 |
# Capture start time for logging
|
| 140 |
Starttime = datetime.now(),
|
| 141 |
-
print(f"
|
| 142 |
audio_path = None
|
| 143 |
if upload_path:
|
| 144 |
audio_path = upload_path
|
|
@@ -158,7 +238,15 @@ def process_audio(upload_path, record_path, url, sys_prompt, user_prompt):
|
|
| 158 |
|
| 159 |
if CheckURL:
|
| 160 |
# Get the transcription from youtube
|
| 161 |
-
text_input = Youtubetranscription_summarizer.main(url.strip()) # Youtube files are transcribed and summarized
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
tmp_to_cleanup.append(text_input)
|
| 163 |
else:
|
| 164 |
audio_path = download_to_temp_mp3(url.strip())
|
|
|
|
| 9 |
import json
|
| 10 |
import subprocess
|
| 11 |
import Youtubetranscription_summarizer
|
| 12 |
+
from app.app.Youtubeextraction import extract # Youtube download helper functions
|
| 13 |
+
#from pydantic import BaseModel, AnyUrl # Pydantic models for request validation in yiutube extraction
|
| 14 |
+
#from fastapi import FastAPI, HTTPException # FastAPI for building the API
|
| 15 |
+
#app = FastAPI() ## Initialize FastAPI app for testing in local
|
| 16 |
+
#from extractor.app.storage import upload_and_sign # Youtube storage helper functions
|
| 17 |
import re
|
| 18 |
|
| 19 |
# --- LLM call (Azure OpenAI with API key) -----------------------------------
|
|
|
|
| 29 |
deployment = os.getenv("AC_MODEL_DEPLOYMENT")
|
| 30 |
api_version = os.getenv("AC_OPENAI_API_VERSION")
|
| 31 |
|
| 32 |
+
|
| 33 |
if not endpoint or not api_key or not deployment:
|
| 34 |
return "Server misconfiguration: required env vars missing."
|
| 35 |
# Reset json_text for logging
|
|
|
|
| 90 |
)
|
| 91 |
Enddate = datetime.now()
|
| 92 |
Callduration = Enddate - Starttime[0]
|
| 93 |
+
print(f"AudioChatSummarizer API call with a duration of {Callduration}: prompt_length={len(user_prompt or '')}, "
|
| 94 |
f"audio_size={len(audio_b64 or '')}, text_input_size={len(json_text or '')}")
|
| 95 |
return response.choices[0].message.content
|
| 96 |
|
|
|
|
| 135 |
tmp.write(chunk)
|
| 136 |
return tmp.name
|
| 137 |
|
| 138 |
+
# function to read files
|
| 139 |
+
def file_read(filepath):
|
| 140 |
+
file_data = []
|
| 141 |
+
|
| 142 |
+
try:
|
| 143 |
+
with open(filepath, "rb") as f:
|
| 144 |
+
file_data = f.read()
|
| 145 |
+
|
| 146 |
+
print(f"Successfully validated {file_path} and read {len(file_data)} bytes.")
|
| 147 |
+
except Exception as e:
|
| 148 |
+
print(f"Could not read {file_path}: {e}")
|
| 149 |
+
|
| 150 |
+
return file_data
|
| 151 |
+
|
| 152 |
+
###Download youtube video and extract audio using yt-dlp and ffmpeg
|
| 153 |
+
#### Fixing code to resolve 404 error
|
| 154 |
+
|
| 155 |
+
def fetch_audio_from_youtube(youtube_url: str) -> str:
|
| 156 |
+
"""
|
| 157 |
+
Calls the extractor service and returns the signed audio URL.
|
| 158 |
+
- Tries POST /extract with youtube_url as a query param (your current server shape).
|
| 159 |
+
- Falls back to sending youtube_url in JSON body if needed.
|
| 160 |
+
- Accepts either JSON {"audio_url": "..."} or a plain string URL.
|
| 161 |
+
"""
|
| 162 |
+
EXTRACT_API = os.getenv("AZURE_CONTAINER_APP_FQDN") ## Fast API endpoint for youtube extraction "https://<your-app-fqdn>/extract"
|
| 163 |
+
base = EXTRACT_API.rstrip("/")
|
| 164 |
+
endpoint = base if base.endswith("/extract") else f"{base}/extract"
|
| 165 |
+
|
| 166 |
+
payload = {"format": "wav", "sample_rate": 16000, "mono": True}
|
| 167 |
+
timeout = 90
|
| 168 |
+
|
| 169 |
+
try:
|
| 170 |
+
# 1) Preferred: youtube_url as QUERY PARAM (matches your current API)
|
| 171 |
+
r = requests.post(endpoint, params={"youtube_url": youtube_url},
|
| 172 |
+
json=payload, timeout=timeout)
|
| 173 |
+
if r.status_code == 404 or r.status_code == 422:
|
| 174 |
+
# 2) Fallback: youtube_url in JSON body (if your API switches later)
|
| 175 |
+
body = {"youtube_url": youtube_url, **payload}
|
| 176 |
+
r = requests.post(endpoint, json=body, timeout=timeout)
|
| 177 |
+
|
| 178 |
+
if r.status_code >= 400:
|
| 179 |
+
# log details instead of raising blindly
|
| 180 |
+
print("STATUS:", r.status_code)
|
| 181 |
+
print("HEADERS:", r.headers)
|
| 182 |
+
print("BODY:", r.text[:2000])
|
| 183 |
+
r.raise_for_status()
|
| 184 |
+
|
| 185 |
+
# Response parsing: support dict or plain string
|
| 186 |
+
ctype = r.headers.get("Content-Type", "")
|
| 187 |
+
if "application/json" in ctype:
|
| 188 |
+
data = r.json()
|
| 189 |
+
# If server validates response_model to dict
|
| 190 |
+
if isinstance(data, dict) and "audio_url" in data:
|
| 191 |
+
return data["audio_url"]
|
| 192 |
+
# If server returns plain string in JSON (rare)
|
| 193 |
+
if isinstance(data, str):
|
| 194 |
+
return data
|
| 195 |
+
raise ValueError(f"Unexpected JSON shape: {data}")
|
| 196 |
+
else:
|
| 197 |
+
# Plain text URL response_model=str
|
| 198 |
+
text = r.text.strip()
|
| 199 |
+
if text.startswith("http"):
|
| 200 |
+
return text
|
| 201 |
+
raise ValueError(f"Unexpected text response: {text[:200]}")
|
| 202 |
+
|
| 203 |
+
except Exception as e:
|
| 204 |
+
msg = (f"{datetime.now()}: Error retrieving youtube wave file from Azure instance. "
|
| 205 |
+
f"url={youtube_url} endpoint={endpoint} err={e}")
|
| 206 |
+
print(msg)
|
| 207 |
+
return msg
|
| 208 |
|
| 209 |
+
|
| 210 |
def process_audio(upload_path, record_path, url, sys_prompt, user_prompt):
|
| 211 |
tmp_to_cleanup = []
|
| 212 |
audio_b64 = None
|
| 213 |
text_input = None
|
| 214 |
domaincheck = None
|
| 215 |
+
extract_input = None
|
| 216 |
+
audio_wav = None
|
| 217 |
+
|
| 218 |
try:
|
| 219 |
# Capture start time for logging
|
| 220 |
Starttime = datetime.now(),
|
| 221 |
+
print(f"AudioChatSummarizer API call starts at {datetime.now()}"),
|
| 222 |
audio_path = None
|
| 223 |
if upload_path:
|
| 224 |
audio_path = upload_path
|
|
|
|
| 238 |
|
| 239 |
if CheckURL:
|
| 240 |
# Get the transcription from youtube
|
| 241 |
+
# text_input = Youtubetranscription_summarizer.main(url.strip()) # Youtube files are transcribed and summarized
|
| 242 |
+
#extract_input = extract(url.strip()) # Call for local testing
|
| 243 |
+
# Test wav file transcription using faster-whisper # Call for local testing
|
| 244 |
+
#audio_wav = fetch_audio_from_youtube(extract_input) # Call for local testing
|
| 245 |
+
audio_wav = fetch_audio_from_youtube(url.strip()) # Server API call
|
| 246 |
+
#file_path = "/Users/sayedarizvi/AudioSummarizer/Data/test.wav" # Call for local testing
|
| 247 |
+
#audio_wav = file_path # Call for local testing
|
| 248 |
+
#text_input = Youtubetranscription_summarizer.transcribe_faster_whisper(extract_input, model_name="base.en")# Call for local testing
|
| 249 |
+
text_input = Youtubetranscription_summarizer.transcribe_faster_whisper(audio_wav, model_name="base.en") #Call for server testing
|
| 250 |
tmp_to_cleanup.append(text_input)
|
| 251 |
else:
|
| 252 |
audio_path = download_to_temp_mp3(url.strip())
|
requirements.txt
CHANGED
|
@@ -5,9 +5,8 @@ azure-identity==1.25.0
|
|
| 5 |
azure-ai-projects==1.0.0
|
| 6 |
numpy==1.26.4
|
| 7 |
openai==1.107.3
|
| 8 |
-
yt_dlp==2025.9.
|
| 9 |
faster_whisper==1.2.0
|
| 10 |
fastapi
|
| 11 |
uvicorn[standard]==0.30.6
|
| 12 |
-
azure-storage-blob==12.20.0
|
| 13 |
-
pydantic==2.8.2 ###
|
|
|
|
| 5 |
azure-ai-projects==1.0.0
|
| 6 |
numpy==1.26.4
|
| 7 |
openai==1.107.3
|
| 8 |
+
yt_dlp==2025.9.23
|
| 9 |
faster_whisper==1.2.0
|
| 10 |
fastapi
|
| 11 |
uvicorn[standard]==0.30.6
|
| 12 |
+
azure-storage-blob==12.20.0
|
|
|