minzo456 commited on
Commit
e9a5410
·
verified ·
1 Parent(s): c0aa3b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -66
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import random
3
  import requests
 
4
  from flask import Flask, request, jsonify
5
  from flask_cors import CORS
6
  from datetime import datetime
@@ -15,7 +16,8 @@ except ImportError:
15
  app = Flask(__name__)
16
  CORS(app)
17
 
18
- # AUTHORIZED API KEYS
 
19
  API_KEYS = [
20
  "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",
21
  "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",
@@ -24,103 +26,79 @@ API_KEYS = [
24
  "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"
25
  ]
26
 
27
- def fetch_web_intel(query):
28
- """වැඩිදියුණු කළ වෙබ් සෙවුම් පද්ධතිය"""
29
- intel = ""
30
  try:
31
- ddgs = DDGS()
32
- # සෙවුම් ප්‍රතිඵල 5ක් දක්වා වැඩි කර වඩාත් නිවැරදි තොරතුරු ලබා ගනී
33
- results = ddgs.text(query, max_results=5)
34
- if results:
35
- for i, r in enumerate(results, 1):
36
- intel += f"\n[{i}] Source: {r['title']}\nInfo: {r['body']}\n"
37
- else:
38
- intel = "No direct web matches found for the query."
39
- except Exception:
40
- intel = "Web intelligence node is currently throttled."
41
- return intel
 
 
 
 
42
 
43
  @app.route('/api/chat', methods=['POST'])
44
  def chat():
45
  try:
46
  data = request.json
47
  user_msg = data.get("message", "")
48
- image_url = data.get("image_url")
 
49
  tier = data.get("tier", "prime")
50
 
51
- # 🔱 පද්ධතියේ මූලික මොඩල් සිතියම (Prime & Nexus අනිවාර්යයෙන්ම පවතී)
52
- model_map = {
53
- "prime": "minimax/minimax-m2.5:free",
54
- "nexus": "tencent/hy3-preview:free"
55
- }
56
 
57
- # 🔱 AUTO-SWITCH Logic
58
- if image_url:
59
- # රූපයක් ඇති විට ස්වයංක්‍රීයව Vision මොඩල් එක තෝරා ගනී
60
  selected_model = "nvidia/llama-nemotron-embed-vl-1b-v2:free"
61
- active_tier = "VISION-EMBED"
62
- web_context = "Image analysis in progress. Web search bypassed."
63
  else:
64
- # රූපයක් නැති විට ඔබ ඉල්ලූ Prime හෝ Nexus භාවිතා කරයි
65
  selected_model = model_map.get(tier, "minimax/minimax-m2.5:free")
66
  active_tier = tier.upper()
67
- # Web Search එක මෙහිදී ක්‍රියාත්මක වේ
68
- web_context = fetch_web_intel(user_msg)
69
 
70
- current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
71
-
72
  headers = {
73
  "Authorization": f"Bearer {random.choice(API_KEYS)}",
74
  "Content-Type": "application/json",
75
- "HTTP-Referer": "https://huggingface.co/spaces/minzo456/Elephant-AI-Core",
76
- "X-Title": "Elephant AI Core"
77
  }
78
 
79
- # System Prompt එක තුළට සෙවුම් දත්ත ඇතුළත් කිරීම
80
- system_prompt = (
81
- f"You are the {active_tier} core of Elephant AI, built by MINZO-PRIME.\n"
82
- f"Current Time: {current_time}\n"
83
- f"Real-time Web Context: {web_context}\n"
84
- "Maintain your sovereign, high-intelligence persona at all times."
85
- )
86
-
87
- # Message Content සකස් කිරීම (Text or Multimodal)
88
- if image_url:
89
- user_content = [
90
- {"type": "text", "text": user_msg},
91
- {"type": "image_url", "image_url": {"url": image_url}}
92
- ]
93
- else:
94
- user_content = user_msg
95
 
96
  payload = {
97
  "model": selected_model,
98
  "messages": [
99
- {"role": "system", "content": system_prompt},
100
- {"role": "user", "content": user_content}
101
- ],
102
- "temperature": 0.6 # නිරවද්‍යතාවය සහ නිර්මාණශීලිත්වය අතර සමබරතාවය සඳහා
103
  }
104
 
105
- resp = requests.post(
106
- "https://openrouter.ai/api/v1/chat/completions",
107
- headers=headers,
108
- json=payload,
109
- timeout=60
110
- )
111
-
112
- if resp.status_code == 200:
113
- ai_reply = resp.json()['choices'][0]['message']['content']
114
- return jsonify({"reply": ai_reply})
115
- else:
116
- return jsonify({"reply": f"Node Error: {resp.status_code} - {resp.text}"}), resp.status_code
117
 
118
  except Exception as e:
119
  return jsonify({"reply": f"Core Disruption: {str(e)}"}), 500
120
 
121
  @app.route('/')
122
  def health():
123
- return "🐘 Elephant AI Core: ONLINE | Operator: MINZO-PRIME | Web Intel: ENABLED"
124
 
125
  if __name__ == "__main__":
126
  app.run(host='0.0.0.0', port=7860)
 
1
  import os
2
  import random
3
  import requests
4
+ import base64
5
  from flask import Flask, request, jsonify
6
  from flask_cors import CORS
7
  from datetime import datetime
 
16
  app = Flask(__name__)
17
  CORS(app)
18
 
19
+ # AUTHORIZED CONFIGURATION
20
+ IMGBB_KEY = "c32f54279bcd7d4f766b5eac37fd7327"
21
  API_KEYS = [
22
  "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",
23
  "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",
 
26
  "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"
27
  ]
28
 
29
+ def search_intel(query):
30
+ """RAG-style web retrieval"""
31
+ intel_data = ""
32
  try:
33
+ with DDGS() as ddgs:
34
+ results = list(ddgs.text(query, region='wt-wt', max_results=4))
35
+ if results:
36
+ for r in results:
37
+ intel_data += f"\n- {r['title']}: {r['body']}"
38
+ except: intel_data = "Web access limited."
39
+ return intel_data
40
+
41
+ def upload_to_imgbb(base64_image):
42
+ """Backend-side Image Upload to ImgBB"""
43
+ try:
44
+ payload = {"key": IMGBB_KEY, "image": base64_image}
45
+ res = requests.post("https://api.imgbb.com/1/upload", data=payload)
46
+ return res.json()['data']['url']
47
+ except: return None
48
 
49
  @app.route('/api/chat', methods=['POST'])
50
  def chat():
51
  try:
52
  data = request.json
53
  user_msg = data.get("message", "")
54
+ # Frontend එකෙන් 'image_base64' නමින් data එක එවිය යුතුයි
55
+ image_base_64 = data.get("image_base64")
56
  tier = data.get("tier", "prime")
57
 
58
+ final_image_url = None
59
+ if image_base_64:
60
+ # පින්තූරයක් තිබේ නම් එය Backend එකෙන් Upload කරයි
61
+ final_image_url = upload_to_imgbb(image_base_64)
 
62
 
63
+ # 🔱 Model Selection Logic
64
+ if final_image_url:
 
65
  selected_model = "nvidia/llama-nemotron-embed-vl-1b-v2:free"
66
+ active_tier = "VISION-CORE"
67
+ web_context = "Image analysis mode active."
68
  else:
69
+ model_map = {"prime": "minimax/minimax-m2.5:free", "nexus": "tencent/hy3-preview:free"}
70
  selected_model = model_map.get(tier, "minimax/minimax-m2.5:free")
71
  active_tier = tier.upper()
72
+ web_context = search_intel(user_msg)
 
73
 
 
 
74
  headers = {
75
  "Authorization": f"Bearer {random.choice(API_KEYS)}",
76
  "Content-Type": "application/json",
77
+ "HTTP-Referer": "https://huggingface.co/spaces/minzo456/Elephant-AI-Core"
 
78
  }
79
 
80
+ # Payload Assembly
81
+ content = [{"type": "text", "text": user_msg}]
82
+ if final_image_url:
83
+ content.append({"type": "image_url", "image_url": {"url": final_image_url}})
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  payload = {
86
  "model": selected_model,
87
  "messages": [
88
+ {"role": "system", "content": f"You are {active_tier} mode of Elephant AI by MINZO-PRIME. Web Data: {web_context}"},
89
+ {"role": "user", "content": content}
90
+ ]
 
91
  }
92
 
93
+ resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=60)
94
+ return jsonify({"reply": resp.json()['choices'][0]['message']['content']})
 
 
 
 
 
 
 
 
 
 
95
 
96
  except Exception as e:
97
  return jsonify({"reply": f"Core Disruption: {str(e)}"}), 500
98
 
99
  @app.route('/')
100
  def health():
101
+ return "🐘 Elephant AI Core: ONLINE | Operator: MINZO-PRIME"
102
 
103
  if __name__ == "__main__":
104
  app.run(host='0.0.0.0', port=7860)