silly-letter / app.py
bencser's picture
Update app.py
29685b5 verified
raw
history blame
3.94 kB
import os
import logging
from groq import Groq
from flask import Flask, render_template_string, request
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
# Set up logging
logging.basicConfig(level=logging.DEBUG)
# 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):
prompt = f"Generate a concise silly letter to {parent_name} about their child {child_name}'s detention reasons. Write extreme behaviours in posh British English used in private schools."
try:
completion = client.chat.completions.create(
model=model,
messages=[
{
"role": "user",
"content": prompt
}
],
temperature=0.8,
max_tokens=1024,
top_p=0.65,
stream=False,
stop=None,
)
return completion.choices[0].message.content.strip()
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 = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Silly Detention Letter Generator</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f4f4f4;
}
h1 {
color: #2c3e50;
text-align: center;
}
#letter {
background-color: white;
border: 1px solid #ddd;
padding: 20px;
border-radius: 5px;
white-space: pre-wrap;
margin-bottom: 20px;
}
form {
background-color: white;
padding: 20px;
border-radius: 5px;
margin-bottom: 20px;
}
input[type="text"] {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
input[type="submit"], #regenerate {
display: block;
width: 200px;
margin: 20px auto;
padding: 10px;
background-color: #3498db;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
input[type="submit"]:hover, #regenerate:hover {
background-color: #2980b9;
}
</style>
</head>
<body>
<h1>Silly Detention Letter Generator</h1>
<form method="post">
<input type="text" name="parent_name" placeholder="Parent's Name" required>
<input type="text" name="child_name" placeholder="Child's Name" required>
<input type="submit" value="Generate Letter">
</form>
{% if message %}
<div id="letter">{{ message }}</div>
<button id="regenerate" onclick="location.reload()">Regenerate Letter</button>
{% endif %}
</body>
</html>
"""
# Define Flask routes
@app.route("/", methods=["GET", "POST"])
def home():
try:
if request.method == "POST":
parent_name = request.form["parent_name"]
child_name = request.form["child_name"]
message = generate_text(parent_name, child_name)
else:
message = None
return render_template_string(html_template, message=message)
except Exception as e:
app.logger.error(f"Error in home route: {str(e)}")
return "An error occurred. Please try again later.", 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)