from flask import Flask, request, jsonify import asyncio from hypercorn.asyncio import serve from hypercorn.config import Config import os os.environ['CURL_CA_BUNDLE'] = '' def install_package(): command = "pip install git+https://github.com/johnpaulbin/googletranslate" os.system(command) install_package() from googletranslate import translate import json import random app = Flask(__name__) @app.route('/', methods=['GET']) def home(): return "HI! Use /translate POST" # Load the JSON data into memory def load_json_data(file_path): with open(file_path, 'r') as file: data = json.load(file) return data # Assuming your JSON structure is a list of dictionaries json_data = load_json_data('englishspanishpairs.json') @app.route('/spanish') def random_spanish_pair(): # Select a random English-Spanish pair random_pair = random.choice(json_data) return jsonify(random_pair) @app.route('/translate', methods=['POST']) def dotranslate(): data = request.get_json() txt = data.get('txt') src = data.get('src', 'en') dest = data.get('dest', 'es') if txt: translation = translate(txt, dest=dest, src=src) return jsonify({'translation': translation}), 200 else: return jsonify({'error': 'No text provided'}), 400 if __name__ == "__main__": config = Config() config.bind = ["0.0.0.0:7860"] # You can specify the host and port here asyncio.run(serve(app, config))