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 = """ Silly Detention Letter Generator

Silly Detention Letter Generator

""" # Define Flask routes @app.route("/", methods=["GET"]) def home(): return render_template_string(html_template) @app.route("/generate", methods=["POST"]) @limiter.limit("5/minute") def generate(): parent_name = request.form["parent_name"] child_name = request.form["child_name"] language = request.form["language"] def generate_streaming_response(): for chunk in generate_text(parent_name, child_name, language): if hasattr(chunk.choices[0].delta, 'content'): yield chunk.choices[0].delta.content or "" return Response(stream_with_context(generate_streaming_response()), content_type='text/plain') if __name__ == "__main__": app.run(host="0.0.0.0", port=7860)