ghosthets commited on
Commit
a5e6555
·
verified ·
1 Parent(s): d2666b2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import flask # Gradio nahi, Flask hi rakhte hain
2
+ from flask import request, jsonify
3
+ # from transformers import pipeline # अब इसकी ज़रूरत नहीं
4
+ # import torch # अब इसकी ज़रूरत नहीं
5
+
6
+ from ctransformers import AutoModelForCausalLM # ctransformers से मॉडल लोड करेंगे
7
+
8
+ app = flask.Flask(__name__)
9
+
10
+ # ===========================
11
+ # LOAD MODEL
12
+ # ===========================
13
+ model_id = "TheBloke/Mistral-7B-Instruct-v0.2-GGUF"
14
+ print("🔄 Loading model...")
15
+
16
+ try:
17
+ # ctransformers का उपयोग करके GGUF मॉडल को CPU पर लोड करें
18
+ ai = AutoModelForCausalLM.from_pretrained(
19
+ model_id,
20
+ model_file="mistral-7b-instruct-v0.2.Q4_K_M.gguf", # GGUF फ़ाइल का नाम
21
+ model_type="mistral",
22
+ gpu_layers=0 # CPU पर चलाने के लिए
23
+ )
24
+ print("✅ Model loaded!")
25
+ except Exception as e:
26
+ print(f"❌ Error loading model: {e}")
27
+ # Fallback/Exit strategy here if loading fails
28
+
29
+ # ===========================
30
+ # CHAT API
31
+ # ===========================
32
+ @app.route('/chat', methods=['POST'])
33
+ def chat():
34
+ try:
35
+ data = request.get_json()
36
+ msg = data.get("message", "")
37
+ if not msg:
38
+ return jsonify({"error": "No message sent"}), 400
39
+
40
+ # ctransformers से response generate करें
41
+ output = ai(msg, max_new_tokens=200, temperature=0.7)
42
+ return jsonify({"reply": output})
43
+ except Exception as e:
44
+ return jsonify({"error": str(e)}), 500
45
+
46
+ # ===========================
47
+ # RUN SERVER
48
+ # ===========================
49
+ if __name__ == "__main__":
50
+ app.run(host='0.0.0.0', port=7860)