Spaces:
Running
Running
Vedika commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
|
|
|
|
|
|
| 3 |
from flask import Flask, request, Response, stream_with_context, render_template_string
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
|
@@ -17,6 +19,53 @@ if BASE_URL:
|
|
| 17 |
else:
|
| 18 |
INVOKE_URL = BASE_URL
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
@app.route('/')
|
| 21 |
def home():
|
| 22 |
try:
|
|
@@ -36,20 +85,36 @@ def chat():
|
|
| 36 |
user_message = data.get("message", "")
|
| 37 |
attachments = data.get("attachments", [])
|
| 38 |
system_prompt = data.get("system_prompt", "")
|
| 39 |
-
|
| 40 |
-
# 🛠️ FIX 1: यहाँ फ्रंटएंड से आ रही हिस्ट्री को रिसीव किया
|
| 41 |
history = data.get("history", [])
|
| 42 |
-
|
| 43 |
max_tokens = data.get("max_tokens", 4096)
|
| 44 |
temperature = data.get("temperature", 0.6)
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
messages = []
|
| 47 |
|
| 48 |
# 1. सिस्टम प्रॉम्प्ट
|
| 49 |
if system_prompt.strip():
|
| 50 |
messages.append({"role": "system", "content": system_prompt})
|
| 51 |
|
| 52 |
-
#
|
| 53 |
for msg in history:
|
| 54 |
role = msg.get("role", "user")
|
| 55 |
content = msg.get("content", "")
|
|
@@ -106,4 +171,4 @@ def chat():
|
|
| 106 |
return Response("Internal Error: Unable to process request securely.", status=500)
|
| 107 |
|
| 108 |
if __name__ == '__main__':
|
| 109 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
+
import urllib.parse
|
| 4 |
+
from bs4 import BeautifulSoup
|
| 5 |
from flask import Flask, request, Response, stream_with_context, render_template_string
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
|
|
|
| 19 |
else:
|
| 20 |
INVOKE_URL = BASE_URL
|
| 21 |
|
| 22 |
+
|
| 23 |
+
# 🌐 --- GOOGLE SEARCH SCRAPER ENGINE --- 🌐
|
| 24 |
+
def google_search_scraper(query, num_results=4):
|
| 25 |
+
"""
|
| 26 |
+
यह फंक्शन Google सर्च से डेटा निकालता है और उसे AI के समझने लायक बनाता है।
|
| 27 |
+
"""
|
| 28 |
+
# Google को धोखा देने के लिए असली ब्राउज़र का User-Agent
|
| 29 |
+
headers = {
|
| 30 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
| 31 |
+
}
|
| 32 |
+
url = f"https://www.google.com/search?q={urllib.parse.quote(query)}&hl=en"
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 36 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 37 |
+
results = []
|
| 38 |
+
|
| 39 |
+
# Google के सर्च रिजल्ट्स 'div.g' के अंदर होते हैं
|
| 40 |
+
for g in soup.find_all('div', class_='g'):
|
| 41 |
+
title_elem = g.find('h3')
|
| 42 |
+
link_elem = g.find('a')
|
| 43 |
+
|
| 44 |
+
if title_elem and link_elem:
|
| 45 |
+
title = title_elem.text
|
| 46 |
+
link = link_elem.get('href', '')
|
| 47 |
+
|
| 48 |
+
# Snippet (डिस्क्रिप्शन) निकालना
|
| 49 |
+
snippet = ""
|
| 50 |
+
snippet_box = g.find('div', class_='VwiC3b')
|
| 51 |
+
if snippet_box:
|
| 52 |
+
snippet = snippet_box.text
|
| 53 |
+
|
| 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 |
+
return [{"error": str(e)}]
|
| 66 |
+
# ---------------------------------------------
|
| 67 |
+
|
| 68 |
+
|
| 69 |
@app.route('/')
|
| 70 |
def home():
|
| 71 |
try:
|
|
|
|
| 85 |
user_message = data.get("message", "")
|
| 86 |
attachments = data.get("attachments", [])
|
| 87 |
system_prompt = data.get("system_prompt", "")
|
|
|
|
|
|
|
| 88 |
history = data.get("history", [])
|
|
|
|
| 89 |
max_tokens = data.get("max_tokens", 4096)
|
| 90 |
temperature = data.get("temperature", 0.6)
|
| 91 |
|
| 92 |
+
# 🚀 --- INTERCEPT & SEARCH LOGIC --- 🚀
|
| 93 |
+
# अगर यूजर के मैसेज में /search या /google है, तो स्क्रैपर चालू करें
|
| 94 |
+
if user_message.strip().lower().startswith("/search "):
|
| 95 |
+
search_query = user_message[8:].strip()
|
| 96 |
+
scraped_data = google_search_scraper(search_query)
|
| 97 |
+
|
| 98 |
+
search_context = f"Real-time Google Search Results for '{search_query}':\n\n"
|
| 99 |
+
for idx, res in enumerate(scraped_data):
|
| 100 |
+
if "error" in res:
|
| 101 |
+
search_context += f"Search Error: {res['error']}\n"
|
| 102 |
+
else:
|
| 103 |
+
search_context += f"{idx+1}. Title: {res['title']}\nSnippet: {res['snippet']}\nLink: {res['link']}\n\n"
|
| 104 |
+
|
| 105 |
+
search_context += "\n[INSTRUCTION FOR AI: Formulate a comprehensive, professional response to the user's query using ONLY the real-time search data provided above. Cite sources if applicable.]"
|
| 106 |
+
|
| 107 |
+
# एआई के लिए प्रॉम्प्ट को मॉडिफाई करना (यूजर को यह बैकग्राउंड डेटा नहीं दिखेगा)
|
| 108 |
+
user_message = f"User Query: {search_query}\n\n[SYSTEM BACKGROUND CONTEXT]\n{search_context}"
|
| 109 |
+
# ---------------------------------------
|
| 110 |
+
|
| 111 |
messages = []
|
| 112 |
|
| 113 |
# 1. सिस्टम प्रॉम्प्ट
|
| 114 |
if system_prompt.strip():
|
| 115 |
messages.append({"role": "system", "content": system_prompt})
|
| 116 |
|
| 117 |
+
# 2. पुरानी हिस्ट्री को मॉडल के पेलोड में जोड़ना
|
| 118 |
for msg in history:
|
| 119 |
role = msg.get("role", "user")
|
| 120 |
content = msg.get("content", "")
|
|
|
|
| 171 |
return Response("Internal Error: Unable to process request securely.", status=500)
|
| 172 |
|
| 173 |
if __name__ == '__main__':
|
| 174 |
+
app.run(host='0.0.0.0', port=7860)
|