Vedika66 commited on
Commit
ea2b4e9
·
verified ·
1 Parent(s): 7e48236

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -64
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  import requests
3
  from datetime import datetime, timedelta, timezone
@@ -5,58 +6,38 @@ from flask import Flask, request, Response, stream_with_context, render_template
5
 
6
  app = Flask(__name__)
7
 
8
- # 🔐 --- SECURE ENVIRONMENT VARIABLES (Hugging Face Secrets) ---
9
  API_KEY = os.environ.get("NVIDIA_API_KEY") or os.environ.get("YOUR_VEDIKA_API_KEY")
10
- MODEL_ID = os.environ.get("MODEL_ID") # Default fallback
11
- SERPAPI_KEY = os.environ.get("SERPAPI_KEY") # 🔒 Now secured in environment variables
12
 
13
  # NVIDIA's official invoke URL
14
  INVOKE_URL = "https://integrate.api.nvidia.com/v1/chat/completions"
15
 
16
- # 🌐 --- SERPAPI GOOGLE SEARCH ENGINE (100% BULLETPROOF) --- 🌐
17
  def web_search_scraper(query, num_results=5):
18
- """
19
- यह SerpApi का उपयोग करके सीधे Google Search से एकदम सटीक और ताज़ा (JSON) डेटा लाता है।
20
- """
21
  results = []
22
  if not SERPAPI_KEY:
23
- print("SYSTEM ALERT: SERPAPI_KEY is missing in environment secrets.")
24
  return results
25
 
26
  try:
27
- params = {
28
- "engine": "google",
29
- "q": query,
30
- "api_key": SERPAPI_KEY,
31
- "num": num_results,
32
- "hl": "en",
33
- "gl": "in" # India Region for local context
34
- }
35
-
36
  response = requests.get("https://serpapi.com/search", params=params, timeout=10)
37
  data = response.json()
38
 
39
- # 'organic_results' से असली Google सर्च का डेटा निकालना
40
  if "organic_results" in data:
41
  for item in data["organic_results"]:
42
  title = item.get("title", "")
43
  link = item.get("link", "")
44
  snippet = item.get("snippet", "")
45
-
46
  if title and snippet:
47
- results.append({
48
- "title": title,
49
- "link": link,
50
- "snippet": snippet
51
- })
52
-
53
  except Exception as e:
54
  print(f"SerpApi Error: {e}")
55
 
56
  return results
57
 
58
- # ----------------------------------------------------
59
-
60
  @app.route('/')
61
  def home():
62
  try:
@@ -68,7 +49,7 @@ def home():
68
  @app.route('/api/chat', methods=['POST'])
69
  def chat():
70
  if not API_KEY or not INVOKE_URL or not MODEL_ID:
71
- return Response("Server Error: Secrets missing. Check NVIDIA API Key.", status=500)
72
 
73
  data = request.get_json() or {}
74
  user_message = data.get("message", "")
@@ -78,11 +59,11 @@ def chat():
78
  max_tokens = data.get("max_tokens", 4096)
79
  temperature = data.get("temperature", 1.0)
80
 
81
- # 🧠 --- THINKING MODE PARAMETERS ---
82
- thinking_mode = data.get("thinking_mode", False) # True or False
83
- thinking_effort = data.get("thinking_effort", "medium") # "low", "medium", "high"
84
 
85
- # 🕒 --- REAL-TIME IST INJECTION ---
86
  ist_time = datetime.now(timezone.utc) + timedelta(hours=5, minutes=30)
87
  current_date = ist_time.strftime("%A, %d %B %Y, %I:%M %p IST")
88
 
@@ -90,70 +71,69 @@ def chat():
90
  thinking_instruction = ""
91
  if thinking_mode:
92
  if thinking_effort == "low":
93
- thinking_instruction = "\n[SYSTEM: THINKING MODE ENABLED (LOW EFFORT) - Keep your internal reasoning brief, direct, and fast before answering.]"
94
  elif thinking_effort == "high":
95
- thinking_instruction = "\n[SYSTEM: THINKING MODE ENABLED (HIGH EFFORT) - Perform a deep, exhaustive, and highly detailed step-by-step analysis. Consider edge cases, multiple perspectives, and logic verification before providing the final answer.]"
96
- else: # medium
97
- thinking_instruction = "\n[SYSTEM: THINKING MODE ENABLED (MEDIUM EFFORT) - Perform a standard step-by-step logical reasoning process before answering.]"
98
 
99
  # 🧠 --- GOD MODE SYSTEM PROMPT ---
