import os import logging from groq import Groq from flask import Flask, render_template_string, request, Response, stream_with_context from flask_limiter import Limiter from flask_limiter.util import get_remote_address from dotenv import load_dotenv load_dotenv() app = Flask(__name__) # Set up logging logging.basicConfig(level=logging.DEBUG) # Set up rate limiter limiter = Limiter( get_remote_address, app=app, default_limits=["5 per minute"], storage_uri="memory://" ) # Set Groq API key client = Groq(api_key=os.getenv("GROQ_API_KEY")) # Set model model = "llama-3.1-70b-versatile" # Define function to generate text def generate_text(parent_name, child_name, language): prompts = { "posh": f"Generate a concise silly letter written in posh British English used in private schools - to {parent_name} about their child {child_name}'s detention reasons. Write extreme but realistic behaviours.", "english": f"Generate a concise silly letter in standard English to {parent_name} about their child {child_name}'s detention reasons. Write extreme but realistic behaviours.", "hungarian": f"Generate a Hungarian language, concise silly letter to {parent_name} about their child {child_name}'s detention reasons. Write extreme but realistic behaviours. Make sur you use formal you." } prompt = prompts[language] try: completion = client.chat.completions.create( model=model, messages=[ { "role": "user", "content": prompt } ], temperature=0.8, max_tokens=1024, top_p=0.65, stream=True, stop=None, ) return completion except Exception as e: app.logger.error(f"Error generating text: {str(e)}") return "An error occurred while generating the text." # HTML template html_template = """