ghosthets commited on
Commit
7339449
·
verified ·
1 Parent(s): 5715944

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import flask
2
+ from flask import request, jsonify
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
+ import torch
5
+
6
+ app = flask.Flask(__name__)
7
+
8
+ model_id = "facebook/blenderbot-400M-distill"
9
+
10
+ print("🔄 Loading fast chat model...")
11
+
12
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
13
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
14
+
15
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
16
+ model.to(device)
17
+
18
+ print("✅ Model loaded instantly!")
19
+
20
+ @app.route('/chat', methods=['POST'])
21
+ def chat():
22
+ try:
23
+ data = request.get_json()
24
+ msg = data.get("message", "")
25
+
26
+ if not msg:
27
+ return jsonify({"error": "No message sent"}), 400
28
+
29
+ inputs = tokenizer(msg, return_tensors="pt").to(device)
30
+ output = model.generate(
31
+ **inputs,
32
+ max_length=200,
33
+ do_sample=True,
34
+ top_p=0.92,
35
+ temperature=0.7
36
+ )
37
+
38
+ reply = tokenizer.decode(output[0], skip_special_tokens=True)
39
+ return jsonify({"reply": reply})
40
+
41
+ except Exception as e:
42
+ return jsonify({"error": str(e)}), 500
43
+
44
+
45
+ if __name__ == "__main__":
46
+ app.run(host='0.0.0.0', port=7860)