Mgolo commited on
Commit
07ab3e5
·
verified ·
1 Parent(s): 26c45c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -30
app.py CHANGED
@@ -41,7 +41,6 @@ def load_whisper_model():
41
 
42
  def transcribe_audio(audio_file):
43
  model = load_whisper_model()
44
- # Save temp file if not a path
45
  if isinstance(audio_file, str):
46
  audio_path = audio_file
47
  else:
@@ -53,6 +52,44 @@ def transcribe_audio(audio_file):
53
  os.remove(audio_path)
54
  return result["text"]
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  def translate(text):
57
  translator = load_wolof_model()
58
  lang_tag = ">>wol<<"
@@ -80,34 +117,6 @@ def translate(text):
80
 
81
  return "\n".join(translated_output)
82
 
83
- def extract_text_from_file(uploaded_file):
84
- file_type = uploaded_file.name.split('.')[-1].lower()
85
- content = uploaded_file.read()
86
-
87
- if file_type == "pdf":
88
- with fitz.open(stream=content, filetype="pdf") as doc:
89
- return "\n".join([page.get_text() for page in doc])
90
- elif file_type == "docx":
91
- doc = docx.Document(uploaded_file)
92
- return "\n".join([para.text for para in doc.paragraphs])
93
- else:
94
- encoding = chardet.detect(content)['encoding']
95
- if encoding:
96
- content = content.decode(encoding, errors='ignore')
97
- if file_type in ("html", "htm"):
98
- soup = BeautifulSoup(content, "html.parser")
99
- return soup.get_text()
100
- elif file_type == "md":
101
- html = markdown2.markdown(content)
102
- soup = BeautifulSoup(html, "html.parser")
103
- return soup.get_text()
104
- elif file_type == "srt":
105
- return re.sub(r"\d+\n\d{2}:\d{2}:\d{2},\d{3} --> .*?\n", "", content)
106
- elif file_type in ("txt", "text"):
107
- return content
108
- else:
109
- raise ValueError("Unsupported file type")
110
-
111
  def process_input(input_mode, text, audio_file, file_obj):
112
  input_text = ""
113
  if input_mode == "Text":
@@ -134,7 +143,7 @@ with gr.Blocks() as demo:
134
  input_mode = gr.Radio(choices=["Text", "Audio", "File"], label="Select input mode", value="Text")
135
 
136
  input_text = gr.Textbox(label="Enter English text", lines=10, visible=True)
137
- audio_input = gr.Audio(label="Upload audio (.wav, .mp3, .m4a)", type="filepath")
138
  file_input = gr.File(file_types=['.pdf', '.docx', '.html', '.htm', '.md', '.srt', '.txt'], label="Upload document", visible=False)
139
 
140
  extracted_text = gr.Textbox(label="Extracted / Transcribed Text", lines=10, interactive=False)
 
41
 
42
  def transcribe_audio(audio_file):
43
  model = load_whisper_model()
 
44
  if isinstance(audio_file, str):
45
  audio_path = audio_file
46
  else:
 
52
  os.remove(audio_path)
53
  return result["text"]
54
 
55
+ def extract_text_from_file(uploaded_file):
56
+ # Handle both filepath (str) and file-like object
57
+ if isinstance(uploaded_file, str):
58
+ file_path = uploaded_file
59
+ file_type = file_path.split('.')[-1].lower()
60
+ with open(file_path, "rb") as f:
61
+ content = f.read()
62
+ else:
63
+ file_type = uploaded_file.name.split('.')[-1].lower()
64
+ content = uploaded_file.read()
65
+
66
+ if file_type == "pdf":
67
+ with fitz.open(stream=content, filetype="pdf") as doc:
68
+ return "\n".join([page.get_text() for page in doc])
69
+ elif file_type == "docx":
70
+ if isinstance(uploaded_file, str):
71
+ doc = docx.Document(file_path)
72
+ else:
73
+ doc = docx.Document(uploaded_file)
74
+ return "\n".join([para.text for para in doc.paragraphs])
75
+ else:
76
+ encoding = chardet.detect(content)['encoding']
77
+ if encoding:
78
+ content = content.decode(encoding, errors='ignore')
79
+ if file_type in ("html", "htm"):
80
+ soup = BeautifulSoup(content, "html.parser")
81
+ return soup.get_text()
82
+ elif file_type == "md":
83
+ html = markdown2.markdown(content)
84
+ soup = BeautifulSoup(html, "html.parser")
85
+ return soup.get_text()
86
+ elif file_type == "srt":
87
+ return re.sub(r"\d+\n\d{2}:\d{2}:\d{2},\d{3} --> .*?\n", "", content)
88
+ elif file_type in ("txt", "text"):
89
+ return content
90
+ else:
91
+ raise ValueError("Unsupported file type")
92
+
93
  def translate(text):
94
  translator = load_wolof_model()
95
  lang_tag = ">>wol<<"
 
117
 
118
  return "\n".join(translated_output)
119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  def process_input(input_mode, text, audio_file, file_obj):
121
  input_text = ""
122
  if input_mode == "Text":
 
143
  input_mode = gr.Radio(choices=["Text", "Audio", "File"], label="Select input mode", value="Text")
144
 
145
  input_text = gr.Textbox(label="Enter English text", lines=10, visible=True)
146
+ audio_input = gr.Audio(label="Upload audio (.wav, .mp3, .m4a)", type="filepath", visible=False)
147
  file_input = gr.File(file_types=['.pdf', '.docx', '.html', '.htm', '.md', '.srt', '.txt'], label="Upload document", visible=False)
148
 
149
  extracted_text = gr.Textbox(label="Extracted / Transcribed Text", lines=10, interactive=False)