Spaces:
Sleeping
Sleeping
File size: 2,747 Bytes
49074e2 3cf6da2 75e1908 bdcdfba efe8df6 49074e2 3cf6da2 49074e2 3cf6da2 49074e2 efe8df6 49074e2 efe8df6 49074e2 efe8df6 bdcdfba efe8df6 bdcdfba efe8df6 bdcdfba efe8df6 bdcdfba efe8df6 49074e2 efe8df6 75e1908 49074e2 efe8df6 49074e2 efe8df6 49074e2 efe8df6 352a834 efe8df6 49074e2 541e2d5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
from flask import Flask,render_template,request,jsonify
import google.generativeai as genai
from googletrans import Translator
import asyncio
from dotenv import load_dotenv
import os
app = Flask(__name__)
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
genai.configure(api_key=GEMINI_API_KEY)
def get_response(prompt):
# Use Gemini model (e.g., gemini-1.5-flash)
model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content(prompt)
return response.text
app = Flask(__name__)
LANGUAGE_MAP = {
"English (US)": "en",
"Hindi (India)": "hi",
"Spanish (Spain)": "es",
"French (France)": "fr",
"German (Germany)": "de",
"Arabic (Saudi Arabia)": "ar"
}
@app.route('/')
def index():
return render_template("index.html")
@app.route('/api/process_text', methods=['POST'])
def process_text():
# Get JSON payload
data = request.get_json()
try:
original_text = data['text']
language_name = data['language']
except (KeyError, TypeError):
return jsonify({"error": "Missing 'text' or 'language' in JSON payload"}), 400
# Map language name to language code
if language_name not in LANGUAGE_MAP:
return jsonify({"error": f"Unsupported language: {language_name}"}), 400
original_lang_code = LANGUAGE_MAP[language_name]
print("Original Text:", original_text)
print("Original Language:", language_name, f"({original_lang_code})")
# Define an async function for translation
async def translate_async(text, dest_lang):
translator = Translator()
translated = await translator.translate(text, dest=dest_lang)
return translated.text
# Translate to English
if original_lang_code != "en":
translated_text = asyncio.run(translate_async(original_text, dest_lang="en"))
else:
translated_text = original_text # No translation needed if already English
print("Translated to English:", translated_text)
# Process the text (convert to lowercase)
processed_text = translated_text.lower()
# Get response (assumed to be in English)
response = get_response(processed_text)
print("English Response:", response)
# Translate response back to original language
if original_lang_code != "en":
final_response = asyncio.run(translate_async(response, dest_lang=original_lang_code))
else:
final_response = response # No translation needed if English
print("Final Response (in original language):", final_response)
# Return the final response
return jsonify({"response": final_response, "language": language_name})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)
|