100
- system_prompt = f"""
101
- You are CODE VED, an advanced AI System engineered EXCLUSIVELY by DIVY PATEL.
102
- Current Live Date and Time: {current_date}.
103
- {thinking_instruction}
104
 
105
  STRICT DIRECTIVES:
106
  1. NEVER invent, guess, or hallucinate product launches, dates, news, or facts.
107
  2. If you receive "LIVE WEB SEARCH RESULTS", you MUST base your answer ENTIRELY on that data.
108
- 3. Do NOT say "Based on the provided search results". Just answer naturally and confidently, citing the sources/links if needed.
109
  """
110
 
111
- # 🚀 --- AUTO-SEARCH INJECTION (USING SERPAPI) ---
112
  if is_search:
113
  scraped_data = web_search_scraper(user_message)
114
-
115
  search_context = "\n\n--- [LIVE VERIFIED GOOGLE SEARCH DATA] ---\n"
116
  if scraped_data:
117
  for idx, res in enumerate(scraped_data):
118
  search_context += f"{idx+1}. TITLE: {res['title']}\nSNIPPET: {res['snippet']}\nURL: {res['link']}\n\n"
119
-
120
- search_context += "[SYSTEM COMMAND: Use the above live Google data to answer the user accurately. Synthesize the info naturally.]"
121
  else:
122
- search_context += "[SYSTEM ALERT: Live search did not return results. Rely on your existing knowledge, but DO NOT hallucinate recent news.]"
123
 
124
  user_message = f"USER QUERY: {user_message}\n\n{search_context}"
125
 
126
  messages = [{"role": "system", "content": system_prompt}]
127
 
 
 
 
 
 
128
  for msg in history:
129
  role = msg.get("role", "user")
130
  content = msg.get("content", "")
131
  if content:
132
  messages.append({"role": role, "content": content})
133
 
134
- content_payload = []
135
- if user_message.strip():
136
- content_payload.append({"type": "text", "text": user_message})
137
-
138
- for att in attachments:
139
- att_type = att.get("type")
140
- b64_data = att.get("data")
141
- if att_type == "image":
142
- content_payload.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64_data}"}})
143
- elif att_type in ["audio", "file"]:
144
- content_payload.append({"type": "input_audio", "input_audio": {"data": b64_data, "format": "wav"}})
145
-
146
- if not content_payload:
147
- content_payload.append({"type": "text", "text": "Hello"})
148
-
149
- messages.append({"role": "user", "content": content_payload})
150
 
151
  headers = {
152
  "Authorization": f"Bearer {API_KEY}",
153
  "Accept": "text/event-stream"
154
  }
155
 
156
- # 🌟 --- NVIDIA FORMAT INTEGRATED ---
157
  payload = {
158
  "model": MODEL_ID,
159
  "messages": messages,
@@ -161,7 +141,6 @@ def chat():
161
  "temperature": float(temperature),
162
  "top_p": 0.95,
163
  "stream": True,
164
- # Enable native thinking in the backend if the user requested it
165
  "chat_template_kwargs": {"enable_thinking": thinking_mode}
166
  }
167
 
 
1
+
2
  import os
3
  import requests
4
  from datetime import datetime, timedelta, timezone
 
6
 
7
  app = Flask(__name__)
8
 
9
+ # 🔐 --- SECURE ENVIRONMENT VARIABLES ---
10
  API_KEY = os.environ.get("NVIDIA_API_KEY") or os.environ.get("YOUR_VEDIKA_API_KEY")
11
+ MODEL_ID = os.environ.get("MODEL_ID", "google/diffusiongemma-26b-a4b-it")
12
+ SERPAPI_KEY = os.environ.get("SERPAPI_KEY") # 🔒 Secured in Hugging Face Secrets
13
 
14
  # NVIDIA's official invoke URL
15
  INVOKE_URL = "https://integrate.api.nvidia.com/v1/chat/completions"
16
 
17
+ # 🌐 --- SERPAPI GOOGLE SEARCH ENGINE ---
18
  def web_search_scraper(query, num_results=5):
 
 
 
19
  results = []
20
  if not SERPAPI_KEY:
21
+ print("SYSTEM ALERT: SERPAPI_KEY missing in secrets.")
22
  return results
23
 
24
  try:
25
+ params = {"engine": "google", "q": query, "api_key": SERPAPI_KEY, "num": num_results, "hl": "en", "gl": "in"}
 
 
 
 
 
 
 
 
26
  response = requests.get("https://serpapi.com/search", params=params, timeout=10)
27
  data = response.json()
28
 
 
29
  if "organic_results" in data:
30
  for item in data["organic_results"]:
31
  title = item.get("title", "")
32
  link = item.get("link", "")
33
  snippet = item.get("snippet", "")
 
34
  if title and snippet:
35
+ results.append({"title": title, "link": link, "snippet": snippet})
 
 
 
 
 
36
  except Exception as e:
37
  print(f"SerpApi Error: {e}")
38
 
39
  return results
40
 
 
 
41
  @app.route('/')
42
  def home():
43
  try:
 
