first commit
This commit is contained in:
108
app.py
Normal file
108
app.py
Normal file
@@ -0,0 +1,108 @@
|
||||
from flask import Flask, render_template, request, jsonify
|
||||
import requests
|
||||
import json
|
||||
import speech_recognition as sr
|
||||
import io
|
||||
import base64
|
||||
import os
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
SUPPORTED_LANGUAGES = {
|
||||
"arabic": "Arabic",
|
||||
"armenian": "Armenian",
|
||||
"azerbaijani": "Azerbaijani",
|
||||
"english": "English",
|
||||
"french": "French",
|
||||
"georgian": "Georgian",
|
||||
"kazakh": "Kazakh",
|
||||
"mandarin": "Mandarin Chinese",
|
||||
"persian": "Persian (Farsi)",
|
||||
"portuguese": "Portuguese",
|
||||
"russian": "Russian",
|
||||
"turkish": "Turkish",
|
||||
"uzbek": "Uzbek"
|
||||
}
|
||||
|
||||
OLLAMA_API_URL = "http://100.64.0.4:11434/api/generate"
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return render_template('index.html', languages=SUPPORTED_LANGUAGES)
|
||||
|
||||
@app.route('/translate', methods=['POST'])
|
||||
def translate():
|
||||
data = request.json
|
||||
source_language = data.get('sourceLanguage')
|
||||
target_language = data.get('targetLanguage')
|
||||
text = data.get('text')
|
||||
|
||||
if not all([source_language, target_language, text]):
|
||||
return jsonify({"error": "Missing required parameters"}), 400
|
||||
|
||||
# Create prompt for Gemma 3
|
||||
prompt = f"""Translate the following text from {source_language} to {target_language}.
|
||||
Text to translate: {text}
|
||||
|
||||
Provide ONLY the translated text with no additional commentary, explanations, or formatting.
|
||||
"""
|
||||
|
||||
# Call Ollama API with the Gemma 3 model
|
||||
payload = {
|
||||
"model": "gemma3:12b",
|
||||
"prompt": prompt,
|
||||
"stream": False
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(OLLAMA_API_URL, json=payload)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
|
||||
# Extract the generated translation
|
||||
translation = result.get("response", "").strip()
|
||||
|
||||
return jsonify({"translation": translation})
|
||||
except Exception as e:
|
||||
return jsonify({"error": f"Translation failed: {str(e)}"}), 500
|
||||
|
||||
@app.route('/speech-to-text', methods=['POST'])
|
||||
def speech_to_text():
|
||||
try:
|
||||
audio_data = request.json.get('audio')
|
||||
language_code = request.json.get('language')
|
||||
|
||||
# Convert base64 audio to file
|
||||
audio_bytes = base64.b64decode(audio_data.split(',')[1])
|
||||
|
||||
# Use speech recognition
|
||||
recognizer = sr.Recognizer()
|
||||
with sr.AudioFile(io.BytesIO(audio_bytes)) as source:
|
||||
audio = recognizer.record(source)
|
||||
|
||||
# Convert speech to text
|
||||
language_code_map = {
|
||||
"arabic": "ar-AR",
|
||||
"armenian": "hy-AM",
|
||||
"azerbaijani": "az-AZ",
|
||||
"english": "en-US",
|
||||
"french": "fr-FR",
|
||||
"georgian": "ka-GE",
|
||||
"kazakh": "kk-KZ",
|
||||
"mandarin": "zh-CN",
|
||||
"persian": "fa-IR",
|
||||
"portuguese": "pt-PT",
|
||||
"russian": "ru-RU",
|
||||
"turkish": "tr-TR",
|
||||
"uzbek": "uz-UZ"
|
||||
}
|
||||
|
||||
lang_code = language_code_map.get(language_code, "en-US")
|
||||
text = recognizer.recognize_google(audio, language=lang_code)
|
||||
|
||||
return jsonify({"text": text})
|
||||
except Exception as e:
|
||||
return jsonify({"error": f"Speech recognition failed: {str(e)}"}), 500
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5005, debug=True)
|
||||
Reference in New Issue
Block a user