Spaces:
Sleeping
Sleeping
File size: 5,085 Bytes
5de26a8 7dabf28 5de26a8 29685b5 5de26a8 7dabf28 5de26a8 011a663 5de26a8 1f13911 238c235 d0b1639 1f13911 7dabf28 29685b5 7dabf28 29685b5 7dabf28 5de26a8 515c2a5 2348d8c 515c2a5 2348d8c 515c2a5 2348d8c 515c2a5 2348d8c 515c2a5 29685b5 1f13911 29685b5 2348d8c 29685b5 515c2a5 2348d8c 515c2a5 29685b5 515c2a5 238c235 1f13911 d0b1639 238c235 29685b5 1f13911 29685b5 515c2a5 29685b5 5de26a8 7dabf28 29685b5 1f13911 238c235 29685b5 238c235 515c2a5 7dabf28 5de26a8 7dabf28 |
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
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, 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=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>
<link href="https://fonts.googleapis.com/css2?family=Lora:wght@600&family=Open+Sans&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Open Sans', sans-serif;
line-height: 1.6;
color: #333;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f4f4f4;
}
h1 {
font-family: 'Lora', serif;
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;
font-family: 'Lora', serif;
font-size: 1.1em;
}
form {
background-color: white;
padding: 20px;
border-radius: 5px;
margin-bottom: 20px;
}
input[type="text"], select {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ddd;
border-radius: 5px;
font-family: 'Open Sans', sans-serif;
}
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;
font-family: 'Open Sans', sans-serif;
}
input[type="submit"]:hover, #regenerate:hover {
background-color: #2980b9;
}
</style>
</head>
<body>
<h1>Silly Detention Letter Generator</h1>
{% if message %}
<div id="letter">{{ message }}</div>
<button id="regenerate" onclick="location.reload()">Regenerate Letter</button>
{% endif %}
<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>
<select name="language" required>
<option value="posh">Posh British English (Private School)</option>
<option value="english">Standard English</option>
<option value="hungarian">Hungarian</option>
</select>
<input type="submit" value="Generate Letter">
</form>
</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"]
language = request.form["language"]
message = generate_text(parent_name, child_name, language)
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) |