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, request
|
5 |
from dotenv import load_dotenv
|
6 |
|
7 |
load_dotenv()
|
@@ -37,10 +37,10 @@ def generate_text(parent_name, child_name, language):
|
|
37 |
temperature=0.8,
|
38 |
max_tokens=1024,
|
39 |
top_p=0.65,
|
40 |
-
stream=
|
41 |
stop=None,
|
42 |
)
|
43 |
-
return completion
|
44 |
except Exception as e:
|
45 |
app.logger.error(f"Error generating text: {str(e)}")
|
46 |
return "An error occurred while generating the text."
|
@@ -113,39 +113,75 @@ html_template = """
|
|
113 |
</head>
|
114 |
<body>
|
115 |
<h1>Silly Detention Letter Generator</h1>
|
116 |
-
|
117 |
-
<
|
118 |
-
<
|
119 |
-
{% endif %}
|
120 |
-
<form method="post">
|
121 |
<input type="text" name="parent_name" placeholder="Parent's Name" required>
|
122 |
<input type="text" name="child_name" placeholder="Child's Name" required>
|
123 |
<select name="language" required>
|
124 |
-
<option value="posh">Posh British English</option>
|
125 |
<option value="english">Standard English</option>
|
126 |
<option value="hungarian">Hungarian</option>
|
127 |
</select>
|
128 |
<input type="submit" value="Generate Letter">
|
129 |
</form>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
</body>
|
131 |
</html>
|
132 |
"""
|
133 |
|
134 |
# Define Flask routes
|
135 |
-
@app.route("/", methods=["GET"
|
136 |
def home():
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
|
|
149 |
|
150 |
if __name__ == "__main__":
|
151 |
app.run(host="0.0.0.0", port=7860)
|
|
|
1 |
import os
|
2 |
import logging
|
3 |
from groq import Groq
|
4 |
+
from flask import Flask, render_template_string, request, Response, stream_with_context
|
5 |
from dotenv import load_dotenv
|
6 |
|
7 |
load_dotenv()
|
|
|
37 |
temperature=0.8,
|
38 |
max_tokens=1024,
|
39 |
top_p=0.65,
|
40 |
+
stream=True,
|
41 |
stop=None,
|
42 |
)
|
43 |
+
return completion
|
44 |
except Exception as e:
|
45 |
app.logger.error(f"Error generating text: {str(e)}")
|
46 |
return "An error occurred while generating the text."
|
|
|
113 |
</head>
|
114 |
<body>
|
115 |
<h1>Silly Detention Letter Generator</h1>
|
116 |
+
<div id="letter"></div>
|
117 |
+
<button id="regenerate" style="display: none;" onclick="regenerateLetter()">Regenerate Letter</button>
|
118 |
+
<form id="letterForm" onsubmit="generateLetter(event)">
|
|
|
|
|
119 |
<input type="text" name="parent_name" placeholder="Parent's Name" required>
|
120 |
<input type="text" name="child_name" placeholder="Child's Name" required>
|
121 |
<select name="language" required>
|
122 |
+
<option value="posh">Posh British English (Private School)</option>
|
123 |
<option value="english">Standard English</option>
|
124 |
<option value="hungarian">Hungarian</option>
|
125 |
</select>
|
126 |
<input type="submit" value="Generate Letter">
|
127 |
</form>
|
128 |
+
<script>
|
129 |
+
function generateLetter(event) {
|
130 |
+
event.preventDefault();
|
131 |
+
const form = event.target;
|
132 |
+
const formData = new FormData(form);
|
133 |
+
|
134 |
+
fetch('/generate', {
|
135 |
+
method: 'POST',
|
136 |
+
body: formData
|
137 |
+
})
|
138 |
+
.then(response => {
|
139 |
+
const reader = response.body.getReader();
|
140 |
+
const decoder = new TextDecoder();
|
141 |
+
const letterDiv = document.getElementById('letter');
|
142 |
+
letterDiv.innerHTML = '';
|
143 |
+
|
144 |
+
function read() {
|
145 |
+
reader.read().then(({ done, value }) => {
|
146 |
+
if (done) {
|
147 |
+
document.getElementById('regenerate').style.display = 'block';
|
148 |
+
return;
|
149 |
+
}
|
150 |
+
const chunk = decoder.decode(value);
|
151 |
+
letterDiv.innerHTML += chunk;
|
152 |
+
read();
|
153 |
+
});
|
154 |
+
}
|
155 |
+
|
156 |
+
read();
|
157 |
+
});
|
158 |
+
}
|
159 |
+
|
160 |
+
function regenerateLetter() {
|
161 |
+
document.getElementById('letterForm').dispatchEvent(new Event('submit'));
|
162 |
+
}
|
163 |
+
</script>
|
164 |
</body>
|
165 |
</html>
|
166 |
"""
|
167 |
|
168 |
# Define Flask routes
|
169 |
+
@app.route("/", methods=["GET"])
|
170 |
def home():
|
171 |
+
return render_template_string(html_template)
|
172 |
+
|
173 |
+
@app.route("/generate", methods=["POST"])
|
174 |
+
def generate():
|
175 |
+
parent_name = request.form["parent_name"]
|
176 |
+
child_name = request.form["child_name"]
|
177 |
+
language = request.form["language"]
|
178 |
+
|
179 |
+
def generate_streaming_response():
|
180 |
+
for chunk in generate_text(parent_name, child_name, language):
|
181 |
+
if hasattr(chunk.choices[0].delta, 'content'):
|
182 |
+
yield chunk.choices[0].delta.content or ""
|
183 |
+
|
184 |
+
return Response(stream_with_context(generate_streaming_response()), content_type='text/plain')
|
185 |
|
186 |
if __name__ == "__main__":
|
187 |
app.run(host="0.0.0.0", port=7860)
|