Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
-
import time
|
| 4 |
from flask import Flask, request, jsonify
|
| 5 |
from flask_cors import CORS
|
| 6 |
import firebase_admin
|
|
@@ -16,23 +15,17 @@ db = firestore.client()
|
|
| 16 |
|
| 17 |
# --- SECRETS ---
|
| 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 |
-
# MODELS
|
| 23 |
TEXT_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 24 |
-
|
| 25 |
-
VISION_MODELS = [
|
| 26 |
-
"meta-llama/Llama-3.2-11B-Vision-Instruct",
|
| 27 |
-
"Qwen/Qwen2-VL-7B-Instruct",
|
| 28 |
-
"microsoft/Phi-3.5-vision-instruct"
|
| 29 |
-
]
|
| 30 |
-
|
| 31 |
-
ROUTER_URL = "https://router.huggingface.co/v1/chat/completions"
|
| 32 |
|
| 33 |
@app.route('/', methods=['GET'])
|
| 34 |
def home():
|
| 35 |
-
return jsonify({"status": "TS AI Brain (
|
| 36 |
|
| 37 |
@app.route('/api/chat', methods=['POST'])
|
| 38 |
def chat():
|
|
@@ -41,10 +34,10 @@ def chat():
|
|
| 41 |
image_url = None
|
| 42 |
|
| 43 |
if is_form_data or request.files:
|
| 44 |
-
user_message = request.form.get("message", "
|
| 45 |
image_file = request.files.get("image")
|
| 46 |
if image_file:
|
| 47 |
-
# 1. TELEGRAM
|
| 48 |
tg_url = f"https://api.telegram.org/bot{TG_BOT_TOKEN}/sendPhoto"
|
| 49 |
files = {'photo': (image_file.filename, image_file.read(), image_file.mimetype)}
|
| 50 |
data = {'chat_id': TG_CHAT_ID}
|
|
@@ -55,62 +48,43 @@ def chat():
|
|
| 55 |
file_info = requests.get(f"https://api.telegram.org/bot{TG_BOT_TOKEN}/getFile?file_id={file_id}").json()
|
| 56 |
image_url = f"https://api.telegram.org/file/bot{TG_BOT_TOKEN}/{file_info['result']['file_path']}"
|
| 57 |
except Exception as e:
|
| 58 |
-
return jsonify({"error": "Telegram Fail"
|
| 59 |
|
| 60 |
else:
|
| 61 |
-
|
| 62 |
-
user_message = data.get("message", "")
|
| 63 |
|
| 64 |
# LOAD HISTORY
|
| 65 |
doc_ref = db.collection(u'chats').document(u'TS_Boss_Session')
|
| 66 |
doc = doc_ref.get()
|
| 67 |
-
history = doc.to_dict().get('messages', []) if doc.exists else [{"role": "system", "content": "Tum TS Boss ke
|
| 68 |
-
|
| 69 |
-
headers = {
|
| 70 |
-
"Authorization": f"Bearer {HF_TOKEN}",
|
| 71 |
-
"Content-Type": "application/json",
|
| 72 |
-
"x-wait-for-model": "true"
|
| 73 |
-
}
|
| 74 |
|
| 75 |
try:
|
| 76 |
-
reply = ""
|
| 77 |
if image_url:
|
| 78 |
-
# --- 2.
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
if "choices" in result:
|
| 90 |
-
reply = result["choices"]["message"]["content"]
|
| 91 |
-
break # Agar ek model ne jawab de diya, toh loop rok do
|
| 92 |
-
else:
|
| 93 |
-
last_error = result
|
| 94 |
-
print(f"Model {model_name} failed, trying next...")
|
| 95 |
-
|
| 96 |
-
if not reply:
|
| 97 |
-
return jsonify({"error": "Saare Vision Models down hain!", "last_api_error": last_error}), 500
|
| 98 |
-
|
| 99 |
-
history.append({"role": "user", "content": f"[Boss sent an image] {user_message}"})
|
| 100 |
else:
|
| 101 |
-
# --- 3. TEXT
|
| 102 |
history.append({"role": "user", "content": user_message})
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
| 105 |
reply = resp.json()["choices"][0]["message"]["content"]
|
| 106 |
|
| 107 |
-
# Save and return
|
| 108 |
history.append({"role": "assistant", "content": reply})
|
| 109 |
doc_ref.set({'messages': history})
|
| 110 |
return jsonify({"reply": reply})
|
| 111 |
|
| 112 |
except Exception as e:
|
| 113 |
-
return jsonify({"error": "
|
| 114 |
|
| 115 |
if __name__ == '__main__':
|
| 116 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
|
|
|
| 3 |
from flask import Flask, request, jsonify
|
| 4 |
from flask_cors import CORS
|
| 5 |
import firebase_admin
|
|
|
|
| 15 |
|
| 16 |
# --- SECRETS ---
|
| 17 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 18 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 19 |
TG_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN")
|
| 20 |
TG_CHAT_ID = os.environ.get("TELEGRAM_CHAT_ID")
|
| 21 |
|
| 22 |
+
# MODELS
|
| 23 |
TEXT_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 24 |
+
VISION_MODEL = "llama-3.2-11b-vision-preview" # Groq ka superfast vision model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
@app.route('/', methods=['GET'])
|
| 27 |
def home():
|
| 28 |
+
return jsonify({"status": "TS AI Brain (Groq-Vision Hybrid) is Live! 🔥"})
|
| 29 |
|
| 30 |
@app.route('/api/chat', methods=['POST'])
|
| 31 |
def chat():
|
|
|
|
| 34 |
image_url = None
|
| 35 |
|
| 36 |
if is_form_data or request.files:
|
| 37 |
+
user_message = request.form.get("message", "Explain this image.")
|
| 38 |
image_file = request.files.get("image")
|
| 39 |
if image_file:
|
| 40 |
+
# 1. TELEGRAM STORAGE (Working perfectly)
|
| 41 |
tg_url = f"https://api.telegram.org/bot{TG_BOT_TOKEN}/sendPhoto"
|
| 42 |
files = {'photo': (image_file.filename, image_file.read(), image_file.mimetype)}
|
| 43 |
data = {'chat_id': TG_CHAT_ID}
|
|
|
|
| 48 |
file_info = requests.get(f"https://api.telegram.org/bot{TG_BOT_TOKEN}/getFile?file_id={file_id}").json()
|
| 49 |
image_url = f"https://api.telegram.org/file/bot{TG_BOT_TOKEN}/{file_info['result']['file_path']}"
|
| 50 |
except Exception as e:
|
| 51 |
+
return jsonify({"error": "Telegram Fail"}), 500
|
| 52 |
|
| 53 |
else:
|
| 54 |
+
user_message = request.json.get("message", "")
|
|
|
|
| 55 |
|
| 56 |
# LOAD HISTORY
|
| 57 |
doc_ref = db.collection(u'chats').document(u'TS_Boss_Session')
|
| 58 |
doc = doc_ref.get()
|
| 59 |
+
history = doc.to_dict().get('messages', []) if doc.exists else [{"role": "system", "content": "Tum TS Boss ke AI ho. Hinglish mein baat karo."}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
try:
|
|
|
|
| 62 |
if image_url:
|
| 63 |
+
# --- 2. VISION CALL VIA GROQ (No more Router errors!) ---
|
| 64 |
+
groq_url = "https://api.groq.com/openai/v1/chat/completions"
|
| 65 |
+
headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
|
| 66 |
+
payload = {
|
| 67 |
+
"model": VISION_MODEL,
|
| 68 |
+
"messages": [{"role": "user", "content": [{"type": "text", "text": user_message}, {"type": "image_url", "image_url": {"url": image_url}}]}],
|
| 69 |
+
}
|
| 70 |
+
resp = requests.post(groq_url, headers=headers, json=payload)
|
| 71 |
+
reply = resp.json()["choices"]["message"]["content"]
|
| 72 |
+
history.append({"role": "user", "content": f"[Screenshot] {user_message}"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
else:
|
| 74 |
+
# --- 3. TEXT CALL VIA HUGGING FACE (32B) ---
|
| 75 |
history.append({"role": "user", "content": user_message})
|
| 76 |
+
hf_url = "https://router.huggingface.co/v1/chat/completions"
|
| 77 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"}
|
| 78 |
+
payload = {"model": TEXT_MODEL, "messages": history}
|
| 79 |
+
resp = requests.post(hf_url, headers=headers, json=payload)
|
| 80 |
reply = resp.json()["choices"][0]["message"]["content"]
|
| 81 |
|
|
|
|
| 82 |
history.append({"role": "assistant", "content": reply})
|
| 83 |
doc_ref.set({'messages': history})
|
| 84 |
return jsonify({"reply": reply})
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
+
return jsonify({"error": "Vision API Error", "details": str(e)}), 500
|
| 88 |
|
| 89 |
if __name__ == '__main__':
|
| 90 |
app.run(host='0.0.0.0', port=7860)
|