Spaces:
Paused
Paused
Update app.py
Browse files6 unmodified lines7891011121314151617187 unmodified lines26272829303132333435363738394041424344454647484950515253543 unmodified lines5859606162636465666768697071727374757677787980816 unmodified linesfrom flask_cors import CORSapp = Flask(__name__)CORS(app, resources={r"/*": {"origins": "*"}})# 🔱 SECURE KEY LOADING & CLEANINGRAW_KEYS = os.getenv("OR_KEYS", "")# New lines හෝ spaces තිබුණොත් ඒවා පිරිසිදු කර Keys වෙන් කරගනීAPI_KEYS = [k.strip() for k in RAW_KEYS.replace("\n", ",").split(",") if k.strip()]def fetch_web_intel(query):intel = ""7 unmodified lines@app.route('/api/chat', methods=['POST'])def chat():if not API_KEYS:return "Error: No API Keys found in Secrets.", 500try:data = request.jsonuser_msg = data.get("message", "")web_data = fetch_web_intel(user_msg)headers = {"Authorization": f"Bearer {random.choice(API_KEYS)}","Content-Type": "application/json"}payload = {"model": "minimax/minimax-m2.5:free","messages": [{"role": "system", "content": f"You are Elephant AI by MINZO-PRIME. Use this web data if relevant: {web_data}"},{"role": "user", "content": user_msg}],"stream": True}def generate():resp = requests.post("https://openrouter.ai/api/v1/chat/completions",headers=headers,3 unmodified lines)for line in resp.iter_lines():if line:decoded = line.decode('utf-8').replace("data: ", "")if "[DONE]" in decoded:breaktry:chunk = json.loads(decoded)content = chunk['choices'][0]['delta'].get('content', '')yield contentexcept:continuereturn Response(stream_with_context(generate()), mimetype='text/plain')except Exception as e:return str(e), 500@app.route('/')def health():return "🐘 Elephant AI Core is Online. Port: 7860"if __name__ == "__main__":app.run(host='0.0.0.0', port=7860)6 unmodified lines7891011121314151617181920212223247 unmodified lines323334353637383940414243444546474849505152535455565758596061623 unmodified lines6667686970717273747576777879808182838485868788899091926 unmodified linesfrom flask_cors import CORSapp = Flask(__name__)# 🔱 සියලුම Origins වලට අවසර ලබා දීම (CORS Fix)CORS(app, resources={r"/*": {"origins": "*"}})# 🔱 API KEYS DIRECT INTEGRATION# ඔබේ Keys 5ම මෙහි ඉතා නිවැරදිව පේළියෙන් පේළියට ඇතAPI_KEYS = ["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","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","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","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","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"]def fetch_web_intel(query):intel = ""7 unmodified lines@app.route('/api/chat', methods=['POST'])def chat():data = request.jsonuser_msg = data.get("message", "")web_data = fetch_web_intel(user_msg)# 🔱 Random Key Rotationselected_key = random.choice(API_KEYS)headers = {"Authorization": f"Bearer {selected_key}","Content-Type": "application/json","HTTP-Referer": "https://huggingface.co/spaces/minzo456/Elephant-AI-Core","X-Title": "Elephant AI Core"}payload = {"model": "minimax/minimax-m2.5:free","messages": [{"role": "system", "content": f"You are Elephant AI, a professional assistant for Commander MINZO-PRIME. Web Data: {web_data}"},{"role": "user", "content": user_msg}],"stream": True}def generate():try:resp = requests.post("https://openrouter.ai/api/v1/chat/completions",headers=headers,3 unmodified lines)for line in resp.iter_lines():if line:decoded = line.decode('utf-8')if decoded.startswith("data: "):data_str = decoded[6:]if data_str.strip() == "[DONE]": breaktry:chunk = json.loads(data_str)content = chunk['choices'][0]['delta'].get('content', '')if content: yield contentexcept: continueexcept Exception as e:yield f"Core Connectivity Error: {str(e)}"response = Response(stream_with_context(generate()), mimetype='text/plain')# 🔱 Buffering වැළැක්වීමට අවශ්ය Headersresponse.headers['X-Accel-Buffering'] = 'no'response.headers['Cache-Control'] = 'no-cache'return response@app.route('/')def health():return "🐘 Elephant AI Core Status: ACTIVE | Authorized: MINZO-PRIME"if __name__ == "__main__":app.run(host='0.0.0.0', port=7860)
app.py+51-40