Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,11 +21,11 @@ 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"
|
| 25 |
|
| 26 |
@app.route('/', methods=['GET'])
|
| 27 |
def home():
|
| 28 |
-
return jsonify({"status": "TS AI Brain (
|
| 29 |
|
| 30 |
@app.route('/api/chat', methods=['POST'])
|
| 31 |
def chat():
|
|
@@ -34,10 +34,10 @@ def chat():
|
|
| 34 |
image_url = None
|
| 35 |
|
| 36 |
if is_form_data or request.files:
|
| 37 |
-
user_message = request.form.get("message", "
|
| 38 |
image_file = request.files.get("image")
|
| 39 |
if image_file:
|
| 40 |
-
#
|
| 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}
|
|
@@ -47,8 +47,10 @@ def chat():
|
|
| 47 |
file_id = tg_resp['result']['photo'][-1]['file_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
|
| 52 |
|
| 53 |
else:
|
| 54 |
user_message = request.json.get("message", "")
|
|
@@ -60,7 +62,7 @@ def chat():
|
|
| 60 |
|
| 61 |
try:
|
| 62 |
if image_url:
|
| 63 |
-
# ---
|
| 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 = {
|
|
@@ -68,10 +70,17 @@ def chat():
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
history.append({"role": "user", "content": f"[Screenshot] {user_message}"})
|
| 73 |
else:
|
| 74 |
-
# ---
|
| 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"}
|
|
@@ -84,7 +93,7 @@ def chat():
|
|
| 84 |
return jsonify({"reply": reply})
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
-
return jsonify({"error": "
|
| 88 |
|
| 89 |
if __name__ == '__main__':
|
| 90 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 21 |
|
| 22 |
# MODELS
|
| 23 |
TEXT_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 24 |
+
VISION_MODEL = "llama-3.2-11b-vision-preview"
|
| 25 |
|
| 26 |
@app.route('/', methods=['GET'])
|
| 27 |
def home():
|
| 28 |
+
return jsonify({"status": "TS AI Brain (Debug Mode) 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", "What is in this image?")
|
| 38 |
image_file = request.files.get("image")
|
| 39 |
if image_file:
|
| 40 |
+
# TELEGRAM UPLOAD
|
| 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}
|
|
|
|
| 47 |
file_id = tg_resp['result']['photo'][-1]['file_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 |
+
else:
|
| 51 |
+
return jsonify({"error": "Telegram error", "details": tg_resp}), 500
|
| 52 |
except Exception as e:
|
| 53 |
+
return jsonify({"error": "Telegram Crash", "details": str(e)}), 500
|
| 54 |
|
| 55 |
else:
|
| 56 |
user_message = request.json.get("message", "")
|
|
|
|
| 62 |
|
| 63 |
try:
|
| 64 |
if image_url:
|
| 65 |
+
# --- VISION CALL VIA GROQ ---
|
| 66 |
groq_url = "https://api.groq.com/openai/v1/chat/completions"
|
| 67 |
headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
|
| 68 |
payload = {
|
|
|
|
| 70 |
"messages": [{"role": "user", "content": [{"type": "text", "text": user_message}, {"type": "image_url", "image_url": {"url": image_url}}]}],
|
| 71 |
}
|
| 72 |
resp = requests.post(groq_url, headers=headers, json=payload)
|
| 73 |
+
result = resp.json()
|
| 74 |
+
|
| 75 |
+
# AGAR RESPONSE MEIN ERROR HAI TOH BATAYO
|
| 76 |
+
if "choices" in result:
|
| 77 |
+
reply = result["choices"]["message"]["content"]
|
| 78 |
+
else:
|
| 79 |
+
return jsonify({"error": "Groq ne mana kar diya!", "raw_response": result}), 500
|
| 80 |
+
|
| 81 |
history.append({"role": "user", "content": f"[Screenshot] {user_message}"})
|
| 82 |
else:
|
| 83 |
+
# --- TEXT CALL VIA HUGGING FACE ---
|
| 84 |
history.append({"role": "user", "content": user_message})
|
| 85 |
hf_url = "https://router.huggingface.co/v1/chat/completions"
|
| 86 |
headers = {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"}
|
|
|
|
| 93 |
return jsonify({"reply": reply})
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
+
return jsonify({"error": "System Error", "details": str(e)}), 500
|
| 97 |
|
| 98 |
if __name__ == '__main__':
|
| 99 |
app.run(host='0.0.0.0', port=7860)
|