Madhuslista commited on
Commit
478e831
1 Parent(s): d4877e4

Feature: Add function to create a temp file to hold the returned text

Browse files
Files changed (1) hide show
  1. lib/media.py +34 -0
lib/media.py CHANGED
@@ -1,8 +1,13 @@
1
  #!/usr/bin/python
2
  # -*- coding: utf-8 -*-
3
 
 
 
4
  import subprocess
 
5
  from pathlib import Path
 
 
6
  from huggingface_hub import hf_hub_url as hf_link
7
 
8
  from .config import (
@@ -91,6 +96,35 @@ def save_file(audio_path, transcript_folder_path, text):
91
 
92
  return output_transcript_pathstr, link
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  # -->> Execute <<----------------------
95
 
96
 
 
1
  #!/usr/bin/python
2
  # -*- coding: utf-8 -*-
3
 
4
+ import os
5
+ import secrets
6
  import subprocess
7
+ import tempfile
8
  from pathlib import Path
9
+
10
+ from gradio.utils import NamedString
11
  from huggingface_hub import hf_hub_url as hf_link
12
 
13
  from .config import (
 
96
 
97
  return output_transcript_pathstr, link
98
 
99
+ def create_tmp_file(text, dir_path, file_name, ext):
100
+ """Creates a temporary file and returns the path to it"""
101
+ # Create the dir_path if it doesn't exist
102
+ dir_path = str(Path(dir_path) / str(secrets.token_hex(10)))
103
+ Path(dir_path).mkdir(parents=True, exist_ok=True)
104
+
105
+ # Create a temporary file
106
+ tmp = tempfile.NamedTemporaryFile(
107
+ delete=False,
108
+ dir=dir_path,
109
+ suffix=ext
110
+ )
111
+
112
+ # Write the text to the file
113
+ tmp.write(text.encode("utf-8"))
114
+ tmp.seek(0)
115
+ tmp.close()
116
+
117
+ # Rename it
118
+ file_path = Path(dir_path) / (file_name + ext)
119
+ tmp_name = tmp.name
120
+ os.rename(tmp_name, file_path)
121
+
122
+
123
+ # Return the path to the file
124
+ return NamedString(file_path)
125
+
126
+
127
+
128
  # -->> Execute <<----------------------
129
 
130