Adarsh Shirawalmath commited on
Commit
b655a64
·
1 Parent(s): 137ef4d
Files changed (1) hide show
  1. app/main.py +29 -34
app/main.py CHANGED
@@ -12,6 +12,8 @@ import io
12
  import google.generativeai as genai
13
  import time
14
  from collections import deque
 
 
15
 
16
  logging.basicConfig(level=logging.INFO)
17
  logger = logging.getLogger(__name__)
@@ -36,6 +38,10 @@ class ChatMessage(BaseModel):
36
  message: str
37
  transcript: str
38
 
 
 
 
 
39
  RATE_LIMIT = 15
40
  RATE_WINDOW = 60
41
  request_timestamps = deque()
@@ -127,46 +133,35 @@ def _patched_execute_request(url, method="GET", headers=None, data=None):
127
  request_module._execute_request = _patched_execute_request
128
 
129
  @app.post("/transcribe")
130
- async def transcribe_video(video: VideoURL):
 
131
  try:
132
- yt = pytubefix.YouTube(str(video.url))
133
- audio_stream = yt.streams.filter(only_audio=True, file_extension='mp4').first()
134
- if not audio_stream:
135
- raise HTTPException(status_code=400, detail="No audio stream found for this video")
136
-
137
- video_description = yt.description
138
-
139
- temp_dir = "/tmp" # Use /tmp directory for Hugging Face Spaces
140
- audio_file = audio_stream.download(output_path=temp_dir)
141
- audio_file_mp3 = audio_file + ".mp3"
142
- os.rename(audio_file, audio_file_mp3)
143
-
144
- transcript = await transcribe_audio(audio_file_mp3)
145
-
146
- if not transcript:
147
- raise HTTPException(status_code=500, detail="Transcription failed")
148
-
149
- full_text = transcript['results']['channels'][0]['alternatives'][0]['transcript']
150
-
151
- summary = generate_summary(full_text, video_description, video.summary_length)
152
- quiz = generate_quiz(full_text, video_description)
153
-
154
- result = {
155
- "transcription": full_text,
156
- "summary": summary,
157
- "video_description": video_description,
158
- "quiz": quiz,
159
- "detailed_transcript": transcript
160
  }
 
 
 
 
161
 
162
- os.remove(audio_file_mp3)
 
 
163
 
164
- return result
 
165
 
 
 
166
  except Exception as e:
167
- logger.error(f"Error processing video: {str(e)}")
168
- logger.exception("Full traceback:")
169
- raise HTTPException(status_code=500, detail=str(e))
170
 
171
  @app.post("/generate_audio_summary")
172
  async def generate_audio_summary(summary: str):
 
12
  import google.generativeai as genai
13
  import time
14
  from collections import deque
15
+ import yt_dlp
16
+
17
 
18
  logging.basicConfig(level=logging.INFO)
19
  logger = logging.getLogger(__name__)
 
38
  message: str
39
  transcript: str
40
 
41
+ class VideoRequest(BaseModel):
42
+ url: str
43
+
44
+
45
  RATE_LIMIT = 15
46
  RATE_WINDOW = 60
47
  request_timestamps = deque()
 
133
  request_module._execute_request = _patched_execute_request
134
 
135
  @app.post("/transcribe")
136
+ async def transcribe_video(request: VideoRequest):
137
+ url = request.url
138
  try:
139
+ # Download the audio from the YouTube video
140
+ ydl_opts = {
141
+ 'format': 'bestaudio/best',
142
+ 'postprocessors': [{
143
+ 'key': 'FFmpegExtractAudio',
144
+ 'preferredcodec': 'mp3',
145
+ 'preferredquality': '192',
146
+ }],
147
+ 'outtmpl': 'downloads/%(id)s.%(ext)s',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  }
149
+
150
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
151
+ info_dict = ydl.extract_info(url, download=True)
152
+ audio_file_path = ydl.prepare_filename(info_dict)
153
 
154
+ # Check if the audio file exists
155
+ if not os.path.exists(audio_file_path):
156
+ raise HTTPException(status_code=500, detail="Failed to download the audio")
157
 
158
+ # Return the path of the downloaded audio file
159
+ return {"audio_file": audio_file_path}
160
 
161
+ except yt_dlp.utils.DownloadError as e:
162
+ raise HTTPException(status_code=500, detail=f"Download error: {str(e)}")
163
  except Exception as e:
164
+ raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
 
 
165
 
166
  @app.post("/generate_audio_summary")
167
  async def generate_audio_summary(summary: str):