Athspi commited on
Commit
965bd2d
·
verified ·
1 Parent(s): f003c90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
app.py CHANGED
@@ -53,23 +53,28 @@ def translate_audio():
53
  if not audio_file or audio_file.filename == '':
54
  return jsonify({'error': 'Invalid audio file'}), 400
55
 
56
- # Save temporary audio file
57
- filename = secure_filename(audio_file.filename)
58
- temp_input_path = os.path.join(tempfile.gettempdir(), filename)
59
- audio_file.save(temp_input_path)
60
 
61
  # Transcribe audio using Gemini
62
  model = genai.GenerativeModel("gemini-2.0-flash")
63
- with open(temp_input_path, "rb") as f:
64
- audio_data = f.read()
65
 
66
- # Use Gemini to transcribe the audio
67
- prompt = "Transcribe the following audio file into text. Return only the transcribed text with no additional commentary or explanations."
68
- response = model.generate_content([prompt, audio_data])
 
 
 
 
 
 
 
69
  transcription = response.text.strip()
70
 
71
  # Translate text using Gemini
72
- prompt = f"Translate the following text to {target_language} preserving meaning and cultural nuances:\n\n{transcription}"
73
  response = model.generate_content(prompt)
74
  translated_text = response.text.strip()
75
 
@@ -111,4 +116,4 @@ def download_file(filename):
111
  return jsonify({'error': 'File not found'}), 404
112
 
113
  if __name__ == '__main__':
114
- app.run(host="0.0.0.0", port=7860)
 
53
  if not audio_file or audio_file.filename == '':
54
  return jsonify({'error': 'Invalid audio file'}), 400
55
 
56
+ # Validate MIME type
57
+ allowed_mime_types = ['audio/wav', 'audio/mpeg', 'audio/mp4', 'audio/webm']
58
+ if audio_file.mimetype not in allowed_mime_types:
59
+ return jsonify({'error': f'Unsupported file type: {audio_file.mimetype}'}), 400
60
 
61
  # Transcribe audio using Gemini
62
  model = genai.GenerativeModel("gemini-2.0-flash")
 
 
63
 
64
+ # Create proper audio blob
65
+ audio_blob = {
66
+ 'mime_type': audio_file.mimetype,
67
+ 'data': audio_file.read()
68
+ }
69
+
70
+ # Get transcription
71
+ convo = model.start_chat()
72
+ convo.send_message("You are a professional transcriber. Transcribe this audio accurately and verbatim in the original language. Respond only with the transcription.")
73
+ response = convo.send_message(audio_blob)
74
  transcription = response.text.strip()
75
 
76
  # Translate text using Gemini
77
+ prompt = f"Translate the following text to {target_language} preserving meaning and cultural nuances. Respond only with the translation:\n\n{transcription}"
78
  response = model.generate_content(prompt)
79
  translated_text = response.text.strip()
80
 
 
116
  return jsonify({'error': 'File not found'}), 404
117
 
118
  if __name__ == '__main__':
119
+ app.run(host='0.0.0.0', port=5000, debug=True)