File size: 1,480 Bytes
939c80c
 
 
 
6bc2af2
 
939c80c
5abd3c7
 
 
3affa92
5abd3c7
3affa92
 
 
5abd3c7
 
 
3affa92
 
5abd3c7
3affa92
5abd3c7
 
 
939c80c
5abd3c7
 
 
 
 
3affa92
5abd3c7
 
939c80c
5abd3c7
 
 
 
 
939c80c
5abd3c7
 
3e07850
 
5abd3c7
 
 
3e07850
5abd3c7
 
 
3e07850
5abd3c7
 
3e07850
939c80c
 
 
 
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
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 flask"
    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))