Mgolo commited on
Commit
89263ca
·
verified ·
1 Parent(s): 4f9d2c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -6
app.py CHANGED
@@ -43,9 +43,18 @@ def load_models():
43
  def load_whisper_model():
44
  return whisper.load_model("base")
45
 
46
- def transcribe_audio(audio_path):
47
- whisper_model = load_whisper_model()
48
- return whisper_model.transcribe(audio_path)["text"]
 
 
 
 
 
 
 
 
 
49
 
50
  def translate(text, target_lang):
51
  en_dar_translator, en_hau_translator, en_wol_translator = load_models()
@@ -90,14 +99,24 @@ def translate(text, target_lang):
90
 
91
  # --- Extract text from file ---
92
  def extract_text_from_file(uploaded_file):
93
- file_type = uploaded_file.name.split('.')[-1].lower()
94
- content = uploaded_file.read()
 
 
 
 
 
 
 
95
 
96
  if file_type == "pdf":
97
  with fitz.open(stream=content, filetype="pdf") as doc:
98
  return "\n".join([page.get_text() for page in doc])
99
  elif file_type == "docx":
100
- doc = docx.Document(uploaded_file)
 
 
 
101
  return "\n".join([para.text for para in doc.paragraphs])
102
  else:
103
  encoding = chardet.detect(content)['encoding']
 
43
  def load_whisper_model():
44
  return whisper.load_model("base")
45
 
46
+ def transcribe_audio(audio_file):
47
+ model = load_whisper_model()
48
+ if isinstance(audio_file, str):
49
+ audio_path = audio_file
50
+ else:
51
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
52
+ tmp.write(audio_file.read())
53
+ audio_path = tmp.name
54
+ result = model.transcribe(audio_path)
55
+ if not isinstance(audio_file, str):
56
+ os.remove(audio_path)
57
+ return result["text"]
58
 
59
  def translate(text, target_lang):
60
  en_dar_translator, en_hau_translator, en_wol_translator = load_models()
 
99
 
100
  # --- Extract text from file ---
101
  def extract_text_from_file(uploaded_file):
102
+ # Handle both filepath (str) and file-like object
103
+ if isinstance(uploaded_file, str):
104
+ file_path = uploaded_file
105
+ file_type = file_path.split('.')[-1].lower()
106
+ with open(file_path, "rb") as f:
107
+ content = f.read()
108
+ else:
109
+ file_type = uploaded_file.name.split('.')[-1].lower()
110
+ content = uploaded_file.read()
111
 
112
  if file_type == "pdf":
113
  with fitz.open(stream=content, filetype="pdf") as doc:
114
  return "\n".join([page.get_text() for page in doc])
115
  elif file_type == "docx":
116
+ if isinstance(uploaded_file, str):
117
+ doc = docx.Document(file_path)
118
+ else:
119
+ doc = docx.Document(uploaded_file)
120
  return "\n".join([para.text for para in doc.paragraphs])
121
  else:
122
  encoding = chardet.detect(content)['encoding']