Divyansh Kushwaha commited on
Commit
220d7b5
·
1 Parent(s): 1971996
Files changed (2) hide show
  1. api.py +6 -2
  2. app.py +2 -2
api.py CHANGED
@@ -1,5 +1,5 @@
1
  from fastapi import FastAPI, Query,HTTPException
2
- from fastapi.responses import JSONResponse, FileResponse
3
  from google.cloud import texttospeech
4
  from google.oauth2.service_account import Credentials
5
  from langchain.schema import HumanMessage
@@ -119,7 +119,11 @@ def download_json():
119
  def download_audio():
120
  if os.path.exists(AUDIO_FILE_PATH):
121
  print(f"Serving Hindi audio file from: {AUDIO_FILE_PATH}")
122
- return FileResponse(AUDIO_FILE_PATH, media_type="audio/mp3", filename="hindi_summary.mp3")
 
 
 
 
123
  else:
124
  print(f"Audio file not found at: {AUDIO_FILE_PATH}")
125
  raise HTTPException(status_code=404, detail="Audio file not found.")
 
1
  from fastapi import FastAPI, Query,HTTPException
2
+ from fastapi.responses import JSONResponse, FileResponse, StreamingResponse
3
  from google.cloud import texttospeech
4
  from google.oauth2.service_account import Credentials
5
  from langchain.schema import HumanMessage
 
119
  def download_audio():
120
  if os.path.exists(AUDIO_FILE_PATH):
121
  print(f"Serving Hindi audio file from: {AUDIO_FILE_PATH}")
122
+ def iterfile(): # Generator to read the file in chunks
123
+ with open(AUDIO_FILE_PATH, mode="rb") as file_like:
124
+ yield from file_like
125
+
126
+ return StreamingResponse(iterfile(), media_type="audio/mp3") # Stream audio as bytes
127
  else:
128
  print(f"Audio file not found at: {AUDIO_FILE_PATH}")
129
  raise HTTPException(status_code=404, detail="Audio file not found.")
app.py CHANGED
@@ -72,9 +72,9 @@ if st.button("Generate Summary"):
72
  audio_url = f"{BASE_URL}/downloadHindiAudio"
73
  try:
74
  # Fetch audio data using the API
75
- response = requests.get(audio_url)
76
  if response.status_code == 200:
77
- st.audio(response.content, format="audio/mp3") # Pass the audio content directly
78
  else:
79
  st.error(f"Error: {response.status_code}, {response.text}")
80
  except Exception as e:
 
72
  audio_url = f"{BASE_URL}/downloadHindiAudio"
73
  try:
74
  # Fetch audio data using the API
75
+ response = requests.get(audio_url, stream=True) # Stream the audio data
76
  if response.status_code == 200:
77
+ st.audio(response.content, format="audio/mp3") # Pass the audio content directly as bytes
78
  else:
79
  st.error(f"Error: {response.status_code}, {response.text}")
80
  except Exception as e: