helloperson123 commited on
Commit
5850e2b
Β·
verified Β·
1 Parent(s): e54025f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -13
app.py CHANGED
@@ -1,17 +1,44 @@
1
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
 
2
 
3
- # βœ… Tiny model that fits in Hugging Face Spaces free storage
4
- model_name = "sshleifer/tiny-gpt2"
5
 
6
- print("πŸš€ Loading model, please wait...")
7
- pipe = pipeline("text-generation", model=model_name)
8
 
9
- print("βœ… Model loaded! Try generating some text.\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- while True:
12
- prompt = input("You: ")
13
- if prompt.lower() in ["exit", "quit"]:
14
- break
15
- result = pipe(prompt, max_length=100)[0]['generated_text']
16
- print("\nAI:", result)
17
- print()
 
 
 
 
 
 
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)