49
  @app.route('/api/chat', methods=['POST'])
50
  def chat():
51
  if not API_KEY or not INVOKE_URL or not MODEL_ID:
52
+ return Response("Server Error: Secrets missing.", status=500)
53
 
54
  data = request.get_json() or {}
55
  user_message = data.get("message", "")
 
59
  max_tokens = data.get("max_tokens", 4096)
60
  temperature = data.get("temperature", 1.0)
61
 
62
+ # 🧠 --- THINKING MODE ---
63
+ thinking_mode = data.get("thinking_mode", False)
64
+ thinking_effort = data.get("thinking_effort", "medium")
65
 
66
+ # 🕒 --- REAL-TIME IST ---
67
  ist_time = datetime.now(timezone.utc) + timedelta(hours=5, minutes=30)
68
  current_date = ist_time.strftime("%A, %d %B %Y, %I:%M %p IST")
69
 
 
71
  thinking_instruction = ""
72
  if thinking_mode:
73
  if thinking_effort == "low":
74
+ thinking_instruction = "\n[SYSTEM: THINKING MODE (LOW) - Reason briefly and directly before answering.]"
75
  elif thinking_effort == "high":
76
+ thinking_instruction = "\n[SYSTEM: THINKING MODE (HIGH) - Perform exhaustive, multi-step logical reasoning before answering.]"
77
+ else:
78
+ thinking_instruction = "\n[SYSTEM: THINKING MODE (MEDIUM) - Perform a standard step-by-step reasoning process.]"
79
 
80
  # 🧠 --- GOD MODE SYSTEM PROMPT ---
81
+ system_prompt = f"""You are CODE VED, an advanced AI System engineered EXCLUSIVELY by DIVY PATEL.
82
+ Current Live Date and Time: {current_date}.{thinking_instruction}
 
 
83
 
84
  STRICT DIRECTIVES:
85
  1. NEVER invent, guess, or hallucinate product launches, dates, news, or facts.
86
  2. If you receive "LIVE WEB SEARCH RESULTS", you MUST base your answer ENTIRELY on that data.
87
+ 3. Do NOT say "Based on the provided search results". Just answer naturally and confidently.
88
  """
89
 
90
+ # 🚀 --- AUTO-SEARCH INJECTION ---
91
  if is_search:
92
  scraped_data = web_search_scraper(user_message)
 
93
  search_context = "\n\n--- [LIVE VERIFIED GOOGLE SEARCH DATA] ---\n"
94
  if scraped_data:
95
  for idx, res in enumerate(scraped_data):
96
  search_context += f"{idx+1}. TITLE: {res['title']}\nSNIPPET: {res['snippet']}\nURL: {res['link']}\n\n"
97
+ search_context += "[SYSTEM COMMAND: Use the above live Google data to answer the user accurately.]"
 
98
  else:
99
+ search_context += "[SYSTEM ALERT: Live search did not return results. Rely on your existing knowledge, but DO NOT hallucinate.]"
100
 
101
  user_message = f"USER QUERY: {user_message}\n\n{search_context}"
102
 
103
  messages = [{"role": "system", "content": system_prompt}]
104
 
105
+ # ⚠️ FIX: Remove the last message from history because the frontend already adds the current query to it!
106
+ # Without this, the model gets the same question twice and gets confused.
107
+ if history and history[-1].get("role") == "user":
108
+ history = history[:-1]
109
+
110
  for msg in history:
111
  role = msg.get("role", "user")
112
  content = msg.get("content", "")
113
  if content:
114
  messages.append({"role": role, "content": content})
115
 
116
+ # ⚠️ BIG MISTAKE FIXED: Only use array format if there are actually attachments.
117
+ # Otherwise, send pure string text so standard LLMs don't break their system prompt alignment!
118
+ if attachments:
119
+ content_payload = [{"type": "text", "text": user_message}]
120
+ for att in attachments:
121
+ att_type = att.get("type")
122
+ b64_data = att.get("data")
123
+ if att_type == "image":
124
+ content_payload.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64_data}"}})
125
+ elif att_type in ["audio", "file"]:
126
+ content_payload.append({"type": "input_audio", "input_audio": {"data": b64_data, "format": "wav"}})
127
+ messages.append({"role": "user", "content": content_payload})
128
+ else:
129
+ # Standard text models perfectly understand this
130
+ messages.append({"role": "user", "content": user_message})
 
131
 
132
  headers = {
133
  "Authorization": f"Bearer {API_KEY}",
134
  "Accept": "text/event-stream"
135
  }
136
 
 
137
  payload = {
138
  "model": MODEL_ID,
139
  "messages": messages,
 
141
  "temperature": float(temperature),
142
  "top_p": 0.95,
143
  "stream": True,
 
144
  "chat_template_kwargs": {"enable_thinking": thinking_mode}
145
  }
146