Spaces:
Running
Running
File size: 3,133 Bytes
939c80c 6bc2af2 5abd3c7 f40ebd6 3affa92 5abd3c7 3affa92 5abd3c7 939c80c 5abd3c7 3affa92 5abd3c7 939c80c 5abd3c7 939c80c 2ac09a0 836cf05 2ac09a0 836cf05 2ac09a0 836cf05 681ec4e 836cf05 2ac09a0 836cf05 2ac09a0 836cf05 2ac09a0 836cf05 2ac09a0 836cf05 2ac09a0 836cf05 2ac09a0 836cf05 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 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
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'] = ''
from googletranslate import translate
import json
import random
import re
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)
english_words = set()
spanish_words = set()
# Populate the word lists
for pair in json_data:
if "english" in pair:
english_words.update(re.findall(r'\b\w+\b', pair["english"]))
if "spanish" in pair:
spanish_words.update(re.findall(r'\b\w+\b', pair["spanish"]))
def get_distractors(target_word, all_words, num_distractors=3):
"""
Get distractor words.
"""
distractors = set()
while len(distractors) < num_distractors:
distractor = random.choice(list(all_words))
if distractor.lower() != target_word.lower():
distractors.add(distractor)
return list(distractors)
@app.route('/fillgame')
def random_spanish_pair2():
# Select a random English-Spanish pair
random_pair = random.choice(json_data)
# Choose either English or Spanish for the fill-in-the-blank game
if random.choice([True, False]):
sentence = random_pair.get('english', "")
language = 'english'
word_set = english_words
else:
sentence = random_pair.get('spanish', "")
language = 'spanish'
word_set = spanish_words
# Split the sentence into words
words = re.findall(r'\b\w+\b', sentence)
# Choose a random word to replace with blank
blank_word = random.choice(words)
sentence_with_blank = sentence.replace(blank_word, "_____")
# Get distractors
distractors = get_distractors(blank_word, word_set)
# Combine correct word with distractors and shuffle
options = [blank_word] + distractors
random.shuffle(options)
# Return the sentence with a blank, options, and the correct word
return jsonify({
'sentence': sentence_with_blank,
'options': options,
'correctWord': blank_word,
'language': language
})
@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)) |