Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,11 +34,13 @@ def get_weather_from_ip(ip_address):
|
|
| 34 |
try:
|
| 35 |
ip_data = requests.get(f'https://ipinfo.io/{ip_address}/json').json()
|
| 36 |
loc = ip_data.get('loc')
|
| 37 |
-
if not loc:
|
|
|
|
| 38 |
lat, lon = loc.split(',')
|
| 39 |
if ip_data.get('country') != 'US':
|
| 40 |
return {'error': 'Weather only available in US'}
|
| 41 |
-
|
|
|
|
| 42 |
current = forecast['properties']['periods'][0]
|
| 43 |
return {
|
| 44 |
'ip': ip_address,
|
|
@@ -51,28 +53,41 @@ def get_weather_from_ip(ip_address):
|
|
| 51 |
@app.route('/search')
|
| 52 |
def search():
|
| 53 |
q = request.args.get('q')
|
| 54 |
-
if not q:
|
|
|
|
| 55 |
n = request.args.get('n', default=10, type=int)
|
| 56 |
return jsonify({'query': q, 'results': scrape_startpage(q, n)})
|
| 57 |
|
| 58 |
@app.route('/weather')
|
| 59 |
def weather():
|
| 60 |
ip = request.args.get('ip')
|
| 61 |
-
if not ip:
|
|
|
|
| 62 |
data = get_weather_from_ip(ip)
|
| 63 |
-
if 'error' in data:
|
|
|
|
| 64 |
return jsonify(data)
|
| 65 |
|
| 66 |
@app.route('/')
|
| 67 |
def health():
|
| 68 |
return jsonify({'status': 'running', 'message': 'GridLock search API'})
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
def morsify_key():
|
| 71 |
prefix = "nvapi-"
|
| 72 |
-
rest_enc = "
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
return prefix + decoded
|
| 76 |
|
| 77 |
@app.route('/v1/chat/completions', methods=['POST'])
|
| 78 |
def chat_completions():
|
|
@@ -81,7 +96,7 @@ def chat_completions():
|
|
| 81 |
return jsonify({'error': 'Missing messages'}), 400
|
| 82 |
stream = data.get('stream', True)
|
| 83 |
api_key = morsify_key()
|
| 84 |
-
print(f"[DEBUG] Using API key: {api_key}")
|
| 85 |
payload = {
|
| 86 |
"model": data.get("model", "meta/llama-4-scout-17b-16e-instruct"),
|
| 87 |
"messages": data["messages"],
|
|
|
|
| 34 |
try:
|
| 35 |
ip_data = requests.get(f'https://ipinfo.io/{ip_address}/json').json()
|
| 36 |
loc = ip_data.get('loc')
|
| 37 |
+
if not loc:
|
| 38 |
+
return {'error': 'No location'}
|
| 39 |
lat, lon = loc.split(',')
|
| 40 |
if ip_data.get('country') != 'US':
|
| 41 |
return {'error': 'Weather only available in US'}
|
| 42 |
+
points = requests.get(f'https://api.weather.gov/points/{lat},{lon}').json()
|
| 43 |
+
forecast = requests.get(points['properties']['forecast']).json()
|
| 44 |
current = forecast['properties']['periods'][0]
|
| 45 |
return {
|
| 46 |
'ip': ip_address,
|
|
|
|
| 53 |
@app.route('/search')
|
| 54 |
def search():
|
| 55 |
q = request.args.get('q')
|
| 56 |
+
if not q:
|
| 57 |
+
return jsonify({'error': 'Missing query'}), 400
|
| 58 |
n = request.args.get('n', default=10, type=int)
|
| 59 |
return jsonify({'query': q, 'results': scrape_startpage(q, n)})
|
| 60 |
|
| 61 |
@app.route('/weather')
|
| 62 |
def weather():
|
| 63 |
ip = request.args.get('ip')
|
| 64 |
+
if not ip:
|
| 65 |
+
return jsonify({'error': 'Missing ip'}), 400
|
| 66 |
data = get_weather_from_ip(ip)
|
| 67 |
+
if 'error' in data:
|
| 68 |
+
return jsonify(data), 400
|
| 69 |
return jsonify(data)
|
| 70 |
|
| 71 |
@app.route('/')
|
| 72 |
def health():
|
| 73 |
return jsonify({'status': 'running', 'message': 'GridLock search API'})
|
| 74 |
|
| 75 |
+
def safe_shift_decode(text, shift):
|
| 76 |
+
out = []
|
| 77 |
+
for ch in text:
|
| 78 |
+
code = ord(ch)
|
| 79 |
+
if 32 <= code <= 126:
|
| 80 |
+
new = 32 + ((code - 32 - shift) % 95)
|
| 81 |
+
out.append(chr(new))
|
| 82 |
+
else:
|
| 83 |
+
out.append(ch)
|
| 84 |
+
return ''.join(out)
|
| 85 |
+
|
| 86 |
def morsify_key():
|
| 87 |
prefix = "nvapi-"
|
| 88 |
+
rest_enc = "y~Fvqm|30?3u:|mA822;:>;:m=?;=483"
|
| 89 |
+
decoded_rest = safe_shift_decode(rest_enc, 8)
|
| 90 |
+
return prefix + decoded_rest
|
|
|
|
| 91 |
|
| 92 |
@app.route('/v1/chat/completions', methods=['POST'])
|
| 93 |
def chat_completions():
|
|
|
|
| 96 |
return jsonify({'error': 'Missing messages'}), 400
|
| 97 |
stream = data.get('stream', True)
|
| 98 |
api_key = morsify_key()
|
| 99 |
+
print(f"[DEBUG] Using API key: {api_key}")
|
| 100 |
payload = {
|
| 101 |
"model": data.get("model", "meta/llama-4-scout-17b-16e-instruct"),
|
| 102 |
"messages": data["messages"],
|