Tmeena commited on
Commit
fd6c122
1 Parent(s): e14806c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -8
app.py CHANGED
@@ -1,20 +1,21 @@
1
- import streamlit as st
2
- import base64
3
  import os
 
 
4
  from flask import Flask, request, jsonify
5
  from pydub import AudioSegment
6
  import whisper
7
  from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
8
 
9
- # Define cache directory
10
- os.environ['HF_HOME'] = '/app/cache'
 
11
 
12
- # Load the Whisper model with a specified cache directory
13
- whisper_model = whisper.load_model("base", download_root="/app/cache")
14
 
15
  # Load the translation model and tokenizer
16
- tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M", cache_dir="/app/cache")
17
- translation_model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M", cache_dir="/app/cache")
18
 
19
  def preprocess_audio(audio_path):
20
  """Convert audio to 16kHz mono WAV format."""
@@ -57,3 +58,24 @@ def handle_request(audio_base64, source_lang, target_lang):
57
  os.remove(audio_file_path)
58
 
59
  return {"transcribed_text": spoken_text, "translated_text": translated_text}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import base64
3
+ import tempfile
4
  from flask import Flask, request, jsonify
5
  from pydub import AudioSegment
6
  import whisper
7
  from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
8
 
9
+ # Set the cache directory to a writable location
10
+ os.environ["TRANSFORMERS_CACHE"] = "/app/cache" # Use a subdirectory in your working directory
11
+ os.environ["HF_HOME"] = "/app/cache" # Set HF_HOME for Hugging Face libraries
12
 
13
+ # Load the Whisper model
14
+ whisper_model = whisper.load_model("base")
15
 
16
  # Load the translation model and tokenizer
17
+ tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M")
18
+ translation_model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M")
19
 
20
  def preprocess_audio(audio_path):
21
  """Convert audio to 16kHz mono WAV format."""
 
58
  os.remove(audio_file_path)
59
 
60
  return {"transcribed_text": spoken_text, "translated_text": translated_text}
61
+
62
+ # Flask for handling external POST requests
63
+ app = Flask(__name__)
64
+
65
+ @app.route('/translate', methods=['POST'])
66
+ def translate():
67
+ """API endpoint for handling audio translation."""
68
+ data = request.json
69
+ if 'audio' not in data or 'source_lang' not in data or 'target_lang' not in data:
70
+ return jsonify({"error": "Invalid request format"}), 400
71
+
72
+ audio_base64 = data['audio']
73
+ source_lang = data['source_lang']
74
+ target_lang = data['target_lang']
75
+
76
+ # Call the handle_request function to process the request
77
+ response = handle_request(audio_base64, source_lang, target_lang)
78
+ return jsonify(response)
79
+
80
+ if __name__ == "__main__":
81
+ app.run(host='0.0.0.0', port=7860)