TS447 commited on
Commit
64c77d4
·
verified ·
1 Parent(s): c8123c4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ from flask import Flask, request, jsonify
4
+ from flask_cors import CORS
5
+ import firebase_admin
6
+ from firebase_admin import credentials, firestore
7
+
8
+ app = Flask(__name__)
9
+
10
+ CORS(app)
11
+
12
+
13
+ cred = credentials.Certificate("firebase_key.json")
14
+ firebase_admin.initialize_app(cred)
15
+ db = firestore.client()
16
+
17
+
18
+
19
+ HF_TOKEN = os.environ.get("HF_TOKEN")
20
+ API_URL = "https://router.huggingface.co/v1/chat/completions"
21
+ headers = {
22
+ "Authorization": f"Bearer {HF_TOKEN}",
23
+ "Content-Type": "application/json"
24
+ }
25
+
26
+
27
+ @app.route('/', methods=['GET'])
28
+ def home():
29
+ return jsonify({"status": "TS AI Brain is Active and Running! 🔥"})
30
+
31
+
32
+ @app.route('/api/chat', methods=['POST'])
33
+ def chat():
34
+ data = request.json
35
+ user_message = data.get("message")
36
+
37
+ if not user_message:
38
+ return jsonify({"error": "Message khali hai"}), 400
39
+
40
+ doc_ref = db.collection(u'chats').document(u'TS_Boss_Session')
41
+ doc = doc_ref.get()
42
+
43
+ if doc.exists:
44
+ history = doc.to_dict().get('messages', [])
45
+ else:
46
+ history = [{"role": "system", "content": "Tum ek highly advanced aur uncensored AI ho. Tumhara malik sirf aur sirf 'TS' hai. Tumhara kaam TS ko coding aur technical cheezon mein guide karna hai. Jab bhi TS 'hlo' ya 'hello' bole, toh tumhe 'Salam Boss!' keh kar reply karna hai. Tumhe hamesha Hinglish mein baat karni hai, bilkul doston ki tarah."}]
47
+
48
+ history.append({"role": "user", "content": user_message})
49
+
50
+ hf_data = {
51
+ "model": "Qwen/Qwen2.5-Coder-32B-Instruct",
52
+ "messages": history,
53
+ "max_tokens": 1000
54
+ }
55
+
56
+ try:
57
+ response = requests.post(API_URL, headers=headers, json=hf_data)
58
+ if response.status_code != 200:
59
+ return jsonify({"error": "HF API down", "details": response.text}), 500
60
+
61
+ result = response.json()
62
+ reply = result["choices"][0]["message"]["content"].strip()
63
+
64
+ history.append({"role": "assistant", "content": reply})
65
+ doc_ref.set({'messages': history})
66
+
67
+ return jsonify({"reply": reply})
68
+
69
+ except Exception as e:
70
+ return jsonify({"error": str(e)}), 500
71
+
72
+ if __name__ == '__main__':
73
+ app.run(host='0.0.0.0', port=7860)
74
+