Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import os
|
2 |
import logging
|
3 |
from groq import Groq
|
4 |
-
from flask import Flask, render_template_string
|
5 |
from dotenv import load_dotenv
|
6 |
|
7 |
load_dotenv()
|
@@ -17,21 +17,19 @@ client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
|
17 |
# Set model
|
18 |
model = "llama-3.1-70b-versatile"
|
19 |
|
20 |
-
# Set fixed prompt
|
21 |
-
fixed_prompt = "generate a concise silly letter to parents about their child detention reasons. write extreme behaviours"
|
22 |
-
|
23 |
# Define function to generate text
|
24 |
-
def generate_text():
|
|
|
25 |
try:
|
26 |
completion = client.chat.completions.create(
|
27 |
model=model,
|
28 |
messages=[
|
29 |
{
|
30 |
"role": "user",
|
31 |
-
"content":
|
32 |
}
|
33 |
],
|
34 |
-
temperature=0.
|
35 |
max_tokens=1024,
|
36 |
top_p=0.65,
|
37 |
stream=False,
|
@@ -72,7 +70,20 @@ html_template = """
|
|
72 |
white-space: pre-wrap;
|
73 |
margin-bottom: 20px;
|
74 |
}
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
display: block;
|
77 |
width: 200px;
|
78 |
margin: 20px auto;
|
@@ -84,24 +95,36 @@ html_template = """
|
|
84 |
cursor: pointer;
|
85 |
font-size: 16px;
|
86 |
}
|
87 |
-
#regenerate:hover {
|
88 |
background-color: #2980b9;
|
89 |
}
|
90 |
</style>
|
91 |
</head>
|
92 |
<body>
|
93 |
<h1>Silly Detention Letter Generator</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
<div id="letter">{{ message }}</div>
|
95 |
<button id="regenerate" onclick="location.reload()">Regenerate Letter</button>
|
|
|
96 |
</body>
|
97 |
</html>
|
98 |
"""
|
99 |
|
100 |
-
# Define Flask
|
101 |
-
@app.route("/")
|
102 |
def home():
|
103 |
try:
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
105 |
return render_template_string(html_template, message=message)
|
106 |
except Exception as e:
|
107 |
app.logger.error(f"Error in home route: {str(e)}")
|
|
|
1 |
import os
|
2 |
import logging
|
3 |
from groq import Groq
|
4 |
+
from flask import Flask, render_template_string, request
|
5 |
from dotenv import load_dotenv
|
6 |
|
7 |
load_dotenv()
|
|
|
17 |
# Set model
|
18 |
model = "llama-3.1-70b-versatile"
|
19 |
|
|
|
|
|
|
|
20 |
# Define function to generate text
|
21 |
+
def generate_text(parent_name, child_name):
|
22 |
+
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."
|
23 |
try:
|
24 |
completion = client.chat.completions.create(
|
25 |
model=model,
|
26 |
messages=[
|
27 |
{
|
28 |
"role": "user",
|
29 |
+
"content": prompt
|
30 |
}
|
31 |
],
|
32 |
+
temperature=0.8,
|
33 |
max_tokens=1024,
|
34 |
top_p=0.65,
|
35 |
stream=False,
|
|
|
70 |
white-space: pre-wrap;
|
71 |
margin-bottom: 20px;
|
72 |
}
|
73 |
+
form {
|
74 |
+
background-color: white;
|
75 |
+
padding: 20px;
|
76 |
+
border-radius: 5px;
|
77 |
+
margin-bottom: 20px;
|
78 |
+
}
|
79 |
+
input[type="text"] {
|
80 |
+
width: 100%;
|
81 |
+
padding: 10px;
|
82 |
+
margin-bottom: 10px;
|
83 |
+
border: 1px solid #ddd;
|
84 |
+
border-radius: 5px;
|
85 |
+
}
|
86 |
+
input[type="submit"], #regenerate {
|
87 |
display: block;
|
88 |
width: 200px;
|
89 |
margin: 20px auto;
|
|
|
95 |
cursor: pointer;
|
96 |
font-size: 16px;
|
97 |
}
|
98 |
+
input[type="submit"]:hover, #regenerate:hover {
|
99 |
background-color: #2980b9;
|
100 |
}
|
101 |
</style>
|
102 |
</head>
|
103 |
<body>
|
104 |
<h1>Silly Detention Letter Generator</h1>
|
105 |
+
<form method="post">
|
106 |
+
<input type="text" name="parent_name" placeholder="Parent's Name" required>
|
107 |
+
<input type="text" name="child_name" placeholder="Child's Name" required>
|
108 |
+
<input type="submit" value="Generate Letter">
|
109 |
+
</form>
|
110 |
+
{% if message %}
|
111 |
<div id="letter">{{ message }}</div>
|
112 |
<button id="regenerate" onclick="location.reload()">Regenerate Letter</button>
|
113 |
+
{% endif %}
|
114 |
</body>
|
115 |
</html>
|
116 |
"""
|
117 |
|
118 |
+
# Define Flask routes
|
119 |
+
@app.route("/", methods=["GET", "POST"])
|
120 |
def home():
|
121 |
try:
|
122 |
+
if request.method == "POST":
|
123 |
+
parent_name = request.form["parent_name"]
|
124 |
+
child_name = request.form["child_name"]
|
125 |
+
message = generate_text(parent_name, child_name)
|
126 |
+
else:
|
127 |
+
message = None
|
128 |
return render_template_string(html_template, message=message)
|
129 |
except Exception as e:
|
130 |
app.logger.error(f"Error in home route: {str(e)}")
|