Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
from flask import Flask,render_template,request,jsonify
|
| 2 |
import openai
|
| 3 |
from googletrans import Translator
|
| 4 |
-
import os
|
| 5 |
-
from dotenv import load_dotenv
|
| 6 |
import asyncio
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
|
@@ -23,34 +24,68 @@ def get_response(prompt):
|
|
| 23 |
|
| 24 |
app = Flask(__name__)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
@app.route('/')
|
| 27 |
def index():
|
| 28 |
return render_template("index.html")
|
| 29 |
|
| 30 |
@app.route('/api/process_text', methods=['POST'])
|
| 31 |
def process_text():
|
|
|
|
| 32 |
data = request.get_json()
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# Define an async function for translation
|
| 37 |
-
async def translate_async(text):
|
| 38 |
translator = Translator()
|
| 39 |
-
translated = await translator.translate(text, dest=
|
| 40 |
return translated.text
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
print("
|
| 46 |
-
print("Translated:", translated_text)
|
| 47 |
|
|
|
|
| 48 |
processed_text = translated_text.lower()
|
| 49 |
|
|
|
|
| 50 |
response = get_response(processed_text)
|
| 51 |
-
print(response)
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
if __name__ == '__main__':
|
| 56 |
-
app.run(
|
|
|
|
| 1 |
from flask import Flask,render_template,request,jsonify
|
| 2 |
import openai
|
| 3 |
from googletrans import Translator
|
|
|
|
|
|
|
| 4 |
import asyncio
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
|
|
|
| 24 |
|
| 25 |
app = Flask(__name__)
|
| 26 |
|
| 27 |
+
LANGUAGE_MAP = {
|
| 28 |
+
"English (US)": "en",
|
| 29 |
+
"Hindi (India)": "hi",
|
| 30 |
+
"Spanish (Spain)": "es",
|
| 31 |
+
"French (France)": "fr",
|
| 32 |
+
"German (Germany)": "de",
|
| 33 |
+
"Arabic (Saudi Arabia)": "ar"
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
@app.route('/')
|
| 37 |
def index():
|
| 38 |
return render_template("index.html")
|
| 39 |
|
| 40 |
@app.route('/api/process_text', methods=['POST'])
|
| 41 |
def process_text():
|
| 42 |
+
# Get JSON payload
|
| 43 |
data = request.get_json()
|
| 44 |
+
try:
|
| 45 |
+
original_text = data['text']
|
| 46 |
+
language_name = data['language']
|
| 47 |
+
except (KeyError, TypeError):
|
| 48 |
+
return jsonify({"error": "Missing 'text' or 'language' in JSON payload"}), 400
|
| 49 |
+
|
| 50 |
+
# Map language name to language code
|
| 51 |
+
if language_name not in LANGUAGE_MAP:
|
| 52 |
+
return jsonify({"error": f"Unsupported language: {language_name}"}), 400
|
| 53 |
+
original_lang_code = LANGUAGE_MAP[language_name]
|
| 54 |
+
|
| 55 |
+
print("Original Text:", original_text)
|
| 56 |
+
print("Original Language:", language_name, f"({original_lang_code})")
|
| 57 |
|
| 58 |
# Define an async function for translation
|
| 59 |
+
async def translate_async(text, dest_lang):
|
| 60 |
translator = Translator()
|
| 61 |
+
translated = await translator.translate(text, dest=dest_lang)
|
| 62 |
return translated.text
|
| 63 |
|
| 64 |
+
# Translate to English
|
| 65 |
+
if original_lang_code != "en":
|
| 66 |
+
translated_text = asyncio.run(translate_async(original_text, dest_lang="en"))
|
| 67 |
+
else:
|
| 68 |
+
translated_text = original_text # No translation needed if already English
|
| 69 |
|
| 70 |
+
print("Translated to English:", translated_text)
|
|
|
|
| 71 |
|
| 72 |
+
# Process the text (convert to lowercase)
|
| 73 |
processed_text = translated_text.lower()
|
| 74 |
|
| 75 |
+
# Get response (assumed to be in English)
|
| 76 |
response = get_response(processed_text)
|
| 77 |
+
print("English Response:", response)
|
| 78 |
|
| 79 |
+
# Translate response back to original language
|
| 80 |
+
if original_lang_code != "en":
|
| 81 |
+
final_response = asyncio.run(translate_async(response, dest_lang=original_lang_code))
|
| 82 |
+
else:
|
| 83 |
+
final_response = response # No translation needed if English
|
| 84 |
|
| 85 |
+
print("Final Response (in original language):", final_response)
|
| 86 |
+
|
| 87 |
+
# Return the final response
|
| 88 |
+
return jsonify({"response": final_response, "language": language_name})
|
| 89 |
+
|
| 90 |
if __name__ == '__main__':
|
| 91 |
+
app.run(debug=True)
|