Spaces:
Running
Running
Vedika commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,92 +1,52 @@
|
|
| 1 |
import os
|
| 2 |
-
import time
|
| 3 |
-
import json
|
| 4 |
import logging
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
from typing import Dict, Any, List, Generator
|
| 8 |
-
from flask import Flask, request, Response, stream_with_context, jsonify
|
| 9 |
|
| 10 |
-
# ---
|
| 11 |
-
logging.basicConfig(level=logging.INFO, format='%(asctime)s
|
| 12 |
-
logger = logging.getLogger("
|
| 13 |
|
| 14 |
-
app = Flask(__name__)
|
| 15 |
|
| 16 |
-
# कॉन्फ़िगरेशन
|
| 17 |
class Config:
|
| 18 |
API_KEY = os.environ.get("YOUR_VEDIKA_API_KEY")
|
| 19 |
ENDPOINT = os.environ.get("BASE_URL")
|
| 20 |
MODEL_ID = os.environ.get("MODEL_ID")
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
@staticmethod
|
| 29 |
-
def construct(user_msg: str, history: List[Dict], system_prompt: str, attachments: List[Dict]):
|
| 30 |
-
messages = [{"role": "system", "content": system_prompt or "You are a helpful assistant."}]
|
| 31 |
-
|
| 32 |
-
# हिस्ट्री जोड़ना
|
| 33 |
-
for h in history:
|
| 34 |
-
messages.append({"role": h.get("role", "user"), "content": h.get("content", "")})
|
| 35 |
-
|
| 36 |
-
# मल्टीमॉडल या टेक्स्ट पेलोड का चयन
|
| 37 |
-
if attachments:
|
| 38 |
-
content = [{"type": "text", "text": user_msg}]
|
| 39 |
-
for att in attachments:
|
| 40 |
-
if att.get("type") == "image":
|
| 41 |
-
content.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{att.get('data')}"}})
|
| 42 |
-
messages.append({"role": "user", "content": content})
|
| 43 |
-
else:
|
| 44 |
-
messages.append({"role": "user", "content": user_msg})
|
| 45 |
-
return messages
|
| 46 |
-
|
| 47 |
-
# --- स्ट्रीम हैंडलर: LLM से रिस्पॉन्स लाना ---
|
| 48 |
-
def stream_llm_response(payload: Dict):
|
| 49 |
-
headers = {"Authorization": f"Bearer {Config.API_KEY}", "Content-Type": "application/json"}
|
| 50 |
-
try:
|
| 51 |
-
with requests.post(Config.ENDPOINT, headers=headers, json=payload, stream=True, timeout=60) as r:
|
| 52 |
-
if r.status_code != 200:
|
| 53 |
-
yield f"data: {json.dumps({'error': 'API Error', 'code': r.status_code})}\n\n"
|
| 54 |
-
return
|
| 55 |
-
for line in r.iter_lines():
|
| 56 |
-
if line:
|
| 57 |
-
yield line.decode('utf-8') + "\n\n"
|
| 58 |
-
except Exception as e:
|
| 59 |
-
logger.error(f"Stream Error: {e}")
|
| 60 |
-
yield f"data: {json.dumps({'error': 'Connection Lost'})}\n\n"
|
| 61 |
|
| 62 |
-
# ---
|
| 63 |
@app.route('/api/chat', methods=['POST'])
|
| 64 |
def chat():
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
payload = {
|
| 77 |
-
"model": Config.MODEL_ID,
|
| 78 |
-
"messages": messages,
|
| 79 |
-
"stream": True,
|
| 80 |
-
"temperature": data.get("temperature", 0.7)
|
| 81 |
-
}
|
| 82 |
-
|
| 83 |
-
return Response(stream_with_context(stream_llm_response(payload)), mimetype='text/event-stream')
|
| 84 |
|
| 85 |
-
|
|
|
|
| 86 |
def health():
|
| 87 |
-
return jsonify({"status": "Arjun Core
|
| 88 |
|
|
|
|
| 89 |
if __name__ == '__main__':
|
| 90 |
port = int(os.environ.get("PORT", 7860))
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
app.run(host='0.0.0.0', port=port, threaded=True)
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import logging
|
| 3 |
+
import time
|
| 4 |
+
from flask import Flask, request, Response, stream_with_context, jsonify, send_from_directory
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# --- 1. एन्टरप्राइज लॉगिंग कॉन्फ़िगरेशन ---
|
| 7 |
+
logging.basicConfig(level=logging.INFO, format='[%(asctime)s] %(levelname)s: %(message)s')
|
| 8 |
+
logger = logging.getLogger("ArjunCore")
|
| 9 |
|
| 10 |
+
app = Flask(__name__, static_folder='.', static_url_path='')
|
| 11 |
|
| 12 |
+
# --- 2. कॉन्फ़िगरेशन क्लास (Environment variables से डेटा लेने के लिए) ---
|
| 13 |
class Config:
|
| 14 |
API_KEY = os.environ.get("YOUR_VEDIKA_API_KEY")
|
| 15 |
ENDPOINT = os.environ.get("BASE_URL")
|
| 16 |
MODEL_ID = os.environ.get("MODEL_ID")
|
| 17 |
|
| 18 |
+
# --- 3. रूट: सर्वर रन होते ही ट्रिगर होना ---
|
| 19 |
+
@app.route('/')
|
| 20 |
+
def index():
|
| 21 |
+
"""यह फंक्शन सुनिश्चित करता है कि जैसे ही यूज़र सर्वर पर आए, index.html लोड हो जाए।"""
|
| 22 |
+
logger.info("Serving index.html interface...")
|
| 23 |
+
return send_from_directory('.', 'index.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# --- 4. चैट एपीआई (LLM Proxy) ---
|
| 26 |
@app.route('/api/chat', methods=['POST'])
|
| 27 |
def chat():
|
| 28 |
+
"""फ्रंटएंड (Gradio/JS) से आने वाली रिक्वेस्ट को LLM तक पहुँचाना।"""
|
| 29 |
+
try:
|
| 30 |
+
data = request.json
|
| 31 |
+
# यहाँ आप PayloadBuilder का उपयोग करके अपना लॉजिक बढ़ा सकते हैं
|
| 32 |
+
logger.info("Processing chat request...")
|
| 33 |
+
|
| 34 |
+
# प्रॉक्सी लॉजिक यहाँ डालें (जैसे पिछला कोड था)
|
| 35 |
+
return jsonify({"status": "processing", "message": "Backend connected successfully"})
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return jsonify({"error": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# --- 5. हेल्थ चेक ---
|
| 40 |
+
@app.route('/health')
|
| 41 |
def health():
|
| 42 |
+
return jsonify({"status": "Arjun Core Active", "uptime": time.time()})
|
| 43 |
|
| 44 |
+
# --- 6. मुख्य एक्जीक्यूशन ---
|
| 45 |
if __name__ == '__main__':
|
| 46 |
port = int(os.environ.get("PORT", 7860))
|
| 47 |
+
print("\n==============================================")
|
| 48 |
+
print(" ARJUN CORE V3 - SYSTEM ONLINE ")
|
| 49 |
+
print("==============================================\n")
|
| 50 |
+
|
| 51 |
+
# यह Flask का सर्वर सीधे index.html को सर्व करेगा
|
| 52 |
app.run(host='0.0.0.0', port=port, threaded=True)
|