Mbonea commited on
Commit
7fc9e46
·
1 Parent(s): 7a1123a

upload to telegram

Browse files
App/Transcription/TranscriptionRoutes.py CHANGED
@@ -9,6 +9,7 @@ from .Schemas import (
9
  )
10
  from App import bot
11
  import aiofiles, os, re
 
12
  import tempfile
13
  from celery.result import AsyncResult
14
  from App.Worker import transcription_task, downloadfile
@@ -27,6 +28,10 @@ from App.Embedding.utils.Initialize import delete_documents
27
 
28
  transcription_router = APIRouter(tags=["Transcription"])
29
 
 
 
 
 
30
 
31
  @transcription_router.get("/download-audio")
32
  async def download_audio(
@@ -38,14 +43,17 @@ async def download_audio(
38
  ),
39
  user: UserSchema = Depends(get_token_owner),
40
  ):
41
- youtube_url= url
 
42
  parsed_url = urlparse(youtube_url)
43
 
44
  # Get the query parameters
45
  query_parameters = parse_qs(parsed_url.query)
46
 
47
  # Get the value of the 'v' parameter
48
- v_param_value = query_parameters.get('v', [])[0] if 'v' in query_parameters else None
 
 
49
  url = f"https://www.youtube.com/watch?v={v_param_value}"
50
  if user == None:
51
  return {"code": 400, "message": "doesn't exist", "payload": None}
@@ -57,9 +65,9 @@ async def download_audio(
57
  with yt_dlp.YoutubeDL(ydl_opts_info) as ydl:
58
  info_dict = ydl.extract_info(url, download=False)
59
  video_title = info_dict.get("title", None)
60
-
61
  sanitized_title = re.sub(
62
- r"(?u)[^-\w.]", "", video_title
63
  ) # Ensure the title is file-friendly
64
  filename = f"{sanitized_title}.mp3"
65
  file_path = os.path.join("./", "Downloads", filename)
@@ -70,7 +78,7 @@ async def download_audio(
70
  }
71
 
72
  task = downloadfile.delay(url=url, ydl_opts=ydl_opts, model_size=model)
73
- response = {"task_id": task.id, "file_name": f"{video_title}.mp3"}
74
  transcription_enrty = await Transcriptions.objects.create(
75
  user=user, youtubeLink=url, **response
76
  )
@@ -124,10 +132,12 @@ async def create_file(
124
  ),
125
  user: UserSchema = Depends(get_token_owner),
126
  ):
 
 
127
  # Write the file to disk asynchronously
128
  Upload_dir = ""
129
  try:
130
- async with aiofiles.open(file.filename, "wb") as f:
131
  while contents := await file.read(1024 * 1):
132
  await f.write(contents)
133
 
@@ -139,11 +149,11 @@ async def create_file(
139
  await file.close()
140
 
141
  # celery task
142
- task = transcription_task.delay(file.filename, model)
143
 
144
  # create a transcription entry
145
  transcription_enrty = await Transcriptions.objects.create(
146
- task_id=task.id, user=user, file_name=file.filename
147
  )
148
  background_tasks.add_task(perform_background_task, file=file, task_id=task.id)
149
  return {
@@ -162,7 +172,7 @@ async def get_status(task_id):
162
  result = BaseTranscription(**entry.__dict__)
163
 
164
  if result.status == "SUCCESS":
165
- result.percentage='100'
166
  return result
167
  task_result = AsyncResult(task_id)
168
  # print(task_result.result)
@@ -175,17 +185,17 @@ async def get_status(task_id):
175
 
176
  if task_result.status == "SUCCESS":
177
  trans = TranscriptionMetadata(**task_result.result)
178
- percentage='100'
179
  await entry.update(**trans.dict())
180
  else:
181
  try:
182
  _trans = TranscriptionMetadata(**task_result.result)
183
- percentage=_trans.percentage
184
  except Exception as e:
185
  print(e)
186
  return {"payload": None, "message": "Nothing found", "code": 200}
187
  await entry.update(**_trans.dict())
188
 
189
  result = BaseTranscription(**entry.__dict__)
190
- result.percentage=percentage
191
- return result
 
9
  )
10
  from App import bot
11
  import aiofiles, os, re
12
+ import uuid
13
  import tempfile
14
  from celery.result import AsyncResult
15
  from App.Worker import transcription_task, downloadfile
 
28
 
29
  transcription_router = APIRouter(tags=["Transcription"])
30
 
31
+ def genUUID():
32
+ uuid_value = uuid.uuid4()
33
+ short_uuid = str(uuid_value)[:6]
34
+ return short_uuid
35
 
36
  @transcription_router.get("/download-audio")
37
  async def download_audio(
 
43
  ),
44
  user: UserSchema = Depends(get_token_owner),
45
  ):
