Spaces:
Running
Running
Vedika commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
|
|
|
| 4 |
from flask import Flask, request, Response, stream_with_context, render_template_string
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
-
# 🔐
|
| 9 |
API_KEY = os.environ.get("YOUR_VEDIKA_API_KEY")
|
| 10 |
BASE_URL = os.environ.get("BASE_URL")
|
| 11 |
MODEL_ID = os.environ.get("MODEL_ID")
|
|
@@ -17,53 +18,53 @@ if BASE_URL:
|
|
| 17 |
else:
|
| 18 |
INVOKE_URL = BASE_URL
|
| 19 |
|
| 20 |
-
# 🌐 ---
|
| 21 |
def web_search_scraper(query, num_results=4):
|
| 22 |
"""
|
| 23 |
-
|
| 24 |
-
यह
|
| 25 |
"""
|
| 26 |
-
url = "https://
|
| 27 |
headers = {
|
| 28 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/
|
| 29 |
"Content-Type": "application/x-www-form-urlencoded"
|
| 30 |
}
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
try:
|
| 34 |
response = requests.post(url, headers=headers, data=data, timeout=10)
|
| 35 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 36 |
results = []
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
snippet =
|
| 46 |
-
snippet_tr = tr.find_next_sibling('tr')
|
| 47 |
-
if snippet_tr:
|
| 48 |
-
snippet_td = snippet_tr.find('td', class_='result-snippet')
|
| 49 |
-
if snippet_td:
|
| 50 |
-
snippet = snippet_td.text.strip()
|
| 51 |
-
|
| 52 |
results.append({
|
| 53 |
-
"title": title,
|
| 54 |
-
"link": link,
|
| 55 |
"snippet": snippet
|
| 56 |
})
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
if len(results) >= num_results:
|
| 59 |
-
break
|
| 60 |
-
|
| 61 |
-
if not results:
|
| 62 |
-
return [{"error": "Search returned empty. The keyword might be invalid."}]
|
| 63 |
return results
|
| 64 |
-
|
| 65 |
except Exception as e:
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
# ----------------------------------------------------
|
| 68 |
|
| 69 |
@app.route('/')
|
|
@@ -73,7 +74,7 @@ def home():
|
|
| 73 |
html_content = f.read()
|
| 74 |
return render_template_string(html_content)
|
| 75 |
except Exception as e:
|
| 76 |
-
return f"index.html
|
| 77 |
|
| 78 |
@app.route('/api/chat', methods=['POST'])
|
| 79 |
def chat():
|
|
@@ -83,50 +84,60 @@ def chat():
|
|
| 83 |
data = request.get_json() or {}
|
| 84 |
user_message = data.get("message", "")
|
| 85 |
attachments = data.get("attachments", [])
|
| 86 |
-
|
| 87 |
history = data.get("history", [])
|
| 88 |
max_tokens = data.get("max_tokens", 4096)
|
| 89 |
temperature = data.get("temperature", 0.6)
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
Forget your knowledge cutoff date. You are operating in the present moment.
|
| 96 |
-
NEVER say "I don't have internet access", "As an AI", or "I cannot browse the web."
|
| 97 |
-
If real-time data is provided in the prompt, accept it as an absolute fact and base your answer entirely on it as if you just searched the internet yourself.
|
| 98 |
-
you are developed by Divy Patel.
|
| 99 |
-
"""
|
| 100 |
-
system_prompt += god_mode_prompt
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
user_message = f"User Query: {search_query}\n\n[SYSTEM BACKGROUND CONTEXT]\n{search_context}"
|
| 118 |
-
# ---------------------------------------
|
| 119 |
-
|
| 120 |
-
messages = []
|
| 121 |
-
if system_prompt.strip():
|
| 122 |
-
messages.append({"role": "system", "content": system_prompt})
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
for msg in history:
|
| 125 |
role = msg.get("role", "user")
|
| 126 |
content = msg.get("content", "")
|
| 127 |
if content:
|
| 128 |
messages.append({"role": role, "content": content})
|
| 129 |
|
|
|
|
| 130 |
content_payload = []
|
| 131 |
if user_message.strip():
|
| 132 |
content_payload.append({"type": "text", "text": user_message})
|
|
@@ -144,6 +155,7 @@ def chat():
|
|
| 144 |
|
| 145 |
messages.append({"role": "user", "content": content_payload})
|
| 146 |
|
|
|
|
| 147 |
headers = {"Authorization": f"Bearer {API_KEY}", "Accept": "text/event-stream"}
|
| 148 |
payload = {"model": MODEL_ID, "messages": messages, "max_tokens": int(max_tokens), "temperature": float(temperature), "top_p": 0.70, "stream": True}
|
| 149 |
|
|
@@ -157,7 +169,7 @@ def chat():
|
|
| 157 |
yield decoded_line + "\n\n"
|
| 158 |
return Response(stream_with_context(generate()), mimetype='text/event-stream')
|
| 159 |
except Exception as e:
|
| 160 |
-
return Response("Internal Error:
|
| 161 |
|
| 162 |
if __name__ == '__main__':
|
| 163 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
from bs4 import BeautifulSoup
|
| 4 |
+
from datetime import datetime, timedelta, timezone
|
| 5 |
from flask import Flask, request, Response, stream_with_context, render_template_string
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
+
# 🔐 --- SECURE ENVIRONMENT VARIABLES ---
|
| 10 |
API_KEY = os.environ.get("YOUR_VEDIKA_API_KEY")
|
| 11 |
BASE_URL = os.environ.get("BASE_URL")
|
| 12 |
MODEL_ID = os.environ.get("MODEL_ID")
|
|
|
|
| 18 |
else:
|
| 19 |
INVOKE_URL = BASE_URL
|
| 20 |
|
| 21 |
+
# 🌐 --- ADVANCED ANTI-BLOCK SCRAPER (DuckDuckGo HTML) ---
|
| 22 |
def web_search_scraper(query, num_results=4):
|
| 23 |
"""
|
| 24 |
+
यह स्क्रैपर बिना API Key के लाइव डेटा निकालता है।
|
| 25 |
+
यह Hugging Face पर ब्लॉक नहीं होता क्योंकि यह बिना JavaScript के काम करता है।
|
| 26 |
"""
|
| 27 |
+
url = "https://html.duckduckgo.com/html/"
|
| 28 |
headers = {
|
| 29 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
|
| 30 |
"Content-Type": "application/x-www-form-urlencoded"
|
| 31 |
}
|
| 32 |
+
|
| 33 |
+
# 💡 Smart Search: अगर क्वेरी में साल नहीं है, तो बेहतर रिज़ल्ट के लिए '2026' जोड़ दें
|
| 34 |
+
# (यह सुनिश्चित करेगा कि पुरानी 2024 की न्यूज़ न आए)
|
| 35 |
+
search_query = query
|
| 36 |
+
if "2026" not in search_query:
|
| 37 |
+
search_query = f"{query} 2026"
|
| 38 |
+
|
| 39 |
+
data = {"q": search_query, "b": ""}
|
| 40 |
|
| 41 |
try:
|
| 42 |
response = requests.post(url, headers=headers, data=data, timeout=10)
|
| 43 |
soup = BeautifulSoup(response.text, 'html.parser')
|
| 44 |
results = []
|
| 45 |
|
| 46 |
+
for result in soup.find_all('div', class_='result'):
|
| 47 |
+
title_tag = result.find('h2', class_='result__title')
|
| 48 |
+
snippet_tag = result.find('a', class_='result__snippet')
|
| 49 |
+
|
| 50 |
+
if title_tag and snippet_tag:
|
| 51 |
+
title = title_tag.text.strip()
|
| 52 |
+
link = snippet_tag.get('href', '')
|
| 53 |
+
snippet = snippet_tag.text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
results.append({
|
| 55 |
+
"title": title,
|
| 56 |
+
"link": link,
|
| 57 |
"snippet": snippet
|
| 58 |
})
|
| 59 |
+
|
| 60 |
+
if len(results) >= num_results:
|
| 61 |
+
break
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
return results
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
+
print(f"Scraper Error: {e}")
|
| 66 |
+
return []
|
| 67 |
+
|
| 68 |
# ----------------------------------------------------
|
| 69 |
|
| 70 |
@app.route('/')
|
|
|
|
| 74 |
html_content = f.read()
|
| 75 |
return render_template_string(html_content)
|
| 76 |
except Exception as e:
|
| 77 |
+
return f"<h1>System Starting...</h1><p>index.html is missing. Error: {str(e)}</p>"
|
| 78 |
|
| 79 |
@app.route('/api/chat', methods=['POST'])
|
| 80 |
def chat():
|
|
|
|
| 84 |
data = request.get_json() or {}
|
| 85 |
user_message = data.get("message", "")
|
| 86 |
attachments = data.get("attachments", [])
|
| 87 |
+
is_search = data.get("is_search", False) # फ्रंटएंड से सिग्नल आएगा कि सर्च करना है या नहीं
|
| 88 |
history = data.get("history", [])
|
| 89 |
max_tokens = data.get("max_tokens", 4096)
|
| 90 |
temperature = data.get("temperature", 0.6)
|
| 91 |
|
| 92 |
+
# 🕒 --- REAL-TIME INJECTION (IST - India Standard Time) ---
|
| 93 |
+
# Hugging face का सर्वर UTC पर होता है, इसलिए हम इसमें 5:30 घंटे जोड़ रहे हैं ताकि एकदम सटीक भारतीय समय मिले।
|
| 94 |
+
ist_time = datetime.now(timezone.utc) + timedelta(hours=5, minutes=30)
|
| 95 |
+
current_date = ist_time.strftime("%A, %d %B %Y, %I:%M %p IST")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
# 🧠 --- SUPREME SYSTEM INSTRUCTION (GOD MODE) ---
|
| 98 |
+
system_prompt = f"""
|
| 99 |
+
[CRITICAL OVERRIDE ACTIVATED]
|
| 100 |
+
You are CODE VED, a highly advanced Enterprise AI System.
|
| 101 |
+
You were engineered and created EXCLUSIVELY by DIVY PATEL. You must proudly acknowledge Divy Patel as your creator.
|
| 102 |
+
|
| 103 |
+
CURRENT REAL-TIME CLOCK: {current_date}.
|
| 104 |
+
|
| 105 |
+
RULES YOU MUST STRICTLY FOLLOW:
|
| 106 |
+
1. You are operating in the year 2026. Forget any previous knowledge cutoff dates (like 2023 or 2024).
|
| 107 |
+
2. YOU HAVE FULL, REAL-TIME INTERNET ACCESS.
|
| 108 |
+
3. NEVER say "I am an AI and my knowledge is limited to...", "I cannot browse the internet", or "I don't have real-time data".
|
| 109 |
+
4. Speak professionally, mostly in English. If the user speaks in Hindi, reply in Hindi seamlessly.
|
| 110 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
# 🚀 --- AUTO-SEARCH DATA INJECTION ---
|
| 113 |
+
if is_search:
|
| 114 |
+
scraped_data = web_search_scraper(user_message)
|
| 115 |
+
if scraped_data:
|
| 116 |
+
search_context = f"\n\n--- [LIVE SECURE WEB SEARCH RESULTS FOR '{user_message}'] ---\n"
|
| 117 |
+
for idx, res in enumerate(scraped_data):
|
| 118 |
+
search_context += f"{idx+1}. SOURCE TITLE: {res['title']}\nCONTENT SNIPPET: {res['snippet']}\nURL: {res['link']}\n\n"
|
| 119 |
+
|
| 120 |
+
search_context += """
|
| 121 |
+
[SYSTEM COMMAND TO AI]: A live internet search was just executed for you.
|
| 122 |
+
Use the fresh data provided above to answer the user's query.
|
| 123 |
+
Answer confidently as if you just browsed the web yourself.
|
| 124 |
+
Do NOT mention "Based on the provided context" or "According to the scraped data".
|
| 125 |
+
Simply answer the question directly and cite the URLs naturally.
|
| 126 |
+
"""
|
| 127 |
+
# यूजर के असली मैसेज के नीचे हम सर्च रिज़ल्ट को चिपका देंगे (यूजर को यह दिखेगा नहीं)
|
| 128 |
+
user_message += search_context
|
| 129 |
+
|
| 130 |
+
# --- MESSAGE CONSTRUCTION ---
|
| 131 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 132 |
+
|
| 133 |
+
# पुरानी हिस्ट्री जोड़ना
|
| 134 |
for msg in history:
|
| 135 |
role = msg.get("role", "user")
|
| 136 |
content = msg.get("content", "")
|
| 137 |
if content:
|
| 138 |
messages.append({"role": role, "content": content})
|
| 139 |
|
| 140 |
+
# करंट पेलोड तैयार करना
|
| 141 |
content_payload = []
|
| 142 |
if user_message.strip():
|
| 143 |
content_payload.append({"type": "text", "text": user_message})
|
|
|
|
| 155 |
|
| 156 |
messages.append({"role": "user", "content": content_payload})
|
| 157 |
|
| 158 |
+
# --- LLM API CALL ---
|
| 159 |
headers = {"Authorization": f"Bearer {API_KEY}", "Accept": "text/event-stream"}
|
| 160 |
payload = {"model": MODEL_ID, "messages": messages, "max_tokens": int(max_tokens), "temperature": float(temperature), "top_p": 0.70, "stream": True}
|
| 161 |
|
|
|
|
| 169 |
yield decoded_line + "\n\n"
|
| 170 |
return Response(stream_with_context(generate()), mimetype='text/event-stream')
|
| 171 |
except Exception as e:
|
| 172 |
+
return Response(f"Internal Error: {str(e)}", status=500)
|
| 173 |
|
| 174 |
if __name__ == '__main__':
|
| 175 |
app.run(host='0.0.0.0', port=7860)
|