Vedika66 commited on
Commit
40a703c
·
verified ·
1 Parent(s): 322d20b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +172 -31
app.py CHANGED
@@ -1,31 +1,172 @@
1
- import requests, base64
2
-
3
- invoke_url = "https://integrate.api.nvidia.com/v1/chat/completions"
4
- stream = False
5
-
6
- def read_b64(path):
7
- with open(path, "rb") as f:
8
- return base64.b64encode(f.read()).decode()
9
-
10
- headers = {
11
- "Authorization": "Bearer $NVIDIA_API_KEY",
12
- "Accept": "text/event-stream" if stream else "application/json"
13
- }
14
-
15
- payload = {
16
- "model": "google/diffusiongemma-26b-a4b-it",
17
- "messages": [{"role":"user","content":""}],
18
- "max_tokens": 4096,
19
- "temperature": 1.00,
20
- "top_p": 0.95,
21
- "stream": stream,
22
- "chat_template_kwargs": {"enable_thinking":True},
23
- }
24
-
25
- response = requests.post(invoke_url, headers=headers, json=payload, stream=stream)
26
- if stream:
27
- for line in response.iter_lines():
28
- if line:
29
- print(line.decode("utf-8"))
30
- else:
31
- print(response.json())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import urllib.parse
4
+ from bs4 import BeautifulSoup
5
+ from datetime import datetime, timedelta, timezone
6
+ from flask import Flask, request, Response, stream_with_context, render_template_string
7
+
8
+ app = Flask(__name__)
9
+
10
+ # 🔐 --- SECURE ENVIRONMENT VARIABLES ---
11
+ # यह आपके Hugging Face Secrets से अपने आप लोड हो जाएगा
12
+ API_KEY = os.environ.get("NVIDIA_API_KEY") or os.environ.get("YOUR_VEDIKA_API_KEY")
13
+ MODEL_ID = os.environ.get("MODEL_ID")
14
+
15
+ # NVIDIA का डायरेक्ट URL (BASE_URL का लॉजिक पूरी तरह हटा दिया गया है)
16
+ INVOKE_URL = "https://integrate.api.nvidia.com/v1/chat/completions"
17
+
18
+ # 🌐 --- BULLETPROOF SCRAPER (News + Wiki) ---
19
+ def web_search_scraper(query, num_results=4):
20
+ results = []
21
+ headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"}
22
+
23
+ # 1. Google News RSS (Never blocks, gives real latest news)
24
+ try:
25
+ rss_url = f"https://news.google.com/rss/search?q={urllib.parse.quote(query)}&hl=en-IN&gl=IN&ceid=IN:en"
26
+ res = requests.get(rss_url, headers=headers, timeout=5)
27
+ soup = BeautifulSoup(res.content, 'xml') # Use XML parser
28
+ items = soup.find_all('item')
29
+
30
+ for item in items[:num_results]:
31
+ title = item.title.text if item.title else ""
32
+ link = item.link.text if item.link else ""
33
+ pub_date = item.pubDate.text if item.pubDate else ""
34
+ if title:
35
+ results.append({
36
+ "title": title,
37
+ "link": link,
38
+ "snippet": f"Published: {pub_date}"
39
+ })
40
+ except Exception as e:
41
+ pass
42
+
43
+ # 2. Wikipedia API Fallback (Never blocks)
44
+ if len(results) < 2:
45
+ try:
46
+ wiki_url = f"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={urllib.parse.quote(query)}&utf8=&format=json"
47
+ res = requests.get(wiki_url, headers=headers, timeout=5).json()
48
+ for item in res.get('query', {}).get('search', [])[:num_results]:
49
+ clean_snippet = BeautifulSoup(item['snippet'], "html.parser").text
50
+ results.append({
51
+ "title": item['title'],
52
+ "link": f"https://en.wikipedia.org/wiki/{urllib.parse.quote(item['title'])}",
53
+ "snippet": clean_snippet
54
+ })
55
+ except Exception as e:
56
+ pass
57
+
58
+ return results
59
+
60
+ # ----------------------------------------------------
61
+
62
+ @app.route('/')
63
+ def home():
64
+ try:
65
+ with open('index.html', 'r', encoding='utf-8') as f:
66
+ return render_template_string(f.read())
67
+ except Exception as e:
68
+ return f"<h1>System Error</h1><p>index.html missing: {str(e)}</p>"
69
+
70
+ @app.route('/api/chat', methods=['POST'])
71
+ def chat():
72
+ if not API_KEY or not INVOKE_URL or not MODEL_ID:
73
+ return Response("Server Error: Secrets missing.", status=500)
74
+
75
+ data = request.get_json() or {}
76
+ user_message = data.get("message", "")
77
+ attachments = data.get("attachments", [])
78
+ is_search = data.get("is_search", False)
79
+ history = data.get("history", [])
80
+ max_tokens = data.get("max_tokens", 4096)
81
+ temperature = data.get("temperature", 1.00) # Updated to NVIDIA default if not passed
82
+
83
+ # 🕒 --- REAL-TIME IST INJECTION ---
84
+ ist_time = datetime.now(timezone.utc) + timedelta(hours=5, minutes=30)
85
+ current_date = ist_time.strftime("%A, %d %B %Y, %I:%M %p IST")
86
+
87
+ # 🧠 --- STRICT SYSTEM PROMPT (ANTI-HALLUCINATION) ---
88
+ system_prompt = f"""
89
+ You are CODE VED, an advanced AI System engineered EXCLUSIVELY by DIVY PATEL.
90
+ Current Date: {current_date}.
91
+
92
+ STRICT DIRECTIVES:
93
+ 1. NEVER invent, guess, or hallucinate product launches, dates, news, or facts.
94
+ 2. If you are provided with live search data, base your answer PURELY on that data.
95
+ 3. If you lack data to answer a question about a recent event, you MUST say: "I do not have the verified live data for this right now."
96
+ """
97
+
98
+ # 🚀 --- AUTO-SEARCH LOGIC WITH FAILSAFE ---
99
+ if is_search:
100
+ scraped_data = web_search_scraper(user_message)
101
+
102
+ if scraped_data:
103
+ search_context = "\n\n--- [LIVE VERIFIED WEB DATA] ---\n"
104
+ for idx, res in enumerate(scraped_data):
105
+ search_context += f"{idx+1}. TITLE: {res['title']}\nSNIPPET: {res['snippet']}\nURL: {res['link']}\n\n"
106
+
107
+ search_context += "[SYSTEM: Use the above verified data to answer. Cite sources.]"
108
+ else:
109
+ search_context = """
110
+ \n\n[CRITICAL SYSTEM ALERT: LIVE SEARCH FAILED OR BLOCKED.
111
+ YOU HAVE NO NEW DATA FOR THIS QUERY.
112
+ MANDATORY RULE: DO NOT INVENT ANY NEWS OR DATES. TELL THE USER HONESTLY THAT YOU COULD NOT FETCH THE LATEST LIVE DATA.]
113
+ """
114
+
115
+ user_message = f"{search_context}\n\nUSER QUERY: {user_message}"
116
+
117
+ messages = [{"role": "system", "content": system_prompt}]
118
+
119
+ for msg in history:
120
+ role = msg.get("role", "user")
121
+ content = msg.get("content", "")
122
+ if content:
123
+ messages.append({"role": role, "content": content})
124
+
125
+ content_payload = []
126
+ if user_message.strip():
127
+ content_payload.append({"type": "text", "text": user_message})
128
+
129
+ for att in attachments:
130
+ att_type = att.get("type")
131
+ b64_data = att.get("data")
132
+ if att_type == "image":
133
+ # Image formatting compatible with standard APIs
134
+ content_payload.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64_data}"}})
135
+ elif att_type in ["audio", "file"]:
136
+ content_payload.append({"type": "input_audio", "input_audio": {"data": b64_data, "format": "wav"}})
137
+
138
+ if not content_payload:
139
+ content_payload.append({"type": "text", "text": "Hello"})
140
+
141
+ messages.append({"role": "user", "content": content_payload})
142
+
143
+ headers = {
144
+ "Authorization": f"Bearer {API_KEY}",
145
+ "Accept": "text/event-stream"
146
+ }
147
+
148
+ # 🌟 --- NEW NVIDIA FORMAT PAYLOAD INTEGRATED HERE --- 🌟
149
+ payload = {
150
+ "model": MODEL_ID, # Dynamically fetching from your secrets
151
+ "messages": messages,
152
+ "max_tokens": int(max_tokens),
153
+ "temperature": float(temperature),
154
+ "top_p": 0.95, # As per your format
155
+ "stream": True, # Stream is kept True for fast UI response
156
+ "chat_template_kwargs": {"enable_thinking": True} # 👈 New NVIDIA feature added
157
+ }
158
+
159
+ try:
160
+ response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=True)
161
+ def generate():
162
+ for line in response.iter_lines():
163
+ if line:
164
+ decoded_line = line.decode("utf-8")
165
+ if decoded_line.startswith("data: "):
166
+ yield decoded_line + "\n\n"
167
+ return Response(stream_with_context(generate()), mimetype='text/event-stream')
168
+ except Exception as e:
169
+ return Response(f"Internal Error: {str(e)}", status=500)
170
+
171
+ if __name__ == '__main__':
172
+ app.run(host='0.0.0.0', port=7860)