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_pair1(): # Select a random English-Spanish pair random_pair = random.choice(json_data) return jsonify(random_pair) def is_word(s): """ Check if the string 's' is a word (contains only alphabetic characters). """ return s.isalpha() # Lists to store English and Spanish words separately english_words = set() spanish_words = set() # Populate the word lists for pair in json_data: if "english" in pair: # Extract words from the English sentence and filter out numbers english_words.update(filter(is_word, re.findall(r'\b\w+\b', pair["english"]))) if "spanish" in pair: # Extract words from the Spanish sentence and filter out numbers spanish_words.update(filter(is_word, re.findall(r'\b\w+\b', pair["spanish"]))) def get_distractors(target_word, all_words, num_distractors=3): """ Get distractor words from the same language. """ 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 and filter out non-words words = filter(is_word, re.findall(r'\b\w+\b', sentence)) # Choose a random word to replace with blank blank_word = random.choice(list(words)) sentence_with_blank = sentence.replace(blank_word, "_____") # Get distractors from the same language 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))