46
+
47
+ youtube_url = url
48
  parsed_url = urlparse(youtube_url)
49
 
50
  # Get the query parameters
51
  query_parameters = parse_qs(parsed_url.query)
52
 
53
  # Get the value of the 'v' parameter
54
+ v_param_value = (
55
+ query_parameters.get("v", [])[0] if "v" in query_parameters else None
56
+ )
57
  url = f"https://www.youtube.com/watch?v={v_param_value}"
58
  if user == None:
59
  return {"code": 400, "message": "doesn't exist", "payload": None}
 
65
  with yt_dlp.YoutubeDL(ydl_opts_info) as ydl:
66
  info_dict = ydl.extract_info(url, download=False)
67
  video_title = info_dict.get("title", None)
68
+ short_uuid = genUUID()
69
  sanitized_title = re.sub(
70
+ r"(?u)[^-\w.]", "", short_uuid
71
  ) # Ensure the title is file-friendly
72
  filename = f"{sanitized_title}.mp3"
73
  file_path = os.path.join("./", "Downloads", filename)
 
78
  }
79
 
80
  task = downloadfile.delay(url=url, ydl_opts=ydl_opts, model_size=model)
81
+ response = {"task_id": task.id, "file_name": video_title }
82
  transcription_enrty = await Transcriptions.objects.create(
83
  user=user, youtubeLink=url, **response
84
  )
 
132
  ),
133
  user: UserSchema = Depends(get_token_owner),
134
  ):
135
+ extension = file.filename.split('.')[-1]
136
+ file_name = f'{genUUID()}.{extension}'
137
  # Write the file to disk asynchronously
138
  Upload_dir = ""
139
  try:
140
+ async with aiofiles.open(file_name, "wb") as f:
141
  while contents := await file.read(1024 * 1):
142
  await f.write(contents)
143
 
 
149
  await file.close()
150
 
151
  # celery task
152
+ task = transcription_task.delay(file_name, model)
153
 
154
  # create a transcription entry
155
  transcription_enrty = await Transcriptions.objects.create(
156
+ task_id=task.id, user=user, file_name=file_name
157
  )
158
  background_tasks.add_task(perform_background_task, file=file, task_id=task.id)
159
  return {
 
172
  result = BaseTranscription(**entry.__dict__)
173
 
174
  if result.status == "SUCCESS":
175
+ result.percentage = "100"
176
  return result
177
  task_result = AsyncResult(task_id)
178
  # print(task_result.result)
 
185
 
186
  if task_result.status == "SUCCESS":
187
  trans = TranscriptionMetadata(**task_result.result)
188
+ percentage = "100"
189
  await entry.update(**trans.dict())
190
  else:
191
  try:
192
  _trans = TranscriptionMetadata(**task_result.result)
193
+ percentage = _trans.percentage
194
  except Exception as e:
195
  print(e)
196
  return {"payload": None, "message": "Nothing found", "code": 200}
197
  await entry.update(**_trans.dict())
198
 
199
  result = BaseTranscription(**entry.__dict__)
200
+ result.percentage = percentage
201
+ return result
App/app.py CHANGED
@@ -47,7 +47,7 @@ def authjwt_exception_handler(request: Request, exc: AuthJWTException):
47
 
48
  @app.on_event("startup")
49
  async def startup_event():
50
- # await bot.start(bot_token="6183919505:AAEhHFt4mI18bQeAf2Lj7AePXFRPVLrOFM8")
51
  # await upload_bot.start()
52
  # await models.create_all()
53
  # models.metadata.create_all()
 
47
 
48
  @app.on_event("startup")
49
  async def startup_event():
50
+ await bot.start(bot_token="6183919505:AAEhHFt4mI18bQeAf2Lj7AePXFRPVLrOFM8")
51
  # await upload_bot.start()
52
  # await models.create_all()
53
  # models.metadata.create_all()