minzo456 commited on
Commit
d3dd669
·
verified ·
1 Parent(s): a72b6f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -10
app.py CHANGED
@@ -5,7 +5,7 @@ from flask_cors import CORS
5
  app = Flask(__name__)
6
  CORS(app)
7
 
8
- # 🔱 ඔබ ලබා දුන් API Keys
9
  API_KEYS = [
10
  "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",
11
  "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",
@@ -14,39 +14,73 @@ API_KEYS = [
14
  "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"
15
  ]
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  @app.route('/api/chat', methods=['POST'])
18
  def chat():
19
  try:
20
  data = request.json
21
  user_msg = data.get("message", "")
22
 
 
 
 
 
23
  headers = {
24
  "Authorization": f"Bearer {random.choice(API_KEYS)}",
25
- "Content-Type": "application/json"
 
 
26
  }
27
 
 
 
 
 
 
 
28
  payload = {
29
  "model": "minimax/minimax-m2.5:free",
30
  "messages": [
31
- {"role": "system", "content": "You are Elephant AI created by MINZO-PRIME. Be helpful and professional."},
32
  {"role": "user", "content": user_msg}
33
  ],
34
- "stream": False # 🔱 ස්ථාවරත්වය සඳහා Streaming අක්‍රීය කළා
35
  }
36
 
37
- resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
 
 
 
 
 
 
38
  res_data = resp.json()
39
 
40
- # පිළිතුර ලබා ගැනීම
41
- ai_reply = res_data['choices'][0]['message']['content']
42
- return jsonify({"reply": ai_reply})
 
 
43
 
44
  except Exception as e:
45
- return jsonify({"reply": f"Core Error: {str(e)}"}), 500
46
 
47
  @app.route('/')
48
  def health():
49
- return "🐘 Elephant AI Core: ONLINE"
50
 
51
  if __name__ == "__main__":
52
  app.run(host='0.0.0.0', port=7860)
 
5
  app = Flask(__name__)
6
  CORS(app)
7
 
8
+ # 🔱 AUTHORIZED API KEYS
9
  API_KEYS = [
10
  "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",
11
  "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",
 
14
  "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"
15
  ]
16
 
17
+ def fetch_web_intel(query):
18
+ """අන්තර්ජාලයෙන් නවතම තොරතුරු ලබා ගැනීම (RSS Intelligence)"""
19
+ intel = ""
20
+ try:
21
+ # Google News RSS හරහා සෙවීම
22
+ search_url = f'https://news.google.com/rss/search?q={query}&hl=en-US'
23
+ feed = feedparser.parse(search_url)
24
+ # මුල් ලිපි 3ක තොරතුරු එකතු කිරීම
25
+ for entry in feed.entries[:3]:
26
+ intel += f"\n- Title: {entry.title} (Source: {entry.source.text if 'source' in entry else 'Web'})"
27
+ except Exception as e:
28
+ print(f"Web Intel Error: {e}")
29
+ return intel
30
+
31
  @app.route('/api/chat', methods=['POST'])
32
  def chat():
33
  try:
34
  data = request.json
35
  user_msg = data.get("message", "")
36
 
37
+ # 1. 🔱 WEB SEARCH EXECUTION
38
+ # පද්ධතිය බුද්ධිමත් ලෙස සෙවුම් වාරයක් ක්‍රියාත්මක කරයි
39
+ web_context = fetch_web_intel(user_msg)
40
+
41
  headers = {
42
  "Authorization": f"Bearer {random.choice(API_KEYS)}",
43
+ "Content-Type": "application/json",
44
+ "HTTP-Referer": "https://huggingface.co/spaces/minzo456/Elephant-AI-Core",
45
+ "X-Title": "Elephant AI Core"
46
  }
47
 
48
+ # 2. 🔱 INTEGRATING INTEL INTO SYSTEM PROMPT
49
+ system_prompt = f"""You are Elephant AI, an advanced intelligence created by MINZO-PRIME.
50
+ You have access to real-time web information.
51
+ If relevant, use this fresh data to answer: {web_context}
52
+ Always prioritize accuracy and be professional."""
53
+
54
  payload = {
55
  "model": "minimax/minimax-m2.5:free",
56
  "messages": [
57
+ {"role": "system", "content": system_prompt},
58
  {"role": "user", "content": user_msg}
59
  ],
60
+ "stream": False
61
  }
62
 
63
+ resp = requests.post(
64
+ "https://openrouter.ai/api/v1/chat/completions",
65
+ headers=headers,
66
+ json=payload,
67
+ timeout=45
68
+ )
69
+
70
  res_data = resp.json()
71
 
72
+ if 'choices' in res_data:
73
+ ai_reply = res_data['choices'][0]['message']['content']
74
+ return jsonify({"reply": ai_reply})
75
+ else:
76
+ return jsonify({"reply": "Core Error: Failed to retrieve intelligence from OpenRouter."}), 500
77
 
78
  except Exception as e:
79
+ return jsonify({"reply": f"Internal System Error: {str(e)}"}), 500
80
 
81
  @app.route('/')
82
  def health():
83
+ return "🐘 Elephant AI Core: ONLINE | WEB INTEL: ACTIVE"
84
 
85
  if __name__ == "__main__":
86
  app.run(host='0.0.0.0', port=7860)