Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,44 @@
|
|
| 1 |
-
from transformers import
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
model_name = "sshleifer/tiny-gpt2"
|
| 5 |
|
| 6 |
-
|
| 7 |
-
pipe = pipeline("text-generation", model=model_name)
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
from flask import Flask, request, render_template_string
|
| 3 |
|
| 4 |
+
app = Flask(__name__)
|
|
|
|
| 5 |
|
| 6 |
+
pipe = pipeline("text-generation", model="sshleifer/tiny-gpt2")
|
|
|
|
| 7 |
|
| 8 |
+
HTML_PAGE = """
|
| 9 |
+
<!DOCTYPE html>
|
| 10 |
+
<html>
|
| 11 |
+
<head>
|
| 12 |
+
<title>TinyGPT2 Chat</title>
|
| 13 |
+
<style>
|
| 14 |
+
body { font-family: sans-serif; margin: 40px; }
|
| 15 |
+
textarea { width: 100%; height: 100px; }
|
| 16 |
+
button { margin-top: 10px; padding: 8px 16px; }
|
| 17 |
+
.output { margin-top: 20px; white-space: pre-wrap; }
|
| 18 |
+
</style>
|
| 19 |
+
</head>
|
| 20 |
+
<body>
|
| 21 |
+
<h1>π€ TinyGPT2 Chat</h1>
|
| 22 |
+
<form method="POST">
|
| 23 |
+
<textarea name="prompt" placeholder="Type your message here...">{{prompt}}</textarea><br>
|
| 24 |
+
<button type="submit">Generate</button>
|
| 25 |
+
</form>
|
| 26 |
+
{% if output %}
|
| 27 |
+
<div class="output"><strong>AI:</strong> {{output}}</div>
|
| 28 |
+
{% endif %}
|
| 29 |
+
</body>
|
| 30 |
+
</html>
|
| 31 |
+
"""
|
| 32 |
|
| 33 |
+
@app.route("/", methods=["GET", "POST"])
|
| 34 |
+
def chat():
|
| 35 |
+
output = ""
|
| 36 |
+
prompt = ""
|
| 37 |
+
if request.method == "POST":
|
| 38 |
+
prompt = request.form["prompt"]
|
| 39 |
+
result = pipe(prompt, max_length=100)[0]["generated_text"]
|
| 40 |
+
output = result
|
| 41 |
+
return render_template_string(HTML_PAGE, output=output, prompt=prompt)
|
| 42 |
+
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
app.run(host="0.0.0.0", port=7860)
|