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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -27
app.py CHANGED
@@ -15,59 +15,117 @@ 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)
 
15
  db = firestore.client()
16
 
17
 
18
+ HF_TOKEN = os.environ.get("HF_TOKEN")
19
+ TG_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN")
20
+ TG_CHAT_ID = os.environ.get("TELEGRAM_CHAT_ID")
21
 
 
22
  API_URL = "https://router.huggingface.co/v1/chat/completions"
 
 
 
 
23
 
24
 
25
+ TEXT_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct" # Pehla Dimaag (Coding/Logic)
26
+ VISION_MODEL = "meta-llama/Llama-3.2-11B-Vision-Instruct" # Dusra Dimaag (Aankhein)
27
+
28
  @app.route('/', methods=['GET'])
29
  def home():
30
+ return jsonify({"status": "TS AI Brain (Dual Module + Telegram Godown) is Active! 🔥"})
 
31
 
32
  @app.route('/api/chat', methods=['POST'])
33
  def chat():
 
 
34
 
35
+ is_form_data = 'multipart/form-data' in request.content_type if request.content_type else False
36
+
37
+ user_message = ""
38
+ image_url = None
39
 
40
+ if is_form_data or request.files:
41
+ user_message = request.form.get("message", "Is photo ko dhyan se dekh kar batao isme kya hai aur kya problem/error hai.")
42
+ image_file = request.files.get("image")
43
+
44
+ if image_file:
45
+
46
+ tg_url = f"https://api.telegram.org/bot{TG_BOT_TOKEN}/sendPhoto"
47
+ files = {'photo': (image_file.filename, image_file.read(), image_file.mimetype)}
48
+ data = {'chat_id': TG_CHAT_ID}
49
+
50
+ try:
51
+ tg_resp = requests.post(tg_url, data=data, files=files).json()
52
+ if tg_resp.get('ok'):
53
+
54
+ file_id = tg_resp['result']['photo'][-1]['file_id']
55
+ file_info = requests.get(f"https://api.telegram.org/bot{TG_BOT_TOKEN}/getFile?file_id={file_id}").json()
56
+ file_path = file_info['result']['file_path']
57
+ image_url = f"https://api.telegram.org/file/bot{TG_BOT_TOKEN}/{file_path}"
58
+ except Exception as e:
59
+ return jsonify({"error": "Telegram upload fail ho gaya", "details": str(e)}), 500
60
+ else:
61
+
62
+ data = request.json
63
+ user_message = data.get("message", "")
64
+
65
+ if not user_message and not image_url:
66
+ return jsonify({"error": "Message ya photo khali hai"}), 400
67
+
68
+
69
  doc_ref = db.collection(u'chats').document(u'TS_Boss_Session')
70
  doc = doc_ref.get()
 
71
  if doc.exists:
72
  history = doc.to_dict().get('messages', [])
73
  else:
74
+ history = [{"role": "system", "content": "Tum TS Boss ke personal aur highly advanced AI ho. Tumhe hamesha Hinglish mein baat karni hai, bilkul doston ki tarah."}]
 
 
75
 
76
+ headers = {
77
+ "Authorization": f"Bearer {HF_TOKEN}",
78
+ "Content-Type": "application/json"
 
79
  }
80
 
81
  try:
82
+ if image_url:
83
+
84
+ vision_payload = {
85
+ "model": VISION_MODEL,
86
+ "messages": [
87
+ {
88
+ "role": "user",
89
+ "content": [
90
+ {"type": "text", "text": user_message},
91
+ {"type": "image_url", "image_url": {"url": image_url}}
92
+ ]
93
+ }
94
+ ],
95
+ "max_tokens": 1500
96
+ }
97
+ response = requests.post(API_URL, headers=headers, json=vision_payload)
98
+ if response.status_code != 200:
99
+ return jsonify({"error": "Vision API down", "details": response.text}), 500
100
 
101
+ reply = response.json()["choices"]["message"]["content"].strip()
102
+
103
+
104
+ history.append({"role": "user", "content": f"[Boss ne ek Screenshot bheja tha] {user_message}"})
105
+ history.append({"role": "assistant", "content": reply})
106
+
107
+ else:
108
+
109
+ history.append({"role": "user", "content": user_message})
110
+ text_payload = {
111
+ "model": TEXT_MODEL,
112
+ "messages": history,
113
+ "max_tokens": 1500
114
+ }
115
+ response = requests.post(API_URL, headers=headers, json=text_payload)
116
+ if response.status_code != 200:
117
+ return jsonify({"error": "Text API down", "details": response.text}), 500
118
+
119
+ reply = response.json()["choices"][0]["message"]["content"].strip()
120
+ history.append({"role": "assistant", "content": reply})
121
+
122
 
 
123
  doc_ref.set({'messages': history})
124
 
125
  return jsonify({"reply": reply})
126
+
127
  except Exception as e:
128
+ return jsonify({"error": "Dimaag fat gaya!", "details": str(e)}), 500
129
 
130
  if __name__ == '__main__':
131
  app.run(host='0.0.0.0', port=7860)